From 94f8368cf9e5077050525e32a24188402f077074 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Sun, 2 Jan 2011 01:13:56 -0800 Subject: [PATCH] First pass at new https server --- lib/http.js | 1 + lib/https.js | 22 ++++++++++++++++ lib/tls.js | 14 ++++++++-- test/simple/test-https-simple.js | 44 ++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 lib/https.js create mode 100644 test/simple/test-https-simple.js diff --git a/lib/http.js b/lib/http.js index 5594a8ff1c4..786f373e743 100644 --- a/lib/http.js +++ b/lib/http.js @@ -871,6 +871,7 @@ function connectionListener(socket) { return false; // Not a HEAD response. (Not even a response!) }; } +exports._connectionListener = connectionListener; function Client() { diff --git a/lib/https.js b/lib/https.js new file mode 100644 index 00000000000..c3e17a36027 --- /dev/null +++ b/lib/https.js @@ -0,0 +1,22 @@ +var tls = require('tls'); +var http = require('http'); +var inherits = require('util').inherits; + + +function Server(opts, requestListener) { + if (!(this instanceof Server)) return new Server(opts, requestListener); + tls.Server.call(this, opts, http._connectionListener); + + if (requestListener) { + this.addListener('request', requestListener); + } +} +inherits(Server, tls.Server); + + +exports.Server = Server; + + +exports.createServer = function(opts, requestListener) { + return new Server(opts, requestListener); +}; diff --git a/lib/tls.js b/lib/tls.js index bcb036edc84..08fa742ac80 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -77,6 +77,11 @@ CryptoStream.prototype.resume = function() { }; +CryptoStream.prototype.setTimeout = function(n) { + if (this.socket) this.socket.setTimeout(n); +}; + + function parseCertString (s) { // EG '/C=US/ST=CA/L=SF/O=Joyent/OU=Node.js/CN=ca1/emailAddress=ry@tinyclouds.org' var out = {}; @@ -183,8 +188,13 @@ CryptoStream.prototype._blow = function() { } while ((chunkBytes > 0) && (pool.used + bytesRead < pool.length)); if (bytesRead > 0) { - chunk = pool.slice(0, bytesRead); - this.emit('data', chunk); + if (this._events && this._events['data']) { + chunk = pool.slice(0, bytesRead); + this.emit('data', chunk); + } + + // Optimization: emit the original buffer with end points + if (this.ondata) this.ondata(pool, 0, bytesRead); } } while (bytesRead > 0 && this._writeState === true); }; diff --git a/test/simple/test-https-simple.js b/test/simple/test-https-simple.js new file mode 100644 index 00000000000..a90c0269e67 --- /dev/null +++ b/test/simple/test-https-simple.js @@ -0,0 +1,44 @@ +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 exec = require('child_process').exec; + +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 = 'hello world\n'; + +var server = https.createServer(options, function (req, res) { + reqCount++; + console.log("got request"); + res.writeHead(200, { 'content-type': 'text/plain' }); + res.end(body); +}) + +function afterCurl (err, stdout, stderr) { + if (err) throw err; + server.close(); + common.error(common.inspect(stdout)); + assert.equal(body, stdout); +}; + +server.listen(common.PORT, function () { + var cmd = 'curl --insecure https://127.0.0.1:' + common.PORT + '/'; + console.error("executing %j", cmd); + exec(cmd, afterCurl); +}); + +process.on('exit', function () { + assert.equal(1, reqCount); +});