Skip to content

Commit bf68dc3

Browse files
committed
[api test doc] Improve HTTPS support. Update minor documentation. Change tests accordingly.
1 parent 4d18ac1 commit bf68dc3

15 files changed

+211
-69
lines changed

examples/basic-proxy-https.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
basic-proxy-https.js: Basic example of proxying over HTTPS
3+
4+
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining
7+
a copy of this software and associated documentation files (the
8+
"Software"), to deal in the Software without restriction, including
9+
without limitation the rights to use, copy, modify, merge, publish,
10+
distribute, sublicense, and/or sell copies of the Software, and to
11+
permit persons to whom the Software is furnished to do so, subject to
12+
the following conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
25+
*/
26+
27+
var https = require('https'),
28+
http = require('http'),
29+
util = require('util'),
30+
colors = require('colors'),
31+
httpProxy = require('./../lib/node-http-proxy'),
32+
helpers = require('./../test/helpers');
33+
34+
var opts = helpers.loadHttps();
35+
36+
//
37+
// Crete the target HTTPS server
38+
//
39+
https.createServer(opts, function (req, res) {
40+
res.writeHead(200, {'Content-Type': 'text/plain'});
41+
res.write('hello https\n');
42+
res.end();
43+
}).listen(8000);
44+
45+
//
46+
// Create the proxy server listening on port 443.
47+
//
48+
httpProxy.createServer(443, 'localhost', {
49+
https: opts,
50+
}).listen(8080);
51+
52+
util.puts('https proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
53+
util.puts('https server '.blue + 'started '.green.bold + 'on port '.blue + '8080 '.yellow);

examples/basic-proxy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
demo.js: http proxy for node.js
2+
basic-proxy.js: Basic example of proxying over HTTP
33
44
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
55

examples/forward-proxy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
demo.js: http proxy for node.js
2+
forward-proxy.js: Example of proxying over HTTP with additional forward proxy
33
44
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
55

examples/latent-proxy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
demo.js: http proxy for node.js
2+
latent-proxy.js: Example of proxying over HTTP with latency
33
44
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
55

examples/proxy-table.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
demo.js: http proxy for node.js
2+
proxy-table.js: Example of proxying over HTTP with proxy table
33
44
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
55

examples/standalone-proxy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
demo.js: http proxy for node.js
2+
standalone-proxy.js: Example of proxying over HTTP inside of a standalone HTTP server.
33
44
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
55

examples/web-socket-proxy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
demo.js: http proxy for node.js
2+
web-socket-proxy.js: Example of proxying over HTTP and WebSockets.
33
44
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
55

lib/node-http-proxy.js

+20-15
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ exports.version = [0, 4, 2];
4141
// #### @host {string} Host of the agent to get
4242
// #### @port {number} Port of the agent to get
4343
// #### @secure {boolean} Value indicating whether or not to use HTTPS
44-
// Retreives an agent from the `http` module
44+
// Retreives an agent from the `http` or `https` module
4545
// and sets the `maxSockets` property appropriately.
4646
//
4747
function _getAgent (host, port, secure) {
@@ -55,18 +55,19 @@ function _getAgent (host, port, secure) {
5555
}
5656

5757
//
58-
// ### function _getProtocol (outgoing, https)
59-
// #### @outgoing {Object} Outgoing request options
58+
// ### function _getProtocol (secure, outgoing)
6059
// #### @secure {Object|boolean} Settings for `https`
60+
// #### @outgoing {Object} Outgoing request options
6161
// Returns the appropriate protocol based on the settings in
6262
// `secure`. If the protocol is `https` this function will update
6363
// the options in `outgoing` as appropriate by adding `ca`, `key`,
6464
// and `cert` if they exist in `secure`.
6565
//
66-
function _getProtocol (outgoing, secure) {
66+
function _getProtocol (secure, outgoing) {
6767
var protocol = secure ? https : http;
6868

6969
if (typeof secure === 'object') {
70+
outgoing = outgoing || {};
7071
['ca', 'cert', 'key'].forEach(function (prop) {
7172
if (secure[prop]) {
7273
outgoing[prop] = secure[prop];
@@ -110,11 +111,10 @@ exports.setMaxSockets = function (value) {
110111
// * `httpPRoxy.createServer(function (req, res, proxy) { ... })`
111112
//
112113
exports.createServer = function () {
113-
var args, callback, port, host, forward,
114-
silent, options, proxy, server;
115-
116-
args = Array.prototype.slice.call(arguments);
117-
callback = typeof args[args.length - 1] === 'function' && args.pop();
114+
var args = Array.prototype.slice.call(arguments),
115+
callback = typeof args[0] === 'function' && args.shift(),
116+
options = {},
117+
port, host, forward, silent, proxy, server;
118118

119119
if (args.length >= 2) {
120120
port = args[0];
@@ -129,7 +129,8 @@ exports.createServer = function () {
129129
}
130130

131131
proxy = new HttpProxy(options);
132-
server = http.createServer(function (req, res) {
132+
133+
handler = function (req, res) {
133134
if (callback) {
134135
//
135136
// If we were passed a callback to process the request
@@ -160,7 +161,11 @@ exports.createServer = function () {
160161
//
161162
throw new Error('Cannot proxy without port, host, or router.')
162163
}
163-
});
164+
};
165+
166+
server = options.https
167+
? https.createServer(options.https, handler)
168+
: http.createServer(handler);
164169

165170
server.on('close', function () {
166171
proxy.close();
@@ -187,7 +192,7 @@ exports.createServer = function () {
187192
// Set the proxy on the server so it is available
188193
// to the consumer of the server
189194
//
190-
server.proxy = proxy;
195+
//server.proxy = proxy;
191196

192197
return server;
193198
};
@@ -374,12 +379,12 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
374379
path: req.url,
375380
headers: req.headers
376381
};
377-
382+
378383
// Force the `connection` header to be 'close' until
379384
// node.js core re-implements 'keep-alive'.
380385
outgoing.headers['connection'] = 'close';
381386

382-
protocol = _getProtocol(outgoing, options.https || this.https);
387+
protocol = _getProtocol(options.https || this.https, outgoing);
383388

384389
// Open new HTTP request to internal resource with will act as a reverse proxy pass
385390
reverseProxy = protocol.request(outgoing, function (response) {
@@ -474,7 +479,7 @@ HttpProxy.prototype._forwardRequest = function (req) {
474479
// node.js core re-implements 'keep-alive'.
475480
outgoing.headers['connection'] = 'close';
476481

477-
protocol = _getProtocol(outgoing, this.forward.https);
482+
protocol = _getProtocol(this.forward.https, outgoing);
478483

479484
// Open new HTTP request to internal resource with will act as a reverse proxy pass
480485
forwardProxy = protocol.request(outgoing, function (response) {

test/fixtures/agent2-cert.pem

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIB7DCCAZYCCQC7gs0MDNn6MTANBgkqhkiG9w0BAQUFADB9MQswCQYDVQQGEwJV
3+
UzELMAkGA1UECBMCQ0ExCzAJBgNVBAcTAlNGMQ8wDQYDVQQKEwZKb3llbnQxEDAO
4+
BgNVBAsTB05vZGUuanMxDzANBgNVBAMTBmFnZW50MjEgMB4GCSqGSIb3DQEJARYR
5+
cnlAdGlueWNsb3Vkcy5vcmcwHhcNMTEwMzE0MTgyOTEyWhcNMzgwNzI5MTgyOTEy
6+
WjB9MQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExCzAJBgNVBAcTAlNGMQ8wDQYD
7+
VQQKEwZKb3llbnQxEDAOBgNVBAsTB05vZGUuanMxDzANBgNVBAMTBmFnZW50MjEg
8+
MB4GCSqGSIb3DQEJARYRcnlAdGlueWNsb3Vkcy5vcmcwXDANBgkqhkiG9w0BAQEF
9+
AANLADBIAkEAyXb8FrRdKbhrKLgLSsn61i1C7w7fVVVd7OQsmV/7p9WB2lWFiDlC
10+
WKGU9SiIz/A6wNZDUAuc2E+VwtpCT561AQIDAQABMA0GCSqGSIb3DQEBBQUAA0EA
11+
C8HzpuNhFLCI3A5KkBS5zHAQax6TFUOhbpBCR0aTDbJ6F1liDTK1lmU/BjvPoj+9
12+
1LHwrmh29rK8kBPEjmymCQ==
13+
-----END CERTIFICATE-----

test/fixtures/agent2-csr.pem

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-----BEGIN CERTIFICATE REQUEST-----
2+
MIIBXTCCAQcCAQAwfTELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMQswCQYDVQQH
3+
EwJTRjEPMA0GA1UEChMGSm95ZW50MRAwDgYDVQQLEwdOb2RlLmpzMQ8wDQYDVQQD
4+
EwZhZ2VudDIxIDAeBgkqhkiG9w0BCQEWEXJ5QHRpbnljbG91ZHMub3JnMFwwDQYJ
5+
KoZIhvcNAQEBBQADSwAwSAJBAMl2/Ba0XSm4ayi4C0rJ+tYtQu8O31VVXezkLJlf
6+
+6fVgdpVhYg5QlihlPUoiM/wOsDWQ1ALnNhPlcLaQk+etQECAwEAAaAlMCMGCSqG
7+
SIb3DQEJBzEWExRBIGNoYWxsZW5nZSBwYXNzd29yZDANBgkqhkiG9w0BAQUFAANB
8+
AJnll2pt5l0pzskQSpjjLVTlFDFmJr/AZ3UK8v0WxBjYjCe5Jx4YehkChpxIyDUm
9+
U3J9q9MDUf0+Y2+EGkssFfk=
10+
-----END CERTIFICATE REQUEST-----

test/fixtures/agent2-key.pem

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-----BEGIN RSA PRIVATE KEY-----
2+
MIIBOgIBAAJBAMl2/Ba0XSm4ayi4C0rJ+tYtQu8O31VVXezkLJlf+6fVgdpVhYg5
3+
QlihlPUoiM/wOsDWQ1ALnNhPlcLaQk+etQECAwEAAQJBAMT6Bf34+UHKY1ObpsbH
4+
9u2jsVblFq1rWvs8GPMY6oertzvwm3DpuSUp7PTgOB1nLTLYtCERbQ4ovtN8tn3p
5+
OHUCIQDzIEGsoCr5vlxXvy2zJwu+fxYuhTZWMVuo1397L0VyhwIhANQh+yzqUgaf
6+
WRtSB4T2W7ADtJI35ET61jKBty3CqJY3AiAIwju7dVW3A5WeD6Qc1SZGKZvp9yCb
7+
AFI2BfVwwaY11wIgXF3PeGcvACMyMWsuSv7aPXHfliswAbkWuzcwA4TW01ECIGWa
8+
cgsDvVFxmfM5NPSuT/UDTa6R5BFISB5ea0N0AR3I
9+
-----END RSA PRIVATE KEY-----

test/fixtures/agent2.cnf

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[ req ]
2+
default_bits = 1024
3+
days = 999
4+
distinguished_name = req_distinguished_name
5+
attributes = req_attributes
6+
prompt = no
7+
8+
[ req_distinguished_name ]
9+
C = US
10+
ST = CA
11+
L = SF
12+
O = Joyent
13+
OU = Node.js
14+
CN = agent2
15+
emailAddress = [email protected]
16+
17+
[ req_attributes ]
18+
challengePassword = A challenge password
19+

0 commit comments

Comments
 (0)