Skip to content
Myr

refactoring to use connects static server

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 9099c61 · Browse files · 3 files+60 added−118 removed
This commit

State

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

Diff stats

3 files+60 added−118 removed

  • tests:+43 added−55 removed
  • other:+17 added−63 removed

3 files+60 added−118 removed

modified:lib/response.js+17 added-63 removed
10 unmodified lines
1112var fs = require('fs')13 , http = require('http')14 , path = require('path')15 , utils = require('connect').utils15 , connect = require('connect')16 , utils = connect.utils17 , parseRange = require('./utils').parseRange18 , res = http.ServerResponse.prototype19 , send = connect.static.send20 , mime = require('mime');2122/**23 * Send a response with the given `body` and optional `headers` and `status` code.
87 unmodified lines
111};112113/**114 * Transfer the file at the given `path`. Automatically sets 113 * the _Content-Type_ response header via `res.contentType()`. 115 * the _Content-Type_ response header field. `next()` is called116 * when `path` is a directory, or when an error occurs.117 *115 * Accepts an optional callback `fn(err, path, stream)`.118 * Options:119 *120 * - `maxAge` defaulting to 0121 * - `root` root directory for relative filenames122 *123 * @param {String} path118 * @param {Function} fn124 * @param {Object} options125 * @api public126 */127122res.sendfile = function(path, fn){123 var self = this124 , ranges = self.req.headers.range125 , head = 'HEAD' == self.req.method;126127 if (~path.indexOf('..')) return this.send(403);128129 function error(err) {130 self.removeHeader('Content-Disposition');131 if (fn) {132 fn(err, path);133 } else {134 self.req.next(err);135 }136 }137138 fs.stat(path, function(err, stat){139 if (err) return error(err);140 var status = 200;141142 // We have a Range request143 if (ranges) {144 ranges = parseRange(stat.size, ranges);145 // Valid146 if (ranges) {147 var stream = fs.createReadStream(path, ranges[0])148 , start = ranges[0].start149 , end = ranges[0].end;150 status = 206;151 self.header('Content-Range', 'bytes '152 + start153 + '-'154 + end155 + '/'156 + stat.size);157 // Invalid158 } else {159 return self.send(416);160 }161 // Stream the entire file162 } else {163 var stream = fs.createReadStream(path);164 self.header('Content-Length', stat.size);165 }166167 // Transfer168 self.contentType(path);169 self.header('Accept-Ranges', 'bytes');170 self.writeHead(status, self.headers);171 if (head) return self.end();172 stream.pipe(self);173 fn && fn(null, path, stream);174 });128res.sendfile = function(path, options){129 options = options || {};130 options.path = path;131 send(this.req, this, this.req.next, options);132};133134/**135 * Set _Content-Type_ response header passed through `mime.lookup()`.
34 unmodified lines
170};171172/**173 * Transfer the file at the given `path`, with optional 217 * `filename` as an attachment. Once transferred, or if an218 * error occurs `fn` is called with the error and path.174 * `filename` as an attachment.175 *176 * @param {String} path177 * @param {String} filename222 * @param {Function} fn178 * @return {Type}179 * @api public180 */181227res.download = function(path, filename, fn){228 this.attachment(filename || path).sendfile(path, fn);182res.download = function(path, filename){183 this.attachment(filename || path).sendfile(path);184};185186/**187 * Set or get response header `name` with optional `val`.
134 unmodified lines
322 return undefined === val323 ? this.locals[name]324 : this.locals[name] = val;325};371
modified:support/connectnot shown

Its text is not shown, modified.

modified:test/response.test.js+43 added-55 removed
227 unmodified lines
228 var app = express.createServer();229230 app.get('/*', function(req, res, next){231 var file = req.params[0]232 , filePath = __dirname + '/fixtures/' + file;233 res.sendfile(filePath, function(err, path){234 assert.equal(path, filePath);235 if (err) next(err);236 });232 , path = __dirname + '/fixtures/' + file;233 res.sendfile(path);234 });235 236 app.use(express.errorHandler());237
7 unmodified lines
245 { url: '/hello.haml' },246 { body: '%p Hello World', status: 200, headers: { 'Content-Type': 'application/octet-stream' }});247 assert.response(app,248 { url: '/doesNotExist' },252 { body: 'Internal Server Error', status: 500 });249 { body: 'Cannot GET /doesNotExist', status: 404 });250 assert.response(app,251 { url: '/partials' },255 { body: 'Internal Server Error', status: 500 });252 { body: 'Cannot GET /partials', status: 404 });253 assert.response(app,254 { url: '/large.json' },255 { status: 200, headers: { 'Content-Type': 'application/json' }});256 },257 258 'test #sendfile() Accept-Ranges': function(){259 var app = express.createServer();260 261 app.get('/*', function(req, res, next){262 var file = req.params[0]263 , path = __dirname + '/fixtures/' + file;264 res.sendfile(path);265 });266 267 assert.response(app,268 { url: '/large.json' },269 { headers: { 'Accept-Ranges': 'bytes' }});270 },271 272 'test #sendfile() Range': function(){273 var app = express.createServer();274 275 app.get('/*', function(req, res, next){276 var file = req.params[0]277 , path = __dirname + '/fixtures/' + file;278 res.sendfile(path);279 });280 281 assert.response(app,282 { url: '/large.json', headers: { 'Range': 'bytes=0-499' }},283 { headers: {284 'Content-Type': 'application/json',285 'Content-Range': 'bytes 0-499/2535'286 }, status: 206 });287 },288 261 'test #sendfile() Accept-Ranges': function(){289 'test #sendfile() Range invalid syntax': function(){290 var app = express.createServer();291 292 app.get('/*', function(req, res, next){293 var file = req.params[0]294 , path = __dirname + '/fixtures/' + file;295 res.sendfile(path);296 });269 270 assert.response(app,271 { url: '/large.json' },272 { headers: { 'Accept-Ranges': 'bytes' }});273 },297 275 'test #sendfile() Range': function(){276 var app = express.createServer();277 278 app.get('/*', function(req, res, next){279 var file = req.params[0]280 , path = __dirname + '/fixtures/' + file;281 res.sendfile(path);282 });283298 assert.response(app,285 { url: '/large.json', headers: { 'Range': 'bytes=0-499' }},286 { headers: {287 'Content-Type': 'application/json',288 'Content-Range': 'bytes 0-499/2535'289 }, status: 206 });290 },291292 'test #sendfile() Range invalid syntax': function(){293 var app = express.createServer();294 295 app.get('/*', function(req, res, next){296 var file = req.params[0]297 , path = __dirname + '/fixtures/' + file;298 res.sendfile(path);299 });300301 assert.response(app,299 { url: '/large.json', headers: { 'Range': 'basdytes=asdf' }},300 { body: 'Requested Range Not Satisfiable', status: 416 });301 },305302 303 'test #sendfile() Range invalid range': function(){304 var app = express.createServer();305 306 app.get('/*', function(req, res, next){307 var file = req.params[0]308 , path = __dirname + '/fixtures/' + file;309 res.sendfile(path);310 });314311 312 assert.response(app,313 { url: '/large.json', headers: { 'Range': 'bytes=500-10' }},314 { body: 'Requested Range Not Satisfiable', status: 416 });315 },316 320 'test #download()': function(beforeExit){321 var app = express.createServer()322 , calls = 0;317 'test #download()': function(){318 var app = express.createServer();319 320 app.get('/json', function(req, res, next){321 var filePath = __dirname + '/fixtures/user.json';326 res.download(filePath, 'account.json', function(err, path, stream){327 ++calls;328 path.should.equal(filePath);329 stream.should.be.an.instanceof(Stream);330 });322 res.download(filePath, 'account.json');323 });332324 325 app.get('/*', function(req, res, next){326 res.download(__dirname + '/fixtures/' + req.params[0]);327 });328
6 unmodified lines
335 { body: '{"name":"tj"}', status: 200, headers: {336 'Content-Type': 'application/json',337 'Content-Disposition': 'attachment; filename="user.json"'338 }});347339 340 assert.response(app,341 { url: '/json' },342 { body: '{"name":"tj"}', status: 200, headers: {343 'Content-Type': 'application/json',
5 unmodified lines
349 { url: '/missing' },350 function(res){351 assert.equal(null, res.headers['content-disposition']);352 });361362 beforeExit(function(){363 calls.should.equal(1);364 });353 },354 355 'test #cookie()': function(){356 var app = express.createServer();
43 unmodified lines