Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: allow HTTP to return an HTTPS server #1101

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion doc/api/http.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};


Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var server = http.createServer(function(req, res) {
}, 1);

});
assert(server instanceof http.Server);
server.listen(common.PORT);

server.httpAllowHalfOpen = true;
Expand Down Expand Up @@ -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);
Expand Down
50 changes: 50 additions & 0 deletions test/parallel/test-https-from-http.js
Original file line number Diff line number Diff line change
@@ -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);
});