diff --git a/doc/api/https.markdown b/doc/api/https.markdown new file mode 100644 index 0000000000..ac1edf04c3 --- /dev/null +++ b/doc/api/https.markdown @@ -0,0 +1,59 @@ +## HTTPS + +HTTPS is the HTTP protocol over TLS/SSL. In Node this is implemented as a +separate module. + +## https.Server +## https.createServer + +Example: + + // curl -k https://localhost:8000/ + var https = require('https'); + var fs = require('fs'); + + var options = { + key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'), + cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem') + }; + + https.createServer(options, function (req, res) { + res.writeHead(200); + res.end("hello world\n"); + }).listen(8000); + + +## https.request + +Makes a request to a secure web server. + +Example: + + var https = require('https'); + + var options = { + host: 'encrypted.google.com', + port: 443, + path: '/', + method: 'GET' + }; + + var req = https.request(options, function(res) { + console.log("statusCode: ", res.statusCode); + console.log("headers: ", res.headers); + + res.on('data', function(d) { + process.stdout.write(d); + }); + }); + req.end(); + + req.on('error', function(e) { + console.error(e); + }); + + + + + + diff --git a/lib/crypto.js b/lib/crypto.js index f7f6437533..2b6fc0e1d5 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -15,9 +15,9 @@ try { } -function Credentials(method) { +function Credentials(secureProtocol) { if (!(this instanceof Credentials)) { - return new Credentials(method); + return new Credentials(secureProtocol); } if (!crypto) { @@ -26,8 +26,8 @@ function Credentials(method) { this.context = new SecureContext(); - if (method) { - this.context.init(method); + if (secureProtocol) { + this.context.init(secureProtocol); } else { this.context.init(); } @@ -39,7 +39,7 @@ exports.Credentials = Credentials; exports.createCredentials = function(options) { if (!options) options = {}; - var c = new Credentials(options.method); + var c = new Credentials(options.secureProtocol); if (options.key) c.context.setKey(options.key); diff --git a/lib/http.js b/lib/http.js index d9633024d7..6e4a066de3 100644 --- a/lib/http.js +++ b/lib/http.js @@ -917,6 +917,7 @@ function Agent(host, port) { this.maxSockets = 5; } util.inherits(Agent, EventEmitter); +exports.Agent = Agent; Agent.prototype.appendMessage = function(options) { @@ -1149,16 +1150,19 @@ function getAgent(host, port) { } -exports.request = function(options, cb) { - var agent = getAgent(options.host, options.port); +exports._requestFromAgent = function(agent, options, cb) { var req = agent.appendMessage(options); - if (cb) req.once('response', cb); - return req; }; +exports.request = function(options, cb) { + var agent = getAgent(options.host, options.port); + return exports._requestFromAgent(agent, options, cb); +}; + + exports.get = function(options, cb) { options.method = 'GET'; var req = exports.request(options, cb); diff --git a/lib/https.js b/lib/https.js index c3e17a3602..e2555f0017 100644 --- a/lib/https.js +++ b/lib/https.js @@ -20,3 +20,42 @@ exports.Server = Server; exports.createServer = function(opts, requestListener) { return new Server(opts, requestListener); }; + + +// HTTPS agents. +var agents = {}; + +function Agent(options) { + http.Agent.call(this, options.host, options.port); + + this.options = options; +} +inherits(Agent, http.Agent); + + +Agent.prototype._getConnection = function(host, port, cb) { + var s = tls.connect(port, host, this.options, function() { + // do other checks here? + if (cb) cb(); + }); + + return s; +}; + + +function getAgent(options) { + var id = options.host + ':' + options.port; + var agent = agents[id]; + + if (!agent) { + agent = agents[id] = new Agent(options); + } + + return agent; +} + + +exports.request = function(options, cb) { + var agent = getAgent(options); + return http._requestFromAgent(agent, options, cb); +};