Permalink
Browse files

Initial pass at https client

  • Loading branch information...
1 parent 86e6870 commit e65f6b4ce1246fa32a30d4041f568acb93c7ca24 @ry ry committed Jan 21, 2011
Showing with 111 additions and 9 deletions.
  1. +59 −0 doc/api/https.markdown
  2. +5 −5 lib/crypto.js
  3. +8 −4 lib/http.js
  4. +39 −0 lib/https.js
View
@@ -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);
+ });
+
+
+
+
+
+
View
@@ -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);
View
@@ -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);
View
@@ -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);
+};

0 comments on commit e65f6b4

Please sign in to comment.