Skip to content
Myr

response.js

This file

Path

lib/response.js

722 lines · 16.5 KiB · JavaScript

Last changed by

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

History of this file

Take it

The walk stopped after 200 commits: lines older than 9fda13b point at it.

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 1–4why, one step back
1/**
2 * Module dependencies.3 */4

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2d49c0d

line 5why, one step back
5var http = require('http')

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 6–8why, one step back
6 , path = require('path')
7 , connect = require('connect')8 , utils = connect.utils

update connectTJ Holowaychukbefore adoption612fc47

line 9why, one step back
9 , sign = require('cookie-signature').sign

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 10–11why, one step back
10 , normalizeType = require('./utils').normalizeType
11 , normalizeTypes = require('./utils').normalizeTypes

add etag utilTJ Holowaychukbefore adoptionf2f0976

line 12why, one step back
12 , etag = require('./utils').etag

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

line 13why, one step back
13 , statusCodes = http.STATUS_CODES

fixing cookies for connect 2.3.1TJ Holowaychukbefore adoption02d4384

line 14why, one step back
14 , cookie = require('cookie')

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

line 15why, one step back
15 , send = require('send')

Fixed: expose connects mime module. Cloases #1165TJ Holowaychukbefore adoption8956731

line 16why, one step back
16 , mime = connect.mime

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 17–42why, one step back
17 , basename = path.basename
18 , extname = path.extname19 , join = path.join;2021/**22 * Response prototype.23 */2425var res = module.exports = {26 __proto__: http.ServerResponse.prototype27};2829/**30 * Set status `code`.31 *32 * @param {Number} code33 * @return {ServerResponse}34 * @api public35 */3637res.status = function(code){38 this.statusCode = code;39 return this;40};4142/**

Added `res.links(obj)`TJ Holowaychukbefore adoptione6129d8

lines 43–63why, one step back
43 * Set Link header field with the given `links`.
44 *45 * Examples:46 *47 * res.links({48 * next: 'http://api.example.com/users?page=2',49 * last: 'http://api.example.com/users?page=5'50 * });51 *52 * @param {Object} links53 * @return {ServerResponse}54 * @api public55 */5657res.links = function(links){58 return this.set('Link', Object.keys(links).map(function(rel){59 return '<' + links[rel] + '>; rel="' + rel + '"';60 }).join(', '));61};6263/**

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 64–79why, one step back
64 * Send a response.
65 *66 * Examples:67 *68 * res.send(new Buffer('wahoo'));69 * res.send({ some: 'json' });70 * res.send('<p>some html</p>');71 * res.send(404, 'Sorry, cant find that');72 * res.send(404);73 *74 * @param {Mixed} body or status75 * @param {Mixed} body76 * @return {ServerResponse}77 * @api public78 */79

mergeTJ Holowaychukbefore adoption214f913

