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

This commit is contained in:
Alex Indigo 2016-09-14 04:03:48 -07:00 committed by Jarrett Cruger
parent cbd5777060
commit b781af641a
2 changed files with 54 additions and 11 deletions

View File

@ -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'] || '';
},
/**

View File

@ -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');
@ -17,7 +18,6 @@ Object.defineProperty(gen, 'port', {
}
});
describe('lib/http-proxy.js', function() {
describe('#createProxyServer', function() {
it.skip('should throw without options', function() {
@ -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'
// }),
@ -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',
@ -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');
@ -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 };