Skip to content

Commit 2c11b01

Browse files
author
Alex Ivashchenko
committed
Update HTTPS handling for modern days
1 parent 4d5ec50 commit 2c11b01

3 files changed

Lines changed: 47 additions & 30 deletions

File tree

lib/form_data.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ FormData.prototype.submit = function(params, cb) {
306306
options.headers = this.getHeaders(params.headers);
307307

308308
// https if specified, fallback to http in any other case
309-
if (params.protocol == 'https:') {
309+
if (options.protocol == 'https:') {
310310
request = https.request(options);
311311
} else {
312312
request = http.request(options);
Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,54 @@
1+
var https = require('https');
12
var common = require('../common');
23
var assert = common.assert;
34
var FormData = require(common.dir.lib + '/form_data');
5+
var server;
46

5-
var form = new FormData();
7+
function submitForm() {
68

7-
form.submit({
8-
protocol: 'https:',
9-
hostname: 'localhost',
10-
port: common.httpsPort,
11-
pathname: '/',
12-
ca: common.httpsServerCert
13-
}, function(err, res) {
9+
var form = new FormData();
1410

15-
if (err) {
16-
throw err;
17-
}
11+
form.append('field', 'value');
1812

19-
assert.strictEqual(res.statusCode, 200);
20-
assert.strictEqual(res.headers['x-success'], 'OK');
13+
form.submit({
14+
protocol: 'https:',
15+
hostname: 'localhost',
16+
port: common.httpsPort,
17+
pathname: '/',
18+
// for self-signed certs on localhost
19+
secureOptions: require('constants').SSL_OP_NO_TLSv1_2,
20+
ca: common.httpsServerCert
21+
}, function(err, res) {
2122

22-
// unstuck new streams
23-
res.resume();
23+
if (err) {
24+
throw err;
25+
}
26+
27+
assert.strictEqual(res.statusCode, 200);
28+
assert.strictEqual(res.headers['x-success'], 'OK');
29+
30+
// unstuck new streams
31+
res.resume();
32+
33+
server.close();
34+
});
35+
}
36+
37+
// create https server
38+
server = https.createServer({
39+
key: common.httpsServerKey,
40+
cert: common.httpsServerCert
41+
}, function(req, res) {
42+
43+
// old and simple
44+
req.on('data', function(){});
45+
46+
req.on('end', function()
47+
{
48+
res.writeHead(200, {'x-success': 'OK'});
49+
res.end('Great Success');
50+
});
2451
});
52+
53+
// when https server ready submit form
54+
server.listen(common.httpsPort, submitForm);

test/static.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// serves static files
22
var http = require('http');
3-
var https = require('https');
43
var fs = require('fs');
54
var path = require('path');
65
var mime = require('mime-types');
@@ -12,11 +11,6 @@ if (!fs.existsSync(common.dir.tmp))
1211
fs.mkdirSync(common.dir.tmp);
1312
}
1413

15-
var httpsOptions = {
16-
key: common.httpsServerKey,
17-
cert: common.httpsServerCert
18-
};
19-
2014
module.exports = function(callback) {
2115

2216
// create http server
@@ -33,12 +27,5 @@ module.exports = function(callback) {
3327
fs.createReadStream(target).pipe(res);
3428
});
3529

36-
// create https server
37-
var httpsServer = https.createServer(httpsOptions, function(req, res) {
38-
res.writeHead(200, {'x-success': 'OK'});
39-
res.end('Great Success');
40-
});
41-
42-
// dirty&simple
43-
httpServer.listen(common.staticPort, httpsServer.listen.bind(httpsServer, common.httpsPort, callback.bind(undefined, httpServer, httpsServer)));
30+
httpServer.listen(common.staticPort, callback.bind(undefined, httpServer));
4431
};

0 commit comments

Comments
 (0)