lines 80–130why, one step back
80res.send = function(body){
81 var req = this.req82 , head = 'HEAD' == req.method83 , len;8485 // allow status / body86 if (2 == arguments.length) {87 // res.send(body, status) backwards compat88 if ('number' != typeof body && 'number' == typeof arguments[1]) {89 this.statusCode = arguments[1];90 } else {91 this.statusCode = body;92 body = arguments[1];93 }94 }9596 switch (typeof body) {97 // response status98 case 'number':99 this.get('Content-Type') || this.type('txt');100 this.statusCode = body;101 body = http.STATUS_CODES[body];102 break;103 // string defaulting to html104 case 'string':105 if (!this.get('Content-Type')) {106 this.charset = this.charset || 'utf-8';107 this.type('html');108 }109 break;110 case 'boolean':111 case 'object':112 if (null == body) {113 body = '';114 } else if (Buffer.isBuffer(body)) {115 this.get('Content-Type') || this.type('bin');116 } else {117 return this.json(body);118 }119 break;120 }121122 // populate Content-Length123 if (undefined !== body && !this.get('Content-Length')) {124 this.set('Content-Length', len = Buffer.isBuffer(body)125 ? body.length126 : Buffer.byteLength(body));127 }128129 // ETag support130 // TODO: W/ support

Revert "add 'etag' option"TJ Holowaychukbefore adoption9bd86cd

line 131why, one step back
131 if (len > 1024) {

add etag utilTJ Holowaychukbefore adoptionf2f0976

lines 132–134why, one step back
132 if (!this.get('ETag')) {
133 this.set('ETag', etag(body));134 }

mergeTJ Holowaychukbefore adoption214f913

lines 135–143why, one step back
135 }
136137 // freshness138 if (req.fresh) this.statusCode = 304;139140 // strip irrelevant headers141 if (204 == this.statusCode || 304 == this.statusCode) {142 this.removeHeader('Content-Type');143 this.removeHeader('Content-Length');

Merge pull request #1451 from aweeks/fix-304-must-not-contain-bodyTJ Holowaychukbefore adoption3c4fd57

line 144why, one step back
144 this.removeHeader('Transfer-Encoding');

mergeTJ Holowaychukbefore adoption214f913

lines 145–151why, one step back
145 body = '';
146 }147148 // respond149 this.end(head ? null : body);150 return this;151};

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 152–171why, one step back
152
153/**154 * Send JSON response.155 *156 * Examples:157 *158 * res.json(null);159 * res.json({ user: 'tj' });160 * res.json(500, 'oh noes!');161 * res.json(404, 'I dont have that');162 *163 * @param {Mixed} obj or status164 * @param {Mixed} obj165 * @return {ServerResponse}166 * @api public167 */168169res.json = function(obj){170 // allow status / body171 if (2 == arguments.length) {

Added `res.json(obj, status)` support back for BCTJ Holowaychukbefore adoption70d6841

lines 172–178why, one step back
172 // res.json(body, status) backwards compat
173 if ('number' == typeof arguments[1]) {174 this.statusCode = arguments[1];175 } else {176 this.statusCode = obj;177 obj = arguments[1];178 }

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 179–180why, one step back
179 }
180

docsTJ Holowaychukbefore adoption2e8d44b

line 181why, one step back
181 // settings

add res.jsonp(). Closes #1307TJ Holowaychukbefore adoptiona6caa26

lines 182–185why, one step back
182 var app = this.app;
183 var replacer = app.get('json replacer');184 var spaces = app.get('json spaces');185 var body = JSON.stringify(obj, replacer, spaces);

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

line 186why, one step back
186

docsTJ Holowaychukbefore adoption2e8d44b

line 187why, one step back
187 // content-type

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

line 188why, one step back
188 this.charset = this.charset || 'utf-8';

refactorTJ Holowaychukbefore adoption88072a5

line 189why, one step back
189 this.get('Content-Type') || this.set('Content-Type', 'application/json');

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 190why, one step back
190

add res.jsonp(). Closes #1307TJ Holowaychukbefore adoptiona6caa26

