Skip to content
This repository was archived by the owner on Jun 18, 2026. It is now read-only.

Commit f2b1080

Browse files
committed
https-proxy-agent: add support for proxy "headers"
Inspired from, and closes TooTallNate#5. Thanks @mrgiba!
1 parent 277c006 commit f2b1080

2 files changed

Lines changed: 36 additions & 7 deletions

File tree

https-proxy-agent.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,16 @@ function connect (req, opts, fn) {
191191

192192
var hostname = opts.host + ':' + opts.port;
193193
var msg = 'CONNECT ' + hostname + ' HTTP/1.1\r\n';
194-
var auth = proxy.auth;
195-
if (auth) {
196-
msg += 'Proxy-Authorization: Basic ' + new Buffer(auth).toString('base64') + '\r\n';
194+
195+
var headers = extend({}, proxy.headers);
196+
if (proxy.auth) {
197+
headers['Proxy-Authorization'] = 'Basic ' + new Buffer(proxy.auth).toString('base64');
197198
}
198-
msg += 'Host: ' + hostname + '\r\n' +
199-
'Connection: close\r\n' +
200-
'\r\n';
201-
socket.write(msg);
199+
headers['Host'] = hostname;
200+
headers['Connection'] = 'close';
201+
Object.keys(headers).forEach(function (name) {
202+
msg += name + ': ' + headers[name] + '\r\n';
203+
});
204+
205+
socket.write(msg + '\r\n');
202206
};

test/test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,31 @@ describe('HttpsProxyAgent', function () {
223223
done();
224224
});
225225
});
226+
227+
it('should allow custom proxy "headers"', function (done) {
228+
server.once('connect', function (req, socket, head) {
229+
assert.equal('CONNECT', req.method);
230+
assert.equal('bar', req.headers.foo);
231+
socket.destroy();
232+
done();
233+
});
234+
235+
var uri = 'http://127.0.0.1:' + serverPort;
236+
var proxyOpts = url.parse(uri);
237+
proxyOpts.headers = {
238+
'Foo': 'bar'
239+
};
240+
var agent = new HttpsProxyAgent(proxyOpts);
241+
242+
var opts = {};
243+
// `host` and `port` don't really matter since the proxy will reject anyways
244+
opts.host = '127.0.0.1';
245+
opts.port = 80;
246+
opts.agent = agent;
247+
248+
http.get(opts);
249+
});
250+
226251
});
227252

228253
describe('"https" module', function () {

0 commit comments

Comments
 (0)