Add httpsOptions option to create https.Server

Note: This does not work with the current version of node-http-proxy
and node 0.12.0 because req.connection.pair is not defined.
This commit is contained in:
Rob Wu
2015-05-06 12:07:31 +02:00
parent 1ee157fc0f
commit 80da247827
5 changed files with 82 additions and 28 deletions

View File

@@ -98,6 +98,8 @@ The following options are recognized by both methods:
`createServer` recognizes the following option as well:
* `httpProxyOptions` - Options for http-proxy. The documentation for these options can be found [here](https://github.com/nodejitsu/node-http-proxy#options).
* `httpsOptions` - If set, a `https.Server` will be created. The given options are passed to the
[`https.createServer`](https://nodejs.org/api/https.html#https_https_createserver_options_requestlistener) method.
## Dependencies

View File

@@ -4,7 +4,6 @@
'use strict';
/* jshint node:true, eqnull:true, sub:true, quotmark:single, unused:true */
var http = require('http');
var httpProxy = require('http-proxy');
var net = require('net');
var url = require('url');
@@ -308,7 +307,13 @@ exports.createServer = function createServer(options) {
}
var proxy = httpProxy.createServer(httpProxyOptions);
var server = http.createServer(getHandler(options, proxy));
var requestHandler = getHandler(options, proxy);
var server;
if (options.httpsOptions) {
server = require('https').createServer(options.httpsOptions, requestHandler);
} else {
server = require('http').createServer(requestHandler);
}
// When the server fails, just show a 404 instead of Internal server error
proxy.on('error', function(err, req, res) {

12
test/cert.pem Normal file
View File

@@ -0,0 +1,12 @@
-----BEGIN CERTIFICATE-----
MIIBsTCCARoCCQDp0DuED0RAJzANBgkqhkiG9w0BAQsFADAdMRswGQYDVQQDDBJj
b3JzLWFueXdoZXJlIHRlc3QwHhcNMTUwNTA2MDcyOTM1WhcNMTUwNjA1MDcyOTM1
WjAdMRswGQYDVQQDDBJjb3JzLWFueXdoZXJlIHRlc3QwgZ8wDQYJKoZIhvcNAQEB
BQADgY0AMIGJAoGBALzTF5ClJKvkB6h9h7kLORV+mMV3ySDs+oGZn0NgXM+yb9Zh
69r5e95zZJl/V432LFdy0hkEcVteUkC2REWG8D4COGfiwWsXyZdaP1qqLpDpPAMm
v6xFHjW6rVuxzfr4GUjE0Zh9Fg2R2SbtCOcHS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oELwRL8
RYXzc1Mr2fDdZDgwgjg67JQqIhOQ3E4RGKPgZ+E7Pk3/
-----END RSA PRIVATE KEY-----

View File

@@ -236,32 +236,6 @@ describe('Basic functionality', function() {
}, done);
});
// Skipped because x-forwarded-proto == http and port == 80
it.skip('X-Forwarded-* headers (https)', function(done) {
request(cors_anywhere)
.get('/https://example.com/echoheaders')
.set('test-include-xfwd', '')
.expect('Access-Control-Allow-Origin', '*')
.expectJSON({
host: 'example.com',
'x-forwarded-port': '443',
'x-forwarded-proto': 'https',
}, done);
});
// Skipped because x-forwarded-proto == http
it.skip('X-Forwarded-* headers (https, non-standard port)', function(done) {
request(cors_anywhere)
.get('/https://example.com:1337/echoheaders')
.set('test-include-xfwd', '')
.expect('Access-Control-Allow-Origin', '*')
.expectJSON({
host: 'example.com:1337',
'x-forwarded-port': '1337',
'x-forwarded-proto': 'https',
}, done);
});
it('Ignore cookies', function(done) {
request(cors_anywhere)
.get('/example.com/setcookie')
@@ -277,7 +251,53 @@ describe('Basic functionality', function() {
})
.end(done);
});
});
describe('server on https', function() {
var NODE_TLS_REJECT_UNAUTHORIZED;
before(function() {
cors_anywhere = createServer({
httpsOptions: {
key: fs.readFileSync(path.join(__dirname, 'key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'cert.pem')),
},
});
// Disable certificate validation in case the certificate expires.
NODE_TLS_REJECT_UNAUTHORIZED = process.env.NODE_TLS_REJECT_UNAUTHORIZED;
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
});
after(function(done) {
if (NODE_TLS_REJECT_UNAUTHORIZED === undefined) {
delete process.env.NODE_TLS_REJECT_UNAUTHORIZED;
} else {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = NODE_TLS_REJECT_UNAUTHORIZED;
}
stopServer(done);
});
it('X-Forwarded-* headers (https)', function(done) {
request(cors_anywhere)
.get('/https://example.com/echoheaders')
.set('test-include-xfwd', '')
.expect('Access-Control-Allow-Origin', '*')
.expectJSON({
host: 'example.com',
'x-forwarded-port': '443',
'x-forwarded-proto': 'https',
}, done);
});
it('X-Forwarded-* headers (https, non-standard port)', function(done) {
request(cors_anywhere)
.get('/https://example.com:1337/echoheaders')
.set('test-include-xfwd', '')
.expect('Access-Control-Allow-Origin', '*')
.expectJSON({
host: 'example.com:1337',
'x-forwarded-port': '1337',
'x-forwarded-proto': 'https',
}, done);
});
});
describe('requireHeader', function() {