Skip to content
Myr

Added conditional-GET support to res.send()

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 430699c · Browse files · 6 files+94 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+94 added−4 removed

  • other:+94 added−4 removed

6 files+94 added−4 removed

added:examples/big-view/index.js+24 added
added:examples/big-view/pets.jade+12 added
1style2 body {3 padding: 50px;4 font: 16px "Helvetica Neue", Helvetica;5 }67table8 for pet in pets9 tr10 td= pet.name11 td= pet.age12 td= pet.species
modified:lib/response.js+21 added-4 removed
16 unmodified lines
17 , normalizeType = require('./utils').normalizeType18 , normalizeTypes = require('./utils').normalizeTypes19 , statusCodes = http.STATUS_CODES20 , send = connect.static.send21 , crc32 = require('crc').crc3222 , mime = require('mime')23 , basename = path.basename24 , extname = path.extname25 , join = path.join;
37 unmodified lines
63 */6465res.send = function(body){66 var req = this.req66 , head = 'HEAD' == req.method;67 , head = 'HEAD' == req.method68 , len;6970 // allow status / body71 if (2 == arguments.length) {72 this.statusCode = body;
5 unmodified lines
7879 switch (typeof body) {80 // response status81 case 'number':82<<<<<<< HEAD83 this.get('Content-Type') || this.type('txt');84=======85 this.get('Content-Type') || this.type('.txt');86>>>>>>> feature/send-etag87 this.statusCode = body;88 body = http.STATUS_CODES[body];89 break;90 // string defaulting to html
7 unmodified lines
98 case 'object':99 if (null == body) {100 body = '';101 } else if (Buffer.isBuffer(body)) {102<<<<<<< HEAD103 this.get('Content-Type') || this.type('bin');104=======105 this.get('Content-Type') || this.type('.bin');106>>>>>>> feature/send-etag107 } else {108 return this.json(body);109 }110 break;111 }112113 // populate Content-Length114 if (undefined !== body && !this.get('Content-Length')) {105 this.set('Content-Length', Buffer.isBuffer(body)106 ? body.length107 : Buffer.byteLength(body));115 len = Buffer.isBuffer(body) ? body.length : Buffer.byteLength(body);116 this.set('Content-Length', len);117 }118119 // ETag support120 // TODO: W/ support121 // TODO: Buffer support for crc32122 if (len > 1024 && 'string' == typeof body) {123 this.set('ETag', crc32(body));124 if (req.fresh) this.statusCode = 304;125 }126127 // strip irrelevant headers128 if (204 == this.statusCode || 304 == this.statusCode) {129 this.removeHeader('Content-Type');130 this.removeHeader('Content-Length');
528 unmodified lines
modified:support/app.js+18 added
modified:support/bench+18 added
8 unmodified lines
9 | cut -d ' ' -f 7 \10 | xargs echo "$2:"11}1213bench_conditional() {14 ab -n 5000 -c 50 -H "If-None-Match: $3" -k -q http://127.0.0.1:8000$1 \15 | grep "Requests per" \16 | cut -d ' ' -f 7 \17 | xargs echo "$2:"18}1920sleep .521bench / "Hello World"22bench /blog "Mounted Hello World"23<<<<<<< HEAD24bench /blog/admin "Mounted 2 Hello World"25=======26>>>>>>> feature/send-etag27bench /middleware "Middleware"28bench /match "Router"29bench /render "Render"30bench /json "JSON tiny"31<<<<<<< HEAD32bench /json/15 "JSON small"33bench /json/50 "JSON medium"34bench /json/150 "JSON large"35=======36bench /json/small "JSON small"37bench /json/large "JSON large"38bench_conditional /json/large "JSON large etag" -13554356803940# 1.25 mb/s41>>>>>>> feature/send-etag4243kill -9 $pid
modified:package.json+1 added