merge
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 1021c86 · Browse files · 2 files·+76 added−2 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+76 added−2 removed
- other:+76 added−2 removed
Provenance
- Commit
214f913- Tree
7d0e981- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
1021c86
2 files changed+76 added−2 removed
modified:lib/response.js+75 added-1 removed
11 unmodified lines12 , statusCodes = http.STATUS_CODES13 , send = connect.static.send14 , cookie = require('cookie')15 , send = require('send')16+ , crc = require('crc')17 , mime = connect.mime18 , basename = path.basename19 , extname = path.extname20 , join = path.join;56 unmodified lines77 * @return {ServerResponse}78 * @api public79 */80 80-res.send = require('response-send');81+res.send = function(body){82+ var req = this.req83+ , head = 'HEAD' == req.method84+ , len;85 86+ // allow status / body87+ if (2 == arguments.length) {88+ // res.send(body, status) backwards compat89+ if ('number' != typeof body && 'number' == typeof arguments[1]) {90+ this.statusCode = arguments[1];91+ } else {92+ this.statusCode = body;93+ body = arguments[1];94+ }95+ }96+97+ // convert string objects to primitives98+ if (body instanceof String) body = body.toString();99+100+ switch (typeof body) {101+ // response status102+ case 'number':103+ this.get('Content-Type') || this.type('txt');104+ this.statusCode = body;105+ body = http.STATUS_CODES[body];106+ break;107+ // string defaulting to html108+ case 'string':109+ if (!this.get('Content-Type')) {110+ this.charset = this.charset || 'utf-8';111+ this.type('html');112+ }113+ break;114+ case 'boolean':115+ case 'object':116+ if (null == body) {117+ body = '';118+ } else if (Buffer.isBuffer(body)) {119+ this.get('Content-Type') || this.type('bin');120+ } else {121+ return this.json(body);122+ }123+ break;124+ }125+126+ // populate Content-Length127+ if (undefined !== body && !this.get('Content-Length')) {128+ this.set('Content-Length', len = Buffer.isBuffer(body)129+ ? body.length130+ : Buffer.byteLength(body));131+ }132+133+ // ETag support134+ // TODO: W/ support135+ if (len > 1024) {136+ if (!this.get('ETag')) this.set('ETag', Buffer.isBuffer(body)137+ ? crc.buffer.crc32(body)138+ : crc.crc32(body));139+ }140+141+ // freshness142+ if (req.fresh) this.statusCode = 304;143+144+ // strip irrelevant headers145+ if (204 == this.statusCode || 304 == this.statusCode) {146+ this.removeHeader('Content-Type');147+ this.removeHeader('Content-Length');148+ body = '';149+ }150+151+ // respond152+ this.end(head ? null : body);153+ return this;154+};155+156 /**157 * Send JSON response.158 *159 * Examples:518 unmodified linesmodified:package.json+1 added-1 removed
11 unmodified lines12131415161617181920212234 unmodified lines11 unmodified lines "dependencies": { "connect": "2.4.1", "commander": "0.6.1", "range-parser": "0.0.4", "response-send": "0.0.1", "mkdirp": "0.3.3", "cookie": "0.0.3", "crc": "0.2.0", "fresh": "0.1.0", "methods": "0.0.1", "send": "0.0.3", "debug": "*"34 unmodified lines