Skip to content

Commit

Permalink
[api test doc] Improve HTTPS support. Update minor documentation. Cha…
Browse files Browse the repository at this point in the history
…nge tests accordingly.
  • Loading branch information
indexzero committed Apr 17, 2011
1 parent 4d18ac1 commit bf68dc3
Show file tree
Hide file tree
Showing 15 changed files with 211 additions and 69 deletions.
53 changes: 53 additions & 0 deletions examples/basic-proxy-https.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
basic-proxy-https.js: Basic example of proxying over HTTPS
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

var https = require('https'),
http = require('http'),
util = require('util'),
colors = require('colors'),
httpProxy = require('./../lib/node-http-proxy'),
helpers = require('./../test/helpers');

var opts = helpers.loadHttps();

//
// Crete the target HTTPS server
//
https.createServer(opts, function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('hello https\n');
res.end();
}).listen(8000);

//
// Create the proxy server listening on port 443.
//
httpProxy.createServer(443, 'localhost', {
https: opts,
}).listen(8080);

util.puts('https proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
util.puts('https server '.blue + 'started '.green.bold + 'on port '.blue + '8080 '.yellow);
2 changes: 1 addition & 1 deletion examples/basic-proxy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
demo.js: http proxy for node.js
basic-proxy.js: Basic example of proxying over HTTP
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
Expand Down
2 changes: 1 addition & 1 deletion examples/forward-proxy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
demo.js: http proxy for node.js
forward-proxy.js: Example of proxying over HTTP with additional forward proxy
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
Expand Down
2 changes: 1 addition & 1 deletion examples/latent-proxy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
demo.js: http proxy for node.js
latent-proxy.js: Example of proxying over HTTP with latency
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
Expand Down
2 changes: 1 addition & 1 deletion examples/proxy-table.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
demo.js: http proxy for node.js
proxy-table.js: Example of proxying over HTTP with proxy table
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
Expand Down
2 changes: 1 addition & 1 deletion examples/standalone-proxy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
demo.js: http proxy for node.js
standalone-proxy.js: Example of proxying over HTTP inside of a standalone HTTP server.
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
Expand Down
2 changes: 1 addition & 1 deletion examples/web-socket-proxy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
demo.js: http proxy for node.js
web-socket-proxy.js: Example of proxying over HTTP and WebSockets.
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
Expand Down
35 changes: 20 additions & 15 deletions lib/node-http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ exports.version = [0, 4, 2];
// #### @host {string} Host of the agent to get
// #### @port {number} Port of the agent to get
// #### @secure {boolean} Value indicating whether or not to use HTTPS
// Retreives an agent from the `http` module
// Retreives an agent from the `http` or `https` module
// and sets the `maxSockets` property appropriately.
//
function _getAgent (host, port, secure) {
Expand All @@ -55,18 +55,19 @@ function _getAgent (host, port, secure) {
}

//
// ### function _getProtocol (outgoing, https)
// #### @outgoing {Object} Outgoing request options
// ### function _getProtocol (secure, outgoing)
// #### @secure {Object|boolean} Settings for `https`
// #### @outgoing {Object} Outgoing request options
// Returns the appropriate protocol based on the settings in
// `secure`. If the protocol is `https` this function will update
// the options in `outgoing` as appropriate by adding `ca`, `key`,
// and `cert` if they exist in `secure`.
//
function _getProtocol (outgoing, secure) {
function _getProtocol (secure, outgoing) {
var protocol = secure ? https : http;

if (typeof secure === 'object') {
outgoing = outgoing || {};
['ca', 'cert', 'key'].forEach(function (prop) {
if (secure[prop]) {
outgoing[prop] = secure[prop];
Expand Down Expand Up @@ -110,11 +111,10 @@ exports.setMaxSockets = function (value) {
// * `httpPRoxy.createServer(function (req, res, proxy) { ... })`
//
exports.createServer = function () {
var args, callback, port, host, forward,
silent, options, proxy, server;

args = Array.prototype.slice.call(arguments);
callback = typeof args[args.length - 1] === 'function' && args.pop();
var args = Array.prototype.slice.call(arguments),
callback = typeof args[0] === 'function' && args.shift(),
options = {},
port, host, forward, silent, proxy, server;

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

proxy = new HttpProxy(options);
server = http.createServer(function (req, res) {

handler = function (req, res) {
if (callback) {
//
// If we were passed a callback to process the request
Expand Down Expand Up @@ -160,7 +161,11 @@ exports.createServer = function () {
//
throw new Error('Cannot proxy without port, host, or router.')
}
});
};

server = options.https
? https.createServer(options.https, handler)
: http.createServer(handler);

server.on('close', function () {
proxy.close();
Expand All @@ -187,7 +192,7 @@ exports.createServer = function () {
// Set the proxy on the server so it is available
// to the consumer of the server
//
server.proxy = proxy;
//server.proxy = proxy;

return server;
};
Expand Down Expand Up @@ -374,12 +379,12 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
path: req.url,
headers: req.headers
};

// Force the `connection` header to be 'close' until
// node.js core re-implements 'keep-alive'.
outgoing.headers['connection'] = 'close';

protocol = _getProtocol(outgoing, options.https || this.https);
protocol = _getProtocol(options.https || this.https, outgoing);

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

protocol = _getProtocol(outgoing, this.forward.https);
protocol = _getProtocol(this.forward.https, outgoing);

// Open new HTTP request to internal resource with will act as a reverse proxy pass
forwardProxy = protocol.request(outgoing, function (response) {
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/agent2-cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-----BEGIN CERTIFICATE-----
MIIB7DCCAZYCCQC7gs0MDNn6MTANBgkqhkiG9w0BAQUFADB9MQswCQYDVQQGEwJV
UzELMAkGA1UECBMCQ0ExCzAJBgNVBAcTAlNGMQ8wDQYDVQQKEwZKb3llbnQxEDAO
BgNVBAsTB05vZGUuanMxDzANBgNVBAMTBmFnZW50MjEgMB4GCSqGSIb3DQEJARYR
cnlAdGlueWNsb3Vkcy5vcmcwHhcNMTEwMzE0MTgyOTEyWhcNMzgwNzI5MTgyOTEy
WjB9MQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExCzAJBgNVBAcTAlNGMQ8wDQYD
VQQKEwZKb3llbnQxEDAOBgNVBAsTB05vZGUuanMxDzANBgNVBAMTBmFnZW50MjEg
MB4GCSqGSIb3DQEJARYRcnlAdGlueWNsb3Vkcy5vcmcwXDANBgkqhkiG9w0BAQEF
AANLADBIAkEAyXb8FrRdKbhrKLgLSsn61i1C7w7fVVVd7OQsmV/7p9WB2lWFiDlC
WKGU9SiIz/A6wNZDUAuc2E+VwtpCT561AQIDAQABMA0GCSqGSIb3DQEBBQUAA0EA
C8HzpuNhFLCI3A5KkBS5zHAQax6TFUOhbpBCR0aTDbJ6F1liDTK1lmU/BjvPoj+9
1LHwrmh29rK8kBPEjmymCQ==
-----END CERTIFICATE-----
10 changes: 10 additions & 0 deletions test/fixtures/agent2-csr.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-----BEGIN CERTIFICATE REQUEST-----
MIIBXTCCAQcCAQAwfTELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMQswCQYDVQQH
EwJTRjEPMA0GA1UEChMGSm95ZW50MRAwDgYDVQQLEwdOb2RlLmpzMQ8wDQYDVQQD
EwZhZ2VudDIxIDAeBgkqhkiG9w0BCQEWEXJ5QHRpbnljbG91ZHMub3JnMFwwDQYJ
KoZIhvcNAQEBBQADSwAwSAJBAMl2/Ba0XSm4ayi4C0rJ+tYtQu8O31VVXezkLJlf
+6fVgdpVhYg5QlihlPUoiM/wOsDWQ1ALnNhPlcLaQk+etQECAwEAAaAlMCMGCSqG
SIb3DQEJBzEWExRBIGNoYWxsZW5nZSBwYXNzd29yZDANBgkqhkiG9w0BAQUFAANB
AJnll2pt5l0pzskQSpjjLVTlFDFmJr/AZ3UK8v0WxBjYjCe5Jx4YehkChpxIyDUm
U3J9q9MDUf0+Y2+EGkssFfk=
-----END CERTIFICATE REQUEST-----
9 changes: 9 additions & 0 deletions test/fixtures/agent2-key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBAMl2/Ba0XSm4ayi4C0rJ+tYtQu8O31VVXezkLJlf+6fVgdpVhYg5
QlihlPUoiM/wOsDWQ1ALnNhPlcLaQk+etQECAwEAAQJBAMT6Bf34+UHKY1ObpsbH
9u2jsVblFq1rWvs8GPMY6oertzvwm3DpuSUp7PTgOB1nLTLYtCERbQ4ovtN8tn3p
OHUCIQDzIEGsoCr5vlxXvy2zJwu+fxYuhTZWMVuo1397L0VyhwIhANQh+yzqUgaf
WRtSB4T2W7ADtJI35ET61jKBty3CqJY3AiAIwju7dVW3A5WeD6Qc1SZGKZvp9yCb
AFI2BfVwwaY11wIgXF3PeGcvACMyMWsuSv7aPXHfliswAbkWuzcwA4TW01ECIGWa
cgsDvVFxmfM5NPSuT/UDTa6R5BFISB5ea0N0AR3I
-----END RSA PRIVATE KEY-----
19 changes: 19 additions & 0 deletions test/fixtures/agent2.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[ req ]
default_bits = 1024
days = 999
distinguished_name = req_distinguished_name
attributes = req_attributes
prompt = no

[ req_distinguished_name ]
C = US
ST = CA
L = SF
O = Joyent
OU = Node.js
CN = agent2
emailAddress = [email protected]

[ req_attributes ]
challengePassword = A challenge password

Loading

0 comments on commit bf68dc3

Please sign in to comment.