lines 191–225why, one step back
191 return this.send(body);
192};193194/**195 * Send JSON response with JSONP callback support.196 *197 * Examples:198 *199 * res.jsonp(null);200 * res.jsonp({ user: 'tj' });201 * res.jsonp(500, 'oh noes!');202 * res.jsonp(404, 'I dont have that');203 *204 * @param {Mixed} obj or status205 * @param {Mixed} obj206 * @return {ServerResponse}207 * @api public208 */209210res.jsonp = function(obj){211 // allow status / body212 if (2 == arguments.length) {213 // res.json(body, status) backwards compat214 if ('number' == typeof arguments[1]) {215 this.statusCode = arguments[1];216 } else {217 this.statusCode = obj;218 obj = arguments[1];219 }220 }221222 // settings223 var app = this.app;224 var replacer = app.get('json replacer');225 var spaces = app.get('json spaces');

fix jsonp whitespace escape. Closes #1132Guillermo Rauchbefore adoption64a2349

lines 226–228why, one step back
226 var body = JSON.stringify(obj, replacer, spaces)
227 .replace(/\u2028/g, '\\u2028')228 .replace(/\u2029/g, '\\u2029');

add res.jsonp(). Closes #1307TJ Holowaychukbefore adoptiona6caa26

lines 229–233why, one step back
229 var callback = this.req.query[app.get('jsonp callback name')];
230231 // content-type232 this.charset = this.charset || 'utf-8';233 this.set('Content-Type', 'application/json');

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 234why, one step back
234

docsTJ Holowaychukbefore adoption2e8d44b

line 235why, one step back
235 // jsonp

add res.jsonp(). Closes #1307TJ Holowaychukbefore adoptiona6caa26

line 236why, one step back
236 if (callback) {

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

line 237why, one step back
237 this.set('Content-Type', 'text/javascript');

add cb && cb(payload) to `res.jsonp()`. Closes #1374TJ Holowaychukbefore adoptionb3936b9

lines 238–239why, one step back
238 var cb = callback.replace(/[^\[\]\w$.]/g, '');
239 body = cb + ' && ' + cb + '(' + body + ');';

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 240–246why, one step back
240 }
241242 return this.send(body);243};244245/**246 * Transfer the file at the given `path`.

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 247why, one step back
247 *

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 248–268why, one step back
248 * Automatically sets the _Content-Type_ response header field.
249 * The callback `fn(err)` is invoked when the transfer is complete250 * or when an error occurs. Be sure to check `res.sentHeader`251 * if you wish to attempt responding, as the header and some data252 * may have already been transferred.253 *254 * Options:255 *256 * - `maxAge` defaulting to 0257 * - `root` root directory for relative filenames258 *259 * Examples:260 *261 * The following example illustrates how `res.sendfile()` may262 * be used as an alternative for the `static()` middleware for263 * dynamic situations. The code backing `res.sendfile()` is actually264 * the same code, so HTTP cache support etc is identical.265 *266 * app.get('/user/:uid/photos/:file', function(req, res){267 * var uid = req.params.uid268 * , file = req.params.file;

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 269why, one step back
269 *

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 270–288why, one step back
270 * req.user.mayViewFilesFrom(uid, function(yes){
271 * if (yes) {272 * res.sendfile('/uploads/' + uid + '/' + file);273 * } else {274 * res.send(403, 'Sorry! you cant see that.');275 * }276 * });277 * });278 *279 * @param {String} path280 * @param {Object|Function} options or fn281 * @param {Function} fn282 * @api public283 */284285res.sendfile = function(path, options, fn){286 var self = this287 , req = self.req288 , next = this.req.next

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

lines 289–290why, one step back
289 , options = options || {}
290 , done;

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 291–297why, one step back
291
292 // support function as second arg293 if ('function' == typeof options) {294 fn = options;295 options = {};296 }297

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

lines 298–299why, one step back
298 // socket errors
299 req.socket.on('error', error);

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

line 300why, one step back
300

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

lines 301–304why, one step back
301 // errors
302 function error(err) {303 if (done) return;304 done = true;

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

line 305why, one step back
305

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

line 306why, one step back
306 // clean up

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption3865168

line 307why, one step back
307 cleanup();

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

line 308why, one step back
308 if (!self.headerSent) self.removeHeader('Content-Disposition');

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

line 309why, one step back
309

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

lines 310–311why, one step back
310 // callback available
311 if (fn) return fn(err);

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

line 312why, one step back
312

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

lines 313–314why, one step back
313 // list in limbo if there's no callback
314 if (self.headerSent) return;

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

line 315why, one step back
315

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

lines 316–318why, one step back
316 // delegate
317 next(err);318 }

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

line 319why, one step back
319

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

lines 320–322why, one step back
320 // streaming
321 function stream() {322 if (done) return;

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption3865168

line 323why, one step back
323 cleanup();

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

lines 324–326why, one step back
324 if (fn) self.on('finish', fn);
325 }326

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption3865168

lines 327–331why, one step back
327 // cleanup
328 function cleanup() {329 req.socket.removeListener('error', error);330 }331

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

line 332why, one step back
332 // transfer

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

lines 333–339why, one step back
333 var file = send(req, path);
334 if (options.root) file.root(options.root);335 file.maxage(options.maxAge || 0);336 file.on('error', error);337 file.on('directory', next);338 file.on('stream', stream);339 file.pipe(this);

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption3865168

line 340why, one step back
340 this.on('finish', cleanup);

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 341–350why, one step back
341};
342343/**344 * Transfer the file at the given `path` as an attachment.345 *346 * Optionally providing an alternate attachment `filename`,347 * and optional callback `fn(err)`. The callback is invoked348 * when the data transfer is complete, or when an error has349 * ocurred. Be sure to check `res.headerSent` if you plan to respond.350 *

Added res.download() testsTJ Holowaychukbefore adoption5840b42

line 351why, one step back
351 * This method uses `res.sendfile()`.

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 352–365why, one step back
352 *
353 * @param {String} path354 * @param {String|Function} filename or fn355 * @param {Function} fn356 * @api public357 */358359res.download = function(path, filename, fn){360 // support function as second arg361 if ('function' == typeof filename) {362 fn = filename;363 filename = null;364 }365

Added res.download() testsTJ Holowaychukbefore adoption5840b42

lines 366–368why, one step back
366 filename = filename || path;
367 this.set('Content-Disposition', 'attachment; filename="' + basename(filename) + '"');368 return this.sendfile(path, fn);

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 369–413why, one step back
369};
370371/**372 * Set _Content-Type_ response header with `type` through `mime.lookup()`373 * when it does not contain "/", or set the Content-Type to `type` otherwise.374 *375 * Examples:376 *377 * res.type('.html');378 * res.type('html');379 * res.type('json');380 * res.type('application/json');381 * res.type('png');382 *383 * @param {String} type384 * @return {ServerResponse} for chaining385 * @api public386 */387388res.contentType =389res.type = function(type){390 return this.set('Content-Type', ~type.indexOf('/')391 ? type392 : mime.lookup(type));393};394395/**396 * Respond to the Acceptable formats using an `obj`397 * of mime-type callbacks.398 *399 * This method uses `req.accepted`, an array of400 * acceptable types ordered by their quality values.401 * When "Accept" is not present the _first_ callback402 * is invoked, otherwise the first match is used. When403 * no match is performed the server responds with404 * 406 "Not Acceptable".405 *406 * Content-Type is set for you, however if you choose407 * you may alter this within the callback using `res.type()`408 * or `res.set('Content-Type', ...)`.409 *410 * res.format({411 * 'text/plain': function(){412 * res.send('hey');413 * },

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 414why, one step back
414 *

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 415–417why, one step back
415 * 'text/html': function(){
416 * res.send('<p>hey</p>');417 * },

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 418why, one step back
418 *

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 419–430why, one step back
419 * 'appliation/json': function(){
420 * res.send({ message: 'hey' });421 * }422 * });423 *424 * In addition to canonicalized MIME types you may425 * also use extnames mapped to these types:426 *427 * res.format({428 * text: function(){429 * res.send('hey');430 * },

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 431why, one step back
431 *

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 432–434why, one step back
432 * html: function(){
433 * res.send('<p>hey</p>');434 * },

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 435why, one step back
435 *

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 436–440why, one step back
436 * json: function(){
437 * res.send({ message: 'hey' });438 * }439 * });440 *

mention res.format() callbackTJ Holowaychukbefore adoptionfd42b5c

lines 441–442why, one step back
441 * By default Express passes an `Error`
442 * with a `.status` of 406 to `next(err)`

docsTJ Holowaychukbefore adoptiond2baf11

lines 443–445why, one step back
443 * if a match is not made. If you provide
444 * a `.default` callback it will be invoked445 * instead.

mention res.format() callbackTJ Holowaychukbefore adoptionfd42b5c

line 446why, one step back
446 *

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 447–451why, one step back
447 * @param {Object} obj
448 * @return {ServerResponse} for chaining449 * @api public450 */451

