Skip to content

Commit f7a2b79

Browse files
committed
fix: url encoding bug
1 parent 0d8b9b5 commit f7a2b79

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

sw.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,19 @@ function formatSize(n) {
1515
return n.toFixed(1) + 'kMGTP'[i - 1]
1616
}
1717

18+
function escEntity(str, reg) {
19+
return str.replace(reg, s => '&#' + s.charCodeAt(0) + ';')
20+
}
21+
1822
function escHtml(str) {
19-
return str
20-
.replace(/&|<|>/g, s => '&#' + s.charCodeAt(0) + ';')
23+
return escEntity(str, /&|<|>/g)
2124
.replace(/\s/g, '&nbsp;')
2225
}
2326

27+
function escAttr(str) {
28+
return escEntity(str, /&|"/g)
29+
}
30+
2431
async function listDir(dirHandle, dirPath) {
2532
const DIR_PREFIX = '\x00' // for sort
2633
const keys = []
@@ -56,7 +63,7 @@ async function listDir(dirHandle, dirPath) {
5663
<tr>
5764
<td class="icon">${icon}</td>
5865
<td class="size">${size}</td>
59-
<td class="name"><a href="${escape(name)}">${escHtml(name)}</a></td>
66+
<td class="name"><a href="${escAttr(name)}">${escHtml(name)}</a></td>
6067
</tr>`
6168
})
6269

@@ -155,7 +162,7 @@ async function respond(url, req) {
155162
return Response.redirect('/')
156163
}
157164

158-
const dirNames = unescape(url.pathname).replace(/^\/+/, '').split(/\/+/)
165+
const dirNames = decodeURI(url.pathname).replace(/^\/+/, '').split(/\/+/)
159166
const fileName = dirNames.pop() || 'index.html'
160167
const dirHandles = [mRootDirHandle]
161168
let dirHandle = mRootDirHandle
@@ -191,7 +198,7 @@ async function respond(url, req) {
191198
/** @type {ResponseInit} */
192199
const resOpt = {
193200
headers: {
194-
'content-type': file.type,
201+
'content-type': file.type || 'text/plain',
195202
},
196203
}
197204

0 commit comments

Comments
 (0)