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
Provenance
- Commit
347aa56- Tree
6a2539b- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
0fc4101
1 file changed+149 added−150 removed
modified:lib/express/response.js+149 added-150 removed
7 unmodified lines8 /**9 * Module dependencies.10 */11 12-var 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;12+var 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;19 20 /**21 * Header fields supporting multiple values.22 * 22 unmodified lines45 * @api public46 */47 48 http.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+ }54 56- // Defaults57- status = status || 200;55+ // Defaults56+ status = status || 200;57 59- // 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+ }62 64- // 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+ }92 94- // 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+ }99 101- // 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+ }108 110- // 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+ }114 116- // 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 };119 120 /**121 * Transfer the file at the given `path`. Automatically sets 11 unmodified lines133 * @api public134 */135 136 http.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;140 142- if (~path.indexOf('..')) this.send(403);141+ if (~path.indexOf('..')) this.send(403);142 144- 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+ }151 153- 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 };189 190 /**191 * Set _Content-Type_ response header passed through `mime.type()`.9 unmodified lines201 * @api public202 */203 204 http.ServerResponse.prototype.contentType = function(type){206- return this.header('Content-Type', mime.type(type));205+ return this.header('Content-Type', mime.type(type));206 };207 208 /**209 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.3 unmodified lines213 * @api public214 */215 216 http.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 };222 223 /**224 * Transfer the file at the given `path`, with optional 7 unmodified lines232 * @api public233 */234 235 http.ServerResponse.prototype.download = function(path, filename, fn){237- this.attachment(filename || path).sendfile(path, fn);236+ this.attachment(filename || path).sendfile(path, fn);237 };238 239 /**240 * Set or get response header `name` with optional `val`.10 unmodified lines251 * @api public252 */253 254 http.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 };265 266 /**267 * Clear cookie `name`.2 unmodified lines270 * @api public271 */272 273 http.ServerResponse.prototype.clearCookie = function(name){275- this.cookie(name, '', { expires: new Date(1) });274+ this.cookie(name, '', { expires: new Date(1) });275 };276 277 /**278 * Set cookie `name` to `val`.9 unmodified lines288 * @api public289 */290 291 http.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 };295 296 /**297 * Redirect to the given `url` with optional response `status`36 unmodified lines334 * @api public335 */336 337 http.ServerResponse.prototype.redirect = function(url, status){339- var basePath = this.app.set('home') || '/';338+ var basePath = this.app.set('home') || '/';339 341- // 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+ };345 347- // Support custom redirect map348- map.__proto__ = this.app.redirects;346+ // Support custom redirect map347+ map.__proto__ = this.app.redirects;348 350- // 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];353 355- // Perform redirect356- url = mapped || url;354+ // Perform redirect355+ url = mapped || url;356 358- // 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 };