You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

715 lines
18 KiB

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');
15 years ago
var net = require('net');
var events = require('events');
15 years ago
var HTTPParser = process.binding('http_parser').HTTPParser;
var parserFreeList = [];
function newParser (type) {
var parser;
if (parserFreeList.length) {
parser = parserFreeList.shift();
parser.reinitialize(type);
} else {
parser = new HTTPParser(type);
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.asciiSlice(start, start+len);
if (parser.incoming.url) {
parser.incoming.url += slice;
} else {
// Almost always will branch here.
parser.incoming.url = slice;
}
};
parser.onHeaderField = function (b, start, len) {
var slice = b.asciiSlice(start, start+len).toLowerCase();
if (parser.value) {
parser.incoming._addHeaderLine(parser.field, parser.value);
parser.field = null;
parser.value = null;
}
if (parser.field) {
parser.field += slice;
} else {
parser.field = slice;
}
};
parser.onHeaderValue = function (b, start, len) {
var slice = b.asciiSlice(start, start+len);
if (parser.value) {
parser.value += slice;
} else {
parser.value = slice;
}
};
parser.onHeadersComplete = function (info) {
if (parser.field && parser.value) {
parser.incoming._addHeaderLine(parser.field, parser.value);
}
parser.incoming.httpVersionMajor = info.versionMajor;
parser.incoming.httpVersionMinor = info.versionMinor;
if (info.method) {
// server only
parser.incoming.method = info.method;
} else {
// client only
parser.incoming.statusCode = info.statusCode;
}
parser.onIncoming(parser.incoming, info.shouldKeepAlive);
};
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;
switch (enc) {
case 'utf8':
string = b.utf8Slice(start, start+len);
break;
case 'ascii':
string = b.asciiSlice(start, start+len);
break;
case 'binary':
string = b.binarySlice(start, start+len);
break;
15 years ago
default:
throw new Error('Unsupported encoding ' + enc + '. Use Buffer');
15 years ago
}
parser.incoming.emit('data', string);
}
};
parser.onMessageComplete = function () {
parser.incoming.emit("end");
};
}
return parser;
}
function freeParser (parser) {
if (parserFreeList.length < 1000) parserFreeList.push(parser);
}
var CRLF = "\r\n";
var STATUS_CODES = exports.STATUS_CODES = {
16 years ago
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'
};
var connectionExpression = /Connection/i;
var transferEncodingExpression = /Transfer-Encoding/i;
var closeExpression = /close/i;
var chunkExpression = /chunk/i;
var contentLengthExpression = /Content-Length/i;
/* Abstract base class for ServerRequest and ClientResponse. */
15 years ago
function IncomingMessage (socket) {
events.EventEmitter.call(this);
// TODO Remove one of these eventually.
15 years ago
this.socket = socket;
this.connection = socket;
this.httpVersion = null;
this.headers = {};
15 years ago
// request (server) only
this.url = "";
this.method = null;
// response (client) only
this.statusCode = null;
15 years ago
this.client = this.socket;
}
sys.inherits(IncomingMessage, events.EventEmitter);
exports.IncomingMessage = IncomingMessage;
IncomingMessage.prototype._parseQueryString = function () {
throw new Error("_parseQueryString is deprecated. Use require(\"querystring\") to parse query strings.\n");
};
IncomingMessage.prototype.setBodyEncoding = function (enc) {
15 years ago
// 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();
};
IncomingMessage.prototype.pause = function () {
15 years ago
this.socket.pause();
};
IncomingMessage.prototype.resume = function () {
15 years ago
this.socket.resume();
};
IncomingMessage.prototype._addHeaderLine = function (field, value) {
if (field in this.headers) {
15 years ago
// 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;
}
};
15 years ago
function OutgoingMessage (socket) {
events.EventEmitter.call(this, socket);
// TODO Remove one of these eventually.
15 years ago
this.socket = socket;
this.connection = socket;
this.output = [];
this.outputEncodings = [];
this.closeOnFinish = false;
this.chunkedEncoding = false;
this.shouldKeepAlive = true;
this.useChunkedEncodingByDefault = true;
this.flushing = false;
this.headWritten = false;
this.finished = false;
}
sys.inherits(OutgoingMessage, events.EventEmitter);
exports.OutgoingMessage = OutgoingMessage;
OutgoingMessage.prototype._send = function (data, encoding) {
var length = this.output.length;
15 years ago
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)) {
15 years ago
this.output[length-1] = lastData + data;
return;
}
this.output.push(data);
encoding = encoding || "ascii";
this.outputEncodings.push(encoding);
};
OutgoingMessage.prototype.sendHeaderLines = function (firstLine, headers) {
var sentConnectionHeader = false;
var sentContentLengthHeader = false;
var sentTransferEncodingHeader = false;
// 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"
var messageHeader = firstLine;
var field, value;
for (var i in headers) {
if (headers[i] instanceof Array) {
field = headers[i][0];
value = headers[i][1];
} else {
if (!headers.hasOwnProperty(i)) continue;
field = i;
value = headers[i];
}
messageHeader += field + ": " + value + CRLF;
15 years ago
if (connectionExpression.test(field)) {
sentConnectionHeader = true;
if (closeExpression.test(value)) this.closeOnFinish = true;
} else if (transferEncodingExpression.test(field)) {
sentTransferEncodingHeader = true;
if (chunkExpression.test(value)) this.chunkedEncoding = true;
} else if (contentLengthExpression.test(field)) {
sentContentLengthHeader = true;
}
}
15 years ago
// keep-alive logic
if (sentConnectionHeader == false) {
if (this.shouldKeepAlive &&
(sentContentLengthHeader || this.useChunkedEncodingByDefault)) {
messageHeader += "Connection: keep-alive\r\n";
} else {
this.closeOnFinish = true;
messageHeader += "Connection: close\r\n";
}
}
if (sentContentLengthHeader == false && sentTransferEncodingHeader == false) {
if (this.useChunkedEncodingByDefault) {
messageHeader += "Transfer-Encoding: chunked\r\n";
this.chunkedEncoding = true;
}
else {
this.closeOnFinish = true;
}
}
messageHeader += CRLF;
this._send(messageHeader);
// wait until the first body chunk, or close(), is sent to flush.
};
OutgoingMessage.prototype.sendBody = function () {
throw new Error("sendBody() has been renamed to write(). " +
"The 'body' event has been renamed to 'data' and " +
"the 'complete' event has been renamed to 'end'.");
};
OutgoingMessage.prototype.write = function (chunk, encoding) {
if ( (this instanceof ServerResponse) && !this.headWritten) {
throw new Error("writeHead() must be called before write()")
}
encoding = encoding || "ascii";
if (this.chunkedEncoding) {
15 years ago
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);
} else {
this._send(chunk, encoding);
}
if (this.flushing) {
this.flush();
} else {
this.flushing = true;
}
};
OutgoingMessage.prototype.flush = function () {
this.emit("flush");
};
OutgoingMessage.prototype.finish = function () {
throw new Error("finish() has been renamed to close().");
};
OutgoingMessage.prototype.close = function () {
if (this.chunkedEncoding) this._send("0\r\n\r\n"); // last chunk
this.finished = true;
this.flush();
};
function ServerResponse (req) {
15 years ago
OutgoingMessage.call(this, req.socket);
if (req.httpVersionMajor < 1 || req.httpVersionMinor < 1) {
this.useChunkedEncodingByDefault = false;
this.shouldKeepAlive = false;
}
}
sys.inherits(ServerResponse, OutgoingMessage);
exports.ServerResponse = ServerResponse;
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 = {};
}
var statusLine = "HTTP/1.1 " + statusCode.toString() + " "
+ reasonPhrase + CRLF;
this.sendHeaderLines(statusLine, headers);
this.headWritten = true;
};
// TODO eventually remove sendHeader(), writeHeader()
ServerResponse.prototype.sendHeader = ServerResponse.prototype.writeHead;
ServerResponse.prototype.writeHeader = ServerResponse.prototype.writeHead;
15 years ago
function ClientRequest (socket, method, url, headers) {
OutgoingMessage.call(this, socket);
this.shouldKeepAlive = false;
if (method === "GET" || method === "HEAD") {
this.useChunkedEncodingByDefault = false;
} else {
this.useChunkedEncodingByDefault = true;
}
this.closeOnFinish = true;
this.sendHeaderLines(method + " " + url + " HTTP/1.1\r\n", headers);
}
sys.inherits(ClientRequest, OutgoingMessage);
exports.ClientRequest = ClientRequest;
ClientRequest.prototype.finish = function () {
throw new Error( "finish() has been renamed to close() and no longer takes "
+ "a response handler as an argument. Manually add a 'response' listener "
+ "to the request object."
);
};
ClientRequest.prototype.close = function () {
if (arguments.length > 0) {
throw new Error( "ClientRequest.prototype.close does not take any arguments. "
+ "Add a response listener manually to the request object."
);
}
OutgoingMessage.prototype.close.call(this);
};
15 years ago
/* Returns true if the message queue is finished and the socket
* should be closed. */
15 years ago
function flushMessageQueue (socket, queue) {
while (queue[0]) {
var message = queue[0];
while (message.output.length > 0) {
15 years ago
if (!socket.writable) return true;
var data = message.output.shift();
var encoding = message.outputEncodings.shift();
15 years ago
socket.write(data, encoding);
}
15 years ago
if (!message.finished) break;
message.emit("sent");
queue.shift();
if (message.closeOnFinish) return true;
}
return false;
}
15 years ago
function Server (requestListener) {
net.Server.call(this);
this.addListener("request", requestListener);
this.addListener("connection", connectionListener);
}
sys.inherits(Server, net.Server);
exports.Server = Server;
exports.createServer = function (requestListener) {
return new Server(requestListener);
};
15 years ago
function connectionListener (socket) {
var self = this;
// An array of responses for each socket. In pipelined connections
// we need to keep track of the order they were sent.
var responses = [];
15 years ago
var parser = newParser('request');
socket.ondata = function (d, start, end) {
parser.execute(d, start, end - start);
};
socket.onend = function () {
parser.finish();
// unref the parser for easy gc
freeParser(parser);
if (responses.length == 0) {
15 years ago
socket.close();
} else {
responses[responses.length-1].closeOnFinish = true;
16 years ago
}
15 years ago
};
15 years ago
parser.socket = socket;
// 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.
parser.onIncoming = function (req, shouldKeepAlive) {
var res = new ServerResponse(req);
15 years ago
res.shouldKeepAlive = shouldKeepAlive;
15 years ago
res.addListener('flush', function () {
if (flushMessageQueue(socket, responses)) {
socket.close();
}
});
responses.push(res);
15 years ago
15 years ago
self.emit('request', req, res);
};
}
15 years ago
function Client ( ) {
net.Stream.call(this);
var self = this;
var requests = [];
var currentRequest;
15 years ago
var parser = newParser('response');
parser.socket = this;
15 years ago
self._reconnect = function () {
if (self.readyState != "opening") {
debug("HTTP CLIENT: reconnecting readyState = " + self.readyState);
15 years ago
self.connect(self.port, self.host);
}
};
15 years ago
self._pushRequest = function (req) {
req.addListener("flush", function () {
15 years ago
if (self.readyState == "closed") {
debug("HTTP CLIENT request flush. reconnect. readyState = " + self.readyState);
15 years ago
self._reconnect();
return;
}
15 years ago
debug("self flush readyState = " + self.readyState);
15 years ago
if (req == currentRequest) flushMessageQueue(self, [req]);
});
requests.push(req);
};
15 years ago
this.ondata = function (d, start, end) {
parser.execute(d, start, end - start);
};
self.addListener("connect", function () {
parser.reinitialize('response');
debug('requests: ' + sys.inspect(requests));
15 years ago
currentRequest = requests.shift()
currentRequest.flush();
});
15 years ago
self.addListener("end", function () {
parser.finish();
debug("self got end closing. readyState = " + self.readyState);
15 years ago
self.close();
});
self.addListener("close", function (e) {
if (e) {
self.emit("error", e);
return;
}
15 years ago
debug("HTTP CLIENT onClose. readyState = " + self.readyState);
// If there are more requests to handle, reconnect.
if (requests.length > 0) {
15 years ago
self._reconnect();
}
});
15 years ago
parser.onIncoming = function (res) {
debug("incoming response!");
res.addListener('end', function ( ) {
debug("request complete disconnecting. readyState = " + self.readyState);
15 years ago
self.close();
});
currentRequest.emit("response", res);
15 years ago
};
};
15 years ago
sys.inherits(Client, net.Stream);
exports.Client = Client;
exports.createClient = function (port, host) {
var c = new Client;
c.port = port;
c.host = host;
return c;
}
15 years ago
Client.prototype.get = function () {
throw new Error("client.get(...) is now client.request('GET', ...)");
};
15 years ago
Client.prototype.head = function () {
throw new Error("client.head(...) is now client.request('HEAD', ...)");
};
15 years ago
Client.prototype.post = function () {
throw new Error("client.post(...) is now client.request('POST', ...)");
};
15 years ago
Client.prototype.del = function () {
throw new Error("client.del(...) is now client.request('DELETE', ...)");
};
15 years ago
Client.prototype.put = function () {
throw new Error("client.put(...) is now client.request('PUT', ...)");
};
15 years ago
Client.prototype.request = function (method, url, headers) {
if (typeof(url) != "string") { // assume method was omitted, shift arguments
headers = url;
url = method;
method = null;
}
var req = new ClientRequest(this, method || "GET", url, headers);
this._pushRequest(req);
return req;
};
exports.cat = function (url, encoding_, headers_) {
15 years ago
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);
var hasHost = false;
for (var i in headers) {
if (i.toLowerCase() === "host") {
hasHost = true;
break;
}
}
if (!hasHost) headers["Host"] = url.hostname;
var content = "";
var client = exports.createClient(url.port || 80, url.hostname);
var req = client.request((url.pathname || "/")+(url.search || "")+(url.hash || ""), headers);
req.addListener('response', function (res) {
if (res.statusCode < 200 || res.statusCode >= 300) {
if (callback) callback(res.statusCode);
client.close();
return;
}
16 years ago
res.setBodyEncoding(encoding);
res.addListener('data', function (chunk) { content += chunk; });
res.addListener('end', function () {
if (callback) callback(null, content);
});
16 years ago
});
client.addListener("error", function (err) {
if (callback) callback(err);
});
req.close();
16 years ago
};