Added `.default()` support to `res.format()`TJ Holowaychukbefore adoption82b5b12

line 452why, one step back
452res.format = function(obj){

refactor res.format(), dont pass "default" to req.accepts()TJ Holowaychukbefore adoption8edf358

line 453why, one step back
453 var req = this.req

Added `.default()` support to `res.format()`TJ Holowaychukbefore adoption82b5b12

line 454why, one step back
454 , next = req.next;

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

line 455why, one step back
455

Added `.default()` support to `res.format()`TJ Holowaychukbefore adoption82b5b12

lines 456–457why, one step back
456 var fn = obj.default;
457 if (fn) delete obj.default;

refactor res.format(), dont pass "default" to req.accepts()TJ Holowaychukbefore adoption8edf358

line 458why, one step back
458 var keys = Object.keys(obj);

Added `.default()` support to `res.format()`TJ Holowaychukbefore adoption82b5b12

lines 459–461why, one step back
459
460 var key = req.accepts(keys);461

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 462–466why, one step back
462 this.set('Vary', 'Accept');
463464 if (key) {465 this.set('Content-Type', normalizeType(key));466 obj[key](req, this, next);

Fixed res.redirect() 406. Closes #1154TJ Holowaychukbefore adoption1311f2a

lines 467–468why, one step back
467 } else if (fn) {
468 fn();

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 469–496why, one step back
469 } else {
470 var err = new Error('Not Acceptable');471 err.status = 406;472 err.types = normalizeTypes(keys);473 next(err);474 }475476 return this;477};478479/**480 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.481 *482 * @param {String} filename483 * @return {ServerResponse}484 * @api public485 */486487res.attachment = function(filename){488 if (filename) this.type(extname(filename));489 this.set('Content-Disposition', filename490 ? 'attachment; filename="' + basename(filename) + '"'491 : 'attachment');492 return this;493};494495/**496 * Set header `field` to `val`, or pass

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption0acee67

line 497why, one step back
497 * an object of header fields.

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 498–503why, one step back
498 *
499 * Examples:500 *501 * res.set('Accept', 'application/json');502 * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });503 *

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 504why, one step back
504 * Aliased as `res.header()`.

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 505–511why, one step back
505 *
506 * @param {String|Object} field507 * @param {String} val508 * @return {ServerResponse} for chaining509 * @api public510 */511

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 512why, one step back
512res.set =

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 513–557why, one step back
513res.header = function(field, val){
514 if (2 == arguments.length) {515 this.setHeader(field, '' + val);516 } else {517 for (var key in field) {518 this.setHeader(key, '' + field[key]);519 }520 }521 return this;522};523524/**525 * Get value for header `field`.526 *527 * @param {String} field528 * @return {String}529 * @api public530 */531532res.get = function(field){533 return this.getHeader(field);534};535536/**537 * Clear cookie `name`.538 *539 * @param {String} name540 * @param {Object} options541 * @param {ServerResponse} for chaining542 * @api public543 */544545res.clearCookie = function(name, options){546 var opts = { expires: new Date(1), path: '/' };547 return this.cookie(name, '', options548 ? utils.merge(opts, options)549 : opts);550};551552/**553 * Set cookie `name` to `val`, with the given `options`.554 *555 * Options:556 *557 * - `maxAge` max-age in milliseconds, converted to `expires`

Added { signed: true } option to res.cookie()TJ Holowaychukbefore adoption1150a88

line 558why, one step back
558 * - `signed` sign the cookie

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 559–575why, one step back
559 * - `path` defaults to "/"
560 *561 * Examples:562 *563 * // "Remember Me" for 15 minutes564 * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });565 *566 * // save as above567 * res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })568 *569 * @param {String} name570 * @param {String|Object} val571 * @param {Options} options572 * @api public573 */574575res.cookie = function(name, val, options){

Merge pull request #1458 from gmethvin/cookie_optionsTJ Holowaychukbefore adoptiond4e56c1

line 576why, one step back
576 options = utils.merge({}, options);

Added { signed: true } option to res.cookie()TJ Holowaychukbefore adoption1150a88

lines 577–579why, one step back
577 var secret = this.req.secret;
578 var signed = options.signed;579 if (signed && !secret) throw new Error('connect.cookieParser("secret") required for signed cookies');

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

line 580why, one step back
580 if ('object' == typeof val) val = 'j:' + JSON.stringify(val);

update connectTJ Holowaychukbefore adoption612fc47

line 581why, one step back
581 if (signed) val = 's:' + sign(val, secret);

Merge pull request #1458 from gmethvin/cookie_optionsTJ Holowaychukbefore adoptiond4e56c1

lines 582–585why, one step back
582 if ('maxAge' in options) {
583 options.expires = new Date(Date.now() + options.maxAge);584 options.maxAge /= 1000;585 }

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

line 586why, one step back
586 if (null == options.path) options.path = '/';

coerce res.cookie values to stringsTJ Holowaychukbefore adoptionebf60d2

line 587why, one step back
587 this.set('Set-Cookie', cookie.serialize(name, String(val), options));

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 588–603why, one step back
588 return this;
589};590591/**592 * Redirect to the given `url` with optional response `status`593 * defaulting to 302.594 *595 * The given `url` can also be the name of a mapped url, for596 * example by default express supports "back" which redirects597 * to the _Referrer_ or _Referer_ headers or "/".598 *599 * Examples:600 *601 * res.redirect('/foo/bar');602 * res.redirect('http://example.com');603 * res.redirect(301, 'http://example.com');

add backwards compat for res.redirect() status. Closes #1336TJ Holowaychukbefore adoption56ffab0

line 604why, one step back
604 * res.redirect('http://example.com', 301);

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 605–609why, one step back
605 * res.redirect('../login'); // /blog/post/1 -> /blog/login
606 *607 * Mounting:608 *609 * When an application is mounted, and `res.redirect()`

refactor vhost exampleTJ Holowaychukbefore adoptionbb29da5

line 610why, one step back
610 * is given a path that does _not_ lead with "/". For

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 611–633why, one step back
611 * example suppose a "blog" app is mounted at "/blog",
612 * the following redirect would result in "/blog/login":613 *614 * res.redirect('login');615 *616 * While the leading slash would result in a redirect to "/login":617 *618 * res.redirect('/login');619 *620 * @param {String} url621 * @param {Number} code622 * @api public623 */624625res.redirect = function(url){626 var app = this.app627 , req = this.req628 , head = 'HEAD' == req.method629 , status = 302630 , body;631632 // allow status / url633 if (2 == arguments.length) {

add backwards compat for res.redirect() status. Closes #1336TJ Holowaychukbefore adoption56ffab0

lines 634–639why, one step back
634 if ('number' == typeof url) {
635 status = url;636 url = arguments[1];637 } else {638 status = arguments[1];639 }

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 640–648why, one step back
640 }
641642 // setup redirect map643 var map = { back: req.get('Referrer') || '/' };644645 // perform redirect646 url = map[url] || url;647648 // relative

