1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
22 var util = require('util');
23 var net = require('net');
24 var EventEmitter = require('events').EventEmitter;
25 var HTTPParser = process.binding('http_parser').HTTPParser;
26 var assert = require('assert').ok;
28 var common = require('_http_common');
30 var httpSocketSetup = common.httpSocketSetup;
31 var parsers = common.parsers;
32 var freeParser = common.freeParser;
33 var debug = common.debug;
35 var IncomingMessage = require('_http_incoming').IncomingMessage;
36 var OutgoingMessage = require('_http_outgoing').OutgoingMessage;
38 var agent = require('_http_agent');
39 var globalAgent = agent.globalAgent;
42 function ClientRequest(options, cb) {
44 OutgoingMessage.call(self);
46 options = util._extend({}, options);
48 self.agent = util.isUndefined(options.agent) ? globalAgent : options.agent;
50 var defaultPort = options.defaultPort || self.agent.defaultPort;
52 var port = options.port = options.port || defaultPort;
53 var host = options.host = options.hostname || options.host || 'localhost';
55 if (util.isUndefined(options.setHost)) {
59 self.socketPath = options.socketPath;
61 var method = self.method = (options.method || 'GET').toUpperCase();
62 self.path = options.path || '/';
64 self.once('response', cb);
67 if (!util.isArray(options.headers)) {
68 if (options.headers) {
69 var keys = Object.keys(options.headers);
70 for (var i = 0, l = keys.length; i < l; i++) {
72 self.setHeader(key, options.headers[key]);
75 if (host && !this.getHeader('host') && setHost) {
76 var hostHeader = host;
77 if (port && +port !== defaultPort) {
78 hostHeader += ':' + port;
80 this.setHeader('Host', hostHeader);
84 if (options.auth && !this.getHeader('Authorization')) {
86 this.setHeader('Authorization', 'Basic ' +
87 new Buffer(options.auth).toString('base64'));
90 if (method === 'GET' || method === 'HEAD' || method === 'CONNECT') {
91 self.useChunkedEncodingByDefault = false;
93 self.useChunkedEncodingByDefault = true;
96 if (util.isArray(options.headers)) {
97 self._storeHeader(self.method + ' ' + self.path + ' HTTP/1.1\r\n',
99 } else if (self.getHeader('expect')) {
100 self._storeHeader(self.method + ' ' + self.path + ' HTTP/1.1\r\n',
101 self._renderHeaders());
104 if (self.socketPath) {
106 self.shouldKeepAlive = false;
107 var conn = self.agent.createConnection({ path: self.socketPath });
109 } else if (self.agent) {
110 // If there is an agent we should default to Connection:keep-alive,
111 // but only if the Agent will actually reuse the connection!
112 // If it's not a keepAlive agent, and the maxSockets==Infinity, then
113 // there's never a case where this socket will actually be reused
114 var agent = self.agent;
115 if (!agent.keepAlive && !Number.isFinite(agent.maxSockets)) {
117 self.shouldKeepAlive = false;
120 self.shouldKeepAlive = true;
122 self.agent.addRequest(self, options);
124 // No agent, default to Connection:close.
126 self.shouldKeepAlive = false;
127 if (options.createConnection) {
128 var conn = options.createConnection(options);
130 debug('CLIENT use net.createConnection', options);
131 var conn = net.createConnection(options);
136 self._deferToConnect(null, null, function() {
142 util.inherits(ClientRequest, OutgoingMessage);
144 exports.ClientRequest = ClientRequest;
146 ClientRequest.prototype._finish = function() {
147 DTRACE_HTTP_CLIENT_REQUEST(this, this.connection);
148 COUNTER_HTTP_CLIENT_REQUEST();
149 OutgoingMessage.prototype._finish.call(this);
152 ClientRequest.prototype._implicitHeader = function() {
153 this._storeHeader(this.method + ' ' + this.path + ' HTTP/1.1\r\n',
154 this._renderHeaders());
157 ClientRequest.prototype.abort = function() {
158 // If we're aborting, we don't care about any more response data.
162 this.once('response', function(res) {
168 this.socket.destroy();
170 // haven't been assigned a socket yet.
171 // this could be more efficient, it could
172 // remove itself from the pending requests
173 this._deferToConnect('destroy', []);
178 function createHangUpError() {
179 var error = new Error('socket hang up');
180 error.code = 'ECONNRESET';
185 function socketCloseListener() {
187 var parser = socket.parser;
188 var req = socket._httpMessage;
189 debug('HTTP socket close');
191 if (req.res && req.res.readable) {
192 // Socket closed before we emitted 'end' below.
193 req.res.emit('aborted');
195 res.on('end', function() {
199 } else if (!req.res && !req.socket._hadError) {
200 // This socket error fired before we started to
201 // receive a response. The error needs to
202 // fire on the request.
203 req.emit('error', createHangUpError());
204 req.socket._hadError = true;
207 // Too bad. That output wasn't getting written.
208 // This is pretty terrible that it doesn't raise an error.
209 // Fixed better in v0.10
211 req.output.length = 0;
212 if (req.outputEncodings)
213 req.outputEncodings.length = 0;
217 freeParser(parser, req);
221 function socketErrorListener(err) {
223 var parser = socket.parser;
224 var req = socket._httpMessage;
225 debug('SOCKET ERROR:', err.message, err.stack);
228 req.emit('error', err);
229 // For Safety. Some additional errors might fire later on
230 // and we need to make sure we don't double-fire the error event.
231 req.socket._hadError = true;
236 freeParser(parser, req);
241 function socketOnEnd() {
243 var req = this._httpMessage;
244 var parser = this.parser;
246 if (!req.res && !req.socket._hadError) {
247 // If we don't have a response then we know that the socket
248 // ended prematurely and we need to emit an error on the request.
249 req.emit('error', createHangUpError());
250 req.socket._hadError = true;
254 freeParser(parser, req);
259 function socketOnData(d) {
261 var req = this._httpMessage;
262 var parser = this.parser;
266 var ret = parser.execute(d);
267 if (ret instanceof Error) {
268 debug('parse error');
269 freeParser(parser, req);
271 req.emit('error', ret);
272 req.socket._hadError = true;
273 } else if (parser.incoming && parser.incoming.upgrade) {
274 // Upgrade or CONNECT
275 var bytesParsed = ret;
276 var res = parser.incoming;
279 socket.removeListener('data', socketOnData);
280 socket.removeListener('end', socketOnEnd);
283 var bodyHead = d.slice(bytesParsed, d.length);
285 var eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade';
286 if (EventEmitter.listenerCount(req, eventName) > 0) {
287 req.upgradeOrConnect = true;
290 socket.emit('agentRemove');
291 socket.removeListener('close', socketCloseListener);
292 socket.removeListener('error', socketErrorListener);
294 // TODO(isaacs): Need a way to reset a stream to fresh state
295 // IE, not flowing, and not explicitly paused.
296 socket._readableState.flowing = null;
298 req.emit(eventName, res, socket, bodyHead);
301 // Got Upgrade header or CONNECT method, but have no handler.
304 freeParser(parser, req);
305 } else if (parser.incoming && parser.incoming.complete &&
306 // When the status code is 100 (Continue), the server will
307 // send a final response after this client sends a request
308 // body. So, we must not free the parser.
309 parser.incoming.statusCode !== 100) {
310 socket.removeListener('data', socketOnData);
311 socket.removeListener('end', socketOnEnd);
312 freeParser(parser, req);
318 function parserOnIncomingClient(res, shouldKeepAlive) {
319 var socket = this.socket;
320 var req = socket._httpMessage;
323 // propogate "domain" setting...
324 if (req.domain && !res.domain) {
325 debug('setting "res.domain"');
326 res.domain = req.domain;
329 debug('AGENT incoming response!');
332 // We already have a response object, this means the server
333 // sent a double response.
339 // Responses to CONNECT request is handled as Upgrade.
340 if (req.method === 'CONNECT') {
342 return true; // skip body
345 // Responses to HEAD requests are crazy.
346 // HEAD responses aren't allowed to have an entity-body
347 // but *can* have a content-length which actually corresponds
348 // to the content-length of the entity-body had the request
350 var isHeadResponse = req.method == 'HEAD';
351 debug('AGENT isHeadResponse', isHeadResponse);
353 if (res.statusCode == 100) {
354 // restart the parser, as this is a continue message.
355 delete req.res; // Clear res so that we don't hit double-responses.
356 req.emit('continue');
360 if (req.shouldKeepAlive && !shouldKeepAlive && !req.upgradeOrConnect) {
361 // Server MUST respond with Connection:keep-alive for us to enable it.
362 // If we've been upgraded (via WebSockets) we also shouldn't try to
363 // keep the connection open.
364 req.shouldKeepAlive = false;
368 DTRACE_HTTP_CLIENT_RESPONSE(socket, req);
369 COUNTER_HTTP_CLIENT_RESPONSE();
373 // add our listener first, so that we guarantee socket cleanup
374 res.on('end', responseOnEnd);
375 var handled = req.emit('response', res);
377 // If the user did not listen for the 'response' event, then they
378 // can't possibly read the data, so we ._dump() it into the void
379 // so that the socket doesn't hang there in a paused state.
383 return isHeadResponse;
387 function responseOnEnd() {
390 var socket = req.socket;
392 if (!req.shouldKeepAlive) {
393 if (socket.writable) {
394 debug('AGENT socket.destroySoon()');
395 socket.destroySoon();
397 assert(!socket.writable);
399 debug('AGENT socket keep-alive');
401 socket.setTimeout(0, req.timeoutCb);
402 req.timeoutCb = null;
404 socket.removeListener('close', socketCloseListener);
405 socket.removeListener('error', socketErrorListener);
406 // Mark this socket as available, AFTER user-added end
407 // handlers have a chance to run.
408 process.nextTick(function() {
414 ClientRequest.prototype.onSocket = function(socket) {
417 process.nextTick(function() {
418 var parser = parsers.alloc();
420 req.connection = socket;
421 parser.reinitialize(HTTPParser.RESPONSE);
422 parser.socket = socket;
423 parser.incoming = null;
426 socket.parser = parser;
427 socket._httpMessage = req;
429 // Setup "drain" propogation.
430 httpSocketSetup(socket);
432 // Propagate headers limit from request object to parser
433 if (util.isNumber(req.maxHeadersCount)) {
434 parser.maxHeaderPairs = req.maxHeadersCount << 1;
436 // Set default value because parser may be reused from FreeList
437 parser.maxHeaderPairs = 2000;
440 parser.onIncoming = parserOnIncomingClient;
441 socket.on('error', socketErrorListener);
442 socket.on('data', socketOnData);
443 socket.on('end', socketOnEnd);
444 socket.on('close', socketCloseListener);
445 req.emit('socket', socket);
450 ClientRequest.prototype._deferToConnect = function(method, arguments_, cb) {
451 // This function is for calls that need to happen once the socket is
452 // connected and writable. It's an important promisy thing for all the socket
453 // calls that happen either now (when a socket is assigned) or
454 // in the future (when a socket gets assigned out of the pool and is
455 // eventually writable).
457 var onSocket = function() {
458 if (self.socket.writable) {
460 self.socket[method].apply(self.socket, arguments_);
464 self.socket.once('connect', function() {
466 self.socket[method].apply(self.socket, arguments_);
473 self.once('socket', onSocket);
479 ClientRequest.prototype.setTimeout = function(msecs, callback) {
480 if (callback) this.once('timeout', callback);
483 function emitTimeout() {
484 self.emit('timeout');
487 if (this.socket && this.socket.writable) {
489 this.socket.setTimeout(0, this.timeoutCb);
490 this.timeoutCb = emitTimeout;
491 this.socket.setTimeout(msecs, emitTimeout);
495 // Set timeoutCb so that it'll get cleaned up on request end
496 this.timeoutCb = emitTimeout;
498 var sock = this.socket;
499 this.socket.once('connect', function() {
500 sock.setTimeout(msecs, emitTimeout);
505 this.once('socket', function(sock) {
506 sock.setTimeout(msecs, emitTimeout);
510 ClientRequest.prototype.setNoDelay = function() {
511 this._deferToConnect('setNoDelay', arguments);
513 ClientRequest.prototype.setSocketKeepAlive = function() {
514 this._deferToConnect('setKeepAlive', arguments);
517 ClientRequest.prototype.clearTimeout = function(cb) {
518 this.setTimeout(0, cb);