Skip to content
Myr

Merge branch 'range'

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 · parents 426e80d, 4d01b4f · Browse files · 5 files+146 added−8 removed
This commit

State

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

Diff stats

5 files+146 added−8 removed

  • tests:+84 added−1 removed
  • other:+62 added−7 removed

5 files+146 added−8 removed

modified:lib/express/response.js+23 added-4 removed
13 unmodified lines
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;2021/**22 * Header fields supporting multiple values.
109 unmodified lines
132 */133134http.ServerResponse.prototype.sendfile = function(path, fn){135 var self = this,135 streamThreshold = this.app.set('stream threshold') || 32 * 1024;136 streamThreshold = this.app.set('stream threshold') || 32 * 1024,137 ranges = self.req.headers.range;138139 if (~path.indexOf('..')) this.send(403);140141 function error(err) {
7 unmodified lines
149150 fs.stat(path, function(err, stat){151 if (err) return error(err);152 if (stat.size >= streamThreshold) {153 var status = 200;154 if (ranges) ranges = parseRange(stat.size, ranges);155 if (ranges) {156 var stream = fs.createReadStream(path, ranges[0]),157 start = ranges[0].start,158 end = ranges[0].end;159 status = 206;160 self.header('Content-Range', 'bytes '161 + start162 + '-'163 + end164 + '/'165 + stat.size);166 self.header('Content-Length', end - start + 1);167 } else {151 var stream = fs.createReadStream(path);168 var stream = fs.createReadStream(path);169 self.header('Content-Length', stat.size);170 }171 self.contentType(path);153 self.header('Content-Length', stat.size);154 self.writeHead(200, self.headers);172 self.header('Accept-Ranges', 'bytes');173 self.writeHead(status, self.headers);174 pump(stream, self, function(err){175 fn && fn(err, path, true);176 });177 } else {
183 unmodified lines
modified:lib/express/utils.js+35 added-2 removed
modified:test/response.test.js+74 added
216 unmodified lines
217 { url: '/large.json' },218 { status: 200, headers: { 'Content-Length': 2535, 'Content-Type': 'application/json' }});219 },220 221 'test #sendfile() Accept-Ranges': function(assert){222 var app = express.createServer();223 224 app.set('stream threshold', 1024);225 226 app.get('/*', function(req, res, next){227 var file = req.params[0],228 path = __dirname + '/fixtures/' + file;229 res.sendfile(path);230 });231 232 assert.response(app,233 { url: '/large.json' },234 { headers: { 'Accept-Ranges': 'bytes' }});235 },236 237 'test #sendfile() Range': function(assert){238 var app = express.createServer();239 240 app.set('stream threshold', 1024);241 242 app.get('/*', function(req, res, next){243 var file = req.params[0],244 path = __dirname + '/fixtures/' + file;245 res.sendfile(path);246 });247248 assert.response(app,249 { url: '/large.json', headers: { 'Range': 'bytes=0-499' }},250 { headers: {251 'Content-Type': 'application/json',252 'Content-Length': 500,253 'Content-Range': 'bytes 0-499/2535'254 }, status: 206 });255 },256257 'test #sendfile() Range invalid syntax': function(assert){258 var app = express.createServer();259 260 app.set('stream threshold', 1024);261 262 app.get('/*', function(req, res, next){263 var file = req.params[0],264 path = __dirname + '/fixtures/' + file;265 res.sendfile(path);266 });267268 assert.response(app,269 { url: '/large.json', headers: { 'Range': 'basdytes=asdf' }},270 { headers: {271 'Content-Type': 'application/json',272 'Content-Length': 2535273 }, status: 200 });274 },275 276 'test #sendfile() Range invalid range': function(assert){277 var app = express.createServer();278 279 app.set('stream threshold', 1024);280 281 app.get('/*', function(req, res, next){282 var file = req.params[0],283 path = __dirname + '/fixtures/' + file;284 res.sendfile(path);285 });286287 assert.response(app,288 { url: '/large.json', headers: { 'Range': 'bytes=500-10' }},289 { headers: {290 'Content-Type': 'application/json',291 'Content-Length': 2535292 }, status: 200 });293 },294 295 'test #download()': function(assert){296 var app = express.createServer();297 298 app.get('/json', function(req, res, next){
82 unmodified lines
modified:test/utils.test.js+10 added-1 removed
modified:Makefile+4 added-1 removed
43 unmodified lines
44 -I support/jade/lib \45 -I support/ejs/lib \46 $(TESTFLAGS) \47 test/*.test.js4849test-cov:50 @TESTFLAGS=--cov $(MAKE) test5152docs: docs/api.html $(MANPAGES) $(HTMLDOCS)53 @ echo "... generating TOC"54 @./support/toc.js docs/guide.html5556docs/api.html: lib/express/*.js57 dox \58 --private \56 dox --title Express \59 --title Express \60 --desc "High performance web framework for [node](http://nodejs.org)." \61 $(shell find lib/express/* -type f) > $@6263%.1: %.md
14 unmodified lines