Skip to content
Myr

2 spaces in response.js

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 0fc4101 · Browse files · 1 file+149 added−150 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+149 added−150 removed

  • other:+149 added−150 removed

1 file+149 added−150 removed

modified:lib/express/response.js+149 added-150 removed
7 unmodified lines
8/**9 * Module dependencies.10 */1112var fs = require('fs'),13 http = require('http'),14 path = require('path'),15 pump = require('sys').pump,16 utils = require('connect/utils'),17 mime = require('connect/utils').mime,18 parseRange = require('./utils').parseRange,19 Buffer = require('buffer').Buffer;12var fs = require('fs')13 , http = require('http')14 , path = require('path')15 , pump = require('sys').pump16 , utils = require('connect/utils')17 , mime = require('connect/utils').mime18 , parseRange = require('./utils').parseRange;1920/**21 * Header fields supporting multiple values.22 *
22 unmodified lines
45 * @api public46 */4748http.ServerResponse.prototype.send = function(body, headers, status){50 // Allow status as second arg51 if (typeof headers === 'number') {52 status = headers,53 headers = null;54 }49 // Allow status as second arg50 if (typeof headers === 'number') {51 status = headers,52 headers = null;53 }5456 // Defaults57 status = status || 200;55 // Defaults56 status = status || 200;5759 // Allow 0 args as 20460 if (!arguments.length) {61 body = status = 204;62 }58 // Allow 0 args as 20459 if (!arguments.length) {60 body = status = 204;61 }6264 // Determine content type65 switch (typeof body) {66 case 'number':67 if (!this.headers['Content-Type']) {68 this.contentType('.txt');69 }70 body = http.STATUS_CODES[status = body];71 break;72 case 'string':73 if (!this.headers['Content-Type']) {74 this.contentType('.html');75 }76 break;77 case 'object':78 if (body instanceof Buffer) {79 if (!this.headers['Content-Type']) {80 this.contentType('.bin');81 }82 } else {83 if (!this.headers['Content-Type']) {84 this.contentType('.json');85 }86 body = JSON.stringify(body);87 if (this.req.query.callback && this.app.settings['jsonp callback']) {88 body = this.req.query.callback + '(' + body + ');';89 }90 }91 break;92 }63 // Determine content type64 switch (typeof body) {65 case 'number':66 if (!this.headers['Content-Type']) {67 this.contentType('.txt');68 }69 body = http.STATUS_CODES[status = body];70 break;71 case 'string':72 if (!this.headers['Content-Type']) {73 this.contentType('.html');74 }75 break;76 case 'object':77 if (body instanceof Buffer) {78 if (!this.headers['Content-Type']) {79 this.contentType('.bin');80 }81 } else {82 if (!this.headers['Content-Type']) {83 this.contentType('.json');84 }85 body = JSON.stringify(body);86 if (this.req.query.callback && this.app.settings['jsonp callback']) {87 body = this.req.query.callback + '(' + body + ');';88 }89 }90 break;91 }9294 // Populate Content-Length95 if (!this.headers['Content-Length']) {96 this.header('Content-Length', body instanceof Buffer97 ? body.length98 : Buffer.byteLength(body));99 }93 // Populate Content-Length94 if (!this.headers['Content-Length']) {95 this.header('Content-Length', body instanceof Buffer96 ? body.length97 : Buffer.byteLength(body));98 }99101 // Merge headers passed102 if (headers) {103 var fields = Object.keys(headers);104 for (var i = 0, len = fields.length; i < len; ++i) {105 var field = fields[i];106 this.header(field, headers[field]);107 }100 // Merge headers passed101 if (headers) {102 var fields = Object.keys(headers);103 for (var i = 0, len = fields.length; i < len; ++i) {104 var field = fields[i];105 this.header(field, headers[field]);106 }107 }108110 // Strip irrelevant headers111 if (204 === status) {112 delete this.headers['Content-Type'];113 delete this.headers['Content-Length'];114 }109 // Strip irrelevant headers110 if (204 === status) {111 delete this.headers['Content-Type'];112 delete this.headers['Content-Length'];113 }114116 // Respond117 this.writeHead(status, this.headers);118 this.end('HEAD' == this.req.method ? undefined : body);115 // Respond116 this.writeHead(status, this.headers);117 this.end('HEAD' == this.req.method ? undefined : body);118};119120/**121 * Transfer the file at the given `path`. Automatically sets
11 unmodified lines
133 * @api public134 */135136http.ServerResponse.prototype.sendfile = function(path, fn){138 var self = this,139 streamThreshold = this.app.set('stream threshold') || 32 * 1024,140 ranges = self.req.headers.range;137 var self = this138 , streamThreshold = this.app.set('stream threshold') || 32 * 1024139 , ranges = self.req.headers.range;140142 if (~path.indexOf('..')) this.send(403);141 if (~path.indexOf('..')) this.send(403);142144 function error(err) {145 delete self.headers['Content-Disposition'];146 if (fn) {147 fn(err, path);148 } else {149 self.req.next(err);150 }143 function error(err) {144 delete self.headers['Content-Disposition'];145 if (fn) {146 fn(err, path);147 } else {148 self.req.next(err);149 }150 }151153 fs.stat(path, function(err, stat){152 fs.stat(path, function(err, stat){153 if (err) return error(err);154 if (stat.size >= streamThreshold) {155 var status = 200;156 if (ranges) ranges = parseRange(stat.size, ranges);157 if (ranges) {158 var stream = fs.createReadStream(path, ranges[0])159 , start = ranges[0].start160 , end = ranges[0].end;161 status = 206;162 self.header('Content-Range', 'bytes '163 + start164 + '-'165 + end166 + '/'167 + stat.size);168 self.header('Content-Length', end - start + 1);169 } else {170 var stream = fs.createReadStream(path);171 self.header('Content-Length', stat.size);172 }173 self.contentType(path);174 self.header('Accept-Ranges', 'bytes');175 self.writeHead(status, self.headers);176 pump(stream, self, function(err){177 fn && fn(err, path, true);178 });179 } else {180 fs.readFile(path, function(err, buf){181 if (err) return error(err);155 if (stat.size >= streamThreshold) {156 var status = 200;157 if (ranges) ranges = parseRange(stat.size, ranges);158 if (ranges) {159 var stream = fs.createReadStream(path, ranges[0]),160 start = ranges[0].start,161 end = ranges[0].end;162 status = 206;163 self.header('Content-Range', 'bytes '164 + start165 + '-'166 + end167 + '/'168 + stat.size);169 self.header('Content-Length', end - start + 1);170 } else {171 var stream = fs.createReadStream(path);172 self.header('Content-Length', stat.size);173 }174 self.contentType(path);175 self.header('Accept-Ranges', 'bytes');176 self.writeHead(status, self.headers);177 pump(stream, self, function(err){178 fn && fn(err, path, true);179 });180 } else {181 fs.readFile(path, function(err, buf){182 if (err) return error(err);183 self.contentType(path);184 self.send(buf);185 fn && fn(null, path);186 });187 }188 });182 self.contentType(path);183 self.send(buf);184 fn && fn(null, path);185 });186 }187 });188};189190/**191 * Set _Content-Type_ response header passed through `mime.type()`.
9 unmodified lines
201 * @api public202 */203204http.ServerResponse.prototype.contentType = function(type){206 return this.header('Content-Type', mime.type(type));205 return this.header('Content-Type', mime.type(type));206};207208/**209 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.
3 unmodified lines
213 * @api public214 */215216http.ServerResponse.prototype.attachment = function(filename){218 this.header('Content-Disposition', filename219 ? 'attachment; filename="' + path.basename(filename) + '"'220 : 'attachment');221 return this;217 this.header('Content-Disposition', filename218 ? 'attachment; filename="' + path.basename(filename) + '"'219 : 'attachment');220 return this;221};222223/**224 * Transfer the file at the given `path`, with optional
7 unmodified lines
232 * @api public233 */234235http.ServerResponse.prototype.download = function(path, filename, fn){237 this.attachment(filename || path).sendfile(path, fn);236 this.attachment(filename || path).sendfile(path, fn);237};238239/**240 * Set or get response header `name` with optional `val`.
10 unmodified lines
251 * @api public252 */253254http.ServerResponse.prototype.header = function(name, val){256 if (val === undefined) {257 return this.headers[name];255 if (val === undefined) {256 return this.headers[name];257 } else {258 if (this.headers[name] && ~multiple.indexOf(name)) {259 return this.headers[name] += '\r\n' + name + ': ' + val;260 } else {259 if (this.headers[name] && ~multiple.indexOf(name)) {260 return this.headers[name] += '\r\n' + name + ': ' + val;261 } else {262 return this.headers[name] = val;261 return this.headers[name] = val;263 }262 }263 }264};265266/**267 * Clear cookie `name`.
2 unmodified lines
270 * @api public271 */272273http.ServerResponse.prototype.clearCookie = function(name){275 this.cookie(name, '', { expires: new Date(1) });274 this.cookie(name, '', { expires: new Date(1) });275};276277/**278 * Set cookie `name` to `val`.
9 unmodified lines
288 * @api public289 */290291http.ServerResponse.prototype.cookie = function(name, val, options){293 var cookie = utils.serializeCookie(name, val, options);294 this.header('Set-Cookie', cookie);292 var cookie = utils.serializeCookie(name, val, options);293 this.header('Set-Cookie', cookie);294};295296/**297 * Redirect to the given `url` with optional response `status`
36 unmodified lines
334 * @api public335 */336337http.ServerResponse.prototype.redirect = function(url, status){339 var basePath = this.app.set('home') || '/';338 var basePath = this.app.set('home') || '/';339341 // Setup redirect map342 var map = {343 back: this.req.headers.referrer || this.req.headers.referer || basePath,344 home: basePath345 };340 // Setup redirect map341 var map = {342 back: this.req.headers.referrer || this.req.headers.referer || basePath,343 home: basePath344 };345347 // Support custom redirect map348 map.__proto__ = this.app.redirects;346 // Support custom redirect map347 map.__proto__ = this.app.redirects;348350 // Attempt mapped redirect351 var mapped = typeof map[url] === 'function'352 ? map[url](this.req, this)353 : map[url];349 // Attempt mapped redirect350 var mapped = typeof map[url] === 'function'351 ? map[url](this.req, this)352 : map[url];353355 // Perform redirect356 url = mapped || url;354 // Perform redirect355 url = mapped || url;356358 // Respond359 this.send('Redirecting to ' + url, {360 'Content-Type': 'text/plain',361 'Location': url362 }, status || 302);357 // Respond358 this.send('Redirecting to ' + url, {359 'Content-Type': 'text/plain'360 , 'Location': url361 }, status || 302);362};