Skip to content

Commit a0b74b2

Browse files
committed
Added express 'trust proxy' support
no issue - extracted from expressjs/vhost#21 - uses `req.hostname` if it's available. `req.hostname` is set to the `x-forwarded-host` value when `'trust proxy'` setting is enabled in express
1 parent 3e6a713 commit a0b74b2

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

ghost/vhost-middleware/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ property will be populated with an object. This object will have numeric propert
3535
corresponding to each wildcard (or capture group if RegExp object provided) and the
3636
`hostname` that was matched.
3737

38+
Where the `hostname` of the request comes from depends on the type of server you're running.
39+
If you're running a raw Node.js/connect server, this comes from [`req.headers.host`](https://nodejs.org/dist/latest/docs/api/http.html#http_message_headers).
40+
If you're running an express v4 server, this comes from [`req.hostname`](http://expressjs.com/en/4x/api.html#req.hostname).
41+
3842
```js
3943
var connect = require('connect')
4044
var vhost = require('vhost')

ghost/vhost-middleware/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ function vhost(hostname, handle) {
7474
*/
7575

7676
function hostnameof(req) {
77-
var host = req.headers.host;
77+
var host =
78+
req.hostname || // express v4
79+
req.host || // express v3
80+
req.headers.host; // http
7881

7982
if (!host) {
8083
return;

ghost/vhost-middleware/test/vhost.test.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,30 @@ describe('vhost(hostname, server)', function () {
2929
.expect(200, 'tobi', done);
3030
});
3131

32+
it('should route by `req.hostname` (express v4)', function (done) {
33+
const vhosts = [];
34+
35+
vhosts.push(vhost('anotherhost.com', anotherhost));
36+
vhosts.push(vhost('loki.com', loki));
37+
38+
const app = createServer(vhosts, null, function (req) {
39+
// simulate express setting req.hostname based on x-forwarded-host
40+
req.hostname = 'anotherhost.com';
41+
});
42+
43+
function anotherhost(req, res) {
44+
res.end('anotherhost');
45+
}
46+
function loki(req, res) {
47+
res.end('loki');
48+
}
49+
50+
request(app)
51+
.get('/')
52+
.set('Host', 'loki.com')
53+
.expect(200, 'anotherhost', done);
54+
});
55+
3256
it('should ignore port in Host', function (done) {
3357
const app = createServer('tobi.com', function (req, res) {
3458
res.end('tobi');
@@ -234,14 +258,19 @@ describe('vhost(hostname, server)', function () {
234258
});
235259
});
236260

237-
function createServer(hostname, server) {
261+
function createServer(hostname, server, pretest) {
238262
const vhosts = !Array.isArray(hostname)
239263
? [vhost(hostname, server)]
240264
: hostname;
241265

242266
return http.createServer(function onRequest(req, res) {
243-
let index = 0;
267+
// This allows you to perform changes to the request/response
268+
// objects before our assertions
269+
if (pretest) {
270+
pretest(req, res);
271+
}
244272

273+
let index = 0;
245274
function next(err) {
246275
const vhost = vhosts[index];
247276
index = index + 1;

0 commit comments

Comments
 (0)