Skip to content

Commit b5fb561

Browse files
committed
Improve the default redirect response headers
1 parent b752aaa commit b5fb561

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ unreleased
22
==========
33

44
* Fix reading options from options prototype
5+
* Improve the default redirect response headers
56
67
78
- Allow Node.js HTTP server to set `Date` response header

index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,16 @@ function serveStatic(root, options) {
9090
originalUrl.pathname = collapseLeadingSlashes(originalUrl.pathname + '/')
9191

9292
// reformat the URL
93-
var target = url.format(originalUrl)
93+
var loc = url.format(originalUrl)
94+
var msg = 'Redirecting to <a href="' + escapeHtml(loc) + '">' + escapeHtml(loc) + '</a>\n'
9495

9596
// send redirect response
9697
res.statusCode = 303
97-
res.setHeader('Content-Type', 'text/html; charset=utf-8')
98-
res.setHeader('Location', target)
99-
res.end('Redirecting to <a href="' + escapeHtml(target) + '">' + escapeHtml(target) + '</a>\n')
98+
res.setHeader('Content-Type', 'text/html; charset=UTF-8')
99+
res.setHeader('Content-Length', Buffer.byteLength(msg))
100+
res.setHeader('X-Content-Type-Options', 'nosniff')
101+
res.setHeader('Location', loc)
102+
res.end(msg)
100103
})
101104
} else {
102105
// forward to next middleware on directory

0 commit comments

Comments
 (0)