Browse Source

http, https: don't depend on `globalAgent`

For the `request()` and `get()` functions. I could never
really understand why these two functions go through agent
first... Especially since the user could be passing `agent: false`
or a different Agent instance completely, in which `globalAgent`
will be completely bypassed.

Moved the relevant logic from `Agent#request()` into the
`ClientRequest` constructor.

Incidentally, this commit fixes #7012 (which was the original
intent of this commit).
v0.11.12-release
Nathan Rajlich 11 years ago
parent
commit
d6bbb19f1d
  1. 32
      lib/_http_client.js
  2. 6
      lib/http.js
  3. 13
      lib/https.js

32
lib/_http_client.js

@ -21,6 +21,7 @@
var util = require('util'); var util = require('util');
var net = require('net'); var net = require('net');
var url = require('url');
var EventEmitter = require('events').EventEmitter; var EventEmitter = require('events').EventEmitter;
var HTTPParser = process.binding('http_parser').HTTPParser; var HTTPParser = process.binding('http_parser').HTTPParser;
var assert = require('assert').ok; var assert = require('assert').ok;
@ -35,17 +36,39 @@ var debug = common.debug;
var IncomingMessage = require('_http_incoming').IncomingMessage; var IncomingMessage = require('_http_incoming').IncomingMessage;
var OutgoingMessage = require('_http_outgoing').OutgoingMessage; var OutgoingMessage = require('_http_outgoing').OutgoingMessage;
var agent = require('_http_agent'); var Agent = require('_http_agent');
var globalAgent = agent.globalAgent;
function ClientRequest(options, cb) { function ClientRequest(options, cb) {
var self = this; var self = this;
OutgoingMessage.call(self); OutgoingMessage.call(self);
if (util.isString(options)) {
options = url.parse(options);
} else {
options = util._extend({}, options); options = util._extend({}, options);
}
self.agent = util.isUndefined(options.agent) ? globalAgent : options.agent; var agent = options.agent;
var defaultAgent = options._defaultAgent || Agent.globalAgent;
if (agent === false) {
agent = new defaultAgent.constructor();
} else if (util.isNullOrUndefined(agent)) {
agent = defaultAgent;
}
self.agent = agent;
if (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 !== self.agent.protocol) {
throw new Error('Protocol:' + options.protocol + ' not supported.');
}
var defaultPort = options.defaultPort || self.agent.defaultPort; var defaultPort = options.defaultPort || self.agent.defaultPort;
@ -114,8 +137,7 @@ function ClientRequest(options, cb) {
// but only if the Agent will actually reuse the connection! // but only if the Agent will actually reuse the connection!
// If it's not a keepAlive agent, and the maxSockets==Infinity, then // If it's not a keepAlive agent, and the maxSockets==Infinity, then
// there's never a case where this socket will actually be reused // there's never a case where this socket will actually be reused
var agent = self.agent; if (!self.agent.keepAlive && !Number.isFinite(self.agent.maxSockets)) {
if (!agent.keepAlive && !Number.isFinite(agent.maxSockets)) {
self._last = true; self._last = true;
self.shouldKeepAlive = false; self.shouldKeepAlive = false;
} else { } else {

6
lib/http.js

@ -49,11 +49,13 @@ var client = require('_http_client');
var ClientRequest = exports.ClientRequest = client.ClientRequest; var ClientRequest = exports.ClientRequest = client.ClientRequest;
exports.request = function(options, cb) { exports.request = function(options, cb) {
return globalAgent.request(options, cb); return new ClientRequest(options, cb);
}; };
exports.get = function(options, cb) { exports.get = function(options, cb) {
return globalAgent.get(options, cb); var req = exports.request(options, cb);
req.end();
return req;
}; };
exports._connectionListener = server._connectionListener; exports._connectionListener = server._connectionListener;

13
lib/https.js

@ -20,6 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE. // USE OR OTHER DEALINGS IN THE SOFTWARE.
var tls = require('tls'); var tls = require('tls');
var url = require('url');
var http = require('http'); var http = require('http');
var util = require('util'); var util = require('util');
var inherits = require('util').inherits; var inherits = require('util').inherits;
@ -126,9 +127,17 @@ exports.globalAgent = globalAgent;
exports.Agent = Agent; exports.Agent = Agent;
exports.request = function(options, cb) { exports.request = function(options, cb) {
return globalAgent.request(options, cb); if (util.isString(options)) {
options = url.parse(options);
} else {
options = util._extend({}, options);
}
options._defaultAgent = globalAgent;
return http.request(options, cb);
}; };
exports.get = function(options, cb) { exports.get = function(options, cb) {
return globalAgent.get(options, cb); var req = exports.request(options, cb);
req.end();
return req;
}; };

Loading…
Cancel
Save