summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorisaacs <i@izs.me>2013-05-21 14:02:18 -0700
committerisaacs <i@izs.me>2013-07-09 22:31:11 -0700
commit40e92650bb97b736b8e29c5de35c1c29ebd625ef (patch)
tree78f36f5bdf78a57d595e3412a57352fb9a272fef /lib
parent9fc9b87472806147b83c24d85b303c4f75d3021c (diff)
downloadnode-40e92650bb97b736b8e29c5de35c1c29ebd625ef.tar.gz
http: Add agent.get/request methods
Diffstat (limited to 'lib')
-rw-r--r--lib/_http_agent.js41
-rw-r--r--lib/http.js28
-rw-r--r--lib/https.js3
-rw-r--r--lib/net.js2
4 files changed, 45 insertions, 29 deletions
diff --git a/lib/_http_agent.js b/lib/_http_agent.js
index 0938fce41..20fa1b6a2 100644
--- a/lib/_http_agent.js
+++ b/lib/_http_agent.js
@@ -20,8 +20,10 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var net = require('net');
+var url = require('url');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
+var ClientRequest = require('_http_client').ClientRequest;
// New Agent code.
@@ -42,7 +44,9 @@ function Agent(options) {
EventEmitter.call(this);
var self = this;
- self.options = options || {};
+ self.options = util._extend({}, options);
+ // don't confuse net and make it think that we're connecting to a pipe
+ self.options.path = null;
self.requests = {};
self.sockets = {};
self.freeSockets = {};
@@ -91,7 +95,6 @@ function Agent(options) {
}
}
});
- self.createConnection = net.createConnection;
}
util.inherits(Agent, EventEmitter);
@@ -99,7 +102,9 @@ exports.Agent = Agent;
Agent.defaultMaxSockets = Infinity;
+Agent.prototype.createConnection = net.createConnection;
Agent.prototype.defaultPort = 80;
+Agent.prototype.protocol = 'http:';
Agent.prototype.addRequest = function(req, host, port, localAddress) {
var name = host + ':' + port;
if (localAddress) {
@@ -207,5 +212,33 @@ Agent.prototype.destroy = function() {
});
};
-var globalAgent = new Agent();
-exports.globalAgent = globalAgent;
+Agent.prototype.request = function(options, cb) {
+ if (typeof options === 'string') {
+ options = url.parse(options);
+ }
+
+ if (options && options.path && / /.test(options.path)) {
+ // The actual regex is more like /[^A-Za-z0-9\-._~!$&'()*+,;=/:@]/
+ // with an additional rule for ignoring percentage-escaped characters
+ // but that's a) hard to capture in a regular expression that performs
+ // well, and b) possibly too restrictive for real-world usage. That's
+ // why it only scans for spaces because those are guaranteed to create
+ // an invalid request.
+ throw new TypeError('Request path contains unescaped characters.');
+ } else if (options.protocol && options.protocol !== this.protocol) {
+ throw new Error('Protocol:' + options.protocol + ' not supported.');
+ }
+
+ options = util._extend({ agent: this, keepAlive: false }, options);
+
+ if (options.agent === false)
+ options.agent = new Agent(options);
+
+ return new ClientRequest(options, cb);
+};
+
+Agent.prototype.get = function(options, cb) {
+ var req = this.request(options, cb);
+ req.end();
+ return req;
+};
diff --git a/lib/http.js b/lib/http.js
index a1abd1e2e..38bfb4b68 100644
--- a/lib/http.js
+++ b/lib/http.js
@@ -20,7 +20,6 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var util = require('util');
-var url = require('url');
var EventEmitter = require('events').EventEmitter;
@@ -42,40 +41,21 @@ exports.STATUS_CODES = server.STATUS_CODES;
var agent = require('_http_agent');
-
var Agent = exports.Agent = agent.Agent;
-var globalAgent = exports.globalAgent = agent.globalAgent;
+var globalAgent = new Agent();
+exports.globalAgent = globalAgent;
var client = require('_http_client');
var ClientRequest = exports.ClientRequest = client.ClientRequest;
exports.request = function(options, cb) {
- if (typeof options === 'string') {
- options = url.parse(options);
- } else if (options && options.path && / /.test(options.path)) {
- // The actual regex is more like /[^A-Za-z0-9\-._~!$&'()*+,;=/:@]/
- // with an additional rule for ignoring percentage-escaped characters
- // but that's a) hard to capture in a regular expression that performs
- // well, and b) possibly too restrictive for real-world usage. That's
- // why it only scans for spaces because those are guaranteed to create
- // an invalid request.
- throw new TypeError('Request path contains unescaped characters.');
- }
-
- if (options.protocol && options.protocol !== 'http:') {
- throw new Error('Protocol:' + options.protocol + ' not supported.');
- }
-
- return new ClientRequest(options, cb);
+ return globalAgent.request(options, cb);
};
exports.get = function(options, cb) {
- var req = exports.request(options, cb);
- req.end();
- return req;
+ return globalAgent.get(options, cb);
};
-
var httpSocketSetup = common.httpSocketSetup;
exports._connectionListener = server._connectionListener;
diff --git a/lib/https.js b/lib/https.js
index 8631d0d01..18789adab 100644
--- a/lib/https.js
+++ b/lib/https.js
@@ -83,10 +83,11 @@ function createConnection(port, host, options) {
function Agent(options) {
http.Agent.call(this, options);
- this.createConnection = createConnection;
}
inherits(Agent, http.Agent);
Agent.prototype.defaultPort = 443;
+Agent.prototype.protocol = 'https:';
+Agent.prototype.createConnection = createConnection;
var globalAgent = new Agent();
diff --git a/lib/net.js b/lib/net.js
index 0d5b556d0..d30c002d4 100644
--- a/lib/net.js
+++ b/lib/net.js
@@ -77,6 +77,7 @@ exports.createServer = function() {
//
exports.connect = exports.createConnection = function() {
var args = normalizeConnectArgs(arguments);
+ debug('createConnection', args);
var s = new Socket(args[0]);
return Socket.prototype.connect.apply(s, args);
};
@@ -832,6 +833,7 @@ Socket.prototype.connect = function(options, cb) {
var self = this;
var pipe = !!options.path;
+ debug('pipe', pipe, options.path);
if (!this._handle) {
this._handle = pipe ? createPipe() : createTCP();