Skip to content
Myr

Merge tag '3.7.0'

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

authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parents 31b2e2d, b91cd66 · Browse files · 19 files+348 added−111 removed
This commit

State

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

Diff stats

19 files+348 added−111 removed

  • tests:+189 added−17 removed
  • other:+159 added−94 removed

19 files+348 added−111 removed

modified:examples/auth/app.js+2 added-2 removed
modified:lib/application.js+8 added
9 unmodified lines
10var query = require('./middleware/query');11var debug = require('debug')('express:application');12var View = require('./view');13var http = require('http');14var compileTrust = require('./utils').compileTrust;15var deprecate = require('./utils').deprecate;1617/**18 * Application prototype.
31 unmodified lines
50 this.enable('etag');51 var env = process.env.NODE_ENV || 'development';52 this.set('env', env);53 this.set('subdomain offset', 2);54 this.set('trust proxy', false);5556 debug('booting in %s mode', env);5758 // inherit protos
250 unmodified lines
309 if (1 == arguments.length) {310 return this.settings[setting];311 } else {312 this.settings[setting] = val;313314 if (setting === 'trust proxy') {315 debug('compile trust proxy %j', val);316 this.set('trust proxy fn', compileTrust(val));317 }318319 return this;320 }321};322
247 unmodified lines
modified:lib/request.js+41 added-22 removed
6 unmodified lines
7var http = require('http');8var fresh = require('fresh');9var parseRange = require('range-parser');10var parse = require('parseurl');11var proxyaddr = require('proxy-addr');1213/**14 * Request prototype.15 */
223 unmodified lines
239240/**241 * Return the protocol string "http" or "https"242 * when requested with TLS. When the "trust proxy"242 * setting is enabled the "X-Forwarded-Proto" header243 * field will be trusted. If you're running behind244 * a reverse proxy that supplies https for you this245 * may be enabled.243 * setting trusts the socket address, the244 * "X-Forwarded-Proto" header field will be trusted.245 * If you're running behind a reverse proxy that246 * supplies https for you this may be enabled.247 *248 * @return {String}249 * @api public250 */251252req.__defineGetter__('protocol', function(){252 var trustProxy = this.app.get('trust proxy');253 if (this.connection.encrypted) return 'https';254 if (!trustProxy) return 'http';253 var trust = this.app.get('trust proxy fn');254255 if (!trust(this.connection.remoteAddress)) {256 return this.connection.encrypted257 ? 'https'258 : 'http';259 }260261 // Note: X-Forwarded-Proto is normally only ever a262 // single value, but this is to be safe.263 var proto = this.get('X-Forwarded-Proto') || 'http';264 return proto.split(/\s*,\s*/)[0];265});266
10 unmodified lines
277 return 'https' == this.protocol;278});279280/**273 * Return the remote address, or when274 * "trust proxy" is `true` return275 * the upstream addr.281 * Return the remote address from the trusted proxy.282 *283 * The is the remote address on the socket unless284 * "trust proxy" is set.285 *286 * @return {String}287 * @api public288 */289290req.__defineGetter__('ip', function(){282 return this.ips[0] || this.connection.remoteAddress;291 var trust = this.app.get('trust proxy fn');292 return proxyaddr(this, trust);293});294295/**286 * When "trust proxy" is `true`, parse287 * the "X-Forwarded-For" ip address list.296 * When "trust proxy" is set, trusted proxy addresses + client.297 *298 * For example if the value were "client, proxy1, proxy2"299 * you would receive the array `["client", "proxy1", "proxy2"]`291 * where "proxy2" is the furthest down-stream.300 * where "proxy2" is the furthest down-stream and "proxy1" and301 * "proxy2" were trusted.302 *303 * @return {Array}304 * @api public305 */306307req.__defineGetter__('ips', function(){298 var trustProxy = this.app.get('trust proxy');299 var val = this.get('X-Forwarded-For');300 return trustProxy && val301 ? val.split(/ *, */)302 : [];308 var trust = this.app.get('trust proxy fn');309 var addrs = proxyaddr.all(this, trust);310 return addrs.slice(1).reverse();311});312313/**314 * Return subdomains as an array.
31 unmodified lines
346347/**348 * Parse the "Host" header field hostname.349 *350 * When the "trust proxy" setting trusts the socket351 * address, the "X-Forwarded-Host" header field will352 * be trusted.353 *354 * @return {String}355 * @api public356 */357358req.__defineGetter__('host', function(){347 var trustProxy = this.app.get('trust proxy');348 var host = trustProxy && this.get('X-Forwarded-Host');349 host = host || this.get('Host');359 var trust = this.app.get('trust proxy fn');360 var host = this.get('X-Forwarded-Host');361362 if (!host || !trust(this.connection.remoteAddress)) {363 host = this.get('Host');364 }365366 if (!host) return;367368 // IPv6 literal support369 var offset = host[0] === '['370 ? host.indexOf(']') + 1371 : 0;372 var index = host.indexOf(':', offset);373374 return ~index375 ? host.substring(0, index)376 : host;377});
49 unmodified lines
modified:lib/utils.js+43 added-4 removed
4 unmodified lines
5var mime = require('send').mime;6var crc32 = require('buffer-crc32');7var basename = require('path').basename;8var deprecate = require('util').deprecate;9var proxyaddr = require('proxy-addr');1011/**11 * Deprecate function, like core `util.deprecate`12 * Deprecate function, like core `util.deprecate`,13 * but with NODE_ENV and color support.14 *15 * @param {Function} fn16 * @param {String} msg17 * @return {Function}18 * @api private19 */2021exports.deprecate = function(fn, msg){20 return 'test' !== process.env.NODE_ENV21 ? deprecate(fn, 'express: ' + msg)22 : fn;22 if (process.env.NODE_ENV === 'test') return fn;2324 // prepend module name25 msg = 'express: ' + msg;2627 if (process.stderr.isTTY) {28 // colorize29 msg = '\x1b[31;1m' + msg + '\x1b[0m';30 }3132 return deprecate(fn, msg);33};3435/**36 * Return ETag for `body`.
120 unmodified lines
157 }158159 return ret;160}161162/**163 * Compile "proxy trust" value to function.164 *165 * @param {Boolean|String|Number|Array|Function} val166 * @return {Function}167 * @api private168 */169170exports.compileTrust = function(val) {171 if (typeof val === 'function') return val;172173 if (val === true) {174 // Support plain true/false175 return function(){ return true };176 }177178 if (typeof val === 'number') {179 // Support trusting hop count180 return function(a, i){ return i < val };181 }182183 if (typeof val === 'string') {184 // Support comma-separated values185 val = val.split(/ *, */);186 }187188 return proxyaddr.compile(val || []);189}
modified:test/acceptance/auth.js+76 added-16 removed
1var app = require('../../examples/auth/app')2 , request = require('supertest');2var request = require('supertest')34function redirects(to, fn){5 return function(err, res){6 res.statusCode.should.equal(302)7 res.headers.should.have.property('location').match(to);8 fn()9 }10}114function getCookie(res) {5 return res.headers['set-cookie'][0].split(';')[0];6}7
1 unmodified line
9 describe('GET /',function(){10 it('should redirect to /login', function(done){11 request(app)12 .get('/')21 .end(redirects(/login$/, done))13 .expect('Location', '/login')14 .expect(302, done)15 })16 })1725 describe('GET /restricted (w/o cookie)',function(){26 it('should redirect to /login', function(done){18 describe('GET /login',function(){19 it('should render login form', function(done){20 request(app)21 .get('/login')22 .expect(200, /<form/, done)23 })2425 it('should display login error', function(done){26 request(app)27 .post('/login')28 .type('urlencoded')29 .send('username=not-tj&password=foobar')30 .expect('Location', '/login')31 .expect(302, function(err, res){32 if (err) return done(err)33 request(app)34 .get('/login')35 .set('Cookie', getCookie(res))36 .expect(200, /Authentication failed/, done)37 })38 })39 })4041 describe('GET /logout',function(){42 it('should redirect to /', function(done){43 request(app)44 .get('/logout')45 .expect('Location', '/')46 .expect(302, done)47 })48 })4950 describe('GET /restricted',function(){51 it('should redirect to /login without cookie', function(done){52 request(app)53 .get('/restricted')29 .end(redirects(/login$/,done))54 .expect('Location', '/login')55 .expect(302, done)56 })5758 it('should succeed with proper cookie', function(done){59 request(app)60 .post('/login')61 .type('urlencoded')62 .send('username=tj&password=foobar')63 .expect('Location', '/')64 .expect(302, function(err, res){65 if (err) return done(err)66 request(app)67 .get('/restricted')68 .set('Cookie', getCookie(res))69 .expect(200, done)70 })71 })72 })7374 describe('POST /login', function(){34 it('should fail without proper credentials', function(done){75 it('should fail without proper username', function(done){76 request(app)77 .post('/login')78 .type('urlencoded')79 .send('username=not-tj&password=foobar')39 .end(redirects(/login$/, done))80 .expect('Location', '/login')81 .expect(302, done)82 })8384 it('should fail without proper password', function(done){85 request(app)86 .post('/login')87 .type('urlencoded')88 .send('username=tj&password=baz')89 .expect('Location', '/login')90 .expect(302, done)91 })9293 it('should succeed with proper credentials', function(done){94 request(app)95 .post('/login')96 .type('urlencoded')97 .send('username=tj&password=foobar')98 .expect('Location', '/')99 .expect(302, done)100 })101 })42})102})
added:test/support/env.js+2 added
modified:test/middleware.basic.js+1 added-1 removed
modified:test/req.host.js+65 added
67 unmodified lines
68 .post('/')69 .set('Host', '[::1]:3000')70 .expect('[::1]', done);71 })7273 describe('when "trust proxy" is enabled', function(){74 it('should respect X-Forwarded-Host', function(done){75 var app = express();7677 app.enable('trust proxy');7879 app.use(function(req, res){80 res.end(req.host);81 });8283 request(app)84 .get('/')85 .set('Host', 'localhost')86 .set('X-Forwarded-Host', 'example.com')87 .expect('example.com', done);88 })8990 it('should ignore X-Forwarded-Host if socket addr not trusted', function(done){91 var app = express();9293 app.set('trust proxy', '10.0.0.1');9495 app.use(function(req, res){96 res.end(req.host);97 });9899 request(app)100 .get('/')101 .set('Host', 'localhost')102 .set('X-Forwarded-Host', 'example.com')103 .expect('localhost', done);104 })105106 it('should default to Host', function(done){107 var app = express();108109 app.enable('trust proxy');110111 app.use(function(req, res){112 res.end(req.host);113 });114115 request(app)116 .get('/')117 .set('Host', 'example.com')118 .expect('example.com', done);119 })120 })121122 describe('when "trust proxy" is disabled', function(){123 it('should ignore X-Forwarded-Host', function(done){124 var app = express();125126 app.use(function(req, res){127 res.end(req.host);128 });129130 request(app)131 .get('/')132 .set('Host', 'localhost')133 .set('X-Forwarded-Host', 'evil')134 .expect('localhost', done);135 })136 })137 })138})
modified:test/req.ip.js+15 added
18 unmodified lines
19 .get('/')20 .set('X-Forwarded-For', 'client, p1, p2')21 .expect('client', done);22 })2324 it('should return the addr after trusted proxy', function(done){25 var app = express();2627 app.set('trust proxy', 2);2829 app.use(function(req, res, next){30 res.send(req.ip);31 });3233 request(app)34 .get('/')35 .set('X-Forwarded-For', 'client, p1, p2')36 .expect('p1', done);37 })38 })3940 describe('when "trust proxy" is disabled', function(){41 it('should return the remote address', function(done){
31 unmodified lines
modified:test/req.ips.js+15 added
18 unmodified lines
19 .get('/')20 .set('X-Forwarded-For', 'client, p1, p2')21 .expect('["client","p1","p2"]', done);22 })2324 it('should stop at first untrusted', function(done){25 var app = express();2627 app.set('trust proxy', 2);2829 app.use(function(req, res, next){30 res.send(req.ips);31 });3233 request(app)34 .get('/')35 .set('X-Forwarded-For', 'client, p1, p2')36 .expect('["p1","p2"]', done);37 })38 })3940 describe('when "trust proxy" is disabled', function(){41 it('should return an empty array', function(done){
29 unmodified lines
modified:test/req.protocol.js+15 added
30 unmodified lines
31 .set('X-Forwarded-Proto', 'https')32 .expect('https', done);33 })3435 it('should ignore X-Forwarded-Proto if socket addr not trusted', function(done){36 var app = express();3738 app.set('trust proxy', '10.0.0.1');3940 app.use(function(req, res){41 res.end(req.protocol);42 });4344 request(app)45 .get('/')46 .set('X-Forwarded-Proto', 'https')47 .expect('http', done);48 })4950 it('should default to http', function(done){51 var app = express();5253 app.enable('trust proxy');
27 unmodified lines
modified:.gitignore+1 added-4 removed
1coverage.html1coverage/2.DS_Store3lib-cov3*.seed4*.log5*.csv6*.dat
4 unmodified lines
11benchmarks/graphs12testing13node_modules/14testing16.coverage_data17cover_html15test.js16.idea
modified:.npmignore+1 added-2 removed
1.git*2benchmarks/3coverage/4docs/5examples/6support/7test/8testing.js9.DS_Store10.travis.yml10coverage.html11lib-cov11Contributing.md
modified:.travis.yml+1 added
4 unmodified lines
5matrix:6 allow_failures:7 - node_js: "0.11"8 fast_finish: true9script: "npm run-script test-travis"
modified:History.md+31 added
1unreleased2==========34 * fix behavior of multiple `app.VERB` for the same path5 * proper proxy trust with `app.set('trust proxy', trust)`6 - `app.set('trust proxy', 1)` trust first hop7 - `app.set('trust proxy', 'loopback')` trust loopback addresses8 - `app.set('trust proxy', '10.0.0.1')` trust single IP9 - `app.set('trust proxy', '10.0.0.1/16')` trust subnet10 - `app.set('trust proxy', '10.0.0.1, 10.0.0.2')` trust list11 - `app.set('trust proxy', false)` turn off12 - `app.set('trust proxy', true)` trust everything13 * update type-is to 1.2.014 - support suffix matching15164.2.0 / 2014-05-11
91 unmodified lines
108 - `app.router()` - returns the app Router instance109 - `app.route()` - Proxy to the app's `Router#route()` method to create a new route110 - Router & Route - public API1111123.7.0 / 2014-05-18113==================114115 * proper proxy trust with `app.set('trust proxy', trust)`116 - `app.set('trust proxy', 1)` trust first hop117 - `app.set('trust proxy', 'loopback')` trust loopback addresses118 - `app.set('trust proxy', '10.0.0.1')` trust single IP119 - `app.set('trust proxy', '10.0.0.1/16')` trust subnet120 - `app.set('trust proxy', '10.0.0.1, 10.0.0.2')` trust list121 - `app.set('trust proxy', false)` turn off122 - `app.set('trust proxy', true)` trust everything123 * update connect to 2.16.2124 - deprecate `res.headerSent` -- use `res.headersSent`125 - deprecate `res.on("header")` -- use on-headers module instead126 - fix edge-case in `res.appendHeader` that would append in wrong order127 - json: use body-parser128 - urlencoded: use body-parser129 - dep: bytes@1.0.0130 - dep: cookie-parser@1.1.0131 - dep: csurf@1.2.0132 - dep: express-session@1.1.0133 - dep: method-override@1.0.11341353.6.0 / 2014-05-09136==================137138 * deprecate `app.del()` -- use `app.delete()` instead
1312 unmodified lines
modified:index.js+1 added-3 removed
12module.exports = process.env.EXPRESS_COV3 ? require('./lib-cov/express')4 : require('./lib/express');2module.exports = require('./lib/express');
deleted:Makefile-34 removed
12MOCHA_OPTS= --check-leaks3REPORTER = dot45check: test67test: test-unit test-acceptance89test-unit:10 @NODE_ENV=test ./node_modules/.bin/mocha \11 --reporter $(REPORTER) \12 --globals setImmediate,clearImmediate \13 $(MOCHA_OPTS)1415test-acceptance:16 @NODE_ENV=test ./node_modules/.bin/mocha \17 --reporter $(REPORTER) \18 --bail \19 test/acceptance/*.js2021test-cov: lib-cov22 @EXPRESS_COV=1 $(MAKE) test REPORTER=html-cov > coverage.html2324lib-cov:25 @jscoverage lib lib-cov2627bench:28 @$(MAKE) -C benchmarks2930clean:31 rm -f coverage.html32 rm -fr lib-cov3334.PHONY: test test-unit test-acceptance bench clean
modified:package.json+26 added-21 removed
31 unmodified lines
32 "name": "Roman Shtylman",33 "email": "shtylman+expressjs@gmail.com"34 }35 ],36 "keywords": [37 "express",38 "framework",39 "sinatra",40 "web",41 "rest",42 "restful",43 "router",44 "app",45 "api"46 ],47 "repository": "git://github.com/visionmedia/express",48 "license": "MIT",49 "dependencies": {37 "parseurl": "1.0.1",50 "accepts": "1.0.1",39 "type-is": "1.2.0",51 "parseurl": "1.0.1",52 "proxy-addr": "1.0.0",53 "range-parser": "1.0.0",54 "type-is": "1.2.0",55 "cookie": "0.1.2",56 "buffer-crc32": "0.2.1",57 "fresh": "0.2.2",58 "methods": "1.0.0",
7 unmodified lines
66 "path-to-regexp": "0.1.2",67 "debug": "0.8.1"68 },69 "devDependencies": {70 "coveralls": "2.10.0",71 "istanbul": "0.2.10",72 "mocha": "~1.18.2",73 "should": "~3.3.1",74 "supertest": "~0.12.0",75 "body-parser": "~1.1.2",76 "connect-redis": "~2.0.0",77 "ejs": "~1.0.0",78 "jade": "~0.35.0",79 "marked": "0.3.2",80 "multiparty": "~3.2.4",81 "hjs": "~0.0.6",64 "should": "~3.3.1",65 "supertest": "~0.12.0",82 "method-override": "1.0.0",83 "cookie-parser": "1.0.1",84 "express-session": "1.0.4",85 "morgan": "1.0.1",86 "vhost": "1.0.0"87 },72 "keywords": [73 "express",74 "framework",75 "sinatra",76 "web",77 "rest",78 "restful",79 "router",80 "app",81 "api"82 ],83 "repository": "git://github.com/visionmedia/express",84 "scripts": {85 "prepublish": "npm prune",86 "test": "make test"87 },88 "engines": {89 "node": ">= 0.10.0"90 },91 "license": "MIT"91 "scripts": {92 "prepublish": "npm prune",93 "test": "mocha --require test/support/env --reporter dot --check-leaks test/ test/acceptance/",94 "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --require test/support/env --reporter dot --check-leaks test/ test/acceptance/",95 "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --check-leaks test/ test/acceptance/ && cat ./coverage/lcov.info | coveralls"96 }97}
modified:Readme.md+4 added-2 removed
1[![express logo](https://i.cloudup.com/zfY6lL7eFa-3000x3000.png)](http://expressjs.com/)23 Fast, unopinionated, minimalist web framework for [node](http://nodejs.org).45 [![Build Status](https://travis-ci.org/visionmedia/express.svg?branch=master)](https://travis-ci.org/visionmedia/express) [![Gittip](http://img.shields.io/gittip/visionmedia.svg)](https://www.gittip.com/visionmedia/) [![NPM version](https://badge.fury.io/js/express.svg)](http://badge.fury.io/js/express)5 [![NPM version](https://badge.fury.io/js/express.svg)](http://badge.fury.io/js/express) [![Build Status](https://travis-ci.org/visionmedia/express.svg?branch=master)](https://travis-ci.org/visionmedia/express) [![Coverage Status](https://img.shields.io/coveralls/visionmedia/express.svg)](https://coveralls.io/r/visionmedia/express) [![Gittip](http://img.shields.io/gittip/visionmedia.svg)](https://www.gittip.com/visionmedia/)67```js8var express = require('express');9var app = express();
84 unmodified lines
94 $ npm install9596Then run the tests:9798```sh98 $ make test99$ npm test100```101102## Contributors103 104 Author: [TJ Holowaychuk](http://github.com/visionmedia)
6 unmodified lines