From 8ff6b81db1d0e858b1e0dddd01fdc6b7f5dc30d7 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Fri, 14 Nov 2014 15:40:38 -0500 Subject: [PATCH 1/2] http: allow createServer() to return a HTTPS server This commit allows the http module to create a HTTPS server if TLS options are passed to createServer() or the Server() constructor. --- doc/api/http.markdown | 6 ++- lib/_http_server.js | 21 ++++++++- lib/http.js | 4 +- test/parallel/test-http-server.js | 4 ++ test/simple/test-https-from-http.js | 69 +++++++++++++++++++++++++++++ 5 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 test/simple/test-https-from-http.js diff --git a/doc/api/http.markdown b/doc/api/http.markdown index c4af8f407cc4..3e329e2e6af7 100644 --- a/doc/api/http.markdown +++ b/doc/api/http.markdown @@ -57,9 +57,11 @@ A collection of all the standard HTTP response status codes, and the short description of each. For example, `http.STATUS_CODES[404] === 'Not Found'`. -## http.createServer([requestListener]) +## http.createServer([options][, requestListener]) -Returns a new instance of [http.Server](#http_class_http_server). +Returns a new instance of [http.Server](#http_class_http_server). If an `options` object containing valid TLS +arguments is passed, then a HTTPS server will be returned based on the +options. The `requestListener` is a function which is automatically added to the `'request'` event. diff --git a/lib/_http_server.js b/lib/_http_server.js index 99903024c26d..d87cecb1d732 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -213,8 +213,25 @@ ServerResponse.prototype.writeHeader = function() { }; -function Server(requestListener) { - if (!(this instanceof Server)) return new Server(requestListener); +var TLS_ARGS = ['pfx', 'key', 'passphrase', 'cert', 'ca', 'crl', 'ciphers', + 'handshakeTimeout', 'honorCipherOrder', 'requestCert', + 'rejectUnauthorized', 'NPNProtocols', 'SNICallback', + 'sessionIdContext', 'secureProtocol', 'secureOptions']; + +function Server(options, requestListener) { + if (util.isObject(options)) { + // Only return an HTTPS server if valid TLS args are passed + for (var i = 0; i < TLS_ARGS.length; i++) { + if (options.hasOwnProperty(TLS_ARGS[i])) + return new require('https').Server(options, requestListener); + } + } else { + requestListener = options; + } + + if (!(this instanceof Server)) + return new Server(requestListener); + net.Server.call(this, { allowHalfOpen: true }); if (requestListener) { diff --git a/lib/http.js b/lib/http.js index a9cfeddeea5c..1f76ab97e2f3 100644 --- a/lib/http.js +++ b/lib/http.js @@ -39,8 +39,8 @@ exports.get = function(options, cb) { exports._connectionListener = server._connectionListener; const Server = exports.Server = server.Server; -exports.createServer = function(requestListener) { - return new Server(requestListener); +exports.createServer = function(options, requestListener) { + return new Server(options, requestListener); }; diff --git a/test/parallel/test-http-server.js b/test/parallel/test-http-server.js index b96b57998a2c..4ac49119fe73 100644 --- a/test/parallel/test-http-server.js +++ b/test/parallel/test-http-server.js @@ -46,6 +46,7 @@ var server = http.createServer(function(req, res) { }, 1); }); +assert(server instanceof http.Server); server.listen(common.PORT); server.httpAllowHalfOpen = true; @@ -93,6 +94,9 @@ server.on('listening', function() { }); }); +assert(http.createServer() instanceof http.Server); +assert(http.createServer({foo: 1}) instanceof http.Server); + process.on('exit', function() { assert.equal(4, request_number); assert.equal(4, requests_sent); diff --git a/test/simple/test-https-from-http.js b/test/simple/test-https-from-http.js new file mode 100644 index 000000000000..788e986906ba --- /dev/null +++ b/test/simple/test-https-from-http.js @@ -0,0 +1,69 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// 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. + +if (!process.versions.openssl) { + console.error('Skipping because node compiled without OpenSSL.'); + process.exit(0); +} + +var common = require('../common'); +var assert = require('assert'); +var fs = require('fs'); +var http = require('http'); +var https = require('https'); + +var options = { + key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'), + cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem') +}; + +var reqCount = 0; +var body = 'test'; + +var server = http.createServer(options, function(req, res) { + reqCount++; + res.writeHead(200, {'content-type': 'text/plain'}); + res.end(body); +}); + +assert(server instanceof https.Server); + +server.listen(common.PORT, function() { + https.get({ + port: common.PORT, + rejectUnauthorized: false + }, function(res) { + var data = ''; + + res.on('data', function(chunk) { + data += chunk.toString(); + }); + + res.on('end', function() { + assert.equal(data, body); + server.close(); + }); + }); +}); + +process.on('exit', function() { + assert.equal(1, reqCount); +}); From b3b6b24c82948d8afc5efe61ad7ab745e6f28809 Mon Sep 17 00:00:00 2001 From: Brendan Ashworth Date: Sun, 8 Mar 2015 12:44:20 -0700 Subject: [PATCH 2/2] http: rebase and update 8ff6b81 This commit does various changes to cleanup and prep the 8ff6b81 for merging, which include updating the commit to follow new codebase guidelines. The following are the changes: doc/api/http.markdown: - document options lib/http.js: - no changes lib/_http_server.js - changes regarding isObject (#647) and hasOwnProperty (#635) - take tls option rather than guessing based on options test/simple/test-https-from-http.js: - moved to parallel directory, crypto test, removed copyright banner test/parallel/test-http-server.js: - adjust for tls option --- doc/api/http.markdown | 11 +++-- lib/_http_server.js | 15 ++---- test/parallel/test-http-server.js | 1 + test/parallel/test-https-from-http.js | 50 +++++++++++++++++++ test/simple/test-https-from-http.js | 69 --------------------------- 5 files changed, 63 insertions(+), 83 deletions(-) create mode 100644 test/parallel/test-https-from-http.js delete mode 100644 test/simple/test-https-from-http.js diff --git a/doc/api/http.markdown b/doc/api/http.markdown index 3e329e2e6af7..719ae151849f 100644 --- a/doc/api/http.markdown +++ b/doc/api/http.markdown @@ -59,9 +59,12 @@ Found'`. ## http.createServer([options][, requestListener]) -Returns a new instance of [http.Server](#http_class_http_server). If an `options` object containing valid TLS -arguments is passed, then a HTTPS server will be returned based on the -options. +Returns a new instance of [http.Server](#http_class_http_server). + +Options (an optional argument) inherits from [tls.Server][], plus: + +- `tls`: Boolean, whether or not to return an [https.Server][] based on the +options. Defaults to false. The `requestListener` is a function which is automatically added to the `'request'` event. @@ -1083,6 +1086,8 @@ authentication details. [http.Server]: #http_class_http_server [http.request()]: #http_http_request_options_callback [http.request()]: #http_http_request_options_callback +[https.Server]: https.html#https_class_https_server +[tls.Server]: tls.html#tls_tls_createserver_options_secureconnectionlistener [net.Server.close()]: net.html#net_server_close_callback [net.Server.listen(path)]: net.html#net_server_listen_path_callback [net.Server.listen(port)]: net.html#net_server_listen_port_host_backlog_callback diff --git a/lib/_http_server.js b/lib/_http_server.js index d87cecb1d732..3cbbfa7cb1ab 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -213,18 +213,11 @@ ServerResponse.prototype.writeHeader = function() { }; -var TLS_ARGS = ['pfx', 'key', 'passphrase', 'cert', 'ca', 'crl', 'ciphers', - 'handshakeTimeout', 'honorCipherOrder', 'requestCert', - 'rejectUnauthorized', 'NPNProtocols', 'SNICallback', - 'sessionIdContext', 'secureProtocol', 'secureOptions']; - function Server(options, requestListener) { - if (util.isObject(options)) { - // Only return an HTTPS server if valid TLS args are passed - for (var i = 0; i < TLS_ARGS.length; i++) { - if (options.hasOwnProperty(TLS_ARGS[i])) - return new require('https').Server(options, requestListener); - } + if (options !== null && typeof options === 'object') { + // Only return an HTTPS server if options.tls is true + if (options.tls) + return new require('https').Server(options, requestListener); } else { requestListener = options; } diff --git a/test/parallel/test-http-server.js b/test/parallel/test-http-server.js index 4ac49119fe73..22c84287cb72 100644 --- a/test/parallel/test-http-server.js +++ b/test/parallel/test-http-server.js @@ -96,6 +96,7 @@ server.on('listening', function() { assert(http.createServer() instanceof http.Server); assert(http.createServer({foo: 1}) instanceof http.Server); +assert(http.createServer({tls: false}) instanceof http.Server); process.on('exit', function() { assert.equal(4, request_number); diff --git a/test/parallel/test-https-from-http.js b/test/parallel/test-https-from-http.js new file mode 100644 index 000000000000..70101d5aa777 --- /dev/null +++ b/test/parallel/test-https-from-http.js @@ -0,0 +1,50 @@ +var common = require('../common'); +var assert = require('assert'); +var fs = require('fs'); +var http = require('http'); + +if (!common.hasCrypto) { + console.log('1..0 # Skipped: missing crypto'); + process.exit(); +} + +var https = require('https'); + +var options = { + tls: true, + key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'), + cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem') +}; + +var reqCount = 0; +var body = 'test'; + +var server = http.createServer(options, function(req, res) { + reqCount++; + res.writeHead(200, {'content-type': 'text/plain'}); + res.end(body); +}); + +assert(server instanceof https.Server); + +server.listen(common.PORT, function() { + https.get({ + port: common.PORT, + rejectUnauthorized: false + }, function(res) { + var data = ''; + + res.on('data', function(chunk) { + data += chunk.toString(); + }); + + res.on('end', function() { + assert.equal(data, body); + server.close(); + }); + }); +}); + +process.on('exit', function() { + assert.equal(1, reqCount); +}); diff --git a/test/simple/test-https-from-http.js b/test/simple/test-https-from-http.js deleted file mode 100644 index 788e986906ba..000000000000 --- a/test/simple/test-https-from-http.js +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright Joyent, Inc. and other Node contributors. -// -// 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. - -if (!process.versions.openssl) { - console.error('Skipping because node compiled without OpenSSL.'); - process.exit(0); -} - -var common = require('../common'); -var assert = require('assert'); -var fs = require('fs'); -var http = require('http'); -var https = require('https'); - -var options = { - key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'), - cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem') -}; - -var reqCount = 0; -var body = 'test'; - -var server = http.createServer(options, function(req, res) { - reqCount++; - res.writeHead(200, {'content-type': 'text/plain'}); - res.end(body); -}); - -assert(server instanceof https.Server); - -server.listen(common.PORT, function() { - https.get({ - port: common.PORT, - rejectUnauthorized: false - }, function(res) { - var data = ''; - - res.on('data', function(chunk) { - data += chunk.toString(); - }); - - res.on('end', function() { - assert.equal(data, body); - server.close(); - }); - }); -}); - -process.on('exit', function() { - assert.equal(1, reqCount); -});