Skip to content

Commit fb2f313

Browse files
committed
Send complete HTML document in redirect response
1 parent 9484ce6 commit fb2f313

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

HISTORY.md

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

4+
* Send complete HTML document in redirect response
45
* Set default CSP header in redirect response
56

67
1.11.2 / 2017-01-23

index.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,26 @@ function collapseLeadingSlashes (str) {
142142
: str
143143
}
144144

145+
/**
146+
* Create a minimal HTML document.
147+
*
148+
* @param {string} title
149+
* @param {string} body
150+
* @private
151+
*/
152+
153+
function createHtmlDocument (title, body) {
154+
return '<!DOCTYPE html>\n' +
155+
'<html lang="en">\n' +
156+
'<head>\n' +
157+
'<meta charset="utf-8">\n' +
158+
'<title>' + title + '</title>\n' +
159+
'</head>\n' +
160+
'<body>\n' +
161+
'<pre>' + body + '</pre>\n' +
162+
'</body>\n'
163+
}
164+
145165
/**
146166
* Create a directory listener that just 404s.
147167
* @private
@@ -174,16 +194,17 @@ function createRedirectDirectoryListener () {
174194

175195
// reformat the URL
176196
var loc = encodeUrl(url.format(originalUrl))
177-
var msg = 'Redirecting to <a href="' + escapeHtml(loc) + '">' + escapeHtml(loc) + '</a>\n'
197+
var doc = createHtmlDocument('Redirecting', 'Redirecting to <a href="' + escapeHtml(loc) + '">' +
198+
escapeHtml(loc) + '</a>')
178199
var res = this.res
179200

180201
// send redirect response
181202
res.statusCode = 301
182203
res.setHeader('Content-Type', 'text/html; charset=UTF-8')
183-
res.setHeader('Content-Length', Buffer.byteLength(msg))
204+
res.setHeader('Content-Length', Buffer.byteLength(doc))
184205
res.setHeader('Content-Security-Policy', "default-src 'self'")
185206
res.setHeader('X-Content-Type-Options', 'nosniff')
186207
res.setHeader('Location', loc)
187-
res.end(msg)
208+
res.end(doc)
188209
}
189210
}

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ describe('serveStatic()', function () {
473473
.get('/snow')
474474
.expect('Location', '/snow%20%E2%98%83/')
475475
.expect('Content-Type', /html/)
476-
.expect(301, 'Redirecting to <a href="/snow%20%E2%98%83/">/snow%20%E2%98%83/</a>\n', done)
476+
.expect(301, />Redirecting to <a href="\/snow%20%E2%98%83\/">\/snow%20%E2%98%83\/<\/a></, done)
477477
})
478478

479479
it('should respond with default Content-Security-Policy', function (done) {

0 commit comments

Comments
 (0)