@@ -31,6 +31,11 @@ function decodePathname(pathname) {
31
31
? normalized . replace ( / \\ / g, '/' ) : normalized ;
32
32
}
33
33
34
+ const nonUrlSafeCharsRgx = / [ \x00 - \x1F \x20 \x7F - \uFFFF ] + / g;
35
+ function ensureUriEncoded ( text ) {
36
+ return text
37
+ return String ( text ) . replace ( nonUrlSafeCharsRgx , encodeURIComponent ) ;
38
+ }
34
39
35
40
// Check to see if we should try to compress a file with gzip.
36
41
function shouldCompressGzip ( req ) {
@@ -161,7 +166,8 @@ module.exports = function createMiddleware(_dir, _options) {
161
166
if ( opts . weakCompare && clientEtag !== serverEtag
162
167
&& clientEtag !== `W/${ serverEtag } ` && `W/${ clientEtag } ` !== serverEtag ) {
163
168
return false ;
164
- } else if ( ! opts . weakCompare && ( clientEtag !== serverEtag || clientEtag . indexOf ( 'W/' ) === 0 ) ) {
169
+ }
170
+ if ( ! opts . weakCompare && ( clientEtag !== serverEtag || clientEtag . indexOf ( 'W/' ) === 0 ) ) {
165
171
return false ;
166
172
}
167
173
}
@@ -340,8 +346,10 @@ module.exports = function createMiddleware(_dir, _options) {
340
346
} , res , next ) ;
341
347
} else {
342
348
// Try to serve default ./404.html
349
+ const rawUrl = ( handleError ? `/${ path . join ( baseDir , `404.${ defaultExt } ` ) } ` : req . url ) ;
350
+ const encodedUrl = ensureUriEncoded ( rawUrl ) ;
343
351
middleware ( {
344
- url : ( handleError ? `/ ${ path . join ( baseDir , `404. ${ defaultExt } ` ) } ` : req . url ) ,
352
+ url : encodedUrl ,
345
353
headers : req . headers ,
346
354
statusCode : 404 ,
347
355
} , res , next ) ;
@@ -359,7 +367,10 @@ module.exports = function createMiddleware(_dir, _options) {
359
367
if ( ! pathname . match ( / \/ $ / ) ) {
360
368
res . statusCode = 302 ;
361
369
const q = parsed . query ? `?${ parsed . query } ` : '' ;
362
- res . setHeader ( 'location' , `${ parsed . pathname } /${ q } ` ) ;
370
+ res . setHeader (
371
+ 'location' ,
372
+ ensureUriEncoded ( `${ parsed . pathname } /${ q } ` )
373
+ ) ;
363
374
res . end ( ) ;
364
375
return ;
365
376
}
0 commit comments