Skip to content

Commit e6eeec3

Browse files
committed
Add req.hostname
closes expressjs#2179
1 parent 269dc53 commit e6eeec3

File tree

3 files changed

+149
-3
lines changed

3 files changed

+149
-3
lines changed

Diff for: History.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ unreleased
88
- use to set headers on successful file transfer
99
* add `mergeParams` option to `Router`
1010
- merges `req.params` from parent routes
11+
* add `req.hostname` -- correct name for what `req.host` returns
1112
* deprecate things with `depd` module
13+
* deprecate `req.host` -- use `req.hostname` instead
1214
* fix behavior when handling request without routes
1315
* fix handling when `route.all` is only route
1416
* invoke `router.param()` only when route matches

Diff for: lib/request.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ defineGetter(req, 'ips', function ips() {
330330

331331
defineGetter(req, 'subdomains', function subdomains() {
332332
var offset = this.app.get('subdomain offset');
333-
return (this.host || '')
333+
return (this.hostname || '')
334334
.split('.')
335335
.reverse()
336336
.slice(offset);
@@ -348,7 +348,7 @@ defineGetter(req, 'path', function path() {
348348
});
349349

350350
/**
351-
* Parse the "Host" header field hostname.
351+
* Parse the "Host" header field to a hostname.
352352
*
353353
* When the "trust proxy" setting trusts the socket
354354
* address, the "X-Forwarded-Host" header field will
@@ -358,7 +358,7 @@ defineGetter(req, 'path', function path() {
358358
* @api public
359359
*/
360360

361-
defineGetter(req, 'host', function host(){
361+
defineGetter(req, 'hostname', function hostname(){
362362
var trust = this.app.get('trust proxy fn');
363363
var host = this.get('X-Forwarded-Host');
364364

@@ -379,6 +379,12 @@ defineGetter(req, 'host', function host(){
379379
: host;
380380
});
381381

382+
// TODO: change req.host to return host in next major
383+
384+
defineGetter(req, 'host', deprecate.function(function host(){
385+
return this.hostname;
386+
}, 'req.host: Use req.hostname instead'));
387+
382388
/**
383389
* Check if the request is fresh, aka
384390
* Last-Modified and/or the ETag

Diff for: test/req.hostname.js

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
2+
var express = require('../')
3+
, request = require('supertest')
4+
, assert = require('assert');
5+
6+
describe('req', function(){
7+
describe('.hostname', function(){
8+
it('should return the Host when present', function(done){
9+
var app = express();
10+
11+
app.use(function(req, res){
12+
res.end(req.hostname);
13+
});
14+
15+
request(app)
16+
.post('/')
17+
.set('Host', 'example.com')
18+
.expect('example.com', done);
19+
})
20+
21+
it('should strip port number', function(done){
22+
var app = express();
23+
24+
app.use(function(req, res){
25+
res.end(req.hostname);
26+
});
27+
28+
request(app)
29+
.post('/')
30+
.set('Host', 'example.com:3000')
31+
.expect('example.com', done);
32+
})
33+
34+
it('should return undefined otherwise', function(done){
35+
var app = express();
36+
37+
app.use(function(req, res){
38+
req.headers.host = null;
39+
res.end(String(req.hostname));
40+
});
41+
42+
request(app)
43+
.post('/')
44+
.expect('undefined', done);
45+
})
46+
47+
it('should work with IPv6 Host', function(done){
48+
var app = express();
49+
50+
app.use(function(req, res){
51+
res.end(req.hostname);
52+
});
53+
54+
request(app)
55+
.post('/')
56+
.set('Host', '[::1]')
57+
.expect('[::1]', done);
58+
})
59+
60+
it('should work with IPv6 Host and port', function(done){
61+
var app = express();
62+
63+
app.use(function(req, res){
64+
res.end(req.hostname);
65+
});
66+
67+
request(app)
68+
.post('/')
69+
.set('Host', '[::1]:3000')
70+
.expect('[::1]', done);
71+
})
72+
73+
describe('when "trust proxy" is enabled', function(){
74+
it('should respect X-Forwarded-Host', function(done){
75+
var app = express();
76+
77+
app.enable('trust proxy');
78+
79+
app.use(function(req, res){
80+
res.end(req.hostname);
81+
});
82+
83+
request(app)
84+
.get('/')
85+
.set('Host', 'localhost')
86+
.set('X-Forwarded-Host', 'example.com:3000')
87+
.expect('example.com', done);
88+
})
89+
90+
it('should ignore X-Forwarded-Host if socket addr not trusted', function(done){
91+
var app = express();
92+
93+
app.set('trust proxy', '10.0.0.1');
94+
95+
app.use(function(req, res){
96+
res.end(req.hostname);
97+
});
98+
99+
request(app)
100+
.get('/')
101+
.set('Host', 'localhost')
102+
.set('X-Forwarded-Host', 'example.com')
103+
.expect('localhost', done);
104+
})
105+
106+
it('should default to Host', function(done){
107+
var app = express();
108+
109+
app.enable('trust proxy');
110+
111+
app.use(function(req, res){
112+
res.end(req.hostname);
113+
});
114+
115+
request(app)
116+
.get('/')
117+
.set('Host', 'example.com')
118+
.expect('example.com', done);
119+
})
120+
})
121+
122+
describe('when "trust proxy" is disabled', function(){
123+
it('should ignore X-Forwarded-Host', function(done){
124+
var app = express();
125+
126+
app.use(function(req, res){
127+
res.end(req.hostname);
128+
});
129+
130+
request(app)
131+
.get('/')
132+
.set('Host', 'localhost')
133+
.set('X-Forwarded-Host', 'evil')
134+
.expect('localhost', done);
135+
})
136+
})
137+
})
138+
})

0 commit comments

Comments
 (0)