Skip to content

Made it not to crash with omited Host http header #1050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/http-proxy/passes/web-incoming.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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