diff --git a/History.md b/History.md index 291d7bba..eb812c2c 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,9 @@ +4.1.2 / 2014-05-08 +================== + + * fix `req.host` for IPv6 literals + * fix `res.jsonp` error if callback param is object + 4.1.1 / 2014-04-27 ================== @@ -46,6 +52,12 @@ - `app.route()` - Proxy to the app's `Router#route()` method to create a new route - Router & Route - public API +3.5.3 / 2014-05-08 +================== + + * fix `req.host` for IPv6 literals + * fix `res.jsonp` error if callback param is object + 3.5.2 / 2014-04-24 ================== diff --git a/lib/request.js b/lib/request.js index 82e0fb11..de62e9de 100644 --- a/lib/request.js +++ b/lib/request.js @@ -348,7 +348,13 @@ req.__defineGetter__('host', function(){ var host = trustProxy && this.get('X-Forwarded-Host'); host = host || this.get('Host'); if (!host) return; - return host.split(':')[0]; + var offset = host[0] === '[' + ? host.indexOf(']') + 1 + : 0; + var index = host.indexOf(':', offset); + return ~index + ? host.substring(0, index) + : host; }); /** diff --git a/lib/response.js b/lib/response.js index 786833cd..775482c0 100644 --- a/lib/response.js +++ b/lib/response.js @@ -232,9 +232,13 @@ res.jsonp = function(obj){ // content-type this.get('Content-Type') || this.set('Content-Type', 'application/json'); + // fixup callback + if (Array.isArray(callback)) { + callback = callback[0]; + } + // jsonp - if (callback) { - if (Array.isArray(callback)) callback = callback[0]; + if (callback && 'string' === typeof callback) { this.set('Content-Type', 'text/javascript'); var cb = callback.replace(/[^\[\]\w$.]/g, ''); body = 'typeof ' + cb + ' === \'function\' && ' + cb + '(' + body + ');'; diff --git a/package.json b/package.json index 2753dff5..0e3449d8 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "express", "description": "Sinatra inspired web development framework", - "version": "4.1.1", + "version": "4.1.2", "author": "TJ Holowaychuk ", "contributors": [ { diff --git a/test/req.host.js b/test/req.host.js index 4c5c76ba..74024844 100644 --- a/test/req.host.js +++ b/test/req.host.js @@ -18,6 +18,19 @@ describe('req', function(){ .expect('example.com', done); }) + it('should strip port number', function(done){ + var app = express(); + + app.use(function(req, res){ + res.end(req.host); + }); + + request(app) + .post('/') + .set('Host', 'example.com:3000') + .expect('example.com', done); + }) + it('should return undefined otherwise', function(done){ var app = express(); @@ -30,5 +43,31 @@ describe('req', function(){ .post('/') .expect('undefined', done); }) + + it('should work with IPv6 Host', function(done){ + var app = express(); + + app.use(function(req, res){ + res.end(req.host); + }); + + request(app) + .post('/') + .set('Host', '[::1]') + .expect('[::1]', done); + }) + + it('should work with IPv6 Host and port', function(done){ + var app = express(); + + app.use(function(req, res){ + res.end(req.host); + }); + + request(app) + .post('/') + .set('Host', '[::1]:3000') + .expect('[::1]', done); + }) }) }) diff --git a/test/res.jsonp.js b/test/res.jsonp.js index 21a961ce..46381cf7 100644 --- a/test/res.jsonp.js +++ b/test/res.jsonp.js @@ -37,6 +37,22 @@ describe('res', function(){ }) }) + it('should ignore object callback parameter with jsonp', function(done){ + var app = express(); + + app.use(function(req, res){ + res.jsonp({ count: 1 }); + }); + + request(app) + .get('/?callback[a]=something') + .end(function(err, res){ + res.headers.should.have.property('content-type', 'application/json'); + res.text.should.equal('{"count":1}'); + done(); + }) + }) + it('should allow renaming callback', function(done){ var app = express(); @@ -205,7 +221,7 @@ describe('res', function(){ }) }) - describe('.json(status, object)', function(){ + describe('.jsonp(status, object)', function(){ it('should respond with json and set the .statusCode', function(done){ var app = express(); @@ -224,7 +240,7 @@ describe('res', function(){ }) }) - describe('.json(object, status)', function(){ + describe('.jsonp(object, status)', function(){ it('should respond with json and set the .statusCode for backwards compat', function(done){ var app = express();