Skip to content

Commit

Permalink
Made it not to crash with omited Host http header (#1050)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexindigo authored and jcrugzz committed Sep 14, 2016
1 parent cbd5777 commit b781af6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/http-proxy/passes/web-incoming.js
Expand Up @@ -78,8 +78,8 @@ web_o = Object.keys(web_o).map(function(pass) {
(req.headers['x-forwarded-' + header] ? ',' : '') +
values[header];
});
req.headers['x-forwarded-host'] = req.headers['host'];

req.headers['x-forwarded-host'] = req.headers['host'] || '';
},

/**
Expand Down
61 changes: 52 additions & 9 deletions test/lib-http-proxy-test.js
@@ -1,7 +1,8 @@
var httpProxy = require('../lib/http-proxy'),
expect = require('expect.js'),
http = require('http'),
ws = require('ws')
net = require('net'),
ws = require('ws'),
io = require('socket.io'),
SSE = require('sse'),
ioClient = require('socket.io-client');
Expand All @@ -17,7 +18,6 @@ Object.defineProperty(gen, 'port', {
}
});


describe('lib/http-proxy.js', function() {
describe('#createProxyServer', function() {
it.skip('should throw without options', function() {
Expand Down Expand Up @@ -223,11 +223,54 @@ describe('lib/http-proxy.js', function() {
});

testReq.end();
});
});

describe('#createProxyServer with xfwd option', function () {
it('should not throw on empty http host header', function (done) {
var ports = { source: gen.port, proxy: gen.port };
var proxy = httpProxy.createProxyServer({
forward: 'http://127.0.0.1:' + ports.source,
xfwd: true
}).listen(ports.proxy);

var source = http.createServer(function(req, res) {
expect(req.method).to.eql('GET');
expect(req.headers.host.split(':')[1]).to.eql(ports.source);
source.close();
proxy.close();
done();
});

source.listen(ports.source);

var socket = net.connect({port: ports.proxy}, function()
{
socket.write('GET / HTTP/1.0\r\n\r\n');
});

// handle errors
socket.on('error', function()
{
expect.fail('Unexpected socket error');
});

socket.on('data', function(data)
{
socket.end();
});

socket.on('end', function()
{
expect('Socket to finish').to.be.ok();
});

// http.request('http://127.0.0.1:' + ports.proxy, function() {}).end();
})
})
});

// describe('#createProxyServer using the web-incoming passes', function () {
// it('should emit events correclty', function(done) {
// it('should emit events correctly', function(done) {
// var proxy = httpProxy.createProxyServer({
// target: 'http://127.0.0.1:8080'
// }),
Expand Down Expand Up @@ -451,7 +494,7 @@ describe('lib/http-proxy.js', function() {
proxyServer = proxy.listen(ports.proxy),
destiny = new ws.Server({ port: ports.source }, function () {
var key = new Buffer(Math.random().toString()).toString('base64');

var requestOptions = {
port: ports.proxy,
host: '127.0.0.1',
Expand All @@ -465,15 +508,15 @@ describe('lib/http-proxy.js', function() {
};

var req = http.request(requestOptions);

req.on('upgrade', function (res, socket, upgradeHead) {
expect(res.headers['set-cookie'].length).to.be(2);
done();
});

req.end();
});

destiny.on('headers', function (headers) {
headers.push('Set-Cookie: test1=test1');
headers.push('Set-Cookie: test2=test2');
Expand Down Expand Up @@ -554,7 +597,7 @@ describe('lib/http-proxy.js', function() {
});
});
});

it('should forward continuation frames with big payload (including on node 4.x)', function (done) {
var payload = Array(65530).join('0');
var ports = { source: gen.port, proxy: gen.port };
Expand Down

0 comments on commit b781af6

Please sign in to comment.