@@ -15,12 +15,19 @@ function formatSize(n) {
15
15
return n . toFixed ( 1 ) + 'kMGTP' [ i - 1 ]
16
16
}
17
17
18
+ function escEntity ( str , reg ) {
19
+ return str . replace ( reg , s => '&#' + s . charCodeAt ( 0 ) + ';' )
20
+ }
21
+
18
22
function escHtml ( str ) {
19
- return str
20
- . replace ( / & | < | > / g, s => '&#' + s . charCodeAt ( 0 ) + ';' )
23
+ return escEntity ( str , / & | < | > / g)
21
24
. replace ( / \s / g, ' ' )
22
25
}
23
26
27
+ function escAttr ( str ) {
28
+ return escEntity ( str , / & | " / g)
29
+ }
30
+
24
31
async function listDir ( dirHandle , dirPath ) {
25
32
const DIR_PREFIX = '\x00' // for sort
26
33
const keys = [ ]
@@ -56,7 +63,7 @@ async function listDir(dirHandle, dirPath) {
56
63
<tr>
57
64
<td class="icon">${ icon } </td>
58
65
<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>
60
67
</tr>`
61
68
} )
62
69
@@ -155,7 +162,7 @@ async function respond(url, req) {
155
162
return Response . redirect ( '/' )
156
163
}
157
164
158
- const dirNames = unescape ( url . pathname ) . replace ( / ^ \/ + / , '' ) . split ( / \/ + / )
165
+ const dirNames = decodeURI ( url . pathname ) . replace ( / ^ \/ + / , '' ) . split ( / \/ + / )
159
166
const fileName = dirNames . pop ( ) || 'index.html'
160
167
const dirHandles = [ mRootDirHandle ]
161
168
let dirHandle = mRootDirHandle
@@ -191,7 +198,7 @@ async function respond(url, req) {
191
198
/** @type {ResponseInit } */
192
199
const resOpt = {
193
200
headers : {
194
- 'content-type' : file . type ,
201
+ 'content-type' : file . type || 'text/plain' ,
195
202
} ,
196
203
}
197
204
0 commit comments