0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 07:27:22 +01:00
nodejs/lib/http.js

832 lines
22 KiB
JavaScript
Raw Normal View History

var debugLevel = 0;
if ("NODE_DEBUG" in process.env) debugLevel = 1;
function debug (x) {
if (debugLevel > 0) {
process.binding('stdio').writeError(x + "\n");
}
}
var sys = require('sys');
2010-03-20 03:22:04 +01:00
var net = require('net');
var events = require('events');
var Buffer = require('buffer').Buffer;
2009-09-28 12:36:36 +02:00
var FreeList = require('freelist').FreeList;
2010-03-20 03:22:04 +01:00
var HTTPParser = process.binding('http_parser').HTTPParser;
var parsers = new FreeList('parsers', 1000, function () {
var parser = new HTTPParser('request');
2010-03-20 03:22:04 +01:00
parser.onMessageBegin = function () {
parser.incoming = new IncomingMessage(parser.socket);
parser.field = null;
parser.value = null;
};
// Only servers will get URL events.
parser.onURL = function (b, start, len) {
var slice = b.toString('ascii', start, start+len);
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
parser.onHeaderField = function (b, start, len) {
var slice = b.toString('ascii', start, start+len).toLowerCase();
if (parser.value) {
parser.incoming._addHeaderLine(parser.field, parser.value);
2010-03-20 03:22:04 +01:00
parser.field = null;
parser.value = null;
}
if (parser.field) {
parser.field += slice;
} else {
parser.field = slice;
}
};
2010-03-20 03:22:04 +01:00
parser.onHeaderValue = function (b, start, len) {
var slice = b.toString('ascii', start, start+len);
if (parser.value) {
parser.value += slice;
} else {
parser.value = slice;
}
};
2010-03-20 03:22:04 +01:00
parser.onHeadersComplete = function (info) {
if (parser.field && parser.value) {
parser.incoming._addHeaderLine(parser.field, parser.value);
}
2010-03-20 03:22:04 +01:00
parser.incoming.httpVersionMajor = info.versionMajor;
parser.incoming.httpVersionMinor = info.versionMinor;
parser.incoming.httpVersion = info.versionMajor
+ '.'
+ info.versionMinor ;
2010-03-20 03:22:04 +01:00
if (info.method) {
// server only
parser.incoming.method = info.method;
} else {
// client only
parser.incoming.statusCode = info.statusCode;
}
2010-03-20 03:22:04 +01:00
parser.incoming.upgrade = info.upgrade;
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
parser.onIncoming(parser.incoming, info.shouldKeepAlive);
}
};
2010-03-20 03:22:04 +01:00
parser.onBody = function (b, start, len) {
// TODO body encoding?
var enc = parser.incoming._encoding;
if (!enc) {
parser.incoming.emit('data', b.slice(start, start+len));
} else {
var string = b.toString(enc, start, start+len);
parser.incoming.emit('data', string);
}
};
2010-03-20 03:22:04 +01:00
parser.onMessageComplete = function () {
if (!parser.incoming.upgrade) {
// For upgraded connections, also emit this after parser.execute
parser.incoming.emit("end");
}
};
2010-03-20 03:22:04 +01:00
return parser;
});
2010-03-15 21:48:03 +01:00
2009-09-28 12:36:36 +02:00
var CRLF = "\r\n";
var STATUS_CODES = exports.STATUS_CODES = {
2009-07-01 00:49:56 +02:00
100 : 'Continue',
101 : 'Switching Protocols',
200 : 'OK',
201 : 'Created',
202 : 'Accepted',
203 : 'Non-Authoritative Information',
204 : 'No Content',
205 : 'Reset Content',
206 : 'Partial Content',
300 : 'Multiple Choices',
301 : 'Moved Permanently',
302 : 'Moved Temporarily',
303 : 'See Other',
304 : 'Not Modified',
305 : 'Use Proxy',
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',
500 : 'Internal Server Error',
501 : 'Not Implemented',
502 : 'Bad Gateway',
503 : 'Service Unavailable',
504 : 'Gateway Time-out',
505 : 'HTTP Version not supported'
};
2009-05-11 19:08:29 +02: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;
2009-06-26 18:29:57 +02:00
/* Abstract base class for ServerRequest and ClientResponse. */
2010-03-20 03:22:04 +01:00
function IncomingMessage (socket) {
events.EventEmitter.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;
this.httpVersion = null;
this.headers = {};
2009-07-14 18:31:50 +02:00
2009-08-26 22:03:19 +02:00
// request (server) only
this.url = "";
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
}
sys.inherits(IncomingMessage, events.EventEmitter);
exports.IncomingMessage = IncomingMessage;
2009-10-05 14:51:41 +02:00
IncomingMessage.prototype._parseQueryString = function () {
throw new Error("_parseQueryString is deprecated. Use require(\"querystring\") to parse query strings.\n");
2009-10-05 14:51:41 +02:00
};
IncomingMessage.prototype.setBodyEncoding = function (enc) {
2010-03-20 03:22:04 +01:00
// TODO deprecation message?
this.setEncoding(enc);
};
IncomingMessage.prototype.setEncoding = function (enc) {
// TODO check values, error out on bad, and deprecation message?
this._encoding = enc.toLowerCase();
2009-06-26 18:29:57 +02:00
};
IncomingMessage.prototype.pause = function () {
2010-03-20 03:22:04 +01:00
this.socket.pause();
};
IncomingMessage.prototype.resume = function () {
2010-03-20 03:22:04 +01:00
this.socket.resume();
};
IncomingMessage.prototype._addHeaderLine = function (field, value) {
if (field in this.headers) {
2009-08-26 22:03:19 +02:00
// TODO Certain headers like 'Content-Type' should not be concatinated.
// See https://www.google.com/reader/view/?tab=my#overview-page
this.headers[field] += ", " + value;
} else {
this.headers[field] = value;
}
};
2009-07-12 11:48:37 +02:00
2010-03-20 03:22:04 +01:00
function OutgoingMessage (socket) {
events.EventEmitter.call(this, socket);
2010-03-03 22:06:19 +01: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 18:31:50 +02:00
this.output = [];
this.outputEncodings = [];
2009-06-26 18:29:57 +02:00
2009-07-14 18:31:50 +02:00
this.closeOnFinish = false;
2010-03-24 05:31:44 +01:00
this.chunkedEncoding = false;
this.shouldKeepAlive = true;
this.useChunkedEncodingByDefault = true;
this.flushing = false;
this.headWritten = false;
this._hasBody = true;
2009-07-14 18:31:50 +02:00
this.finished = false;
2009-07-16 10:59:40 +02:00
}
sys.inherits(OutgoingMessage, events.EventEmitter);
exports.OutgoingMessage = OutgoingMessage;
OutgoingMessage.prototype._send = function (data, encoding) {
var length = this.output.length;
2010-03-20 03:22:04 +01:00
if (length === 0 || typeof data != 'string') {
this.output.push(data);
encoding = encoding || "ascii";
this.outputEncodings.push(encoding);
return;
}
var lastEncoding = this.outputEncodings[length-1];
var lastData = this.output[length-1];
if ((lastEncoding === encoding) ||
(!encoding && data.constructor === lastData.constructor)) {
2010-03-20 03:22:04 +01:00
this.output[length-1] = lastData + data;
return;
}
2009-07-16 10:59:40 +02:00
this.output.push(data);
encoding = encoding || "ascii";
this.outputEncodings.push(encoding);
2009-07-14 18:31:50 +02:00
};
2010-03-24 05:31:44 +01:00
OutgoingMessage.prototype.sendHeaderLines = function (firstLine, headers) {
var sentConnectionHeader = false;
var sentContentLengthHeader = false;
var sentTransferEncodingHeader = false;
2010-03-24 05:31:44 +01:00
// firstLine in the case of request is: "GET /index.html HTTP/1.1\r\n"
2009-07-14 18:31:50 +02:00
// 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;
var field, value;
2009-06-26 18:29:57 +02:00
if (headers) {
var keys = Object.keys(headers);
var isArray = (headers instanceof Array);
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];
}
2009-08-26 22:03:19 +02:00
messageHeader += field + ": " + value + CRLF;
2009-06-26 18:29:57 +02:00
if (connectionExpression.test(field)) {
sentConnectionHeader = true;
if (closeExpression.test(value)) this.closeOnFinish = true;
2009-07-14 18:31:50 +02:00
} else if (transferEncodingExpression.test(field)) {
sentTransferEncodingHeader = true;
if (chunkExpression.test(value)) this.chunkedEncoding = true;
2009-07-14 18:31:50 +02:00
} else if (contentLengthExpression.test(field)) {
sentContentLengthHeader = true;
2009-07-14 18:31:50 +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)) {
messageHeader += "Connection: keep-alive\r\n";
2009-06-26 18:29:57 +02:00
} else {
2009-07-14 18:31:50 +02:00
this.closeOnFinish = true;
2010-03-24 05:31:44 +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) {
if (this._hasBody) {
if (this.useChunkedEncodingByDefault) {
messageHeader += "Transfer-Encoding: chunked\r\n";
this.chunkedEncoding = true;
} else {
this.closeOnFinish = true;
}
} else {
// Make sure we don't end the 0\r\n\r\n at the end of the message.
this.chunkedEncoding = false;
}
2009-07-14 18:31:50 +02:00
}
2010-03-24 05:31:44 +01:00
messageHeader += CRLF;
2009-07-14 18:31:50 +02:00
2010-03-24 05:31:44 +01:00
this._send(messageHeader);
// wait until the first body chunk, or close(), is sent to flush.
2009-07-14 18:31:50 +02:00
};
OutgoingMessage.prototype.sendBody = function () {
2010-05-01 23:45:14 +02:00
throw new Error("sendBody() has been renamed to write(). ");
};
OutgoingMessage.prototype.write = function (chunk, encoding) {
if ( (this instanceof ServerResponse) && !this.headWritten) {
throw new Error("writeHead() must be called before write()")
}
if (!this._hasBody) {
throw new Error("This type of response MUST NOT have a body.");
}
if (typeof chunk !== "string"
&& !(chunk instanceof Buffer)
&& !Array.isArray(chunk)) {
throw new TypeError("first argument must be a string, Array, or Buffer");
}
2010-01-19 21:20:26 +01:00
encoding = encoding || "ascii";
2010-03-24 05:31:44 +01:00
if (this.chunkedEncoding) {
2010-03-20 03:22:04 +01:00
if (typeof chunk == 'string') {
this._send(process._byteLength(chunk, encoding).toString(16));
} else {
this._send(chunk.length.toString(16));
}
this._send(CRLF);
this._send(chunk, encoding);
this._send(CRLF);
2009-07-14 18:31:50 +02:00
} else {
this._send(chunk, encoding);
2009-07-14 18:31:50 +02:00
}
2009-06-26 18:29:57 +02:00
if (this.flushing) {
this.flush();
} else {
this.flushing = true;
}
2009-07-14 18:31:50 +02:00
};
OutgoingMessage.prototype.flush = function () {
this.emit("flush");
};
OutgoingMessage.prototype.finish = function () {
throw new Error("finish() has been renamed to close().");
};
var closeWarning;
2010-04-06 01:50:05 +02:00
OutgoingMessage.prototype.close = function (data, encoding) {
if (!closeWarning) {
closeWarning = "OutgoingMessage.prototype.close has been renamed to end()";
sys.error(closeWarning);
}
return this.end(data, encoding);
};
OutgoingMessage.prototype.end = function (data, encoding) {
2010-04-06 01:50:05 +02:00
if (data) this.write(data, encoding);
2010-03-24 05:31:44 +01:00
if (this.chunkedEncoding) this._send("0\r\n\r\n"); // last chunk
2009-07-14 18:31:50 +02:00
this.finished = true;
this.flush();
};
function ServerResponse (req) {
2010-03-20 03:22:04 +01:00
OutgoingMessage.call(this, req.socket);
2009-07-14 18:31:50 +02:00
if (req.httpVersionMajor < 1 || req.httpVersionMinor < 1) {
2010-03-24 05:31:44 +01:00
this.useChunkedEncodingByDefault = false;
this.shouldKeepAlive = false;
}
2009-07-16 10:59:40 +02:00
}
sys.inherits(ServerResponse, OutgoingMessage);
exports.ServerResponse = ServerResponse;
2009-07-14 18:31:50 +02:00
2010-02-25 21:54:48 +01:00
ServerResponse.prototype.writeHead = function (statusCode) {
var reasonPhrase, headers, headerIndex;
if (typeof arguments[1] == 'string') {
reasonPhrase = arguments[1];
headerIndex = 2;
} else {
reasonPhrase = STATUS_CODES[statusCode] || "unknown";
headerIndex = 1;
}
if (typeof arguments[headerIndex] == 'object') {
headers = arguments[headerIndex];
} else {
headers = {};
}
2010-03-24 05:31:44 +01:00
var statusLine = "HTTP/1.1 " + statusCode.toString() + " "
+ reasonPhrase + CRLF;
if (statusCode === 204 || statusCode === 304) {
// 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.
this._hasBody = false;
}
2010-03-24 05:31:44 +01:00
this.sendHeaderLines(statusLine, headers);
this.headWritten = true;
2009-07-14 18:31:50 +02:00
};
2010-02-25 21:54:48 +01:00
// TODO eventually remove sendHeader(), writeHeader()
ServerResponse.prototype.sendHeader = ServerResponse.prototype.writeHead;
ServerResponse.prototype.writeHeader = ServerResponse.prototype.writeHead;
2009-07-14 18:31:50 +02:00
2010-03-20 03:22:04 +01:00
function ClientRequest (socket, method, url, headers) {
OutgoingMessage.call(this, socket);
2009-07-14 18:31:50 +02:00
2010-03-24 05:31:44 +01:00
this.shouldKeepAlive = false;
if (method === "GET" || method === "HEAD") {
2010-03-24 05:31:44 +01:00
this.useChunkedEncodingByDefault = false;
} else {
2010-03-24 05:31:44 +01:00
this.useChunkedEncodingByDefault = true;
}
2009-07-14 18:31:50 +02:00
this.closeOnFinish = true;
this.sendHeaderLines(method + " " + url + " HTTP/1.1\r\n", headers);
2009-07-16 10:59:40 +02:00
}
sys.inherits(ClientRequest, OutgoingMessage);
exports.ClientRequest = ClientRequest;
2009-07-14 18:31:50 +02:00
ClientRequest.prototype.finish = function () {
throw new Error( "finish() has been renamed to end() and no longer takes "
+ "a response handler as an argument. Manually add a 'response' listener "
+ "to the request object."
);
};
var clientRequestCloseWarning;
ClientRequest.prototype.close = function () {
if (!clientRequestCloseWarning) {
clientRequestCloseWarning = "Warning: ClientRequest.prototype.close has been renamed to end()";
sys.error(clientRequestCloseWarning);
}
if (arguments.length > 0) {
throw new Error( "ClientRequest.prototype.end does not take any arguments. "
+ "Add a response listener manually to the request object."
);
}
return this.end();
};
ClientRequest.prototype.end = function () {
if (arguments.length > 0) {
throw new Error( "ClientRequest.prototype.end does not take any arguments. "
+ "Add a response listener manually to the request object."
);
}
OutgoingMessage.prototype.end.call(this);
2009-07-14 18:31:50 +02:00
};
2010-03-20 03:22:04 +01:00
/* Returns true if the message queue is finished and the socket
2009-07-14 18:31:50 +02:00
* should be closed. */
2010-03-20 03:22:04 +01:00
function flushMessageQueue (socket, queue) {
2009-07-14 18:31:50 +02:00
while (queue[0]) {
var message = queue[0];
2009-07-14 18:31:50 +02:00
while (message.output.length > 0) {
2010-03-20 03:22:04 +01:00
if (!socket.writable) return true;
var data = message.output.shift();
var encoding = message.outputEncodings.shift();
2010-03-20 03:22:04 +01:00
socket.write(data, encoding);
}
2009-08-26 22:03:19 +02:00
2009-07-14 18:31:50 +02:00
if (!message.finished) break;
2009-07-14 18:31:50 +02:00
message.emit("sent");
queue.shift();
2009-07-14 18:31:50 +02:00
if (message.closeOnFinish) return true;
}
return false;
}
2009-07-16 10:59:40 +02:00
2010-03-20 03:22:04 +01:00
function Server (requestListener) {
net.Server.call(this);
if(requestListener){
this.addListener("request", requestListener);
}
2010-03-20 03:22:04 +01:00
this.addListener("connection", connectionListener);
}
sys.inherits(Server, net.Server);
2010-04-12 21:36:46 +02:00
Server.prototype.setSecure = function (credentials) {
this.secure = true;
this.credentials = credentials;
}
2010-03-20 03:22:04 +01:00
exports.Server = Server;
exports.createServer = function (requestListener) {
return new Server(requestListener);
2009-07-14 18:31:50 +02:00
};
2010-03-20 03:22:04 +01:00
function connectionListener (socket) {
var self = this;
// An array of responses for each socket. In pipelined connections
2009-07-14 18:31:50 +02:00
// we need to keep track of the order they were sent.
var responses = [];
var parser = parsers.alloc();
parser.reinitialize('request');
parser.socket = socket;
2010-03-20 03:22:04 +01:00
2010-04-12 21:36:46 +02:00
if (self.secure) {
socket.setSecure(self.credentials);
2010-04-12 21:36:46 +02:00
}
socket.addListener('error', function (e) {
2010-04-30 19:28:18 +02:00
self.emit('clientError', e);
});
2010-03-20 03:22:04 +01:00
socket.ondata = function (d, start, end) {
var bytesParsed = parser.execute(d, start, end - start);
if (parser.incoming && parser.incoming.upgrade) {
socket.ondata = null;
socket.onend = null;
var req = parser.incoming;
// 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-05-03 20:23:36 +02:00
if (self.listeners("upgrade").length) {
self.emit('upgrade', req, req.socket, upgradeHead);
} else {
2010-05-03 20:23:36 +02:00
// Got upgrade header, but have no handler.
socket.destroy();
}
}
2010-03-20 03:22:04 +01:00
};
socket.onend = function () {
parser.finish();
// unref the parser for easy gc
parsers.free(parser);
2009-07-14 18:31:50 +02:00
if (responses.length == 0) {
socket.end();
2009-07-14 18:31:50 +02:00
} else {
responses[responses.length-1].closeOnFinish = true;
2009-07-01 00:49:56 +02:00
}
2010-03-20 03:22:04 +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-03-24 05:31:44 +01:00
parser.onIncoming = function (req, shouldKeepAlive) {
var res = new ServerResponse(req);
2010-03-20 03:22:04 +01:00
2010-03-24 05:31:44 +01:00
res.shouldKeepAlive = shouldKeepAlive;
2010-03-20 03:22:04 +01:00
res.addListener('flush', function () {
if (flushMessageQueue(socket, responses)) {
socket.end();
}
});
2009-07-14 18:31:50 +02:00
responses.push(res);
2009-08-26 22:03:19 +02:00
2010-03-20 03:22:04 +01:00
self.emit('request', req, res);
};
2009-07-14 18:31:50 +02:00
}
2010-03-20 03:22:04 +01:00
function Client ( ) {
net.Stream.call(this);
var self = this;
2009-07-14 18:31:50 +02:00
var requests = [];
var currentRequest;
var parser = parsers.alloc();
parser.reinitialize('response');
2010-03-20 03:22:04 +01:00
parser.socket = this;
2010-03-20 03:22:04 +01:00
self._reconnect = function () {
if (self.readyState != "opening") {
debug("HTTP CLIENT: reconnecting readyState = " + self.readyState);
2010-03-20 03:22:04 +01:00
self.connect(self.port, self.host);
}
};
2010-03-20 03:22:04 +01:00
self._pushRequest = function (req) {
2009-07-14 18:31:50 +02:00
req.addListener("flush", function () {
2010-03-20 03:22:04 +01:00
if (self.readyState == "closed") {
debug("HTTP CLIENT request flush. reconnect. readyState = " + self.readyState);
2010-03-20 03:22:04 +01:00
self._reconnect();
2009-07-14 18:31:50 +02:00
return;
}
2010-03-20 03:22:04 +01:00
debug("self flush readyState = " + self.readyState);
2010-03-20 03:22:04 +01:00
if (req == currentRequest) flushMessageQueue(self, [req]);
2009-07-14 18:31:50 +02:00
});
requests.push(req);
};
self.ondata = function (d, start, end) {
var bytesParsed = parser.execute(d, start, end - start);
if (parser.incoming && parser.incoming.upgrade) {
var upgradeHead = d.slice(start + bytesParsed, end - start);
parser.incoming.upgradeHead = upgradeHead;
currentRequest.emit("response", parser.incoming);
parser.incoming.emit('end');
self.ondata = null;
self.onend = null
}
2010-03-20 03:22:04 +01:00
};
self.addListener("connect", function () {
2010-04-12 19:57:22 +02:00
if (this.https) {
2010-05-05 06:35:46 +02:00
this.setSecure(this.credentials);
2010-04-12 19:57:22 +02:00
} else {
parser.reinitialize('response');
debug('requests: ' + sys.inspect(requests));
currentRequest = requests.shift()
currentRequest.flush();
}
});
self.addListener("secure", function () {
2010-03-20 03:22:04 +01:00
parser.reinitialize('response');
debug('requests: ' + sys.inspect(requests));
2010-03-20 03:22:04 +01:00
currentRequest = requests.shift()
currentRequest.flush();
2009-06-26 18:29:57 +02:00
});
self.onend = function () {
2010-03-20 03:22:04 +01:00
parser.finish();
debug("self got end closing. readyState = " + self.readyState);
self.end();
};
2010-03-24 05:31:44 +01:00
self.addListener("close", function (e) {
if (e) return;
2009-08-26 22:03:19 +02:00
debug("HTTP CLIENT onClose. readyState = " + self.readyState);
2009-07-14 18:31:50 +02:00
2009-06-26 18:29:57 +02:00
// If there are more requests to handle, reconnect.
if (requests.length > 0) {
2010-03-20 03:22:04 +01:00
self._reconnect();
2010-04-13 01:34:39 +02:00
} else {
parsers.free(parser);
2009-06-26 18:29:57 +02:00
}
});
2010-03-20 03:22:04 +01:00
parser.onIncoming = function (res) {
debug("incoming response!");
res.addListener('end', function ( ) {
debug("request complete disconnecting. readyState = " + self.readyState);
self.end();
2009-07-14 18:31:50 +02:00
});
currentRequest.emit("response", res);
2010-03-20 03:22:04 +01:00
};
2009-06-26 18:29:57 +02:00
};
2010-03-20 03:22:04 +01:00
sys.inherits(Client, net.Stream);
exports.Client = Client;
2010-04-12 19:57:22 +02:00
exports.createClient = function (port, host, https, credentials) {
2010-03-20 03:22:04 +01:00
var c = new Client;
c.port = port;
c.host = host;
2010-04-12 19:57:22 +02:00
c.https = https;
c.credentials = credentials;
2010-03-20 03:22:04 +01:00
return c;
}
Client.prototype.get = function () {
throw new Error("client.get(...) is now client.request('GET', ...)");
2009-06-26 18:29:57 +02:00
};
2010-03-20 03:22:04 +01:00
Client.prototype.head = function () {
throw new Error("client.head(...) is now client.request('HEAD', ...)");
2009-06-26 18:29:57 +02:00
};
2010-03-20 03:22:04 +01:00
Client.prototype.post = function () {
throw new Error("client.post(...) is now client.request('POST', ...)");
2009-06-26 18:29:57 +02:00
};
2010-03-20 03:22:04 +01:00
Client.prototype.del = function () {
throw new Error("client.del(...) is now client.request('DELETE', ...)");
2009-06-26 18:29:57 +02:00
};
2010-03-20 03:22:04 +01:00
Client.prototype.put = function () {
throw new Error("client.put(...) is now client.request('PUT', ...)");
};
2010-03-20 03:22:04 +01:00
Client.prototype.request = function (method, url, headers) {
if (typeof(url) != "string") { // assume method was omitted, shift arguments
headers = url;
url = method;
method = null;
}
2010-03-03 22:06:19 +01:00
var req = new ClientRequest(this, method || "GET", url, headers);
2009-07-14 18:31:50 +02:00
this._pushRequest(req);
return req;
2009-06-26 18:29:57 +02:00
};
2009-05-19 13:12:46 +02:00
2010-02-20 01:26:48 +01:00
exports.cat = function (url, encoding_, headers_) {
2010-03-20 03:22:04 +01:00
var encoding = 'utf8',
2010-02-20 01:26:48 +01:00
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);
2010-05-05 06:35:46 +02:00
2009-12-12 00:46:32 +01:00
var hasHost = false;
if (headers instanceof Array) {
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;
}
2010-02-20 01:26:48 +01:00
}
}
2010-02-20 01:26:48 +01:00
if (!hasHost) headers["Host"] = url.hostname;
var content = "";
2010-05-05 06:35:46 +02:00
var client = exports.createClient(url.port || 80, url.hostname);
var req = client.request((url.pathname || "/")+(url.search || "")+(url.hash || ""), headers);
2009-06-28 19:05:58 +02:00
2010-04-12 19:57:22 +02:00
if (url.protocol=="https:") {
client.https = true;
}
req.addListener('response', function (res) {
2009-06-28 19:05:58 +02:00
if (res.statusCode < 200 || res.statusCode >= 300) {
2010-02-20 01:26:48 +01:00
if (callback) callback(res.statusCode);
client.end();
2009-06-28 19:05:58 +02:00
return;
}
2009-06-22 13:12:47 +02:00
res.setBodyEncoding(encoding);
res.addListener('data', function (chunk) { content += chunk; });
res.addListener('end', function () {
2010-02-20 01:26:48 +01:00
if (callback) callback(null, content);
2009-06-27 20:40:43 +02:00
});
2009-06-22 13:12:47 +02:00
});
2009-06-28 19:05:58 +02:00
2010-02-20 01:26:48 +01:00
client.addListener("error", function (err) {
if (callback) callback(err);
});
req.end();
2009-06-22 13:12:47 +02:00
};