diff --git a/lib/node-http-proxy.js b/lib/node-http-proxy.js index af0dd29..ccbd80b 100644 --- a/lib/node-http-proxy.js +++ b/lib/node-http-proxy.js @@ -78,7 +78,7 @@ exports.createServer = function () { case 'function': callback = arg; break; }; }); - + if (!host && !port && !options) { // // If `host`, `port` and `options` are all not passed, then @@ -285,12 +285,12 @@ exports._getProtocol = function _getProtocol (options) { // containing the relevant `ca`, `key`, and `cert` properties. // exports._getBase = function _getBase (options) { - var result = {}; + var result = function () {}; if (options.https && typeof options.https === 'object') { ['ca', 'cert', 'key'].forEach(function (key) { if (options.https[key]) { - result[key] = options.https[key]; + result.prototype[key] = options.https[key]; } }); } diff --git a/lib/node-http-proxy/http-proxy.js b/lib/node-http-proxy/http-proxy.js index 9461f2a..223452e 100644 --- a/lib/node-http-proxy/http-proxy.js +++ b/lib/node-http-proxy/http-proxy.js @@ -98,7 +98,8 @@ var HttpProxy = exports.HttpProxy = function (options) { // be provided or the operation will fail with an `origin mismatch` // by definition. // - this.source = options.source || { host: 'localhost', port: 8000 }; + this.source = options.source || { host: 'localhost', port: 8000 }; + this.source.https = this.source.https || options.https; this.changeOrigin = options.changeOrigin || false; }; @@ -114,7 +115,7 @@ util.inherits(HttpProxy, events.EventEmitter); HttpProxy.prototype.proxyRequest = function (req, res, buffer) { var self = this, errState = false, - outgoing = new Object(this.target.base), + outgoing = new(this.target.base), reverseProxy; // @@ -286,7 +287,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) { // HttpProxy.prototype._forwardRequest = function (req) { var self = this, - outgoing = new Object(this.forward.base), + outgoing = new(this.forward.base), forwardProxy; // @@ -336,7 +337,7 @@ HttpProxy.prototype._forwardRequest = function (req) { }; // -// ### function proxyWebSocketRequest (req, socket, head, options) +// ### function proxyWebSocketRequest (req, socket, head, buffer) // #### @req {ServerRequest} Websocket request to proxy. // #### @socket {net.Socket} Socket for the underlying HTTP request // #### @head {string} Headers for the Websocket request. @@ -346,7 +347,7 @@ HttpProxy.prototype._forwardRequest = function (req) { // HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer) { var self = this, - outgoing = new Object(this.target.base); + outgoing = new(this.target.base); listeners = {}, errState = false, CRLF = '\r\n'; @@ -478,7 +479,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer) var agent = this.target.agent, protocolName = this.target.https ? 'https' : 'http', portUri = getPort(this.source.port), - remoteHost = options.host + portUri; + remoteHost = this.target.host + portUri; // // Change headers (if requested). @@ -567,7 +568,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer) // Get the Non-Printable data data = data.slice(Buffer.byteLength(sdata), data.length); - if (self.https && !self.target.https) { + if (self.source.https && !self.target.https) { // // If the proxy server is running HTTPS but the client is running // HTTP then replace `ws` with `wss` in the data sent back to the client. diff --git a/test/helpers.js b/test/helpers.js index 9c91abf..036a1e5 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -112,7 +112,6 @@ TestRunner.prototype.assertResponseCode = function (proxyPort, statusCode, creat // // WebSocketTest // - TestRunner.prototype.webSocketTest = function (options) { var self = this; diff --git a/test/proxy-table-test.js b/test/http/http-proxy-table-test.js similarity index 97% rename from test/proxy-table-test.js rename to test/http/http-proxy-table-test.js index 359c82c..87cf6a8 100644 --- a/test/proxy-table-test.js +++ b/test/http/http-proxy-table-test.js @@ -12,11 +12,10 @@ var assert = require('assert'), argv = require('optimist').argv, request = require('request'), vows = require('vows'), - helpers = require('./helpers'), - TestRunner = helpers.TestRunner; + helpers = require('../helpers'); var protocol = argv.https ? 'https' : 'http', - runner = new TestRunner(protocol), + runner = new helpers.TestRunner(protocol), routeFile = path.join(__dirname, 'config.json'); var fileOptions = { diff --git a/test/node-http-proxy-test.js b/test/http/http-proxy-test.js similarity index 98% rename from test/node-http-proxy-test.js rename to test/http/http-proxy-test.js index f05ae5a..5437261 100644 --- a/test/node-http-proxy-test.js +++ b/test/http/http-proxy-test.js @@ -29,7 +29,7 @@ var assert = require('assert'), argv = require('optimist').argv, request = require('request'), vows = require('vows'), - helpers = require('./helpers'); + helpers = require('../helpers'); var forwardOptions = { forward: { diff --git a/test/websocket/websocket-proxy-table-test.js b/test/websocket/websocket-proxy-table-test.js new file mode 100644 index 0000000..a3edf05 --- /dev/null +++ b/test/websocket/websocket-proxy-table-test.js @@ -0,0 +1,104 @@ +/* + node-http-proxy-test.js: http proxy for node.js + + Copyright (c) 2010 Charlie Robbins, Marak Squires and Fedor Indutny + + 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 util = require('util'), + assert = require('assert'), + argv = require('optimist').argv, + colors = require('colors'), + request = require('request'), + vows = require('vows'), + websocket = require('../vendor/websocket'), + helpers = require('../helpers'); + +try { + var utils = require('socket.io/lib/socket.io/utils'), + io = require('socket.io'); +} +catch (ex) { + console.error('Socket.io is required for this example:'); + console.error('npm ' + 'install'.green + ' socket.io@0.6.18'.magenta); + process.exit(1); +} + +var protocol = argv.https ? 'https' : 'http', + wsprotocol = argv.https ? 'wss' : 'ws', + runner = new helpers.TestRunner(protocol); + +vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({ + "When using server created by httpProxy.createServer()": { + "using proxy table with no latency": { + "when an inbound message is sent from a WebSocket client": { + topic: function () { + var that = this + headers = {}; + + runner.webSocketTestWithTable({ + io: io, + host: 'localhost', + wsprotocol: wsprotocol, + protocol: protocol, + router: {'localhost':'localhost:8230'}, + ports: { + target: 8230, + proxy: 8231 + }, + onListen: function (socket) { + socket.on('connection', function (client) { + client.on('message', function (msg) { + that.callback(null, msg, headers); + }); + }); + }, + onWsupgrade: function (req, res) { + headers.request = req; + headers.response = res.headers; + }, + onOpen: function (ws) { + ws.send(utils.encode('from client')); + } + }); + }, + "the target server should receive the message": function (err, msg, headers) { + assert.equal(msg, 'from client'); + }, + "the origin and sec-websocket-origin headers should match": function (err, msg, headers) { + assert.isString(headers.response['sec-websocket-location']); + assert.isTrue(headers.response['sec-websocket-location'].indexOf(wsprotocol) !== -1); + assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']); + } + } + } + } +}).addBatch({ + "When the tests are over": { + topic: function () { + return runner.closeServers(); + }, + "the servers should clean up": function () { + assert.isTrue(true); + } + } +}).export(module); diff --git a/test/web-socket-proxy-test.js b/test/websocket/websocket-proxy-test.js similarity index 78% rename from test/web-socket-proxy-test.js rename to test/websocket/websocket-proxy-test.js index 339bc71..bfc63e6 100644 --- a/test/web-socket-proxy-test.js +++ b/test/websocket/websocket-proxy-test.js @@ -30,8 +30,8 @@ var util = require('util'), colors = require('colors'), request = require('request'), vows = require('vows'), - websocket = require('./../vendor/websocket'), - helpers = require('./helpers'); + websocket = require('../../vendor/websocket'), + helpers = require('../helpers'); try { var utils = require('socket.io/lib/socket.io/utils'), @@ -48,50 +48,6 @@ var protocol = argv.https ? 'https' : 'http', runner = new helpers.TestRunner(protocol); vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({ - "when using proxy table":{ - "with no latency" : { - "when an inbound message is sent from a WebSocket client": { - topic: function () { - var that = this - headers = {}; - - runner.webSocketTestWithTable({ - io: io, - host: 'localhost', - wsprotocol: wsprotocol, - protocol: protocol, - router: {'localhost':'localhost:8230'}, - ports: { - target: 8230, - proxy: 8231 - }, - onListen: function (socket) { - socket.on('connection', function (client) { - client.on('message', function (msg) { - that.callback(null, msg, headers); - }); - }); - }, - onWsupgrade: function (req, res) { - headers.request = req; - headers.response = res.headers; - }, - onOpen: function (ws) { - ws.send(utils.encode('from client')); - } - }); - }, - "the target server should receive the message": function (err, msg, headers) { - assert.equal(msg, 'from client'); - }, - "the origin and sec-websocket-origin headers should match": function (err, msg, headers) { - assert.isString(headers.response['sec-websocket-location']); - assert.isTrue(headers.response['sec-websocket-location'].indexOf(wsprotocol) !== -1); - assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']); - } - } - } - }, "When using server created by httpProxy.createServer()": { "with no latency" : { "when an inbound message is sent from a WebSocket client": {