diff --git a/doc/api/http.markdown b/doc/api/http.markdown index c4af8f407cc447..719ae151849f88 100644 --- a/doc/api/http.markdown +++ b/doc/api/http.markdown @@ -57,10 +57,15 @@ 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). +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. @@ -1081,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 99903024c26d14..3cbbfa7cb1abf2 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -213,8 +213,18 @@ ServerResponse.prototype.writeHeader = function() { }; -function Server(requestListener) { - if (!(this instanceof Server)) return new Server(requestListener); +function 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; + } + + 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 a9cfeddeea5cfa..1f76ab97e2f37c 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 b96b57998a2cb6..22c84287cb72f5 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,10 @@ 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); assert.equal(4, requests_sent); diff --git a/test/parallel/test-https-from-http.js b/test/parallel/test-https-from-http.js new file mode 100644 index 00000000000000..70101d5aa777a4 --- /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); +});