Skip to content
Myr

Fixed res.redirect(). RFC states absolute

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent d12452f · Browse files · 2 files+46 added−36 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+46 added−36 removed

  • tests:+28 added−28 removed
  • other:+18 added−8 removed

2 files+46 added−36 removed

modified:lib/response.js+18 added-8 removed
314 unmodified lines
315 */316317res.redirect = function(url, status){318 var app = this.app319 , req = this.req320 , base = app.set('home') || '/'321 , status = status || 302322 , body;323324 // Setup redirect map325 var map = {325 back: this.req.header('Referrer', base)326 back: req.header('Referrer', base)327 , home: base328 };329330 // Support custom redirect map330 map.__proto__ = this.app.redirects;331 map.__proto__ = app.redirects;332333 // Attempt mapped redirect334 var mapped = 'function' == typeof map[url]334 ? map[url](this.req, this)335 ? map[url](req, this)336 : map[url];337338 // Perform redirect339 url = mapped || url;340341 // Relative342 if (!~url.indexOf('://')) {340 // Respect mount-point341 if (app.route && !~url.indexOf('://')) {342 url = join(app.route, url);343 } 343 // Respect mount-point344 if (app.route) {345 url = join(app.route, url);346 }347348 // Absolute349 var host = req.headers.host350 , tls = req.connection.constructor.name == 'CleartextStream';351 url = 'http' + (tls ? 's' : '') + '://' + host + url;352 }353 354355 // Support text/{plain,html} by default346 if (this.req.accepts('html')) {356 if (req.accepts('html')) {357 body = '<p>' + http.STATUS_CODES[status] + '. Redirecting to <a href="' + url + '">' + url + '</a></p>';358 this.header('Content-Type', 'text/html');359 } else {360 body = http.STATUS_CODES[status] + '. Redirecting to ' + url;
25 unmodified lines
modified:test/response.test.js+28 added-28 removed
245 unmodified lines
246 res.redirect('blog');247 });248249 assert.response(app,250 { url: '/html', headers: { Accept: 'text/html,text/plain' }},250 { url: '/html', headers: { Accept: 'text/html,text/plain', Host: 'foo.com' }},251 { body: '<p>Moved Temporarily. Redirecting to <a href="http://google.com">http://google.com</a></p>' });252253 assert.response(app,254 { url: '/', headers: { Accept: 'text/plain' }},254 { url: '/', headers: { Accept: 'text/plain', Host: 'foo.com' }},255 { body: 'Moved Permanently. Redirecting to http://google.com'256 , status: 301, headers: { Location: 'http://google.com' }});257258 assert.response(app,259 { url: '/back', headers: { Accept: 'text/plain' }},260 { body: 'Moved Temporarily. Redirecting to /'261 , status: 302, headers: { Location: '/', 'Content-Type': 'text/plain' }});259 { url: '/back', headers: { Accept: 'text/plain', Host: 'foo.com' }},260 { body: 'Moved Temporarily. Redirecting to http://foo.com/'261 , status: 302, headers: { Location: 'http://foo.com/', 'Content-Type': 'text/plain' }});262263 assert.response(app,264 { url: '/back', headers: { Referer: '/foo', Accept: 'text/plain' }},265 { body: 'Moved Temporarily. Redirecting to /foo'266 , status: 302, headers: { Location: '/foo' }});264 { url: '/back', headers: { Referer: '/foo', Accept: 'text/plain', Host: 'foo.com' }},265 { body: 'Moved Temporarily. Redirecting to http://foo.com/foo'266 , status: 302, headers: { Location: 'http://foo.com/foo' }});267268 assert.response(app,269 { url: '/back', headers: { Referrer: '/foo', Accept: 'text/plain' }},270 { body: 'Moved Temporarily. Redirecting to /foo'271 , status: 302, headers: { Location: '/foo' }});269 { url: '/back', headers: { Referrer: '/foo', Accept: 'text/plain', Host: 'foo.com' }},270 { body: 'Moved Temporarily. Redirecting to http://foo.com/foo'271 , status: 302, headers: { Location: 'http://foo.com/foo' }});272273 assert.response(app,274 { url: '/home', headers: { Accept: 'text/plain' } },275 { body: 'Moved Temporarily. Redirecting to /'276 , status: 302, headers: { Location: '/' }});274 { url: '/home', headers: { Accept: 'text/plain', Host: 'foo.com' } },275 { body: 'Moved Temporarily. Redirecting to http://foo.com/'276 , status: 302, headers: { Location: 'http://foo.com/' }});277278 assert.response(app2,279 { url: '/', headers: { Accept: 'text/plain' }},279 { url: '/', headers: { Accept: 'text/plain', Host: 'foo.com' }},280 { body: 'Moved Permanently. Redirecting to http://google.com'281 , status: 301, headers: { Location: 'http://google.com' }});282283 assert.response(app2,284 { url: '/back', headers: { Accept: 'text/plain' }},285 { body: 'Moved Temporarily. Redirecting to /blog'286 , status: 302, headers: { Location: '/blog' }});284 { url: '/back', headers: { Accept: 'text/plain', Host: 'foo.com' }},285 { body: 'Moved Temporarily. Redirecting to http://foo.com/blog'286 , status: 302, headers: { Location: 'http://foo.com/blog' }});287288 assert.response(app2,289 { url: '/home', headers: { Accept: 'text/plain' }},290 { body: 'Moved Temporarily. Redirecting to /blog'291 , status: 302, headers: { Location: '/blog' }});289 { url: '/home', headers: { Accept: 'text/plain', Host: 'foo.com' }},290 { body: 'Moved Temporarily. Redirecting to http://foo.com/blog'291 , status: 302, headers: { Location: 'http://foo.com/blog' }});292293 assert.response(app2,294 { url: '/google', headers: { Accept: 'text/plain' }},294 { url: '/google', headers: { Accept: 'text/plain', Host: 'foo.com' }},295 { body: 'Moved Temporarily. Redirecting to http://google.com'296 , status: 302, headers: { Location: 'http://google.com' }});297298 assert.response(app2,299 { url: '/user/12', headers: { Accept: 'text/plain' }},300 { body: 'Moved Temporarily. Redirecting to /user/12/blog'301 , status: 302, headers: { Location: '/user/12/blog', 'X-Foo': 'bar' }});299 { url: '/user/12', headers: { Accept: 'text/plain', Host: 'foo.com' }},300 { body: 'Moved Temporarily. Redirecting to http://foo.com/user/12/blog'301 , status: 302, headers: { Location: 'http://foo.com/user/12/blog', 'X-Foo': 'bar' }});302 },303304 'test #redirect() when mounted': function(){305 var app = express.createServer()
10 unmodified lines
316317 app.use('/blog', blog);318319 assert.response(app,320 { url: '/blog/posts' },320 { url: '/blog/posts', headers: { Host: 'foo.com' }},321 { status: 302322 , headers: { Location: '/blog/posts/all' }});322 , headers: { Location: 'http://foo.com/blog/posts/all' }});323 },324 325 'test #sendfile()': function(){326 var app = express.createServer();
209 unmodified lines
536 res.redirect('/');537 });538539 assert.response(app,540 { url: '/' },540 { url: '/', headers: { Host: 'foo.com' }},541 function(res){542 res.headers['set-cookie']543 .should.eql(['rememberme=yes; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly', 'something=else']);544 });
43 unmodified lines