Initial pass at https client
This commit is contained in:
Родитель
86e687086b
Коммит
e65f6b4ce1
|
@ -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)) {
|
||||
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);
|
||||
|
||||
|
|
14
lib/http.js
14
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,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) {
|
||||
var agent = getAgent(options.host, options.port);
|
||||
var req = agent.appendMessage(options);
|
||||
|
||||
if (cb) req.once('response', cb);
|
||||
|
||||
return req;
|
||||
return exports._requestFromAgent(agent, options, cb);
|
||||
};
|
||||
|
||||
|
||||
|
|
39
lib/https.js
39
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);
|
||||
};
|
||||
|
|
Загрузка…
Ссылка в новой задаче