Skip to content

Commit 6d85da1

Browse files
isaacsry
authored andcommitted
Closes GH-721 Set default host header properly
However, this test is failing for some quite unrelated issue. Getting some odd "socket hangup" crashes, and only the first request ever makes it to the server.
1 parent e1a72f0 commit 6d85da1

File tree

3 files changed

+164
-5
lines changed

3 files changed

+164
-5
lines changed

lib/http.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -862,9 +862,11 @@ ServerResponse.prototype.writeHeader = function() {
862862
};
863863

864864

865-
function ClientRequest(options) {
865+
function ClientRequest(options, defaultPort) {
866866
OutgoingMessage.call(this);
867867

868+
if (!defaultPort) defaultPort = 80;
869+
868870
var method = this.method = (options.method || 'GET').toUpperCase();
869871
this.path = options.path || '/';
870872

@@ -879,7 +881,11 @@ function ClientRequest(options) {
879881
}
880882
// Host header set by default.
881883
if (options.host && !this.getHeader('host')) {
882-
this.setHeader("Host", options.host);
884+
var hostHeader = options.host;
885+
if (options.port && +options.port !== defaultPort) {
886+
hostHeader += ':' + options.port;
887+
}
888+
this.setHeader("Host", hostHeader);
883889
}
884890
}
885891

@@ -1110,7 +1116,7 @@ exports._connectionListener = connectionListener;
11101116
function Agent(options) {
11111117
this.options = options;
11121118
this.host = options.host;
1113-
this.port = options.port || 80;
1119+
this.port = options.port || this.defaultPort;
11141120

11151121
this.queue = [];
11161122
this.sockets = [];
@@ -1122,11 +1128,11 @@ exports.Agent = Agent;
11221128

11231129
Agent.defaultMaxSockets = 5;
11241130

1125-
1131+
Agent.prototype.defaultPort = 80;
11261132
Agent.prototype.appendMessage = function(options) {
11271133
var self = this;
11281134

1129-
var req = new ClientRequest(options);
1135+
var req = new ClientRequest(options, this.defaultPort);
11301136
this.queue.push(req);
11311137
req._queue = this.queue;
11321138

lib/https.js

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ function Agent(options) {
5454
inherits(Agent, http.Agent);
5555

5656

57+
Agent.prototype.defaultPort = 443;
58+
59+
5760
Agent.prototype._getConnection = function(host, port, cb) {
5861
var s = tls.connect(port, host, this.options, function() {
5962
// do other checks here?

test/simple/test-http-host-headers.js

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var http = require('http'),
23+
https = require('https'),
24+
fs = require('fs'),
25+
common = require('../common'),
26+
assert = require('assert'),
27+
options = {
28+
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
29+
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
30+
},
31+
httpServer = http.createServer(reqHandler),
32+
httpsServer = https.createServer(options, reqHandler);
33+
34+
function reqHandler(req, res) {
35+
console.log('Got request: ' + req.headers.host + ' ' + req.url);
36+
assert.equal(req.headers.host, 'localhost:' + common.PORT,
37+
'Wrong host header for req[' + req.url + ']: ' +
38+
req.headers.host);
39+
res.writeHead(200, {});
40+
//process.nextTick(function() { res.end('ok'); });
41+
res.end('ok');
42+
}
43+
44+
function thrower(er) {
45+
throw er;
46+
}
47+
48+
testHttp();
49+
50+
function testHttp() {
51+
52+
console.log('testing http on port ' + common.PORT);
53+
54+
var counter = 0;
55+
56+
function cb() {
57+
counter--;
58+
console.log('back from http request. counter = ' + counter);
59+
if (counter === 0) {
60+
httpServer.close();
61+
testHttps();
62+
}
63+
}
64+
65+
httpServer.listen(common.PORT, function(er) {
66+
console.error('listening on ' + common.PORT);
67+
68+
if (er) throw er;
69+
70+
http.get({ method: 'GET',
71+
path: '/' + (counter++),
72+
host: 'localhost',
73+
//agent: false,
74+
port: common.PORT }, cb).on('error', thrower);
75+
76+
http.request({ method: 'GET',
77+
path: '/' + (counter++),
78+
host: 'localhost',
79+
//agent: false,
80+
port: common.PORT }, cb).on('error', thrower).end();
81+
82+
http.request({ method: 'POST',
83+
path: '/' + (counter++),
84+
host: 'localhost',
85+
//agent: false,
86+
port: common.PORT }, cb).on('error', thrower).end();
87+
88+
http.request({ method: 'PUT',
89+
path: '/' + (counter++),
90+
host: 'localhost',
91+
//agent: false,
92+
port: common.PORT }, cb).on('error', thrower).end();
93+
94+
http.request({ method: 'DELETE',
95+
path: '/' + (counter++),
96+
host: 'localhost',
97+
//agent: false,
98+
port: common.PORT }, cb).on('error', thrower).end();
99+
});
100+
}
101+
102+
function testHttps() {
103+
104+
console.log('testing https on port ' + common.PORT);
105+
106+
var counter = 0;
107+
108+
function cb() {
109+
counter--;
110+
console.log('back from https request. counter = ' + counter);
111+
if (counter === 0) {
112+
httpsServer.close();
113+
console.log('ok');
114+
}
115+
}
116+
117+
httpsServer.listen(common.PORT, function(er) {
118+
if (er) throw er;
119+
120+
https.get({ method: 'GET',
121+
path: '/' + (counter++),
122+
host: 'localhost',
123+
//agent: false,
124+
port: common.PORT }, cb).on('error', thrower);
125+
126+
https.request({ method: 'GET',
127+
path: '/' + (counter++),
128+
host: 'localhost',
129+
//agent: false,
130+
port: common.PORT }, cb).on('error', thrower).end();
131+
132+
https.request({ method: 'POST',
133+
path: '/' + (counter++),
134+
host: 'localhost',
135+
//agent: false,
136+
port: common.PORT }, cb).on('error', thrower).end();
137+
138+
https.request({ method: 'PUT',
139+
path: '/' + (counter++),
140+
host: 'localhost',
141+
//agent: false,
142+
port: common.PORT }, cb).on('error', thrower).end();
143+
144+
https.request({ method: 'DELETE',
145+
path: '/' + (counter++),
146+
host: 'localhost',
147+
//agent: false,
148+
port: common.PORT }, cb).on('error', thrower).end();
149+
});
150+
}

0 commit comments

Comments
 (0)