Initial pass at https client
This commit is contained in:
parent
86e687086b
commit
e65f6b4ce1
59
doc/api/https.markdown
Normal file
59
doc/api/https.markdown
Normal file
@ -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);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -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);
|
||||||
|
|
||||||
|
14
lib/http.js
14
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,13 +1150,16 @@ function getAgent(host, 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) {
|
exports.request = function(options, cb) {
|
||||||
var agent = getAgent(options.host, options.port);
|
var agent = getAgent(options.host, options.port);
|
||||||
var req = agent.appendMessage(options);
|
return exports._requestFromAgent(agent, options, cb);
|
||||||
|
|
||||||
if (cb) req.once('response', cb);
|
|
||||||
|
|
||||||
return req;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
39
lib/https.js
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…
x
Reference in New Issue
Block a user