Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added conditional-GET support to res.send()

  • Loading branch information...
commit a58e3deac27fca9ddbb7e389fdfe7be3aebea639 1 parent 430699c
@tj tj authored
View
24 examples/big-view/index.js
@@ -0,0 +1,24 @@
+
+var express = require('../..')
+ , app = express();
+
+app.set('views', __dirname);
+app.set('view engine', 'jade');
+
+var pets = [];
+
+var n = 1000;
+while (n--) {
+ pets.push({ name: 'Tobi', age: 2, species: 'ferret' });
+ pets.push({ name: 'Loki', age: 1, species: 'ferret' });
+ pets.push({ name: 'Jane', age: 6, species: 'ferret' });
+}
+
+app.use(express.logger('dev'));
+
+app.get('/', function(req, res){
+ res.render('pets', { pets: pets });
+});
+
+app.listen(3000);
+console.log('Express listening on port 3000');
View
12 examples/big-view/pets.jade
@@ -0,0 +1,12 @@
+style
+ body {
+ padding: 50px;
+ font: 16px "Helvetica Neue", Helvetica;
+ }
+
+table
+ for pet in pets
+ tr
+ td= pet.name
+ td= pet.age
+ td= pet.species
View
25 lib/response.js
@@ -18,6 +18,7 @@ var fs = require('fs')
, normalizeTypes = require('./utils').normalizeTypes
, statusCodes = http.STATUS_CODES
, send = connect.static.send
+ , crc32 = require('crc').crc32
, mime = require('mime')
, basename = path.basename
, extname = path.extname
@@ -63,7 +64,8 @@ res.status = function(code){
res.send = function(body){
var req = this.req
- , head = 'HEAD' == req.method;
+ , head = 'HEAD' == req.method
+ , len;
// allow status / body
if (2 == arguments.length) {
@@ -77,7 +79,11 @@ res.send = function(body){
switch (typeof body) {
// response status
case 'number':
+<<<<<<< HEAD
this.get('Content-Type') || this.type('txt');
+=======
+ this.get('Content-Type') || this.type('.txt');
+>>>>>>> feature/send-etag
this.statusCode = body;
body = http.STATUS_CODES[body];
break;
@@ -93,7 +99,11 @@ res.send = function(body){
if (null == body) {
body = '';
} else if (Buffer.isBuffer(body)) {
+<<<<<<< HEAD
this.get('Content-Type') || this.type('bin');
+=======
+ this.get('Content-Type') || this.type('.bin');
+>>>>>>> feature/send-etag
} else {
return this.json(body);
}
@@ -102,9 +112,16 @@ res.send = function(body){
// populate Content-Length
if (undefined !== body && !this.get('Content-Length')) {
- this.set('Content-Length', Buffer.isBuffer(body)
- ? body.length
- : Buffer.byteLength(body));
+ len = Buffer.isBuffer(body) ? body.length : Buffer.byteLength(body);
+ this.set('Content-Length', len);
+ }
+
+ // ETag support
+ // TODO: W/ support
+ // TODO: Buffer support for crc32
+ if (len > 1024 && 'string' == typeof body) {
+ this.set('ETag', crc32(body));
+ if (req.fresh) this.statusCode = 304;
}
// strip irrelevant headers
View
1  package.json
@@ -14,6 +14,7 @@
"commander": "0.5.2",
"mime": "1.2.5",
"mkdirp": "0.3.1",
+ "crc": "0.1.0",
"debug": "*"
},
"devDependencies": {
View
18 support/app.js
@@ -9,18 +9,36 @@ var app = express()
, blog = express()
, admin = express();
+<<<<<<< HEAD
blog.use('/admin', admin);
+=======
+// app.use(express.logger('dev'))
+>>>>>>> feature/send-etag
app.use('/blog', blog);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.locals.self = true;
+var repo = require('../package.json');
+
app.get('/render', function(req, res){
res.render('hello');
});
+<<<<<<< HEAD
admin.get('/', function(req, res){
res.send('Hello World\n');
+=======
+app.get('/json/small', function(req, res){
+ res.send(repo);
+});
+
+app.get('/json/large', function(req, res){
+ var repos = [];
+ var n = 10;
+ while (n--) repos.push(repo);
+ res.send(repos);
+>>>>>>> feature/send-etag
});
blog.get('/', function(req, res){
View
18 support/bench
@@ -10,16 +10,34 @@ bench() {
| xargs echo "$2:"
}
+bench_conditional() {
+ ab -n 5000 -c 50 -H "If-None-Match: $3" -k -q http://127.0.0.1:8000$1 \
+ | grep "Requests per" \
+ | cut -d ' ' -f 7 \
+ | xargs echo "$2:"
+}
+
sleep .5
bench / "Hello World"
bench /blog "Mounted Hello World"
+<<<<<<< HEAD
bench /blog/admin "Mounted 2 Hello World"
+=======
+>>>>>>> feature/send-etag
bench /middleware "Middleware"
bench /match "Router"
bench /render "Render"
bench /json "JSON tiny"
+<<<<<<< HEAD
bench /json/15 "JSON small"
bench /json/50 "JSON medium"
bench /json/150 "JSON large"
+=======
+bench /json/small "JSON small"
+bench /json/large "JSON large"
+bench_conditional /json/large "JSON large etag" -1355435680
+
+# 1.25 mb/s
+>>>>>>> feature/send-etag
kill -9 $pid
Please sign in to comment.
Something went wrong with that request. Please try again.