utilizing nodes progressive response header api
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 0cc6abf · Browse files · 1 file·+13 added−30 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+13 added−30 removed
- other:+13 added−30 removed
Provenance
- Commit
5052128- Tree
bade897- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
0cc6abf
1 file changed+13 added−30 removed
modified:lib/response.js+13 added-30 removed
16 unmodified lines17 , res = http.ServerResponse.prototype18 , mime = require('mime');19 20 /**21- * Header fields supporting multiple values.22- * 23- * @type Array24- */25-26-var multiple = ['Set-Cookie'];27-28-/**21 * Send a response with the given `body` and optional `headers` and `status` code.22 *23 * Examples:24 *22 unmodified lines47 // Defaults48 status = status || 200;49 50 // Allow 0 args as 20459- if (!arguments.length) {60- body = status = 204;61- }51+ if (!arguments.length) body = status = 204;52 53 // Determine content type54 switch (typeof body) {55 case 'number':66- if (!this.headers['Content-Type']) {56+ if (!this.header('Content-Type')) {57 this.contentType('.txt');58 }59 body = http.STATUS_CODES[status = body];60 break;61 case 'string':72- if (!this.headers['Content-Type']) {62+ if (!this.header('Content-Type')) {63 this.contentType('.html');64 }65 break;66 case 'object':67 if (body instanceof Buffer) {78- if (!this.headers['Content-Type']) {68+ if (!this.header('Content-Type')) {69 this.contentType('.bin');70 }71 } else {82- if (!this.headers['Content-Type']) {72+ if (!this.header('Content-Type')) {73 this.contentType('.json');74 }75 body = JSON.stringify(body);86- if (this.req.query.callback && this.app.settings['jsonp callback']) {76+ if (this.req.query.callback && this.app.set('jsonp callback')) {77 this.header('Content-Type', 'text/javascript');78 body = this.req.query.callback.replace(/[^\w$.]/g, '') + '(' + body + ');';79 }80 }81 break;82 }83 84 // Populate Content-Length95- if (!this.headers['Content-Length']) {85+ if (!this.header('Content-Length')) {86 this.header('Content-Length', body instanceof Buffer87 ? body.length88 : Buffer.byteLength(body));89 }8 unmodified lines98 }99 100 // Strip irrelevant headers101 if (204 === status) {112- delete this.headers['Content-Type'];113- delete this.headers['Content-Length'];102+ this.removeHeader('Content-Type');103+ this.removeHeader('Content-Length');104 }105 106 // Respond107 this.writeHead(status, this.headers);18 unmodified lines126 127 if (~path.indexOf('..')) return this.send(403);128 129 function error(err) {140- delete self.headers['Content-Disposition'];130+ self.removeHeader('Content-Disposition');131 if (fn) {132 fn(err, path);133 } else {134 self.req.next(err);95 unmodified lines230 231 /**232 * Set or get response header `name` with optional `val`.233 *244- * Headers that may be set multiple times (as indicated by the `multiple` array)245- * can be called with a value several times, for example:246- *247- * res.header('Set-Cookie', '...');248- * res.header('Set-Cookie', '...');249- *234 * @param {String} name235 * @param {String} val236 * @return {String}237 * @api public238 */239 240 res.header = function(name, val){241 if (val === undefined) {258- return this.headers[name];242+ return this.getHeader(name);243 } else {260- return ~multiple.indexOf(name)261- ? (this.headers[name] = this.headers[name] || []).push(val)262- : this.headers[name] = val;244+ this.setHeader(name, val);245+ return val;246 }247 };248 249 /**122 unmodified lines