add res.redirect("//foo.com") supportTJ Holowaychukbefore adoptiona93d375

line 649why, one step back
649 if (!~url.indexOf('://') && 0 != url.indexOf('//')) {

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 650–652why, one step back
650 var path = app.path();
651652 // relative to path

refactor res.redirect() relative checkTJ Holowaychukbefore adoption362c96c

line 653why, one step back
653 if ('.' == url[0]) {

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 654–663why, one step back
654 url = req.path + '/' + url;
655 // relative to mount-point656 } else if ('/' != url[0]) {657 url = path + '/' + url;658 }659 }660661 // Support text/{plain,html} by default662 this.format({663 text: function(){

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoptionc852693

line 664why, one step back
664 body = statusCodes[status] + '. Redirecting to ' + encodeURI(url);

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 665–667why, one step back
665 },
666667 html: function(){

escape res.redirect() linkTJ Holowaychukbefore adoptionbac0c64

lines 668–669why, one step back
668 var u = utils.escape(url);
669 body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + u + '">' + u + '</a></p>';

Added `.default()` support to `res.format()`TJ Holowaychukbefore adoption82b5b12

lines 670–673why, one step back
670 },
671672 default: function(){673 body = '';

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

line 674why, one step back
674 }

Added `.default()` support to `res.format()`TJ Holowaychukbefore adoption82b5b12

line 675why, one step back
675 });

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 676–679why, one step back
676
677 // Respond678 this.statusCode = status;679 this.set('Location', url);

