## http.Agent
-In node 0.5.3+ there is a new implementation of the HTTP Agent which is used
+In node 0.5.3+ there is a new implementation of the HTTP Agent which is used
for pooling sockets used in HTTP client requests.
-Previously, a single agent instance help the pool for single host+port. The
+Previously, a single agent instance help the pool for single host+port. The
current implementation now holds sockets for any number of hosts.
-The current HTTP Agent also defaults client requests to using
-Connection:keep-alive. If no pending HTTP requests are waiting on a socket
-to become free the socket is closed. This means that node's pool has the
-benefit of keep-alive when under load but still does not require developers
+The current HTTP Agent also defaults client requests to using
+Connection:keep-alive. If no pending HTTP requests are waiting on a socket
+to become free the socket is closed. This means that node's pool has the
+benefit of keep-alive when under load but still does not require developers
to manually close the HTTP clients using keep-alive.
-Sockets are removed from the agent's pool when the socket emits either a
-"close" event or a special "agentRemove" event. This means that if you intend
-to keep one HTTP request open for a long time and don't want it to stay in the
+Sockets are removed from the agent's pool when the socket emits either a
+"close" event or a special "agentRemove" event. This means that if you intend
+to keep one HTTP request open for a long time and don't want it to stay in the
pool you can do something along the lines of:
http.get(options, function(res) {
}).on("socket", function (socket) {
socket.emit("agentRemove");
});
-
+
Alternatively, you could just opt out of pooling entirely using `agent:false`:
http.get({host:'localhost', port:80, path:'/', agent:false}, function (res) {
- method: HTTP request method. Default `'GET'`.
The following options can also be specified.
-However, a global [Agent](http.html#http.Agent) cannot be used.
+However, a global [Agent](http.html#http.Agent) cannot be used.
- key: Private key to use for SSL. Default `null`.
- cert: Public x509 certificate to use. Default `null`.
if (options.port && +options.port !== options.defaultPort) {
hostHeader += ':' + options.port;
}
- this.setHeader("Host", hostHeader);
+ this.setHeader('Host', hostHeader);
}
}
}
if (Array.isArray(options.headers)) {
- self._storeHeader(self.method + ' ' + self.path + ' HTTP/1.1\r\n', options.headers);
+ self._storeHeader(self.method + ' ' + self.path + ' HTTP/1.1\r\n',
+ options.headers);
} else if (self.getHeader('expect')) {
- self._storeHeader(self.method + ' ' + self.path + ' HTTP/1.1\r\n', self._renderHeaders());
+ self._storeHeader(self.method + ' ' + self.path + ' HTTP/1.1\r\n',
+ self._renderHeaders());
}
if (self.socketPath) {
self._last = true;
}
}
- self._deferToConnect(null, null, function () {
+ self._deferToConnect(null, null, function() {
self._flush();
- })
+ });
}
util.inherits(ClientRequest, OutgoingMessage);
ClientRequest.prototype.onSocket = function(socket) {
var req = this;
- process.nextTick(function () {
+ process.nextTick(function() {
var parser = parsers.alloc();
req.socket = socket;
req.connection = socket;
}
out.hostname = newOut.join('.');
- out.host = (out.hostname || '') +
+ out.host = (out.hostname || '') +
((out.port) ? ':' + out.port : '');
out.href += out.host;
}
//to support http.request
if (source.pathname !== undefined || source.search !== undefined) {
source.path = (source.pathname ? source.pathname : '') +
- (source.search ? source.search : '');
+ (source.search ? source.search : '');
}
source.slashes = source.slashes || relative.slashes;
source.href = urlFormat(source);
//to support http.request
if (source.pathname !== undefined || source.search !== undefined) {
source.path = (source.pathname ? source.pathname : '') +
- (source.search ? source.search : '');
+ (source.search ? source.search : '');
}
source.href = urlFormat(source);
return source;
//to support request.http
if (source.pathname !== undefined || source.search !== undefined) {
source.path = (source.pathname ? source.pathname : '') +
- (source.search ? source.search : '');
+ (source.search ? source.search : '');
}
source.auth = relative.auth || source.auth;
source.slashes = source.slashes || relative.slashes;
var common = require('../common');
var assert = require('assert');
var http = require('http');
-var https = require('https');
var url = require('url');
var testURL = url.parse('http://asdf:qwer@localhost:' + common.PORT);
server.close();
});
-server.listen(common.PORT, function () {
+server.listen(common.PORT, function() {
// make the request
http.request(testURL).end();
});
var common = require('../common');
var assert = require('assert');
var http = require('http');
-var https = require('https');
var url = require('url');
var testURL = url.parse('http://asdf:qwer@localhost:' + common.PORT);
server.close();
});
-server.listen(common.PORT, function () {
+server.listen(common.PORT, function() {
// make the request
http.request(testURL).end();
});
server.close();
});
-server.listen(common.PORT, function () {
+server.listen(common.PORT, function() {
// make the request
var clientRequest = https.request(testURL);
// since there is a little magic with the agent
var common = require('../common');
var assert = require('assert');
var http = require('http');
-var https = require('https');
var url = require('url');
var testURL = url.parse('http://localhost:' + common.PORT + '/asdf');
server.close();
});
-server.listen(common.PORT, function () {
+server.listen(common.PORT, function() {
// make the request
http.request(testURL).end();
});
testURL.method = 'POST';
function check(request) {
- //url.parse should not mess with the method
- assert.strictEqual(request.method, 'POST');
- //everything else should be right
- assert.strictEqual(request.url, '/asdf?qwer=zxcv');
- //the host header should use the url.parse.hostname
- assert.strictEqual(request.headers.host,
- testURL.hostname + ':' + testURL.port);
+ //url.parse should not mess with the method
+ assert.strictEqual(request.method, 'POST');
+ //everything else should be right
+ assert.strictEqual(request.url, '/asdf?qwer=zxcv');
+ //the host header should use the url.parse.hostname
+ assert.strictEqual(request.headers.host,
+ testURL.hostname + ':' + testURL.port);
}
var server = http.createServer(function(request, response) {
server.close();
});
-server.listen(common.PORT, function () {
+server.listen(common.PORT, function() {
// make the request
http.request(testURL).end();
});
server.close();
});
-server.listen(common.PORT, function () {
+server.listen(common.PORT, function() {
// make the request
http.request(testURL).end();
});
if (relativeTests2[181][0] === './/g' &&
relativeTests2[181][1] === 'f:/a' &&
relativeTests2[181][2] === 'f://g') {
- relativeTests2.splice(181,1);
+ relativeTests2.splice(181, 1);
}
relativeTests2.forEach(function(relativeTest) {
var actual = url.resolveObject(url.parse(relativeTest[1]), relativeTest[0]),