Browse Source

Initial pass at https client

v0.7.4-release
Ryan Dahl 14 years ago
parent
commit
e65f6b4ce1
  1. 59
      doc/api/https.markdown
  2. 10
      lib/crypto.js
  3. 12
      lib/http.js
  4. 39
      lib/https.js

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

10
lib/crypto.js

@ -15,9 +15,9 @@ try {
} }
function Credentials(method) { function Credentials(secureProtocol) {
if (!(this instanceof Credentials)) { if (!(this instanceof Credentials)) {
return new Credentials(method); return new Credentials(secureProtocol);
} }
if (!crypto) { if (!crypto) {
@ -26,8 +26,8 @@ function Credentials(method) {
this.context = new SecureContext(); this.context = new SecureContext();
if (method) { if (secureProtocol) {
this.context.init(method); this.context.init(secureProtocol);
} else { } else {
this.context.init(); this.context.init();
} }
@ -39,7 +39,7 @@ exports.Credentials = Credentials;
exports.createCredentials = function(options) { exports.createCredentials = function(options) {
if (!options) options = {}; if (!options) options = {};
var c = new Credentials(options.method); var c = new Credentials(options.secureProtocol);
if (options.key) c.context.setKey(options.key); if (options.key) c.context.setKey(options.key);

12
lib/http.js

@ -917,6 +917,7 @@ function Agent(host, port) {
this.maxSockets = 5; this.maxSockets = 5;
} }
util.inherits(Agent, EventEmitter); util.inherits(Agent, EventEmitter);
exports.Agent = Agent;
Agent.prototype.appendMessage = function(options) { Agent.prototype.appendMessage = function(options) {
@ -1149,16 +1150,19 @@ function getAgent(host, port) {
} }
exports.request = function(options, cb) { exports._requestFromAgent = function(agent, options, cb) {
var agent = getAgent(options.host, options.port);
var req = agent.appendMessage(options); var req = agent.appendMessage(options);
if (cb) req.once('response', cb); if (cb) req.once('response', cb);
return req; 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) { exports.get = function(options, cb) {
options.method = 'GET'; options.method = 'GET';
var req = exports.request(options, cb); var req = exports.request(options, cb);

39
lib/https.js

@ -20,3 +20,42 @@ exports.Server = Server;
exports.createServer = function(opts, requestListener) { exports.createServer = function(opts, requestListener) {
return new Server(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);
};

Loading…
Cancel
Save