Skip to content

Commit

Permalink
Use encodeURI() for pathname urls
Browse files Browse the repository at this point in the history
  • Loading branch information
humphd committed Apr 4, 2019
1 parent 59662e9 commit 554675f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions dist/nohost-sw.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function serverInstall() {
console.log('Server installed for first time');

const fs = window.Filer.fs;
fs.writeFile('/readme.txt', 'hello world!', function(err) {
fs.writeFile('/hello world.txt', 'hello world!', function(err) {
if(err) console.error(err);
});
}
Expand Down
7 changes: 5 additions & 2 deletions src/html-formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ function format500(path, err) {
*/
function formatDir(route, dirPath, entries) {
const parent = path.dirname(dirPath) || '/';
// Maintain path sep, but deal with things like spaces in filenames
const url = encodeURI(route + parent);
const header = `
<!DOCTYPE html>
<html><head><title>Index of ${dirPath}</title></head>
Expand All @@ -98,13 +100,14 @@ function formatDir(route, dirPath, entries) {
<th><b>Size</b></th><th><b>Description</b></th></tr>
<tr><th colspan='5'><hr></th></tr>
<tr><td valign='top'><img src='${back}' alt='[DIR]'></td>
<td><a href='${route}${parent}'>Parent Directory</a></td><td>&nbsp;</td>
<td><a href='${url}'>Parent Directory</a></td><td>&nbsp;</td>
<td align='right'> - </td><td>&nbsp;</td></tr>`;
const footer = `<tr><th colspan='5'><hr></th></tr></table>${footerClose}`;

const rows = entries.map(entry => {
const ext = path.extname(entry.name);
const href = `${route}${path.join(dirPath, entry.name)}`;
// Maintain path sep, but deal with things like spaces in filenames
const href = encodeURI(`${route}${path.join(dirPath, entry.name)}`);
let icon;
let alt;

Expand Down
4 changes: 3 additions & 1 deletion src/nohost-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ workbox.routing.registerRoute(
wwwRegex,
({ url }) => {
// Pull the filesystem path off the url
const path = url.pathname.match(wwwRegex)[1];
let path = url.pathname.match(wwwRegex)[1];
// Deal with encoding in the filename (e.g., spaces as %20)
path = decodeURIComponent(path);

// Allow passing `?json` on URL to get back JSON vs. raw response
const formatter =
Expand Down

0 comments on commit 554675f

Please sign in to comment.