Skip to content

Commit

Permalink
Fixes issue #23 odd behaviour with https and cloudflare
Browse files Browse the repository at this point in the history
  • Loading branch information
craigedmunds committed Sep 15, 2016
1 parent 27eab62 commit 2aa7fca
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
16 changes: 12 additions & 4 deletions lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ module.exports = function proxy(req, body, host) {
return new Promise(function (resolve /* , reject */) {
var uri = url.parse(host);
var mod = mods[uri.protocol] || http;
var preq = mod.request({

var h = req.headers;
h['host'] = uri.host;

var r = {
hostname: uri.hostname,
port: uri.port,
method: req.method,
Expand All @@ -35,11 +39,15 @@ module.exports = function proxy(req, body, host) {

servername: uri.hostname,
rejectUnauthorized: false
}, function (pres) {
};

debug('r', r);

var preq = mod.request(r, function (pres) {
resolve(pres);
});

preq.setHeader('Host', uri.host);
// preq.setHeader('Host', uri.host);

debug('req', req.url, 'host', uri.host);

Expand Down
16 changes: 15 additions & 1 deletion test/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var subject = require('../lib/proxy');
var createServer = require('./helpers/server');
var assert = require('assert');
var http = require('http');
var https = require('https');

describe('proxy', function () {
var server, req;
Expand Down Expand Up @@ -39,6 +40,19 @@ describe('proxy', function () {
});
});

it('overrides the host if one is set on the incoming request', function (done) {
server.once('request', function (preq) {
assert.equal(preq.headers.host, server.addr + ':' + server.port);
done();
});

req.headers['host'] = 'A.N.OTHER'

subject(req, [], server.host).catch(function (err) {
done(err);
});
});

it('proxies the request body', function (done) {
var body = [
new Buffer('a'),
Expand Down Expand Up @@ -75,4 +89,4 @@ describe('proxy', function () {
});
});

});
});

0 comments on commit 2aa7fca

Please sign in to comment.