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
Provenance
- Commit
fab9af2- Tree
02217ec- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent 1
426e80d- Parent 2
4d01b4f
5 files changed+146 added−8 removed
modified:lib/express/response.js+23 added-4 removed
13 unmodified lines14 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;20 21 /**22 * Header fields supporting multiple values.109 unmodified lines132 */133 134 http.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;138 139 if (~path.indexOf('..')) this.send(403);140 141 function error(err) {7 unmodified lines149 150 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 linesmodified:lib/express/utils.js+35 added-2 removed
14 unmodified lines151617181919202122236 unmodified lines30313233343435363738394041424344454647484950515253545556575859606162636465666768697071727374757614 unmodified lines * [A](B) becomes <a href="B">A</a> * * @param {String} str * @return {String} * @api public * @api private */
exports.miniMarkdown = function(str){ return String(str)6 unmodified lines * Escape special characters in the given string of html. * * @param {String} html * @return {String} * @api public * @api private */
exports.htmlEscape = function(html) { return String(html) .replace(/&/g, '&') .replace(/"/g, '"') .replace(/</g, '<') .replace(/>/g, '>');};
/** * Parse "Range" header `str` relative to the given file `size`. * * @param {Number} size * @param {String} str * @return {Array} * @api private */
exports.parseRange = function(size, str){ var valid = true; var arr = str.substr(6).split(',').map(function(range){ var range = range.split('-'), start = parseInt(range[0], 10), end = parseInt(range[1], 10);
// -500 if (isNaN(start)) { start = size - end; end = size - 1; // 500- } else if (isNaN(end)) { end = size - 1; }
// Invalid if (isNaN(start) || isNaN(end) || start > end) valid = false;
return { start: start, end: end }; }); return valid ? arr : undefined;};No newline at end of fileNo newline at end of filemodified:test/response.test.js+74 added
216 unmodified lines217 { 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+ });247+248+ 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+ },256+257+ '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+ });267+268+ 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+ });286+287+ 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 linesmodified:test/utils.test.js+10 added-1 removed
4 unmodified lines56789910111213141516171819202122232425264 unmodified lines
var utils = require('express/utils');
module.exports = { 'test miniMarkdown()': function(assert){ 'test .miniMarkdown()': function(assert){ assert.equal('<em>foo</em>', utils.miniMarkdown('_foo_')); assert.equal('<em>foo</em>', utils.miniMarkdown('*foo*')); assert.equal('<strong>foo</strong>', utils.miniMarkdown('**foo**')); assert.equal('<strong>foo</strong>', utils.miniMarkdown('__foo__')); assert.equal('<em>foo</em>_', utils.miniMarkdown('_foo__')); assert.equal('<a href="/login">User Login</a>', utils.miniMarkdown('[User Login](/login)')); }, 'test .parseRange()': function(assert){ assert.eql([{start: 0, end: 499 }], utils.parseRange(1000, 'bytes=0-499')); assert.eql([{start: 40, end: 80 }], utils.parseRange(1000, 'bytes=40-80')); assert.eql([{start: 500, end: 999 }], utils.parseRange(1000, 'bytes=-500')); assert.eql([{start: 500, end: 999 }], utils.parseRange(1000, 'bytes=500-')); assert.eql([{start: 0, end: 0 }], utils.parseRange(1000, 'bytes=0-0')); assert.eql([{start: 999, end: 999 }], utils.parseRange(1000, 'bytes=-1')); }};No newline at end of fileNo newline at end of filemodified:Makefile+4 added-1 removed
43 unmodified lines44 -I support/jade/lib \45 -I support/ejs/lib \46 $(TESTFLAGS) \47 test/*.test.js48+49 test-cov:50 @TESTFLAGS=--cov $(MAKE) test51 52 docs: docs/api.html $(MANPAGES) $(HTMLDOCS)53 @ echo "... generating TOC"54 @./support/toc.js docs/guide.html55 56 docs/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) > $@62 63 %.1: %.md14 unmodified lines