Skip to content

Commit b781af6

Browse files
alexindigojcrugzz
authored andcommitted
Made it not to crash with omited Host http header (http-party#1050)
1 parent cbd5777 commit b781af6

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

lib/http-proxy/passes/web-incoming.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ web_o = Object.keys(web_o).map(function(pass) {
7878
(req.headers['x-forwarded-' + header] ? ',' : '') +
7979
values[header];
8080
});
81-
82-
req.headers['x-forwarded-host'] = req.headers['host'];
81+
82+
req.headers['x-forwarded-host'] = req.headers['host'] || '';
8383
},
8484

8585
/**

test/lib-http-proxy-test.js

+52-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var httpProxy = require('../lib/http-proxy'),
22
expect = require('expect.js'),
33
http = require('http'),
4-
ws = require('ws')
4+
net = require('net'),
5+
ws = require('ws'),
56
io = require('socket.io'),
67
SSE = require('sse'),
78
ioClient = require('socket.io-client');
@@ -17,7 +18,6 @@ Object.defineProperty(gen, 'port', {
1718
}
1819
});
1920

20-
2121
describe('lib/http-proxy.js', function() {
2222
describe('#createProxyServer', function() {
2323
it.skip('should throw without options', function() {
@@ -223,11 +223,54 @@ describe('lib/http-proxy.js', function() {
223223
});
224224

225225
testReq.end();
226+
});
227+
});
228+
229+
describe('#createProxyServer with xfwd option', function () {
230+
it('should not throw on empty http host header', function (done) {
231+
var ports = { source: gen.port, proxy: gen.port };
232+
var proxy = httpProxy.createProxyServer({
233+
forward: 'http://127.0.0.1:' + ports.source,
234+
xfwd: true
235+
}).listen(ports.proxy);
236+
237+
var source = http.createServer(function(req, res) {
238+
expect(req.method).to.eql('GET');
239+
expect(req.headers.host.split(':')[1]).to.eql(ports.source);
240+
source.close();
241+
proxy.close();
242+
done();
243+
});
244+
245+
source.listen(ports.source);
246+
247+
var socket = net.connect({port: ports.proxy}, function()
248+
{
249+
socket.write('GET / HTTP/1.0\r\n\r\n');
250+
});
251+
252+
// handle errors
253+
socket.on('error', function()
254+
{
255+
expect.fail('Unexpected socket error');
256+
});
257+
258+
socket.on('data', function(data)
259+
{
260+
socket.end();
261+
});
262+
263+
socket.on('end', function()
264+
{
265+
expect('Socket to finish').to.be.ok();
266+
});
267+
268+
// http.request('http://127.0.0.1:' + ports.proxy, function() {}).end();
226269
})
227-
})
270+
});
228271

229272
// describe('#createProxyServer using the web-incoming passes', function () {
230-
// it('should emit events correclty', function(done) {
273+
// it('should emit events correctly', function(done) {
231274
// var proxy = httpProxy.createProxyServer({
232275
// target: 'http://127.0.0.1:8080'
233276
// }),
@@ -451,7 +494,7 @@ describe('lib/http-proxy.js', function() {
451494
proxyServer = proxy.listen(ports.proxy),
452495
destiny = new ws.Server({ port: ports.source }, function () {
453496
var key = new Buffer(Math.random().toString()).toString('base64');
454-
497+
455498
var requestOptions = {
456499
port: ports.proxy,
457500
host: '127.0.0.1',
@@ -465,15 +508,15 @@ describe('lib/http-proxy.js', function() {
465508
};
466509

467510
var req = http.request(requestOptions);
468-
511+
469512
req.on('upgrade', function (res, socket, upgradeHead) {
470513
expect(res.headers['set-cookie'].length).to.be(2);
471514
done();
472515
});
473-
516+
474517
req.end();
475518
});
476-
519+
477520
destiny.on('headers', function (headers) {
478521
headers.push('Set-Cookie: test1=test1');
479522
headers.push('Set-Cookie: test2=test2');
@@ -554,7 +597,7 @@ describe('lib/http-proxy.js', function() {
554597
});
555598
});
556599
});
557-
600+
558601
it('should forward continuation frames with big payload (including on node 4.x)', function (done) {
559602
var payload = Array(65530).join('0');
560603
var ports = { source: gen.port, proxy: gen.port };

0 commit comments

Comments
 (0)