Skip to content
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

Fixes issue #23 odd behaviour with https and cloudflare #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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 () {
});
});

});
});