Fixed res.redirect() 406. Closes #1154TJ Holowaychukbefore adoption1311f2a

line 680why, one step back
680 this.set('Content-Length', Buffer.byteLength(body));

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

lines 681–710why, one step back
681 this.end(head ? null : body);
682};683684/**685 * Render `view` with the given `options` and optional callback `fn`.686 * When a callback function is given a response will _not_ be made687 * automatically, otherwise a response of _200_ and _text/html_ is given.688 *689 * Options:690 *691 * - `cache` boolean hinting to the engine it should cache692 * - `filename` filename of the view being rendered693 *694 * @param {String} view695 * @param {Object|Function} options or callback function696 * @param {Function} fn697 * @api public698 */699700res.render = function(view, options, fn){701 var self = this702 , options = options || {}703 , req = this.req704 , app = req.app;705706 // support callback function as second arg707 if ('function' == typeof options) {708 fn = options, options = {};709 }710

remove app.locals.use and res.locals.useTJ Holowaychukbefore adoption4f7c4d1

line 711why, one step back
711 // merge res.locals

refactor res.render() to prevent clobbering "locals"TJ Holowaychukbefore adoption6bebe08

line 712why, one step back
712 options._locals = self.locals;

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

line 713why, one step back
713

remove app.locals.use and res.locals.useTJ Holowaychukbefore adoption4f7c4d1

lines 714–718why, one step back
714 // default callback to respond
715 fn = fn || function(err, str){716 if (err) return req.next(err);717 self.send(str);718 };

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

line 719why, one step back
719

remove app.locals.use and res.locals.useTJ Holowaychukbefore adoption4f7c4d1

lines 720–721why, one step back
720 // render
721 app.render(view, options, fn);

Added url rewriting testTJ Holowaychukbefore adoption9fda13b

line 722why, one step back
722};