2010-10-11 23:04:09 +02:00
|
|
|
var util = require('util');
|
2010-11-29 16:41:08 +01:00
|
|
|
var net = require('net');
|
|
|
|
var stream = require('stream');
|
2011-01-20 11:41:16 +01:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2010-11-29 16:41:08 +01:00
|
|
|
var FreeList = require('freelist').FreeList;
|
|
|
|
var HTTPParser = process.binding('http_parser').HTTPParser;
|
2011-01-28 01:59:28 +01:00
|
|
|
var assert = require('assert').ok;
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2010-03-20 05:49:00 +01:00
|
|
|
|
2010-05-27 02:59:55 +02:00
|
|
|
var debug;
|
2011-01-04 00:27:57 +01:00
|
|
|
if (process.env.NODE_DEBUG && /http/.test(process.env.NODE_DEBUG)) {
|
2010-12-02 03:07:20 +01:00
|
|
|
debug = function(x) { console.error('HTTP: %s', x); };
|
2010-05-27 02:59:55 +02:00
|
|
|
} else {
|
2010-12-02 03:07:20 +01:00
|
|
|
debug = function() { };
|
2010-03-20 05:49:00 +01:00
|
|
|
}
|
|
|
|
|
2010-03-20 03:22:04 +01:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
var parsers = new FreeList('parsers', 1000, function() {
|
2010-04-12 18:27:32 +02:00
|
|
|
var parser = new HTTPParser('request');
|
2010-03-20 03:22:04 +01:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
parser.onMessageBegin = function() {
|
2010-04-12 18:27:32 +02:00
|
|
|
parser.incoming = new IncomingMessage(parser.socket);
|
|
|
|
parser.field = null;
|
|
|
|
parser.value = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Only servers will get URL events.
|
2010-12-02 03:07:20 +01:00
|
|
|
parser.onURL = function(b, start, len) {
|
|
|
|
var slice = b.toString('ascii', start, start + len);
|
2010-04-12 18:27:32 +02:00
|
|
|
if (parser.incoming.url) {
|
|
|
|
parser.incoming.url += slice;
|
|
|
|
} else {
|
|
|
|
// Almost always will branch here.
|
|
|
|
parser.incoming.url = slice;
|
|
|
|
}
|
|
|
|
};
|
2010-03-20 03:22:04 +01:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
parser.onHeaderField = function(b, start, len) {
|
|
|
|
var slice = b.toString('ascii', start, start + len).toLowerCase();
|
2010-07-07 18:43:39 +02:00
|
|
|
if (parser.value != undefined) {
|
2010-04-12 18:27:32 +02:00
|
|
|
parser.incoming._addHeaderLine(parser.field, parser.value);
|
2010-03-20 03:22:04 +01:00
|
|
|
parser.field = null;
|
|
|
|
parser.value = null;
|
2010-04-12 18:27:32 +02:00
|
|
|
}
|
|
|
|
if (parser.field) {
|
|
|
|
parser.field += slice;
|
|
|
|
} else {
|
|
|
|
parser.field = slice;
|
|
|
|
}
|
|
|
|
};
|
2010-03-20 03:22:04 +01:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
parser.onHeaderValue = function(b, start, len) {
|
|
|
|
var slice = b.toString('ascii', start, start + len);
|
2010-04-12 18:27:32 +02:00
|
|
|
if (parser.value) {
|
|
|
|
parser.value += slice;
|
|
|
|
} else {
|
|
|
|
parser.value = slice;
|
|
|
|
}
|
|
|
|
};
|
2010-03-20 03:22:04 +01:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
parser.onHeadersComplete = function(info) {
|
2010-07-07 18:43:39 +02:00
|
|
|
if (parser.field && (parser.value != undefined)) {
|
2010-04-12 18:27:32 +02:00
|
|
|
parser.incoming._addHeaderLine(parser.field, parser.value);
|
2010-09-29 11:38:48 +02:00
|
|
|
parser.field = null;
|
|
|
|
parser.value = null;
|
2010-04-12 18:27:32 +02:00
|
|
|
}
|
2010-03-20 03:22:04 +01:00
|
|
|
|
2010-04-12 18:27:32 +02:00
|
|
|
parser.incoming.httpVersionMajor = info.versionMajor;
|
|
|
|
parser.incoming.httpVersionMinor = info.versionMinor;
|
2010-11-29 16:41:08 +01:00
|
|
|
parser.incoming.httpVersion = info.versionMajor + '.' + info.versionMinor;
|
2010-03-20 03:22:04 +01:00
|
|
|
|
2010-04-12 18:27:32 +02:00
|
|
|
if (info.method) {
|
|
|
|
// server only
|
|
|
|
parser.incoming.method = info.method;
|
|
|
|
} else {
|
|
|
|
// client only
|
2010-11-29 16:41:08 +01:00
|
|
|
parser.incoming.statusCode = info.statusCode;
|
2010-04-12 18:27:32 +02:00
|
|
|
}
|
2010-03-20 03:22:04 +01:00
|
|
|
|
2010-04-14 12:52:15 +02:00
|
|
|
parser.incoming.upgrade = info.upgrade;
|
|
|
|
|
2010-05-26 04:24:30 +02:00
|
|
|
var isHeadResponse = false;
|
|
|
|
|
2010-04-14 12:52:15 +02:00
|
|
|
if (!info.upgrade) {
|
|
|
|
// For upgraded connections, we'll emit this after parser.execute
|
|
|
|
// so that we can capture the first part of the new protocol
|
2010-05-26 04:24:30 +02:00
|
|
|
isHeadResponse = parser.onIncoming(parser.incoming, info.shouldKeepAlive);
|
2010-04-14 12:52:15 +02:00
|
|
|
}
|
2010-05-26 03:39:23 +02:00
|
|
|
|
2010-05-26 04:24:30 +02:00
|
|
|
return isHeadResponse;
|
2010-04-12 18:27:32 +02:00
|
|
|
};
|
2010-03-20 03:22:04 +01:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
parser.onBody = function(b, start, len) {
|
2010-04-12 18:27:32 +02:00
|
|
|
// TODO body encoding?
|
2010-12-02 03:07:20 +01:00
|
|
|
var slice = b.slice(start, start + len);
|
2010-06-16 03:19:25 +02:00
|
|
|
if (parser.incoming._decoder) {
|
|
|
|
var string = parser.incoming._decoder.write(slice);
|
|
|
|
if (string.length) parser.incoming.emit('data', string);
|
2010-04-12 18:27:32 +02:00
|
|
|
} else {
|
2010-06-16 03:19:25 +02:00
|
|
|
parser.incoming.emit('data', slice);
|
2010-04-12 18:27:32 +02:00
|
|
|
}
|
|
|
|
};
|
2010-03-20 03:22:04 +01:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
parser.onMessageComplete = function() {
|
2010-09-29 11:38:48 +02:00
|
|
|
this.incoming.complete = true;
|
|
|
|
if (parser.field && (parser.value != undefined)) {
|
|
|
|
parser.incoming._addHeaderLine(parser.field, parser.value);
|
|
|
|
}
|
2010-04-14 12:52:15 +02:00
|
|
|
if (!parser.incoming.upgrade) {
|
|
|
|
// For upgraded connections, also emit this after parser.execute
|
2010-12-02 03:07:20 +01:00
|
|
|
parser.incoming.emit('end');
|
2010-04-14 12:52:15 +02:00
|
|
|
}
|
2010-04-12 18:27:32 +02:00
|
|
|
};
|
2010-03-20 03:22:04 +01:00
|
|
|
|
|
|
|
return parser;
|
2010-04-12 18:27:32 +02:00
|
|
|
});
|
2010-10-08 01:17:06 +02:00
|
|
|
exports.parsers = parsers;
|
2010-03-15 21:48:03 +01:00
|
|
|
|
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
var CRLF = '\r\n';
|
2009-12-08 01:18:43 +01:00
|
|
|
var STATUS_CODES = exports.STATUS_CODES = {
|
2009-07-01 00:49:56 +02:00
|
|
|
100 : 'Continue',
|
|
|
|
101 : 'Switching Protocols',
|
2010-05-23 22:48:50 +02:00
|
|
|
102 : 'Processing', // RFC 2518, obsoleted by RFC 4918
|
2009-07-01 00:49:56 +02:00
|
|
|
200 : 'OK',
|
|
|
|
201 : 'Created',
|
|
|
|
202 : 'Accepted',
|
|
|
|
203 : 'Non-Authoritative Information',
|
|
|
|
204 : 'No Content',
|
|
|
|
205 : 'Reset Content',
|
|
|
|
206 : 'Partial Content',
|
2010-05-23 22:48:50 +02:00
|
|
|
207 : 'Multi-Status', // RFC 4918
|
2009-07-01 00:49:56 +02:00
|
|
|
300 : 'Multiple Choices',
|
|
|
|
301 : 'Moved Permanently',
|
|
|
|
302 : 'Moved Temporarily',
|
|
|
|
303 : 'See Other',
|
|
|
|
304 : 'Not Modified',
|
|
|
|
305 : 'Use Proxy',
|
2010-05-23 22:48:50 +02:00
|
|
|
307 : 'Temporary Redirect',
|
2009-07-01 00:49:56 +02:00
|
|
|
400 : 'Bad Request',
|
|
|
|
401 : 'Unauthorized',
|
|
|
|
402 : 'Payment Required',
|
|
|
|
403 : 'Forbidden',
|
|
|
|
404 : 'Not Found',
|
|
|
|
405 : 'Method Not Allowed',
|
|
|
|
406 : 'Not Acceptable',
|
|
|
|
407 : 'Proxy Authentication Required',
|
|
|
|
408 : 'Request Time-out',
|
|
|
|
409 : 'Conflict',
|
|
|
|
410 : 'Gone',
|
|
|
|
411 : 'Length Required',
|
|
|
|
412 : 'Precondition Failed',
|
|
|
|
413 : 'Request Entity Too Large',
|
|
|
|
414 : 'Request-URI Too Large',
|
|
|
|
415 : 'Unsupported Media Type',
|
2010-05-23 22:48:50 +02:00
|
|
|
416 : 'Requested Range Not Satisfiable',
|
|
|
|
417 : 'Expectation Failed',
|
|
|
|
418 : 'I\'m a teapot', // RFC 2324
|
|
|
|
422 : 'Unprocessable Entity', // RFC 4918
|
|
|
|
423 : 'Locked', // RFC 4918
|
|
|
|
424 : 'Failed Dependency', // RFC 4918
|
|
|
|
425 : 'Unordered Collection', // RFC 4918
|
|
|
|
426 : 'Upgrade Required', // RFC 2817
|
2009-07-01 00:49:56 +02:00
|
|
|
500 : 'Internal Server Error',
|
|
|
|
501 : 'Not Implemented',
|
|
|
|
502 : 'Bad Gateway',
|
|
|
|
503 : 'Service Unavailable',
|
|
|
|
504 : 'Gateway Time-out',
|
2010-05-23 22:48:50 +02:00
|
|
|
505 : 'HTTP Version not supported',
|
|
|
|
506 : 'Variant Also Negotiates', // RFC 2295
|
|
|
|
507 : 'Insufficient Storage', // RFC 4918
|
|
|
|
509 : 'Bandwidth Limit Exceeded',
|
|
|
|
510 : 'Not Extended' // RFC 2774
|
2009-07-01 00:49:56 +02:00
|
|
|
};
|
2009-05-11 19:08:29 +02:00
|
|
|
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2010-03-24 05:31:44 +01:00
|
|
|
var connectionExpression = /Connection/i;
|
|
|
|
var transferEncodingExpression = /Transfer-Encoding/i;
|
|
|
|
var closeExpression = /close/i;
|
|
|
|
var chunkExpression = /chunk/i;
|
|
|
|
var contentLengthExpression = /Content-Length/i;
|
2010-09-10 03:56:35 +02:00
|
|
|
var expectExpression = /Expect/i;
|
|
|
|
var continueExpression = /100-continue/i;
|
2009-05-11 18:54:52 +02:00
|
|
|
|
2009-06-26 18:29:57 +02:00
|
|
|
|
2009-07-14 11:59:13 +02:00
|
|
|
/* Abstract base class for ServerRequest and ClientResponse. */
|
2010-12-02 03:07:20 +01:00
|
|
|
function IncomingMessage(socket) {
|
2010-10-11 04:38:57 +02:00
|
|
|
stream.Stream.call(this);
|
2009-06-26 18:29:57 +02:00
|
|
|
|
2010-03-24 15:20:56 +01:00
|
|
|
// TODO Remove one of these eventually.
|
2010-03-20 03:22:04 +01:00
|
|
|
this.socket = socket;
|
2010-03-24 15:20:56 +01:00
|
|
|
this.connection = socket;
|
|
|
|
|
2009-07-14 11:59:13 +02:00
|
|
|
this.httpVersion = null;
|
2010-09-29 11:38:48 +02:00
|
|
|
this.complete = false;
|
2009-08-23 12:20:25 +02:00
|
|
|
this.headers = {};
|
2010-09-29 11:38:48 +02:00
|
|
|
this.trailers = {};
|
2009-07-14 18:31:50 +02:00
|
|
|
|
2010-10-11 10:22:38 +02:00
|
|
|
this.readable = true;
|
|
|
|
|
2009-08-26 22:03:19 +02:00
|
|
|
// request (server) only
|
2010-12-02 03:07:20 +01:00
|
|
|
this.url = '';
|
2009-10-04 12:17:50 +02:00
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
this.method = null;
|
|
|
|
|
|
|
|
// response (client) only
|
|
|
|
this.statusCode = null;
|
2010-03-20 03:22:04 +01:00
|
|
|
this.client = this.socket;
|
2009-07-16 10:59:40 +02:00
|
|
|
}
|
2010-10-11 23:04:09 +02:00
|
|
|
util.inherits(IncomingMessage, stream.Stream);
|
2010-11-29 16:41:08 +01:00
|
|
|
|
|
|
|
|
2009-11-05 00:02:15 +01:00
|
|
|
exports.IncomingMessage = IncomingMessage;
|
2009-07-14 11:59:13 +02:00
|
|
|
|
2010-10-12 01:36:12 +02:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
IncomingMessage.prototype.destroy = function(error) {
|
2010-10-12 01:36:12 +02:00
|
|
|
this.socket.destroy(error);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
IncomingMessage.prototype.setEncoding = function(encoding) {
|
|
|
|
var StringDecoder = require('string_decoder').StringDecoder; // lazy load
|
2010-06-16 03:19:25 +02:00
|
|
|
this._decoder = new StringDecoder(encoding);
|
2009-06-26 18:29:57 +02:00
|
|
|
};
|
|
|
|
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
IncomingMessage.prototype.pause = function() {
|
2010-03-20 03:22:04 +01:00
|
|
|
this.socket.pause();
|
2009-08-09 19:16:25 +02:00
|
|
|
};
|
|
|
|
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
IncomingMessage.prototype.resume = function() {
|
2010-03-20 03:22:04 +01:00
|
|
|
this.socket.resume();
|
2009-08-09 19:16:25 +02:00
|
|
|
};
|
|
|
|
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2010-06-21 20:53:17 +02:00
|
|
|
// Add the given (field, value) pair to the message
|
|
|
|
//
|
|
|
|
// Per RFC2616, section 4.2 it is acceptable to join multiple instances of the
|
|
|
|
// same header with a ', ' if the header in question supports specification of
|
|
|
|
// multiple values this way. If not, we declare the first instance the winner
|
|
|
|
// and drop the second. Extended header fields (those beginning with 'x-') are
|
|
|
|
// always joined.
|
2010-12-02 03:07:20 +01:00
|
|
|
IncomingMessage.prototype._addHeaderLine = function(field, value) {
|
|
|
|
var dest = this.complete ? this.trailers : this.headers;
|
|
|
|
|
2010-06-21 20:53:17 +02:00
|
|
|
switch (field) {
|
2010-08-22 01:34:38 +02:00
|
|
|
// Array headers:
|
|
|
|
case 'set-cookie':
|
2010-09-29 11:38:48 +02:00
|
|
|
if (field in dest) {
|
|
|
|
dest[field].push(value);
|
2010-08-22 01:34:38 +02:00
|
|
|
} else {
|
2010-09-29 11:38:48 +02:00
|
|
|
dest[field] = [value];
|
2010-08-22 01:34:38 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Comma separate. Maybe make these arrays?
|
|
|
|
case 'accept':
|
|
|
|
case 'accept-charset':
|
|
|
|
case 'accept-encoding':
|
|
|
|
case 'accept-language':
|
|
|
|
case 'connection':
|
|
|
|
case 'cookie':
|
2010-09-29 11:38:48 +02:00
|
|
|
if (field in dest) {
|
|
|
|
dest[field] += ', ' + value;
|
2010-08-22 01:34:38 +02:00
|
|
|
} else {
|
2010-09-29 11:38:48 +02:00
|
|
|
dest[field] = value;
|
2010-08-22 01:34:38 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
2010-12-02 03:07:20 +01:00
|
|
|
if (field.slice(0, 2) == 'x-') {
|
2010-08-22 01:34:38 +02:00
|
|
|
// except for x-
|
2010-09-29 11:38:48 +02:00
|
|
|
if (field in dest) {
|
|
|
|
dest[field] += ', ' + value;
|
2010-08-22 01:34:38 +02:00
|
|
|
} else {
|
2010-09-29 11:38:48 +02:00
|
|
|
dest[field] = value;
|
2010-08-22 01:34:38 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// drop duplicates
|
2010-09-29 11:38:48 +02:00
|
|
|
if (!(field in dest)) dest[field] = value;
|
2010-08-22 01:34:38 +02:00
|
|
|
}
|
|
|
|
break;
|
2009-08-23 12:20:25 +02:00
|
|
|
}
|
|
|
|
};
|
2009-07-12 11:48:37 +02:00
|
|
|
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
function OutgoingMessage() {
|
2010-10-11 04:38:57 +02:00
|
|
|
stream.Stream.call(this);
|
2010-03-03 22:06:19 +01:00
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
this.output = [];
|
2009-09-13 12:38:59 +02:00
|
|
|
this.outputEncodings = [];
|
2009-06-26 18:29:57 +02:00
|
|
|
|
2010-10-11 10:22:38 +02:00
|
|
|
this.writable = true;
|
|
|
|
|
2010-05-27 02:59:55 +02:00
|
|
|
this._last = false;
|
2010-03-24 05:31:44 +01:00
|
|
|
this.chunkedEncoding = false;
|
|
|
|
this.shouldKeepAlive = true;
|
|
|
|
this.useChunkedEncodingByDefault = true;
|
2009-07-14 11:59:13 +02:00
|
|
|
|
2010-04-22 00:15:21 +02:00
|
|
|
this._hasBody = true;
|
2010-09-09 08:29:35 +02:00
|
|
|
this._trailer = '';
|
2010-04-22 00:15:21 +02:00
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
this.finished = false;
|
2009-07-16 10:59:40 +02:00
|
|
|
}
|
2010-10-11 23:04:09 +02:00
|
|
|
util.inherits(OutgoingMessage, stream.Stream);
|
2010-11-29 16:41:08 +01:00
|
|
|
|
|
|
|
|
2009-11-05 00:02:15 +01:00
|
|
|
exports.OutgoingMessage = OutgoingMessage;
|
2009-07-14 11:59:13 +02:00
|
|
|
|
2010-10-12 01:36:12 +02:00
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
OutgoingMessage.prototype.assignSocket = function(socket) {
|
|
|
|
assert(!socket._httpMessage);
|
|
|
|
socket._httpMessage = this;
|
|
|
|
this.socket = socket;
|
|
|
|
this.connection = socket;
|
|
|
|
this._flush();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-01-20 19:29:54 +01:00
|
|
|
OutgoingMessage.prototype.detachSocket = function(socket) {
|
|
|
|
assert(socket._httpMessage == this);
|
|
|
|
socket._httpMessage = null;
|
|
|
|
this.socket = this.connection = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
OutgoingMessage.prototype.destroy = function(error) {
|
2010-10-12 01:36:12 +02:00
|
|
|
this.socket.destroy(error);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-05-27 02:59:55 +02:00
|
|
|
// This abstract either writing directly to the socket or buffering it.
|
2010-12-02 03:07:20 +01:00
|
|
|
OutgoingMessage.prototype._send = function(data, encoding) {
|
2010-05-28 05:28:12 +02:00
|
|
|
// This is a shameful hack to get the headers and first body chunk onto
|
|
|
|
// the same packet. Future versions of Node are going to take care of
|
|
|
|
// this at a lower level and in a more general way.
|
|
|
|
if (!this._headerSent) {
|
|
|
|
if (typeof data === 'string') {
|
|
|
|
data = this._header + data;
|
|
|
|
} else {
|
|
|
|
this.output.unshift(this._header);
|
|
|
|
this.outputEncodings.unshift('ascii');
|
|
|
|
}
|
|
|
|
this._headerSent = true;
|
|
|
|
}
|
2010-10-12 01:00:36 +02:00
|
|
|
return this._writeRaw(data, encoding);
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2010-05-28 05:28:12 +02:00
|
|
|
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2010-09-10 03:56:35 +02:00
|
|
|
OutgoingMessage.prototype._writeRaw = function(data, encoding) {
|
2011-01-20 19:29:54 +01:00
|
|
|
if (this.connection &&
|
|
|
|
this.connection._httpMessage === this &&
|
|
|
|
this.connection.writable) {
|
2010-05-28 05:28:12 +02:00
|
|
|
// There might be pending data in the this.output buffer.
|
|
|
|
while (this.output.length) {
|
|
|
|
if (!this.connection.writable) {
|
|
|
|
this._buffer(data, encoding);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var c = this.output.shift();
|
|
|
|
var e = this.outputEncodings.shift();
|
|
|
|
this.connection.write(c, e);
|
|
|
|
}
|
|
|
|
|
2010-05-27 02:59:55 +02:00
|
|
|
// Directly write to socket.
|
|
|
|
return this.connection.write(data, encoding);
|
|
|
|
} else {
|
2010-05-28 05:28:12 +02:00
|
|
|
this._buffer(data, encoding);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2010-05-27 02:59:55 +02:00
|
|
|
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
OutgoingMessage.prototype._buffer = function(data, encoding) {
|
2010-05-28 05:28:12 +02:00
|
|
|
if (data.length === 0) return;
|
2010-05-27 02:59:55 +02:00
|
|
|
|
2010-05-28 05:28:12 +02:00
|
|
|
var length = this.output.length;
|
2009-09-14 16:25:41 +02:00
|
|
|
|
2010-05-28 05:28:12 +02:00
|
|
|
if (length === 0 || typeof data != 'string') {
|
2009-09-14 16:25:41 +02:00
|
|
|
this.output.push(data);
|
|
|
|
this.outputEncodings.push(encoding);
|
2010-05-28 05:28:12 +02:00
|
|
|
return false;
|
|
|
|
}
|
2009-09-14 16:25:41 +02:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
var lastEncoding = this.outputEncodings[length - 1];
|
|
|
|
var lastData = this.output[length - 1];
|
2010-05-28 05:28:12 +02:00
|
|
|
|
|
|
|
if ((lastEncoding === encoding) ||
|
|
|
|
(!encoding && data.constructor === lastData.constructor)) {
|
2010-12-02 03:07:20 +01:00
|
|
|
this.output[length - 1] = lastData + data;
|
2010-05-27 02:59:55 +02:00
|
|
|
return false;
|
2009-09-14 16:25:41 +02:00
|
|
|
}
|
2010-05-28 05:28:12 +02:00
|
|
|
|
|
|
|
this.output.push(data);
|
|
|
|
this.outputEncodings.push(encoding);
|
|
|
|
|
|
|
|
return false;
|
2009-07-14 18:31:50 +02:00
|
|
|
};
|
2009-07-14 11:59:13 +02:00
|
|
|
|
2010-05-27 02:59:55 +02:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
OutgoingMessage.prototype._storeHeader = function(firstLine, headers) {
|
2010-03-24 05:31:44 +01:00
|
|
|
var sentConnectionHeader = false;
|
|
|
|
var sentContentLengthHeader = false;
|
|
|
|
var sentTransferEncodingHeader = false;
|
2010-09-10 03:56:35 +02:00
|
|
|
var sentExpect = false;
|
2009-07-31 18:34:27 +02:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
// firstLine in the case of request is: 'GET /index.html HTTP/1.1\r\n'
|
|
|
|
// in the case of response it is: 'HTTP/1.1 200 OK\r\n'
|
2010-03-24 05:31:44 +01:00
|
|
|
var messageHeader = firstLine;
|
2009-08-23 12:20:25 +02:00
|
|
|
var field, value;
|
2010-09-15 16:52:23 +02:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
function store(field, value) {
|
2010-12-02 03:07:20 +01:00
|
|
|
messageHeader += field + ': ' + value + CRLF;
|
2010-09-15 16:52:23 +02:00
|
|
|
|
|
|
|
if (connectionExpression.test(field)) {
|
|
|
|
sentConnectionHeader = true;
|
|
|
|
if (closeExpression.test(value)) {
|
|
|
|
self._last = true;
|
|
|
|
} else {
|
|
|
|
self.shouldKeepAlive = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (transferEncodingExpression.test(field)) {
|
|
|
|
sentTransferEncodingHeader = true;
|
|
|
|
if (chunkExpression.test(value)) self.chunkedEncoding = true;
|
|
|
|
|
|
|
|
} else if (contentLengthExpression.test(field)) {
|
|
|
|
sentContentLengthHeader = true;
|
|
|
|
|
2010-09-10 03:56:35 +02:00
|
|
|
} else if (expectExpression.test(field)) {
|
|
|
|
sentExpect = true;
|
|
|
|
|
2010-09-15 16:52:23 +02:00
|
|
|
}
|
|
|
|
}
|
2009-06-26 18:29:57 +02:00
|
|
|
|
2010-04-12 18:57:24 +02:00
|
|
|
if (headers) {
|
|
|
|
var keys = Object.keys(headers);
|
2010-07-14 23:29:28 +02:00
|
|
|
var isArray = (Array.isArray(headers));
|
2010-12-05 00:20:34 +01:00
|
|
|
var field, value;
|
2010-09-15 16:52:23 +02:00
|
|
|
|
2010-10-04 19:50:35 +02:00
|
|
|
for (var i = 0, l = keys.length; i < l; i++) {
|
2010-04-12 18:57:24 +02:00
|
|
|
var key = keys[i];
|
|
|
|
if (isArray) {
|
|
|
|
field = headers[key][0];
|
|
|
|
value = headers[key][1];
|
|
|
|
} else {
|
|
|
|
field = key;
|
|
|
|
value = headers[key];
|
|
|
|
}
|
2009-08-26 22:03:19 +02:00
|
|
|
|
2010-09-15 16:52:23 +02:00
|
|
|
if (Array.isArray(value)) {
|
2010-10-04 19:50:35 +02:00
|
|
|
for (var j = 0; j < value.length; j++) {
|
|
|
|
store(field, value[j]);
|
2010-08-02 07:22:20 +02:00
|
|
|
}
|
2010-09-15 16:52:23 +02:00
|
|
|
} else {
|
|
|
|
store(field, value);
|
2010-04-12 18:57:24 +02:00
|
|
|
}
|
2009-07-14 18:31:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-26 22:03:19 +02:00
|
|
|
// keep-alive logic
|
2010-03-24 05:31:44 +01:00
|
|
|
if (sentConnectionHeader == false) {
|
|
|
|
if (this.shouldKeepAlive &&
|
|
|
|
(sentContentLengthHeader || this.useChunkedEncodingByDefault)) {
|
2010-12-02 03:07:20 +01:00
|
|
|
messageHeader += 'Connection: keep-alive\r\n';
|
2009-06-26 18:29:57 +02:00
|
|
|
} else {
|
2010-05-27 02:59:55 +02:00
|
|
|
this._last = true;
|
2010-12-02 03:07:20 +01:00
|
|
|
messageHeader += 'Connection: close\r\n';
|
2009-06-26 18:29:57 +02:00
|
|
|
}
|
2009-07-14 18:31:50 +02:00
|
|
|
}
|
|
|
|
|
2010-03-24 05:31:44 +01:00
|
|
|
if (sentContentLengthHeader == false && sentTransferEncodingHeader == false) {
|
2010-04-22 00:15:21 +02:00
|
|
|
if (this._hasBody) {
|
|
|
|
if (this.useChunkedEncodingByDefault) {
|
2010-12-02 03:07:20 +01:00
|
|
|
messageHeader += 'Transfer-Encoding: chunked\r\n';
|
2010-04-22 00:15:21 +02:00
|
|
|
this.chunkedEncoding = true;
|
|
|
|
} else {
|
2010-05-27 02:59:55 +02:00
|
|
|
this._last = true;
|
2010-04-22 00:15:21 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Make sure we don't end the 0\r\n\r\n at the end of the message.
|
|
|
|
this.chunkedEncoding = false;
|
2009-12-23 21:48:14 +01:00
|
|
|
}
|
2009-07-14 18:31:50 +02:00
|
|
|
}
|
|
|
|
|
2010-05-28 05:28:12 +02:00
|
|
|
this._header = messageHeader + CRLF;
|
|
|
|
this._headerSent = false;
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2010-09-10 03:56:35 +02:00
|
|
|
// wait until the first body chunk, or close(), is sent to flush,
|
|
|
|
// UNLESS we're sending Expect: 100-continue.
|
2010-12-02 03:07:20 +01:00
|
|
|
if (sentExpect) this._send('');
|
2009-07-14 18:31:50 +02:00
|
|
|
};
|
|
|
|
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
OutgoingMessage.prototype.write = function(chunk, encoding) {
|
2010-05-27 02:59:55 +02:00
|
|
|
if (!this._header) {
|
2010-12-02 03:07:20 +01:00
|
|
|
throw new Error('You have to call writeHead() before write()');
|
2010-03-14 04:36:45 +01:00
|
|
|
}
|
|
|
|
|
2010-04-22 00:15:21 +02:00
|
|
|
if (!this._hasBody) {
|
2010-12-02 03:07:20 +01:00
|
|
|
console.error('This type of response MUST NOT have a body. ' +
|
|
|
|
'Ignoring write() calls.');
|
2010-08-12 19:06:52 +02:00
|
|
|
return true;
|
2010-04-22 00:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
if (typeof chunk !== 'string' && !Buffer.isBuffer(chunk) &&
|
2010-11-29 16:41:08 +01:00
|
|
|
!Array.isArray(chunk)) {
|
2010-12-02 03:07:20 +01:00
|
|
|
throw new TypeError('first argument must be a string, Array, or Buffer');
|
2010-05-05 03:28:49 +02:00
|
|
|
}
|
|
|
|
|
2010-05-27 02:59:55 +02:00
|
|
|
if (chunk.length === 0) return false;
|
|
|
|
|
|
|
|
var len, ret;
|
2010-03-24 05:31:44 +01:00
|
|
|
if (this.chunkedEncoding) {
|
2010-05-27 02:59:55 +02:00
|
|
|
if (typeof(chunk) === 'string') {
|
|
|
|
len = Buffer.byteLength(chunk, encoding);
|
|
|
|
var chunk = len.toString(16) + CRLF + chunk + CRLF;
|
2010-10-11 23:04:09 +02:00
|
|
|
debug('string chunk = ' + util.inspect(chunk));
|
2010-05-27 02:59:55 +02:00
|
|
|
ret = this._send(chunk, encoding);
|
|
|
|
} else {
|
|
|
|
// buffer
|
|
|
|
len = chunk.length;
|
|
|
|
this._send(len.toString(16) + CRLF);
|
|
|
|
this._send(chunk);
|
|
|
|
ret = this._send(CRLF);
|
2010-03-20 03:22:04 +01:00
|
|
|
}
|
2009-07-14 18:31:50 +02:00
|
|
|
} else {
|
2010-05-27 02:59:55 +02:00
|
|
|
ret = this._send(chunk, encoding);
|
2009-07-14 18:31:50 +02:00
|
|
|
}
|
2009-06-26 18:29:57 +02:00
|
|
|
|
2010-05-27 02:59:55 +02:00
|
|
|
debug('write ret = ' + ret);
|
|
|
|
return ret;
|
2009-07-14 18:31:50 +02:00
|
|
|
};
|
|
|
|
|
2010-09-09 08:29:35 +02:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
OutgoingMessage.prototype.addTrailers = function(headers) {
|
|
|
|
this._trailer = '';
|
2010-09-09 08:29:35 +02:00
|
|
|
var keys = Object.keys(headers);
|
|
|
|
var isArray = (Array.isArray(headers));
|
2010-12-05 00:20:34 +01:00
|
|
|
var field, value;
|
2010-09-09 08:29:35 +02:00
|
|
|
for (var i = 0, l = keys.length; i < l; i++) {
|
|
|
|
var key = keys[i];
|
|
|
|
if (isArray) {
|
|
|
|
field = headers[key][0];
|
|
|
|
value = headers[key][1];
|
|
|
|
} else {
|
|
|
|
field = key;
|
|
|
|
value = headers[key];
|
|
|
|
}
|
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
this._trailer += field + ': ' + value + CRLF;
|
2010-09-09 08:29:35 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
OutgoingMessage.prototype.end = function(data, encoding) {
|
2010-05-27 02:59:55 +02:00
|
|
|
var ret;
|
|
|
|
|
2010-11-29 16:41:08 +01:00
|
|
|
var hot = this._headerSent === false &&
|
2010-12-02 03:07:20 +01:00
|
|
|
typeof(data) === 'string' &&
|
2010-11-29 16:41:08 +01:00
|
|
|
data.length > 0 &&
|
|
|
|
this.output.length === 0 &&
|
2011-01-23 22:43:59 +01:00
|
|
|
this.connection &&
|
2010-11-29 16:41:08 +01:00
|
|
|
this.connection.writable &&
|
2011-01-20 11:41:16 +01:00
|
|
|
this.connection._httpMessage === this;
|
2010-05-28 05:28:12 +02:00
|
|
|
|
|
|
|
if (hot) {
|
|
|
|
// Hot path. They're doing
|
|
|
|
// res.writeHead();
|
|
|
|
// res.end(blah);
|
|
|
|
// HACKY.
|
|
|
|
if (this.chunkedEncoding) {
|
|
|
|
var l = Buffer.byteLength(data, encoding).toString(16);
|
2010-11-29 16:41:08 +01:00
|
|
|
ret = this.connection.write(this._header + l + CRLF +
|
2010-12-02 03:07:20 +01:00
|
|
|
data + '\r\n0\r\n' +
|
|
|
|
this._trailer + '\r\n', encoding);
|
2010-05-28 05:28:12 +02:00
|
|
|
} else {
|
|
|
|
ret = this.connection.write(this._header + data, encoding);
|
|
|
|
}
|
|
|
|
this._headerSent = true;
|
|
|
|
|
|
|
|
} else if (data) {
|
|
|
|
// Normal body write.
|
2010-05-27 02:59:55 +02:00
|
|
|
ret = this.write(data, encoding);
|
|
|
|
}
|
|
|
|
|
2010-05-28 05:28:12 +02:00
|
|
|
if (!hot) {
|
|
|
|
if (this.chunkedEncoding) {
|
2010-09-09 08:29:35 +02:00
|
|
|
ret = this._send('0\r\n' + this._trailer + '\r\n'); // Last chunk.
|
2010-10-01 02:12:56 +02:00
|
|
|
} else {
|
2010-05-28 05:28:12 +02:00
|
|
|
// Force a flush, HACK.
|
|
|
|
ret = this._send('');
|
|
|
|
}
|
2010-05-27 02:59:55 +02:00
|
|
|
}
|
|
|
|
|
2010-05-28 05:28:12 +02:00
|
|
|
this.finished = true;
|
2011-01-25 02:50:10 +01:00
|
|
|
DTRACE_HTTP_SERVER_RESPONSE(this.connection);
|
2010-05-28 05:28:12 +02:00
|
|
|
|
2010-05-27 02:59:55 +02:00
|
|
|
// There is the first message on the outgoing queue, and we've sent
|
|
|
|
// everything to the socket.
|
2011-01-20 11:41:16 +01:00
|
|
|
if (this.output.length === 0 && this.connection._httpMessage === this) {
|
|
|
|
debug('outgoing message end.');
|
|
|
|
this._finish();
|
2010-05-27 02:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2009-07-14 18:31:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
OutgoingMessage.prototype._finish = function() {
|
|
|
|
this.emit('finish');
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
OutgoingMessage.prototype._flush = function() {
|
|
|
|
// This logic is probably a bit confusing. Let me explain a bit:
|
|
|
|
//
|
|
|
|
// In both HTTP servers and clients it is possible to queue up several
|
|
|
|
// outgoing messages. This is easiest to imagine in the case of a client.
|
|
|
|
// Take the following situation:
|
|
|
|
//
|
|
|
|
// req1 = client.request('GET', '/');
|
|
|
|
// req2 = client.request('POST', '/');
|
|
|
|
//
|
|
|
|
// When the user does
|
|
|
|
//
|
|
|
|
// req2.write('hello world\n');
|
|
|
|
//
|
|
|
|
// it's possible that the first request has not been completely flushed to
|
|
|
|
// the socket yet. Thus the outgoing messages need to be prepared to queue
|
|
|
|
// up data internally before sending it on further to the socket's queue.
|
|
|
|
//
|
|
|
|
// This function, outgoingFlush(), is called by both the Server and Client
|
|
|
|
// to attempt to flush any pending messages out to the socket.
|
|
|
|
|
|
|
|
if (!this.socket) return;
|
|
|
|
|
|
|
|
var ret;
|
|
|
|
|
|
|
|
while (this.output.length) {
|
|
|
|
if (!this.socket.writable) return; // XXX Necessary?
|
|
|
|
|
|
|
|
var data = this.output.shift();
|
|
|
|
var encoding = this.outputEncodings.shift();
|
|
|
|
|
|
|
|
ret = this.socket.write(data, encoding);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.finished) {
|
|
|
|
// This is a queue to the server or client to bring in the next this.
|
|
|
|
this._finish();
|
|
|
|
} else if (ret) {
|
|
|
|
this.emit('drain');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
function ServerResponse(req) {
|
2011-01-20 11:41:16 +01:00
|
|
|
OutgoingMessage.call(this);
|
2009-07-14 18:31:50 +02:00
|
|
|
|
2010-06-02 23:46:29 +02:00
|
|
|
if (req.method === 'HEAD') this._hasBody = false;
|
|
|
|
|
2009-12-05 08:37:46 +01:00
|
|
|
if (req.httpVersionMajor < 1 || req.httpVersionMinor < 1) {
|
2010-03-24 05:31:44 +01:00
|
|
|
this.useChunkedEncodingByDefault = false;
|
|
|
|
this.shouldKeepAlive = false;
|
2009-12-05 08:37:46 +01:00
|
|
|
}
|
2009-07-16 10:59:40 +02:00
|
|
|
}
|
2010-10-11 23:04:09 +02:00
|
|
|
util.inherits(ServerResponse, OutgoingMessage);
|
2010-11-29 16:41:08 +01:00
|
|
|
|
|
|
|
|
2009-11-04 12:37:52 +01:00
|
|
|
exports.ServerResponse = ServerResponse;
|
2009-07-14 18:31:50 +02:00
|
|
|
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
ServerResponse.prototype.writeContinue = function() {
|
|
|
|
this._writeRaw('HTTP/1.1 100 Continue' + CRLF + CRLF, 'ascii');
|
2010-11-29 16:41:08 +01:00
|
|
|
this._sent100 = true;
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2010-02-22 01:53:20 +01:00
|
|
|
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
ServerResponse.prototype.writeHead = function(statusCode) {
|
2010-02-22 01:53:20 +01:00
|
|
|
var reasonPhrase, headers, headerIndex;
|
|
|
|
|
|
|
|
if (typeof arguments[1] == 'string') {
|
|
|
|
reasonPhrase = arguments[1];
|
|
|
|
headerIndex = 2;
|
|
|
|
} else {
|
2010-12-02 03:07:20 +01:00
|
|
|
reasonPhrase = STATUS_CODES[statusCode] || 'unknown';
|
2010-02-22 01:53:20 +01:00
|
|
|
headerIndex = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof arguments[headerIndex] == 'object') {
|
|
|
|
headers = arguments[headerIndex];
|
|
|
|
} else {
|
|
|
|
headers = {};
|
|
|
|
}
|
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
var statusLine = 'HTTP/1.1 ' + statusCode.toString() + ' ' +
|
2010-11-29 16:41:08 +01:00
|
|
|
reasonPhrase + CRLF;
|
2010-04-22 00:15:21 +02:00
|
|
|
|
2010-11-29 16:41:08 +01:00
|
|
|
if (statusCode === 204 || statusCode === 304 ||
|
|
|
|
(100 <= statusCode && statusCode <= 199)) {
|
2010-04-22 00:15:21 +02:00
|
|
|
// RFC 2616, 10.2.5:
|
|
|
|
// The 204 response MUST NOT include a message-body, and thus is always
|
|
|
|
// terminated by the first empty line after the header fields.
|
|
|
|
// RFC 2616, 10.3.5:
|
|
|
|
// The 304 response MUST NOT contain a message-body, and thus is always
|
|
|
|
// terminated by the first empty line after the header fields.
|
2010-09-10 03:56:35 +02:00
|
|
|
// RFC 2616, 10.1 Informational 1xx:
|
|
|
|
// This class of status code indicates a provisional response,
|
|
|
|
// consisting only of the Status-Line and optional headers, and is
|
|
|
|
// terminated by an empty line.
|
2010-04-22 00:15:21 +02:00
|
|
|
this._hasBody = false;
|
|
|
|
}
|
|
|
|
|
2010-09-10 03:56:35 +02:00
|
|
|
// don't keep alive connections where the client expects 100 Continue
|
|
|
|
// but we sent a final status; they may put extra bytes on the wire.
|
|
|
|
if (this._expect_continue && ! this._sent100) {
|
2010-11-29 16:41:08 +01:00
|
|
|
this.shouldKeepAlive = false;
|
2010-09-10 03:56:35 +02:00
|
|
|
}
|
2010-04-22 00:15:21 +02:00
|
|
|
|
2010-05-27 02:59:55 +02:00
|
|
|
this._storeHeader(statusLine, headers);
|
2009-07-14 18:31:50 +02:00
|
|
|
};
|
|
|
|
|
2010-09-16 23:49:12 +02:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
ServerResponse.prototype.writeHeader = function() {
|
2010-05-12 21:15:58 +02:00
|
|
|
this.writeHead.apply(this, arguments);
|
|
|
|
};
|
2009-07-14 18:31:50 +02:00
|
|
|
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
function ClientRequest(options) {
|
|
|
|
OutgoingMessage.call(this);
|
|
|
|
|
|
|
|
var method = this.method = (options.method || 'GET').toUpperCase();
|
|
|
|
var path = options.path || '/';
|
2011-01-20 20:21:42 +01:00
|
|
|
var headers = options.headers || {};
|
2011-01-20 11:41:16 +01:00
|
|
|
|
|
|
|
// Host header set by default.
|
|
|
|
if (options.host && !(headers.host || headers.Host || headers.HOST)) {
|
|
|
|
headers.Host = options.host;
|
|
|
|
}
|
2009-07-14 18:31:50 +02:00
|
|
|
|
2010-03-24 05:31:44 +01:00
|
|
|
this.shouldKeepAlive = false;
|
2010-12-02 03:07:20 +01:00
|
|
|
if (method === 'GET' || method === 'HEAD') {
|
2010-03-24 05:31:44 +01:00
|
|
|
this.useChunkedEncodingByDefault = false;
|
2009-07-31 18:34:27 +02:00
|
|
|
} else {
|
2010-03-24 05:31:44 +01:00
|
|
|
this.useChunkedEncodingByDefault = true;
|
2009-07-31 18:34:27 +02:00
|
|
|
}
|
2011-01-20 11:41:16 +01:00
|
|
|
|
|
|
|
// By default keep-alive is off. This is the last message unless otherwise
|
|
|
|
// specified.
|
2010-05-27 02:59:55 +02:00
|
|
|
this._last = true;
|
2009-07-14 18:31:50 +02:00
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
this._storeHeader(method + ' ' + path + ' HTTP/1.1\r\n', headers);
|
2009-07-16 10:59:40 +02:00
|
|
|
}
|
2010-10-11 23:04:09 +02:00
|
|
|
util.inherits(ClientRequest, OutgoingMessage);
|
2010-11-29 16:41:08 +01:00
|
|
|
|
|
|
|
|
2009-11-04 12:37:52 +01:00
|
|
|
exports.ClientRequest = ClientRequest;
|
2009-07-14 18:31:50 +02:00
|
|
|
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2011-02-05 00:14:58 +01:00
|
|
|
ClientRequest.prototype.abort = function() {
|
|
|
|
if (this._queue) {
|
|
|
|
// queued for dispatch
|
|
|
|
assert(!this.connection);
|
|
|
|
var i = this._queue.indexOf(this);
|
|
|
|
this._queue.splice(i, 1);
|
|
|
|
|
|
|
|
} else if (this.connection) {
|
|
|
|
// in-progress
|
|
|
|
var c = this.connection;
|
|
|
|
this.detachSocket(c);
|
|
|
|
c.destroy();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// already complete.
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
function httpSocketSetup(socket) {
|
2010-05-27 02:59:55 +02:00
|
|
|
// NOTE: be sure not to use ondrain elsewhere in this file!
|
2010-12-02 03:07:20 +01:00
|
|
|
socket.ondrain = function() {
|
2011-01-20 11:41:16 +01:00
|
|
|
if (socket._httpMessage) {
|
|
|
|
socket._httpMessage.emit('drain');
|
|
|
|
}
|
2010-05-27 02:59:55 +02:00
|
|
|
};
|
2009-07-14 18:31:50 +02:00
|
|
|
}
|
2009-05-18 19:33:05 +02:00
|
|
|
|
2009-07-16 10:59:40 +02:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
function Server(requestListener) {
|
2010-08-27 22:58:52 +02:00
|
|
|
if (!(this instanceof Server)) return new Server(requestListener);
|
2010-10-26 07:04:39 +02:00
|
|
|
net.Server.call(this, { allowHalfOpen: true });
|
2010-05-02 03:01:06 +02:00
|
|
|
|
2010-10-26 07:04:39 +02:00
|
|
|
if (requestListener) {
|
2010-12-02 03:07:20 +01:00
|
|
|
this.addListener('request', requestListener);
|
2010-04-30 15:35:51 +02:00
|
|
|
}
|
2010-05-02 03:01:06 +02:00
|
|
|
|
2011-02-05 00:14:58 +01:00
|
|
|
// Similar option to this. Too lazy to write my own docs.
|
|
|
|
// http://www.squid-cache.org/Doc/config/half_closed_clients/
|
|
|
|
// http://wiki.squid-cache.org/SquidFaq/InnerWorkings#What_is_a_half-closed_filedescriptor.3F
|
|
|
|
this.httpAllowHalfOpen = false;
|
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
this.addListener('connection', connectionListener);
|
2010-03-20 03:22:04 +01:00
|
|
|
}
|
2010-10-11 23:04:09 +02:00
|
|
|
util.inherits(Server, net.Server);
|
2010-03-20 03:22:04 +01:00
|
|
|
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2010-03-20 03:22:04 +01:00
|
|
|
exports.Server = Server;
|
|
|
|
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
exports.createServer = function(requestListener) {
|
2010-03-20 03:22:04 +01:00
|
|
|
return new Server(requestListener);
|
2009-07-14 18:31:50 +02:00
|
|
|
};
|
2009-05-18 19:33:05 +02:00
|
|
|
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
function connectionListener(socket) {
|
2010-03-20 03:22:04 +01:00
|
|
|
var self = this;
|
2011-01-20 11:41:16 +01:00
|
|
|
var outgoing = [];
|
2011-02-05 00:14:58 +01:00
|
|
|
var incoming = [];
|
|
|
|
|
|
|
|
function abortIncoming() {
|
|
|
|
while (incoming.length) {
|
|
|
|
var req = incoming.shift();
|
|
|
|
req.emit('aborted');
|
|
|
|
}
|
|
|
|
// abort socket._httpMessage ?
|
|
|
|
}
|
2010-05-27 02:59:55 +02:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
debug('SERVER new http connection');
|
2010-05-27 02:59:55 +02:00
|
|
|
|
|
|
|
httpSocketSetup(socket);
|
2009-05-18 19:33:05 +02:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
socket.setTimeout(2 * 60 * 1000); // 2 minute timeout
|
|
|
|
socket.addListener('timeout', function() {
|
2010-05-12 20:42:01 +02:00
|
|
|
socket.destroy();
|
|
|
|
});
|
|
|
|
|
2010-04-12 18:27:32 +02:00
|
|
|
var parser = parsers.alloc();
|
|
|
|
parser.reinitialize('request');
|
|
|
|
parser.socket = socket;
|
2011-01-21 00:55:02 +01:00
|
|
|
parser.incoming = null;
|
2010-03-20 03:22:04 +01:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
socket.addListener('error', function(e) {
|
2010-04-30 19:28:18 +02:00
|
|
|
self.emit('clientError', e);
|
2010-04-23 02:22:03 +02:00
|
|
|
});
|
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
socket.ondata = function(d, start, end) {
|
2010-05-21 00:21:40 +02:00
|
|
|
var ret = parser.execute(d, start, end - start);
|
|
|
|
if (ret instanceof Error) {
|
2010-12-02 03:07:20 +01:00
|
|
|
debug('parse error');
|
2010-05-21 00:21:40 +02:00
|
|
|
socket.destroy(ret);
|
|
|
|
} else if (parser.incoming && parser.incoming.upgrade) {
|
|
|
|
var bytesParsed = ret;
|
2010-04-14 12:52:15 +02:00
|
|
|
socket.ondata = null;
|
|
|
|
socket.onend = null;
|
2010-05-02 03:01:06 +02:00
|
|
|
|
2010-04-30 15:33:11 +02:00
|
|
|
var req = parser.incoming;
|
2010-05-02 03:01:06 +02:00
|
|
|
|
|
|
|
// This is start + byteParsed + 1 due to the error of getting \n
|
|
|
|
// in the upgradeHead from the closing lines of the headers
|
|
|
|
var upgradeHead = d.slice(start + bytesParsed + 1, end);
|
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
if (self.listeners('upgrade').length) {
|
2010-05-02 03:01:06 +02:00
|
|
|
self.emit('upgrade', req, req.socket, upgradeHead);
|
2010-04-30 15:33:11 +02:00
|
|
|
} else {
|
2010-05-03 20:23:36 +02:00
|
|
|
// Got upgrade header, but have no handler.
|
|
|
|
socket.destroy();
|
2010-04-30 15:33:11 +02:00
|
|
|
}
|
2010-04-14 12:52:15 +02:00
|
|
|
}
|
2010-03-20 03:22:04 +01:00
|
|
|
};
|
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
socket.onend = function() {
|
2011-02-05 00:14:58 +01:00
|
|
|
var ret = parser.finish();
|
2010-05-21 00:21:40 +02:00
|
|
|
|
2011-02-05 00:14:58 +01:00
|
|
|
if (ret instanceof Error) {
|
|
|
|
debug('parse error');
|
|
|
|
socket.destroy(ret);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!self.httpAllowHalfOpen) {
|
|
|
|
abortIncoming();
|
|
|
|
socket.end();
|
|
|
|
} else if (outgoing.length) {
|
2011-01-20 11:41:16 +01:00
|
|
|
outgoing[outgoing.length - 1]._last = true;
|
|
|
|
} else if (socket._httpMessage) {
|
|
|
|
socket._httpMessage._last = true;
|
2010-05-27 02:59:55 +02:00
|
|
|
} else {
|
|
|
|
socket.end();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
socket.addListener('close', function() {
|
2011-02-05 00:14:58 +01:00
|
|
|
debug('server socket close');
|
2010-03-20 03:22:04 +01:00
|
|
|
// unref the parser for easy gc
|
2010-04-12 18:27:32 +02:00
|
|
|
parsers.free(parser);
|
2011-02-05 00:14:58 +01:00
|
|
|
|
|
|
|
abortIncoming();
|
2010-05-27 02:59:55 +02:00
|
|
|
});
|
2009-11-21 16:27:02 +01:00
|
|
|
|
2010-03-20 03:22:04 +01:00
|
|
|
// The following callback is issued after the headers have been read on a
|
|
|
|
// new message. In this callback we setup the response object and pass it
|
|
|
|
// to the user.
|
2010-12-02 03:07:20 +01:00
|
|
|
parser.onIncoming = function(req, shouldKeepAlive) {
|
2011-02-05 00:14:58 +01:00
|
|
|
incoming.push(req);
|
|
|
|
|
2009-12-05 08:37:46 +01:00
|
|
|
var res = new ServerResponse(req);
|
2010-05-27 02:59:55 +02:00
|
|
|
debug('server response shouldKeepAlive: ' + shouldKeepAlive);
|
2010-03-24 05:31:44 +01:00
|
|
|
res.shouldKeepAlive = shouldKeepAlive;
|
2011-01-25 02:50:10 +01:00
|
|
|
DTRACE_HTTP_SERVER_REQUEST(req, socket);
|
2011-01-20 11:41:16 +01:00
|
|
|
|
|
|
|
if (socket._httpMessage) {
|
|
|
|
// There are already pending outgoing res, append.
|
|
|
|
outgoing.push(res);
|
|
|
|
} else {
|
|
|
|
res.assignSocket(socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
// When we're finished writing the response, check if this is the last
|
|
|
|
// respose, if so destroy the socket.
|
|
|
|
res.on('finish', function() {
|
2011-02-05 00:14:58 +01:00
|
|
|
assert(incoming[0] === req);
|
|
|
|
incoming.shift();
|
|
|
|
|
2011-01-20 19:29:54 +01:00
|
|
|
res.detachSocket(socket);
|
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
if (res._last) {
|
|
|
|
socket.destroySoon();
|
|
|
|
} else {
|
|
|
|
// start sending the next message
|
|
|
|
var m = outgoing.shift();
|
|
|
|
if (m) {
|
|
|
|
m.assignSocket(socket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2009-08-26 22:03:19 +02:00
|
|
|
|
2010-11-29 16:41:08 +01:00
|
|
|
if ('expect' in req.headers &&
|
|
|
|
(req.httpVersionMajor == 1 && req.httpVersionMinor == 1) &&
|
|
|
|
continueExpression.test(req.headers['expect'])) {
|
|
|
|
res._expect_continue = true;
|
2010-12-02 03:07:20 +01:00
|
|
|
if (self.listeners('checkContinue').length) {
|
|
|
|
self.emit('checkContinue', req, res);
|
2010-11-29 16:41:08 +01:00
|
|
|
} else {
|
|
|
|
res.writeContinue();
|
2010-09-10 03:56:35 +02:00
|
|
|
self.emit('request', req, res);
|
2010-11-29 16:41:08 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.emit('request', req, res);
|
2010-09-10 03:56:35 +02:00
|
|
|
}
|
2010-05-26 04:24:30 +02:00
|
|
|
return false; // Not a HEAD response. (Not even a response!)
|
2010-03-20 03:22:04 +01:00
|
|
|
};
|
2009-07-14 18:31:50 +02:00
|
|
|
}
|
2011-01-02 10:13:56 +01:00
|
|
|
exports._connectionListener = connectionListener;
|
2009-06-26 12:51:27 +02:00
|
|
|
|
2009-06-26 18:30:55 +02:00
|
|
|
|
2011-02-05 11:35:25 +01:00
|
|
|
function Agent(options) {
|
|
|
|
this.options = options;
|
|
|
|
this.host = options.host;
|
|
|
|
this.port = options.port;
|
2010-06-15 19:35:49 +02:00
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
this.queue = [];
|
|
|
|
this.sockets = [];
|
2011-02-05 00:14:58 +01:00
|
|
|
this.maxSockets = Agent.defaultMaxSockets;
|
2011-01-20 11:41:16 +01:00
|
|
|
}
|
|
|
|
util.inherits(Agent, EventEmitter);
|
2011-01-21 22:12:35 +01:00
|
|
|
exports.Agent = Agent;
|
2010-03-20 03:22:04 +01:00
|
|
|
|
2010-09-22 14:51:53 +02:00
|
|
|
|
2011-02-05 00:14:58 +01:00
|
|
|
Agent.defaultMaxSockets = 5;
|
|
|
|
|
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
Agent.prototype.appendMessage = function(options) {
|
|
|
|
var self = this;
|
2010-09-22 14:51:53 +02:00
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
var req = new ClientRequest(options);
|
|
|
|
this.queue.push(req);
|
2011-02-05 00:14:58 +01:00
|
|
|
req._queue = this.queue;
|
2010-11-29 23:07:55 +01:00
|
|
|
|
2011-01-20 19:29:54 +01:00
|
|
|
/*
|
2011-01-20 11:41:16 +01:00
|
|
|
req.on('finish', function () {
|
|
|
|
self._cycle();
|
2009-06-26 18:29:57 +02:00
|
|
|
});
|
2011-01-20 19:29:54 +01:00
|
|
|
*/
|
2009-05-06 14:54:28 +02:00
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
this._cycle();
|
2011-01-20 19:29:54 +01:00
|
|
|
|
|
|
|
return req;
|
2011-01-20 11:41:16 +01:00
|
|
|
};
|
2009-08-26 22:03:19 +02:00
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
|
2011-01-21 01:25:24 +01:00
|
|
|
Agent.prototype._removeSocket = function(socket) {
|
|
|
|
var i = this.sockets.indexOf(socket);
|
|
|
|
if (i >= 0) this.sockets.splice(i, 1);
|
2011-01-24 19:55:30 +01:00
|
|
|
};
|
2011-01-21 01:25:24 +01:00
|
|
|
|
|
|
|
|
2011-01-20 19:29:54 +01:00
|
|
|
Agent.prototype._establishNewConnection = function() {
|
2011-01-20 11:41:16 +01:00
|
|
|
var self = this;
|
|
|
|
assert(this.sockets.length < this.maxSockets);
|
2010-05-27 02:59:55 +02:00
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
// Grab a new "socket". Depending on the implementation of _getConnection
|
|
|
|
// this could either be a raw TCP socket or a TLS stream.
|
2011-01-24 19:55:30 +01:00
|
|
|
var socket = this._getConnection(this.host, this.port, function() {
|
2011-01-22 02:39:45 +01:00
|
|
|
socket._httpConnecting = false;
|
2011-01-21 01:25:24 +01:00
|
|
|
self.emit('connect'); // mostly for the shim.
|
2011-01-24 19:55:30 +01:00
|
|
|
debug('Agent _getConnection callback');
|
2011-01-20 11:41:16 +01:00
|
|
|
self._cycle();
|
2009-06-26 18:29:57 +02:00
|
|
|
});
|
2010-03-20 03:22:04 +01:00
|
|
|
|
2011-01-22 02:39:45 +01:00
|
|
|
// Use this special mark so that we know if the socket is connecting.
|
|
|
|
// TODO: come up with a standard way of specifying that a stream is being
|
|
|
|
// connected across tls and net.
|
|
|
|
socket._httpConnecting = true;
|
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
this.sockets.push(socket);
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2011-01-20 19:29:54 +01:00
|
|
|
// Add a parser to the socket.
|
|
|
|
var parser = parsers.alloc();
|
|
|
|
parser.reinitialize('response');
|
|
|
|
parser.socket = socket;
|
2011-01-21 00:55:02 +01:00
|
|
|
parser.incoming = null;
|
|
|
|
|
|
|
|
socket.on('error', function(err) {
|
2011-01-24 19:55:30 +01:00
|
|
|
debug('AGENT SOCKET ERROR: ' + err.message);
|
|
|
|
var req;
|
2011-01-21 00:55:02 +01:00
|
|
|
if (socket._httpMessage) {
|
2011-01-24 19:55:30 +01:00
|
|
|
req = socket._httpMessage;
|
2011-01-21 00:55:02 +01:00
|
|
|
} else if (self.queue.length) {
|
2011-01-21 02:07:44 +01:00
|
|
|
req = self.queue.shift();
|
2011-02-05 00:14:58 +01:00
|
|
|
assert(req._queue === self.queue);
|
|
|
|
req._queue = null;
|
2011-01-21 00:55:02 +01:00
|
|
|
} else {
|
|
|
|
// No requests on queue? Where is the request
|
|
|
|
assert(0);
|
|
|
|
}
|
2011-01-21 02:07:44 +01:00
|
|
|
|
|
|
|
req.emit('error', err);
|
|
|
|
req._hadError = true; // hacky
|
2011-01-21 00:55:02 +01:00
|
|
|
});
|
2011-01-20 19:29:54 +01:00
|
|
|
|
|
|
|
socket.ondata = function(d, start, end) {
|
|
|
|
var ret = parser.execute(d, start, end - start);
|
|
|
|
if (ret instanceof Error) {
|
|
|
|
debug('parse error');
|
|
|
|
socket.destroy(ret);
|
|
|
|
} else if (parser.incoming && parser.incoming.upgrade) {
|
|
|
|
var bytesParsed = ret;
|
|
|
|
socket.ondata = null;
|
|
|
|
socket.onend = null;
|
|
|
|
|
|
|
|
var res = parser.incoming;
|
2011-01-21 02:07:44 +01:00
|
|
|
assert(socket._httpMessage);
|
|
|
|
socket._httpMessage.res = res;
|
2011-01-20 19:29:54 +01:00
|
|
|
|
|
|
|
// This is start + byteParsed + 1 due to the error of getting \n
|
|
|
|
// in the upgradeHead from the closing lines of the headers
|
|
|
|
var upgradeHead = d.slice(start + bytesParsed + 1, end);
|
|
|
|
|
2011-01-21 01:25:24 +01:00
|
|
|
// Make sure we don't try to send HTTP requests to it.
|
|
|
|
self._removeSocket(socket);
|
|
|
|
|
|
|
|
socket.on('end', function() {
|
|
|
|
self.emit('end');
|
|
|
|
});
|
|
|
|
|
|
|
|
// XXX free the parser?
|
|
|
|
|
2011-01-20 19:29:54 +01:00
|
|
|
if (self.listeners('upgrade').length) {
|
2011-01-21 01:25:24 +01:00
|
|
|
// Emit 'upgrade' on the Agent.
|
|
|
|
self.emit('upgrade', res, socket, upgradeHead);
|
2011-01-20 19:29:54 +01:00
|
|
|
} else {
|
|
|
|
// Got upgrade header, but have no handler.
|
|
|
|
socket.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
socket.onend = function() {
|
2011-01-21 01:25:24 +01:00
|
|
|
self.emit('end'); // mostly for the shim.
|
2011-01-20 19:29:54 +01:00
|
|
|
parser.finish();
|
|
|
|
socket.destroy();
|
|
|
|
};
|
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
// When the socket closes remove it from the list of available sockets.
|
|
|
|
socket.on('close', function() {
|
2011-01-21 02:07:44 +01:00
|
|
|
// This is really hacky: What if someone issues a request, the server
|
|
|
|
// accepts, but then terminates the connection. There is no parse error,
|
|
|
|
// there is no socket-level error. How does the user get informed?
|
|
|
|
// We check to see if the socket has a request, if so if it has a
|
|
|
|
// response (meaning that it emitted a 'response' event). If the socket
|
|
|
|
// has a request but no response and it never emitted an error event:
|
|
|
|
// THEN we need to trigger it manually.
|
|
|
|
// There must be a better way to do this.
|
|
|
|
if (socket._httpMessage &&
|
|
|
|
!socket._httpMessage.res &&
|
|
|
|
!socket._httpMessage._hadError) {
|
|
|
|
socket._httpMessage.emit('error', new Error('socket hang up'));
|
|
|
|
}
|
|
|
|
|
2011-01-21 01:25:24 +01:00
|
|
|
self._removeSocket(socket);
|
2011-01-20 19:29:54 +01:00
|
|
|
// unref the parser for easy gc
|
|
|
|
parsers.free(parser);
|
2011-01-21 23:53:31 +01:00
|
|
|
|
|
|
|
self._cycle();
|
2011-01-20 11:41:16 +01:00
|
|
|
});
|
2011-01-20 19:29:54 +01:00
|
|
|
|
|
|
|
parser.onIncoming = function(res, shouldKeepAlive) {
|
|
|
|
debug('AGENT incoming response!');
|
|
|
|
|
|
|
|
var req = socket._httpMessage;
|
|
|
|
assert(req);
|
|
|
|
|
2011-01-21 00:55:02 +01:00
|
|
|
req.res = res;
|
|
|
|
|
2011-01-20 19:29:54 +01:00
|
|
|
// Responses to HEAD requests are AWFUL. Ask Ryan.
|
|
|
|
// A major oversight in HTTP. Hence this nastiness.
|
|
|
|
var isHeadResponse = req.method == 'HEAD';
|
|
|
|
debug('AGENT isHeadResponse ' + isHeadResponse);
|
|
|
|
|
|
|
|
if (res.statusCode == 100) {
|
|
|
|
// restart the parser, as this is a continue message.
|
|
|
|
req.emit('continue');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.shouldKeepAlive && res.headers.connection === 'close') {
|
|
|
|
req.shouldKeepAlive = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.addListener('end', function() {
|
2011-01-21 23:53:31 +01:00
|
|
|
debug('AGENT request complete');
|
2011-01-20 19:29:54 +01:00
|
|
|
// For the moment we reconnect for every request. FIXME!
|
|
|
|
// All that should be required for keep-alive is to not reconnect,
|
|
|
|
// but outgoingFlush instead.
|
2011-01-21 23:53:31 +01:00
|
|
|
if (!req.shouldKeepAlive) {
|
|
|
|
debug('AGENT socket.end()');
|
|
|
|
socket.end();
|
|
|
|
} else {
|
|
|
|
debug('AGENT socket keep-alive');
|
|
|
|
}
|
2011-01-20 19:29:54 +01:00
|
|
|
|
|
|
|
req.detachSocket(socket);
|
2011-01-21 23:53:31 +01:00
|
|
|
|
|
|
|
assert(!socket._httpMessage);
|
|
|
|
|
2011-01-20 19:29:54 +01:00
|
|
|
self._cycle();
|
|
|
|
});
|
|
|
|
|
|
|
|
req.emit('response', res);
|
|
|
|
|
|
|
|
return isHeadResponse;
|
|
|
|
};
|
2011-01-20 11:41:16 +01:00
|
|
|
};
|
2010-03-20 03:22:04 +01:00
|
|
|
|
2010-11-29 16:41:08 +01:00
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
// Sub-classes can overwrite this method with e.g. something that supplies
|
|
|
|
// TLS streams.
|
|
|
|
Agent.prototype._getConnection = function(host, port, cb) {
|
2011-01-24 19:55:30 +01:00
|
|
|
debug('Agent connected!');
|
|
|
|
var c = net.createConnection(port, host);
|
2011-01-20 11:41:16 +01:00
|
|
|
c.on('connect', cb);
|
2010-03-20 03:22:04 +01:00
|
|
|
return c;
|
2010-05-27 02:59:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
// This method attempts to shuffle items along the queue into one of the
|
|
|
|
// waiting sockets. If a waiting socket cannot be found, it will
|
|
|
|
// start the process of establishing one.
|
|
|
|
Agent.prototype._cycle = function() {
|
2011-01-24 19:55:30 +01:00
|
|
|
debug('Agent _cycle sockets=' + this.sockets.length +
|
|
|
|
' queue=' + this.queue.length);
|
2011-01-22 02:48:14 +01:00
|
|
|
var self = this;
|
2011-01-20 19:29:54 +01:00
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
var first = this.queue[0];
|
|
|
|
if (!first) return;
|
|
|
|
|
2011-01-22 02:39:45 +01:00
|
|
|
|
|
|
|
var haveConnectingSocket = false;
|
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
// First try to find an available socket.
|
|
|
|
for (var i = 0; i < this.sockets.length; i++) {
|
|
|
|
var socket = this.sockets[i];
|
|
|
|
// If the socket doesn't already have a message it's sending out
|
|
|
|
// and the socket is available for writing...
|
|
|
|
if (!socket._httpMessage && (socket.writable && socket.readable)) {
|
2011-01-24 19:55:30 +01:00
|
|
|
debug('Agent found socket, shift');
|
2011-01-20 11:41:16 +01:00
|
|
|
// We found an available connection!
|
|
|
|
this.queue.shift(); // remove first from queue.
|
2011-02-05 00:14:58 +01:00
|
|
|
assert(first._queue === this.queue);
|
|
|
|
first._queue = null;
|
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
first.assignSocket(socket);
|
2011-01-22 02:48:14 +01:00
|
|
|
self._cycle(); // try to dispatch another
|
2011-01-20 11:41:16 +01:00
|
|
|
return;
|
2010-09-30 20:41:50 +02:00
|
|
|
}
|
2011-01-22 02:39:45 +01:00
|
|
|
|
|
|
|
if (socket._httpConnecting) haveConnectingSocket = true;
|
2010-05-27 02:59:55 +02:00
|
|
|
}
|
|
|
|
|
2011-01-22 02:39:45 +01:00
|
|
|
// If no sockets are connecting, and we have space for another we should
|
|
|
|
// be starting a new connection to handle this request.
|
|
|
|
if (!haveConnectingSocket && this.sockets.length < this.maxSockets) {
|
2011-01-20 11:41:16 +01:00
|
|
|
this._establishNewConnection();
|
2010-05-27 02:59:55 +02:00
|
|
|
}
|
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
// All sockets are filled and all sockets are busy.
|
2009-06-26 18:29:57 +02:00
|
|
|
};
|
2009-06-06 23:57:15 +02:00
|
|
|
|
2009-05-19 13:12:46 +02:00
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
// process-wide hash of agents.
|
|
|
|
// keys: "host:port" string
|
|
|
|
// values: instance of Agent
|
|
|
|
// That is, one agent remote host.
|
|
|
|
// TODO currently we never remove agents from this hash. This is a small
|
|
|
|
// memory leak. Have a 2 second timeout after a agent's sockets are to try
|
|
|
|
// to remove it?
|
2011-01-24 19:55:30 +01:00
|
|
|
var agents = {};
|
2010-02-20 01:26:48 +01:00
|
|
|
|
2009-09-15 22:41:25 +02:00
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
function getAgent(host, port) {
|
|
|
|
var id = host + ':' + port;
|
|
|
|
var agent = agents[id];
|
2010-05-05 06:35:46 +02:00
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
if (!agent) {
|
2011-02-05 11:35:25 +01:00
|
|
|
agent = agents[id] = new Agent({ host: host, port: port });
|
2009-09-30 11:54:14 +02:00
|
|
|
}
|
2010-05-05 06:35:46 +02:00
|
|
|
|
2011-01-20 11:41:16 +01:00
|
|
|
return agent;
|
|
|
|
}
|
2011-01-21 22:47:27 +01:00
|
|
|
exports.getAgent = getAgent;
|
2009-06-28 19:05:58 +02:00
|
|
|
|
2010-04-12 19:57:22 +02:00
|
|
|
|
2011-01-23 01:09:50 +01:00
|
|
|
exports._requestFromAgent = function(options, cb) {
|
|
|
|
var req = options.agent.appendMessage(options);
|
|
|
|
req.agent = options.agent;
|
2011-01-21 00:55:02 +01:00
|
|
|
if (cb) req.once('response', cb);
|
2011-01-20 11:41:16 +01:00
|
|
|
return req;
|
2009-06-22 13:12:47 +02:00
|
|
|
};
|
2011-01-20 11:41:16 +01:00
|
|
|
|
|
|
|
|
2011-01-21 22:12:35 +01:00
|
|
|
exports.request = function(options, cb) {
|
2011-01-23 01:09:50 +01:00
|
|
|
if (options.agent === undefined) {
|
|
|
|
options.agent = getAgent(options.host, options.port);
|
|
|
|
} else if (options.agent === false) {
|
2011-02-05 11:35:25 +01:00
|
|
|
options.agent = new Agent(options);
|
2011-01-23 01:09:50 +01:00
|
|
|
}
|
|
|
|
return exports._requestFromAgent(options, cb);
|
2011-01-21 22:12:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-01-20 20:21:42 +01:00
|
|
|
exports.get = function(options, cb) {
|
|
|
|
options.method = 'GET';
|
|
|
|
var req = exports.request(options, cb);
|
|
|
|
req.end();
|
2011-01-21 02:55:13 +01:00
|
|
|
return req;
|
2011-01-20 20:21:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-01-25 07:27:31 +01:00
|
|
|
// Legacy Interface:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function Client() {
|
|
|
|
if (!(this instanceof Client)) return new Client();
|
|
|
|
net.Stream.call(this, { allowHalfOpen: true });
|
2011-01-21 01:25:24 +01:00
|
|
|
var self = this;
|
|
|
|
|
2011-01-25 07:27:31 +01:00
|
|
|
// Possible states:
|
|
|
|
// - disconnected
|
|
|
|
// - connecting
|
|
|
|
// - connected
|
|
|
|
this._state = 'disconnected';
|
2011-01-21 01:25:24 +01:00
|
|
|
|
2011-01-25 07:27:31 +01:00
|
|
|
httpSocketSetup(self);
|
|
|
|
this._outgoing = [];
|
|
|
|
|
|
|
|
function onData(d, start, end) {
|
|
|
|
if (!self.parser) {
|
|
|
|
throw new Error('parser not initialized prior to Client.ondata call');
|
|
|
|
}
|
|
|
|
var ret = self.parser.execute(d, start, end - start);
|
|
|
|
if (ret instanceof Error) {
|
|
|
|
self.destroy(ret);
|
|
|
|
} else if (self.parser.incoming && self.parser.incoming.upgrade) {
|
|
|
|
var bytesParsed = ret;
|
|
|
|
self.ondata = null;
|
|
|
|
self.onend = null;
|
|
|
|
self._state = 'upgraded';
|
|
|
|
|
|
|
|
var res = self.parser.incoming;
|
|
|
|
|
|
|
|
if (self._httpMessage) {
|
|
|
|
self._httpMessage.detachSocket(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
var upgradeHead = d.slice(start + bytesParsed + 1, end);
|
2011-01-21 01:25:24 +01:00
|
|
|
|
2011-01-25 07:27:31 +01:00
|
|
|
if (self.listeners('upgrade').length) {
|
|
|
|
self.emit('upgrade', res, self, upgradeHead);
|
|
|
|
} else {
|
|
|
|
self.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.addListener('connect', function() {
|
|
|
|
debug('CLIENT connected');
|
|
|
|
|
|
|
|
self.ondata = onData;
|
|
|
|
self.onend = onEnd;
|
|
|
|
|
|
|
|
self._state = 'connected';
|
|
|
|
|
|
|
|
self._initParser();
|
|
|
|
|
|
|
|
self._cycle();
|
2011-01-21 01:25:24 +01:00
|
|
|
});
|
|
|
|
|
2011-01-25 07:27:31 +01:00
|
|
|
function onEnd() {
|
|
|
|
if (self.parser) self.parser.finish();
|
|
|
|
debug('CLIENT got end closing. state = ' + self._state);
|
|
|
|
self.end();
|
|
|
|
};
|
|
|
|
|
|
|
|
self.addListener('close', function(e) {
|
|
|
|
self._state = 'disconnected';
|
|
|
|
self._upgraded = false;
|
|
|
|
|
|
|
|
// Free the parser.
|
|
|
|
if (self.parser) {
|
|
|
|
parsers.free(self.parser);
|
|
|
|
self.parser = null;
|
2011-01-21 01:25:24 +01:00
|
|
|
}
|
2011-01-25 07:27:31 +01:00
|
|
|
|
|
|
|
if (e) return;
|
|
|
|
|
|
|
|
// If we have an http message, then drop it
|
|
|
|
var req = self._httpMessage;
|
|
|
|
if (req && !req.res) {
|
|
|
|
req.detachSocket(self);
|
|
|
|
self.emit('error', new Error('socket hang up'));
|
|
|
|
}
|
|
|
|
|
|
|
|
debug('CLIENT onClose. state = ' + self._state);
|
|
|
|
self._cycle();
|
2011-01-21 01:25:24 +01:00
|
|
|
});
|
2011-01-20 20:21:42 +01:00
|
|
|
}
|
2011-01-25 07:27:31 +01:00
|
|
|
util.inherits(Client, net.Stream);
|
|
|
|
|
|
|
|
|
|
|
|
exports.Client = Client;
|
|
|
|
|
|
|
|
|
|
|
|
exports.createClient = function(port, host) {
|
|
|
|
var c = new Client();
|
|
|
|
c.port = port;
|
|
|
|
c.host = host;
|
|
|
|
return c;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Client.prototype._initParser = function() {
|
|
|
|
var self = this;
|
|
|
|
if (!self.parser) self.parser = parsers.alloc();
|
|
|
|
self.parser.reinitialize('response');
|
|
|
|
self.parser.socket = self;
|
|
|
|
self.parser.onIncoming = function(res) {
|
|
|
|
debug('CLIENT incoming response!');
|
|
|
|
|
|
|
|
assert(self._httpMessage);
|
|
|
|
var req = self._httpMessage;
|
|
|
|
|
|
|
|
req.res = res;
|
|
|
|
|
|
|
|
// Responses to HEAD requests are AWFUL. Ask Ryan.
|
|
|
|
// A major oversight in HTTP. Hence this nastiness.
|
|
|
|
var isHeadResponse = req.method == 'HEAD';
|
|
|
|
debug('CLIENT isHeadResponse ' + isHeadResponse);
|
|
|
|
|
|
|
|
if (res.statusCode == 100) {
|
|
|
|
// restart the parser, as this is a continue message.
|
|
|
|
req.emit('continue');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.shouldKeepAlive && res.headers.connection === 'close') {
|
|
|
|
req.shouldKeepAlive = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.addListener('end', function() {
|
|
|
|
debug('CLIENT response complete disconnecting. state = ' + self._state);
|
|
|
|
|
|
|
|
if (!req.shouldKeepAlive) {
|
|
|
|
self.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
req.detachSocket(self);
|
|
|
|
assert(!self._httpMessage);
|
|
|
|
self._cycle();
|
|
|
|
});
|
|
|
|
|
|
|
|
req.emit('response', res);
|
|
|
|
|
|
|
|
return isHeadResponse;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2011-01-20 20:21:42 +01:00
|
|
|
|
2011-01-25 07:27:31 +01:00
|
|
|
Client.prototype._cycle = function() {
|
|
|
|
debug("Client _cycle");
|
|
|
|
if (this._upgraded) return;
|
|
|
|
|
|
|
|
switch (this._state) {
|
|
|
|
case 'connecting':
|
|
|
|
break;
|
2011-01-20 20:21:42 +01:00
|
|
|
|
2011-01-25 07:27:31 +01:00
|
|
|
case 'connected':
|
|
|
|
if (this.writable && this.readable) {
|
|
|
|
debug("Client _cycle shift()");
|
|
|
|
if (this._httpMessage) {
|
|
|
|
this._httpMessage._flush();
|
|
|
|
} else {
|
|
|
|
var req = this._outgoing.shift();
|
|
|
|
if (req) req.assignSocket(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'disconnected':
|
|
|
|
if (this._httpMessage || this._outgoing.length) {
|
|
|
|
this._ensureConnection();
|
|
|
|
}
|
|
|
|
break;
|
2011-01-21 01:25:24 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-01-25 07:27:31 +01:00
|
|
|
Client.prototype._ensureConnection = function() {
|
|
|
|
if (this._state == 'disconnected') {
|
|
|
|
debug('CLIENT reconnecting state = ' + this._state);
|
|
|
|
this.connect(this.port, this.host);
|
|
|
|
this._state = 'connecting';
|
2011-01-21 01:25:24 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-01-25 07:27:31 +01:00
|
|
|
Client.prototype.request = function(method, url, headers) {
|
|
|
|
if (typeof(url) != 'string') {
|
2011-01-20 20:21:42 +01:00
|
|
|
// assume method was omitted, shift arguments
|
2011-01-25 07:27:31 +01:00
|
|
|
headers = url;
|
|
|
|
url = method;
|
2011-01-20 20:21:42 +01:00
|
|
|
method = 'GET';
|
|
|
|
}
|
|
|
|
|
2011-01-25 07:27:31 +01:00
|
|
|
var self = this;
|
|
|
|
|
2011-01-20 20:21:42 +01:00
|
|
|
var options = {
|
2011-01-25 07:27:31 +01:00
|
|
|
method: method || 'GET',
|
|
|
|
path: url || '/',
|
|
|
|
headers: headers
|
2011-01-20 20:21:42 +01:00
|
|
|
};
|
|
|
|
|
2011-01-25 07:27:31 +01:00
|
|
|
var req = new ClientRequest(options);
|
|
|
|
this._outgoing.push(req);
|
|
|
|
this._cycle();
|
2011-01-21 00:55:02 +01:00
|
|
|
|
|
|
|
return req;
|
2011-01-20 20:21:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.cat = function(url, encoding_, headers_) {
|
|
|
|
var encoding = 'utf8',
|
|
|
|
headers = {},
|
|
|
|
callback = null;
|
|
|
|
|
|
|
|
// parse the arguments for the various options... very ugly
|
|
|
|
if (typeof(arguments[1]) == 'string') {
|
|
|
|
encoding = arguments[1];
|
|
|
|
if (typeof(arguments[2]) == 'object') {
|
|
|
|
headers = arguments[2];
|
|
|
|
if (typeof(arguments[3]) == 'function') callback = arguments[3];
|
|
|
|
} else {
|
|
|
|
if (typeof(arguments[2]) == 'function') callback = arguments[2];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// didn't specify encoding
|
|
|
|
if (typeof(arguments[1]) == 'object') {
|
|
|
|
headers = arguments[1];
|
|
|
|
callback = arguments[2];
|
|
|
|
} else {
|
|
|
|
callback = arguments[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var url = require('url').parse(url);
|
|
|
|
|
2011-01-25 07:27:31 +01:00
|
|
|
var hasHost = false;
|
|
|
|
if (Array.isArray(headers)) {
|
|
|
|
for (var i = 0, l = headers.length; i < l; i++) {
|
|
|
|
if (headers[i][0].toLowerCase() === 'host') {
|
|
|
|
hasHost = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (typeof headers === 'Object') {
|
|
|
|
var keys = Object.keys(headers);
|
|
|
|
for (var i = 0, l = keys.length; i < l; i++) {
|
|
|
|
var key = keys[i];
|
|
|
|
if (key.toLowerCase() == 'host') {
|
|
|
|
hasHost = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!hasHost) headers['Host'] = url.hostname;
|
2011-01-20 20:21:42 +01:00
|
|
|
|
|
|
|
var content = '';
|
2011-01-25 07:27:31 +01:00
|
|
|
|
|
|
|
var client = exports.createClient(url.port || 80, url.hostname);
|
|
|
|
var req = client.request((url.pathname || '/') +
|
|
|
|
(url.search || '') +
|
|
|
|
(url.hash || ''),
|
|
|
|
headers);
|
|
|
|
|
|
|
|
if (url.protocol == 'https:') {
|
|
|
|
client.https = true;
|
|
|
|
}
|
|
|
|
|
2011-01-20 20:21:42 +01:00
|
|
|
var callbackSent = false;
|
|
|
|
|
2011-01-25 07:27:31 +01:00
|
|
|
req.addListener('response', function(res) {
|
2011-01-20 20:21:42 +01:00
|
|
|
if (res.statusCode < 200 || res.statusCode >= 300) {
|
|
|
|
if (callback && !callbackSent) {
|
|
|
|
callback(res.statusCode);
|
|
|
|
callbackSent = true;
|
|
|
|
}
|
2011-01-25 07:27:31 +01:00
|
|
|
client.end();
|
2011-01-20 20:21:42 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
res.setEncoding(encoding);
|
|
|
|
res.addListener('data', function(chunk) { content += chunk; });
|
|
|
|
res.addListener('end', function() {
|
|
|
|
if (callback && !callbackSent) {
|
|
|
|
callback(null, content);
|
|
|
|
callbackSent = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2011-01-25 07:27:31 +01:00
|
|
|
client.addListener('error', function(err) {
|
2011-01-20 20:21:42 +01:00
|
|
|
if (callback && !callbackSent) {
|
2011-01-21 02:07:44 +01:00
|
|
|
callback(err);
|
2011-01-20 20:21:42 +01:00
|
|
|
callbackSent = true;
|
|
|
|
}
|
|
|
|
});
|
2011-01-25 07:27:31 +01:00
|
|
|
|
|
|
|
client.addListener('close', function() {
|
|
|
|
if (callback && !callbackSent) {
|
|
|
|
callback(new Error('Connection closed unexpectedly'));
|
|
|
|
callbackSent = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
req.end();
|
2011-01-20 20:21:42 +01:00
|
|
|
};
|