Skip to content
Myr

Merge branch 'etag'

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

authored by visionmedia · committed by visionmedia · parents b40325c, 0fb102e · Browse files · 16 files+101 added−62 removed
This commit

State

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

Diff stats

16 files+101 added−62 removed

  • tests:+21 added−21 removed
  • other:+80 added−41 removed

16 files+101 added−62 removed

modified:lib/express/plugins/body-decoder.js+2 added-2 removed
modified:lib/express/plugins/content-length.js+2 added-3 removed
modified:lib/express/plugins/cookie.js+1 added-1 removed
modified:lib/express/plugins/redirect.js+2 added-2 removed
39 unmodified lines
40 */4142 redirect: function(url, code) {43 if (url === 'back' || url === 'home') url = this[url]44 this.header('location', url)44 this.header('Location', url)45 this.halt(code || 303)46 } 47 })48 }
9 unmodified lines
58 */59 60 request: function(event) {61 event.request.home = set('home') || set('basepath') || '/'62 event.request.back = event.request.header('referrer') || event.request.header('referer')62 event.request.back = event.request.header('Referrer') || event.request.header('Referer')63 }64 }65})
modified:lib/express/plugins/static.js+25 added-5 removed
38 unmodified lines
39 40 Request.include({41 42 /**43 * Transfer static file at the given _path_. All _options_44 * are passed to fs.createReadStream().43 * Transfer static file at the given _path_ with optional _callback_.44 * _options_ are passed to fs.createReadStream().45 *46 * @param {string} path47 * @param {object} options48 * @param {function} callback49 * @return {Request}50 * @api public51 */5252 sendfile: function(path, options) {53 sendfile: function(path, options, callback) {54 var self = this55 if (options instanceof Function)56 callback = options,57 options = {}58 if (path.indexOf('..') !== -1)59 Error.raise('InvalidPathError', "`" + path + "' is not a valid path")55 this.response.chunkedEncoding = true56 return this.stream(fs.createReadStream(path, options))60 fs.stat(path, function(err, stat){61 if (err)62 if (callback !== undefined)63 return callback(err)64 else65 return 'errno' in err && err.errno === 266 ? self.notFound()67 : self.error(err)68 var etag = stat.ino + '-' + stat.size + '-' + Number(stat.mtime)69 if (self.header('If-None-Match') && 70 self.header('If-None-Match') == etag)71 return self.halt(304, null)72 self.header('Content-Length', stat.size)73 self.header('ETag', etag)74 self.stream(fs.createReadStream(path, options))75 })76 return this77 },78 79 /**80 * Transfer static _file_ as an attachment.
17 unmodified lines
modified:lib/express/spec/mocks.js+1 added-1 removed
89 unmodified lines
90 /**91 * Store _code_ and _headers_.92 */93 94 writeHeader: function(code, headers) {94 writeHead: function(code, headers) {95 this.status = code96 this.headers = headers97 },98
80 unmodified lines
modified:lib/express/core.js+3 added-3 removed
225 unmodified lines
226 request.error(err)227 else if (!pendingFiles)228 self.route(request)229 }230 if (request.header('content-type') &&231 request.header('content-type').includes('multipart/form-data')) {230 if (request.header('Content-Type') &&231 request.header('Content-Type').includes('multipart/form-data')) {232 var stream,233 contentLength = parseInt(request.header('content-length')),233 contentLength = parseInt(request.header('Content-Length')),234 maxBodyLength = set('max upload size')235 if (maxBodyLength && contentLength > maxBodyLength)236 return callback(new Error('upload size limit exceeded'))237 stream = multipart.parse(req)
98 unmodified lines
modified:lib/express/http.js+2 added-2 removed
37 unmodified lines
38 data = queryString.stringify(data)39 if (method === 'GET')40 search += (search ? '&' : '?') + data41 else42 headers['content-length'] = data.length,43 headers['content-type'] = 'application/x-www-form-urlencoded'42 headers['Content-Length'] = data.length,43 headers['Content-Type'] = 'application/x-www-form-urlencoded'44 }45 var req = client.request(method, path + search + hash, headers)46 if (data && method !== 'GET') req.write(data)47 req.addListener('response', function(res){
45 unmodified lines
modified:lib/express/request.js+43 added-23 removed
72 unmodified lines
7374 header: function(key, val) {75 return val === undefined76 ? this.headers[key.lowercase]77 : this.response.headers[key.lowercase] = val77 : this.response.headers[key] = val78 },79 80 /**81 * Return the param _key_ value or undefined.
52 unmodified lines
134 * @api public135 */136137 get isXHR() {138 return (this.header('x-requested-with') || '').lowercase === 'xmlhttprequest'138 return (this.header('X-Requested-With') || '').lowercase === 'xmlhttprequest'139 },140141 /**142 * Set response status to _code_.
30 unmodified lines
173 if (encoding instanceof Function)174 callback = encoding,175 encoding = null176 this.status(code = code || 404)177 if (body = body || statusBodies[code])178 return this.respond(body, encoding, callback)177 if (body !== null)178 body = body || statusBodies[code]179 return this.respond(body, encoding, callback)180 },181 182 /**183 * Respond with _body_ and optional _encoding_.
7 unmodified lines
191 respond: function(body, encoding, callback) {192 var self = this193 this.response.body = body194 this.trigger('response', function(err) {194 if (err)195 if (callback !== undefined) callback(err)196 else self.error(err)197 self.response.writeHeader(self.response.status, self.response.headers)198 self.response.write(self.response.body, encoding)195 if (err) self.error(err, callback)196 // TODO: revisit. when this is true node will add Transfer-Encoding: chunked197 // along with a response body, which is not ideal when the user attemps198 // to respond with no body199 if (!body) self.response.useChunkedEncodingByDefault = false200 self.sendHead()201 if (body) self.response.write(self.response.body, encoding)202 self.response.end()203 }, true)204 },205 206 /**207 * Transfer the given _stream_ with optional _callback_.208 *209 * @param {Stream} stream210 * @param {function} callback211 * @return {Request}212 * @api public213 */214
2 unmodified lines
217 first = true218 stream219 .addListener('error', function(err){220 if (first)217 return 'errno' in err && err.errno === 2218 ? self.notFound()219 : self.error(err)221 return self.error(err, callback)222 self.response.end()223 if (callback !== undefined) callback(err)224 })225 .addListener('data', function(data){226 if (first) {225 self.status(200)227 first = false228 self.header('Transfer-Encoding', 'chunked')229 self.status(200)230 self.contentType(stream.path)228 self.response.writeHeader(self.response.status, self.response.headers)231 self.sendHead()232 }233 self.response.write(data, 'binary')234 })235 .addListener('end', function(){233 self.response.end()236 self.trigger('response', function(err){235 if (err) {236 stream.destroy()237 if (callback !== undefined) callback(err)238 else self.error(err)239 }237 if (err)238 stream.destroy(),239 self.error(err, callback)240 else241 self.response.end()242 }, true)243 })244 },245 246 /**247 * Send the response header.248 *249 * @return {Request}250 * @api public251 */252 253 sendHead: function(){254 this.response.writeHead(this.response.status, this.response.headers)255 return this256 },257 258 /**259 * Pass control to the next matching route, or260 * the given _path_.261 *262 * NOTE: _path_ may be the request pathname only,
17 unmodified lines
280 * @api public281 */282283 contentType: function(path) {270 this.header('content-type', mime.type(path))284 this.header('Content-Type', mime.type(path))285 return this286 },287 288 /**
40 unmodified lines
329 * @api public330 */331 332 attachment: function(filename) {319 this.header('content-disposition', filename333 this.header('Content-Disposition', filename334 ? 'attachment; filename="' + filename + '"'335 : 'attachment')336 return this337 },
15 unmodified lines
353 *354 * When "throw exceptions" is enabled the error will be355 * re-thrown, terminating the process (unless otherwise caught).356 *357 * Also a _callback_ function may be supplied, which when defined358 * will be called, by-passing the process mentioned above.359 *360 * @param {Error} err361 * @param {function} callback362 * @return {Request}363 * @settings 'throw exceptions', 'show exceptions'364 * @api public365 */366 349 error: function(err) {367 error: function(err, callback) {368 if (callback)369 return callback(err)370 if (Express.error)371 Express.error.call(this, err)372 else if (this.accepts('html') && set('show exceptions'))373 this.halt(500, showExceptions.render(this, err))
36 unmodified lines
modified:spec/spec.plugins.content-length.js+3 added-3 removed
10 unmodified lines
11 it 'should set the content-length header'12 get('/style.css', function(){13 return 'body { background: #000; }'14 })15 get('/style.css').headers['content-length'].should.eql 2615 get('/style.css').headers['Content-Length'].should.eql 2616 end17 18 it 'should not override when manually set'19 get('/style.css', function(){20 this.header('content-length', 10)20 this.header('Content-Length', 10)21 return 'body { background: #000; }'22 })23 get('/style.css').headers['content-length'].should.eql 1023 get('/style.css').headers['Content-Length'].should.eql 1024 end25 end26 end27 end
1 unmodified line
modified:spec/spec.plugins.cookie.js+2 added-2 removed
71 unmodified lines
72 get('/user', function(){73 this.cookie('SID', '732423sdfs73243', { path: '/', secure: true })74 return '' 75 })76 get('/user').headers['set-cookie'].should.eql 'SID=732423sdfs73243; path=/; secure'76 get('/user').headers['Set-Cookie'].should.eql 'SID=732423sdfs73243; path=/; secure'77 end78 79 it 'should set multiple cookies'80 get('/user', function(){81 this.cookie('SID', '732423sdfs73243', { path: '/', secure: true })82 this.cookie('foo', 'bar')83 return '' 84 })85 get('/user').headers['set-cookie'].should.eql 'SID=732423sdfs73243; path=/; secure\nset-cookie: foo=bar; path=/'85 get('/user').headers['Set-Cookie'].should.eql 'SID=732423sdfs73243; path=/; secure\nSet-Cookie: foo=bar; path=/'86 end87 end88 end89
11 unmodified lines
modified:spec/spec.plugins.js+1 added-1 removed
7 unmodified lines
8 }9 },10 on: {11 response: function(event) {12 if (event.response.headers['content-type'] == mime.type('css'))12 if (event.response.headers['Content-Type'] === mime.type('css'))13 event.response.body = event.response.body.replace('black', '#000')14 }15 }16})
25 unmodified lines
modified:spec/spec.plugins.redirect.js+7 added-7 removed
10 unmodified lines
11 get('/logout', function(){12 this.redirect('/')13 })14 get('/logout').status.should.eql 30315 get('/logout').headers['location'].should.eql '/'15 get('/logout').headers['Location'].should.eql '/'16 end17 18 it 'should allow optional status code'19 get('/logout', function(){20 this.redirect('/', 304)21 })22 get('/logout').status.should.eql 30423 get('/logout').headers['location'].should.eql '/'23 get('/logout').headers['Location'].should.eql '/'24 end25 end26 27 describe 'redirect("back")'
1 unmodified line
29 get('/logout', function(){30 this.redirect('back')31 })32 var response = get('/logout', { headers: { referrer: '/login' }})33 response.headers['location'].should.eql '/login'33 response.headers['Location'].should.eql '/login'34 end35 36 it 'should check referer header'37 get('/logout', function(){38 this.redirect('back')39 })40 var response = get('/logout', { headers: { referer: '/login' }})41 response.headers['location'].should.eql '/login'41 response.headers['Location'].should.eql '/login'42 end43 end44 45 describe 'redirect(home)'
1 unmodified line
47 set('home', '/login')48 get('/logout', function(){49 this.redirect('home')50 })51 get('/logout').headers['location'].should.eql '/login'51 get('/logout').headers['Location'].should.eql '/login'52 end53 54 it 'should check set("basepath")'55 set('basepath', '/web/')56 get('/logout', function(){57 this.redirect('home')58 })59 get('/logout').headers['location'].should.eql '/web/'59 get('/logout').headers['Location'].should.eql '/web/'60 end61 62 it 'should default to "/"'63 get('/logout', function(){64 this.redirect('home')65 })66 get('/logout').headers['location'].should.eql '/'66 get('/logout').headers['Location'].should.eql '/'67 end68 end69 end70end
modified:spec/spec.plugins.session.js+3 added-3 removed
10 unmodified lines
11 describe 'Session'12 describe 'when sid cookie is not present'13 it 'should set sid cookie'14 get('/login', function(){ return '' })15 get('/login').headers['set-cookie'].should.match(/^sid=(\w+);/)15 get('/login').headers['Set-Cookie'].should.match(/^sid=(\w+);/)16 end17 18 describe 'and requesting /favicon.ico'19 it 'should not set sid cookie'
6 unmodified lines
26 describe 'when existing sid cookie is present'27 it 'should not set sid'28 Session.store.commit(new Base(123))29 get('/login', function(){ return '' })30 get('/login', { headers: { cookie: 'sid=123' }}).headers.should.not.have_property 'set-cookie'30 get('/login', { headers: { cookie: 'sid=123' }}).headers.should.not.have_property 'Set-Cookie'31 end32 end33 34 describe 'when unknown sid cookie is present'35 it 'should set new sid'36 get('/login', function(){ return '' })37 var headers= get('/login', { headers: { cookie: 'sid=123' }}).headers38 39 headers.should.have_property 'set-cookie'39 headers.should.have_property 'Set-Cookie'40 headers['set-cookie'].should.not.be '123'41 end42 end 43
97 unmodified lines
modified:spec/spec.plugins.view.js+1 added-1 removed
109 unmodified lines
110 it 'should set the content type based on the last path segment'111 get('/', function(){112 this.render('hello.html.haml')113 })114 get('/').headers['content-type'].should.eql 'text/html'114 get('/').headers['Content-Type'].should.eql 'text/html'115 end116 end117 118 describe 'and layout of the same type does not exist'
86 unmodified lines
modified:spec/spec.request.js+3 added-3 removed
130 unmodified lines
131 get('/style.css', function(){132 this.contentType('css')133 return 'body { background: white; }'134 })135 get('/style.css').headers['content-type'].should.eql 'text/css'135 get('/style.css').headers['Content-Type'].should.eql 'text/css'136 end137 end138 139 describe '#attachment()'
1 unmodified line
141 get('/report', function(){142 this.attachment()143 return 'foo'144 })145 get('/report').headers['content-disposition'].should.eql 'attachment'145 get('/report').headers['Content-Disposition'].should.eql 'attachment'146 end147148 it 'should set attachment filename'149 get('/report', function(){150 this.attachment('report.pdf')151 return 'foo'152 })153 get('/report').headers['content-disposition'].should.eql 'attachment; filename="report.pdf"'153 get('/report').headers['Content-Disposition'].should.eql 'attachment; filename="report.pdf"'154 end155 end156157 describe '#param()'
212 unmodified lines