First pass at new https server

pull/22966/head
Ryan Dahl 2011-01-02 01:13:56 -08:00
parent e4dd5cd6fd
commit 94f8368cf9
4 changed files with 79 additions and 2 deletions

View File

@ -871,6 +871,7 @@ function connectionListener(socket) {
return false; // Not a HEAD response. (Not even a response!)
};
}
exports._connectionListener = connectionListener;
function Client() {

22
lib/https.js 100644
View File

@ -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);
};

View File

@ -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);
};

View File

@ -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);
});