2009-10-31 19:02:30 +01:00
|
|
|
var sys = require('sys');
|
2009-09-28 12:36:36 +02:00
|
|
|
|
|
|
|
var CRLF = "\r\n";
|
|
|
|
var 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
|
|
|
|
2009-05-18 19:33:05 +02:00
|
|
|
/*
|
|
|
|
parseUri 1.2.1
|
|
|
|
(c) 2007 Steven Levithan <stevenlevithan.com>
|
|
|
|
MIT License
|
|
|
|
*/
|
|
|
|
|
2009-06-01 20:59:33 +02:00
|
|
|
function decode (s) {
|
|
|
|
return decodeURIComponent(s.replace(/\+/g, ' '));
|
|
|
|
}
|
|
|
|
|
2009-09-28 12:36:36 +02:00
|
|
|
exports.parseUri = function (str) {
|
|
|
|
var o = exports.parseUri.options,
|
2009-06-22 13:12:47 +02:00
|
|
|
m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
|
|
|
|
uri = {},
|
|
|
|
i = 14;
|
2009-05-18 19:33:05 +02:00
|
|
|
|
|
|
|
while (i--) uri[o.key[i]] = m[i] || "";
|
|
|
|
|
|
|
|
uri[o.q.name] = {};
|
|
|
|
uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
|
2009-06-01 20:59:33 +02:00
|
|
|
if ($1) {
|
2009-09-04 17:35:38 +02:00
|
|
|
try {
|
|
|
|
var key = decode($1);
|
|
|
|
var val = decode($2);
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
2009-06-01 20:59:33 +02:00
|
|
|
uri[o.q.name][key] = val;
|
|
|
|
}
|
2009-05-18 19:33:05 +02:00
|
|
|
});
|
|
|
|
uri.toString = function () { return str; };
|
2009-08-26 22:03:19 +02:00
|
|
|
|
2009-07-01 00:49:56 +02:00
|
|
|
for (i = o.key.length - 1; i >= 0; i--){
|
2009-06-21 16:28:23 +02:00
|
|
|
if (uri[o.key[i]] == "") delete uri[o.key[i]];
|
2009-07-01 00:49:56 +02:00
|
|
|
}
|
2009-08-26 22:03:19 +02:00
|
|
|
|
2009-05-18 19:33:05 +02:00
|
|
|
return uri;
|
|
|
|
};
|
|
|
|
|
2009-09-28 12:36:36 +02:00
|
|
|
exports.parseUri.options = {
|
2009-05-18 19:33:05 +02:00
|
|
|
strictMode: false,
|
2009-08-26 22:03:19 +02:00
|
|
|
key: [
|
2009-07-01 00:49:56 +02:00
|
|
|
"source",
|
|
|
|
"protocol",
|
|
|
|
"authority",
|
|
|
|
"userInfo",
|
|
|
|
"user",
|
|
|
|
"password",
|
|
|
|
"host",
|
|
|
|
"port",
|
|
|
|
"relative",
|
|
|
|
"path",
|
|
|
|
"directory",
|
|
|
|
"file",
|
|
|
|
"query",
|
|
|
|
"anchor"
|
|
|
|
],
|
2009-05-18 19:33:05 +02:00
|
|
|
q: {
|
2009-06-01 20:59:33 +02:00
|
|
|
name: "params",
|
2009-05-18 19:33:05 +02:00
|
|
|
parser: /(?:^|&)([^&=]*)=?([^&]*)/g
|
|
|
|
},
|
|
|
|
parser: {
|
|
|
|
strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
|
|
|
|
loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-07-16 10:59:40 +02:00
|
|
|
|
2009-05-11 18:54:52 +02:00
|
|
|
var close_expression = /close/i;
|
|
|
|
var chunk_expression = /chunk/i;
|
|
|
|
|
2009-06-26 18:29:57 +02:00
|
|
|
|
2009-07-14 11:59:13 +02:00
|
|
|
/* Abstract base class for ServerRequest and ClientResponse. */
|
2009-07-16 10:59:40 +02:00
|
|
|
function IncomingMessage (connection) {
|
2009-10-29 23:34:10 +01:00
|
|
|
process.EventEmitter.call(this);
|
2009-06-26 18:29:57 +02:00
|
|
|
|
2009-07-14 11:59:13 +02:00
|
|
|
this.connection = connection;
|
|
|
|
this.httpVersion = null;
|
2009-08-23 12:20:25 +02:00
|
|
|
this.headers = {};
|
2009-07-14 18:31:50 +02:00
|
|
|
|
2009-08-26 22:03:19 +02:00
|
|
|
// request (server) only
|
2009-10-04 12:17:50 +02:00
|
|
|
this.uri = {
|
|
|
|
full: "",
|
|
|
|
queryString: "",
|
|
|
|
fragment: "",
|
2009-10-05 14:51:41 +02:00
|
|
|
path: "",
|
|
|
|
params: {}
|
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;
|
|
|
|
this.client = this.connection;
|
2009-07-16 10:59:40 +02:00
|
|
|
}
|
2009-10-29 23:34:10 +01:00
|
|
|
process.inherits(IncomingMessage, process.EventEmitter);
|
2009-11-05 00:02:15 +01:00
|
|
|
exports.IncomingMessage = IncomingMessage;
|
2009-07-14 11:59:13 +02:00
|
|
|
|
2009-10-05 14:51:41 +02:00
|
|
|
IncomingMessage.prototype._parseQueryString = function () {
|
|
|
|
var parts = this.uri.queryString.split('&');
|
|
|
|
for (var j = 0; j < parts.length; j++) {
|
|
|
|
var i = parts[j].indexOf('=');
|
|
|
|
if (i < 0) continue;
|
|
|
|
try {
|
|
|
|
var key = decode(parts[j].slice(0,i))
|
|
|
|
var value = decode(parts[j].slice(i+1));
|
|
|
|
this.uri.params[key] = value;
|
|
|
|
} catch (e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-07-14 11:59:13 +02:00
|
|
|
IncomingMessage.prototype.setBodyEncoding = function (enc) {
|
2009-08-26 22:03:19 +02:00
|
|
|
// TODO: Find a cleaner way of doing this.
|
2009-07-14 11:59:13 +02:00
|
|
|
this.connection.setEncoding(enc);
|
2009-06-26 18:29:57 +02:00
|
|
|
};
|
|
|
|
|
2009-08-09 19:16:25 +02:00
|
|
|
IncomingMessage.prototype.pause = function () {
|
|
|
|
this.connection.readPause();
|
|
|
|
};
|
|
|
|
|
|
|
|
IncomingMessage.prototype.resume = function () {
|
|
|
|
this.connection.readResume();
|
|
|
|
};
|
|
|
|
|
2009-08-23 12:20:25 +02:00
|
|
|
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.
|
2009-08-23 12:20:25 +02:00
|
|
|
// 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
|
|
|
|
2009-07-16 10:59:40 +02:00
|
|
|
function OutgoingMessage () {
|
2009-10-29 23:34:10 +01:00
|
|
|
process.EventEmitter.call(this);
|
2009-07-14 11:59:13 +02: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
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
this.closeOnFinish = false;
|
|
|
|
this.chunked_encoding = false;
|
|
|
|
this.should_keep_alive = true;
|
|
|
|
this.use_chunked_encoding_by_default = true;
|
2009-07-14 11:59:13 +02:00
|
|
|
|
2009-09-14 16:25:41 +02:00
|
|
|
this.flushing = false;
|
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
this.finished = false;
|
2009-07-16 10:59:40 +02:00
|
|
|
}
|
2009-10-29 23:34:10 +01:00
|
|
|
process.inherits(OutgoingMessage, process.EventEmitter);
|
2009-11-05 00:02:15 +01:00
|
|
|
exports.OutgoingMessage = OutgoingMessage;
|
2009-07-14 11:59:13 +02:00
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
OutgoingMessage.prototype.send = function (data, encoding) {
|
2009-09-14 16:25:41 +02:00
|
|
|
var length = this.output.length;
|
|
|
|
|
|
|
|
if (length === 0) {
|
|
|
|
this.output.push(data);
|
2009-09-21 12:27:22 +02:00
|
|
|
encoding = encoding || "ascii";
|
2009-09-14 16:25:41 +02:00
|
|
|
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)) {
|
|
|
|
if (lastData.constructor === String) {
|
|
|
|
this.output[length-1] = lastData + data;
|
|
|
|
} else {
|
|
|
|
this.output[length-1] = lastData.concat(data);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-07-16 10:59:40 +02:00
|
|
|
this.output.push(data);
|
2009-09-21 12:27:22 +02:00
|
|
|
encoding = encoding || "ascii";
|
2009-09-13 18:19:03 +02:00
|
|
|
this.outputEncodings.push(encoding);
|
2009-07-14 18:31:50 +02:00
|
|
|
};
|
2009-07-14 11:59:13 +02:00
|
|
|
|
2009-08-23 12:20:25 +02:00
|
|
|
OutgoingMessage.prototype.sendHeaderLines = function (first_line, headers) {
|
2009-07-31 18:34:27 +02:00
|
|
|
var sent_connection_header = false;
|
|
|
|
var sent_content_length_header = false;
|
|
|
|
var sent_transfer_encoding_header = false;
|
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
// first_line 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"
|
2009-08-23 12:20:25 +02:00
|
|
|
var message_header = first_line;
|
|
|
|
var field, value;
|
|
|
|
for (var i in headers) {
|
|
|
|
if (headers instanceof Array) {
|
|
|
|
field = headers[i][0];
|
|
|
|
value = headers[i][1];
|
|
|
|
} else {
|
|
|
|
if (!headers.hasOwnProperty(i)) continue;
|
|
|
|
field = i;
|
|
|
|
value = headers[i];
|
|
|
|
}
|
2009-06-26 18:29:57 +02:00
|
|
|
|
2009-08-23 12:20:25 +02:00
|
|
|
message_header += field + ": " + value + CRLF;
|
2009-08-26 22:03:19 +02:00
|
|
|
|
2009-10-07 16:56:19 +02:00
|
|
|
if ("connection" === field) {
|
2009-07-31 18:34:27 +02:00
|
|
|
sent_connection_header = true;
|
2009-07-14 18:31:50 +02:00
|
|
|
if (close_expression.exec(value)) this.closeOnFinish = true;
|
2009-06-26 18:29:57 +02:00
|
|
|
|
2009-10-07 16:56:19 +02:00
|
|
|
} else if ("transfer-encoding" === field) {
|
2009-07-31 18:34:27 +02:00
|
|
|
sent_transfer_encoding_header = true;
|
2009-07-14 18:31:50 +02:00
|
|
|
if (chunk_expression.exec(value)) this.chunked_encoding = true;
|
|
|
|
|
2009-10-07 16:56:19 +02:00
|
|
|
} else if ("content-length" === field) {
|
2009-07-31 18:34:27 +02:00
|
|
|
sent_content_length_header = true;
|
2009-07-14 18:31:50 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-26 22:03:19 +02:00
|
|
|
// keep-alive logic
|
2009-07-31 18:34:27 +02:00
|
|
|
if (sent_connection_header == false) {
|
2009-07-14 18:31:50 +02:00
|
|
|
if (this.should_keep_alive) {
|
2009-08-23 12:20:25 +02:00
|
|
|
message_header += "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;
|
2009-08-23 12:20:25 +02:00
|
|
|
message_header += "Connection: close\r\n";
|
2009-06-26 18:29:57 +02:00
|
|
|
}
|
2009-07-14 18:31:50 +02:00
|
|
|
}
|
|
|
|
|
2009-07-31 18:34:27 +02:00
|
|
|
if (sent_content_length_header == false && sent_transfer_encoding_header == false) {
|
2009-07-14 18:31:50 +02:00
|
|
|
if (this.use_chunked_encoding_by_default) {
|
2009-08-23 12:20:25 +02:00
|
|
|
message_header += "Transfer-Encoding: chunked\r\n";
|
2009-07-14 18:31:50 +02:00
|
|
|
this.chunked_encoding = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-23 12:20:25 +02:00
|
|
|
message_header += CRLF;
|
2009-07-14 18:31:50 +02:00
|
|
|
|
2009-08-23 12:20:25 +02:00
|
|
|
this.send(message_header);
|
2009-07-16 10:59:40 +02:00
|
|
|
// wait until the first body chunk, or finish(), is sent to flush.
|
2009-07-14 18:31:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
OutgoingMessage.prototype.sendBody = function (chunk, encoding) {
|
|
|
|
if (this.chunked_encoding) {
|
2009-09-14 16:25:41 +02:00
|
|
|
this.send(chunk.length.toString(16));
|
|
|
|
this.send(CRLF);
|
2009-07-14 18:31:50 +02:00
|
|
|
this.send(chunk, encoding);
|
2009-09-14 16:25:41 +02:00
|
|
|
this.send(CRLF);
|
2009-07-14 18:31:50 +02:00
|
|
|
} else {
|
|
|
|
this.send(chunk, encoding);
|
|
|
|
}
|
2009-06-26 18:29:57 +02:00
|
|
|
|
2009-09-14 16:25:41 +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 () {
|
|
|
|
if (this.chunked_encoding) this.send("0\r\n\r\n"); // last chunk
|
|
|
|
this.finished = true;
|
|
|
|
this.flush();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-07-16 10:59:40 +02:00
|
|
|
function ServerResponse () {
|
2009-07-14 18:31:50 +02:00
|
|
|
OutgoingMessage.call(this);
|
|
|
|
|
|
|
|
this.should_keep_alive = true;
|
|
|
|
this.use_chunked_encoding_by_default = true;
|
2009-07-16 10:59:40 +02:00
|
|
|
}
|
2009-10-29 23:34:10 +01:00
|
|
|
process.inherits(ServerResponse, OutgoingMessage);
|
2009-11-04 12:37:52 +01:00
|
|
|
exports.ServerResponse = ServerResponse;
|
2009-07-14 18:31:50 +02:00
|
|
|
|
|
|
|
ServerResponse.prototype.sendHeader = function (statusCode, headers) {
|
2009-09-28 12:36:36 +02:00
|
|
|
var reason = STATUS_CODES[statusCode] || "unknown";
|
2009-07-14 18:31:50 +02:00
|
|
|
var status_line = "HTTP/1.1 " + statusCode.toString() + " " + reason + CRLF;
|
|
|
|
this.sendHeaderLines(status_line, headers);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-08-23 12:20:25 +02:00
|
|
|
function ClientRequest (method, uri, headers) {
|
2009-07-14 18:31:50 +02:00
|
|
|
OutgoingMessage.call(this);
|
|
|
|
|
|
|
|
this.should_keep_alive = false;
|
2009-07-31 18:34:27 +02:00
|
|
|
if (method === "GET" || method === "HEAD") {
|
|
|
|
this.use_chunked_encoding_by_default = false;
|
|
|
|
} else {
|
|
|
|
this.use_chunked_encoding_by_default = true;
|
|
|
|
}
|
2009-07-14 18:31:50 +02:00
|
|
|
this.closeOnFinish = true;
|
|
|
|
|
2009-10-28 15:37:58 +01:00
|
|
|
this.sendHeaderLines(method + " " + uri + " HTTP/1.0\r\n", headers);
|
2009-07-16 10:59:40 +02:00
|
|
|
}
|
2009-10-29 23:34:10 +01:00
|
|
|
process.inherits(ClientRequest, OutgoingMessage);
|
2009-11-04 12:37:52 +01:00
|
|
|
exports.ClientRequest = ClientRequest;
|
2009-07-14 18:31:50 +02:00
|
|
|
|
|
|
|
ClientRequest.prototype.finish = function (responseListener) {
|
|
|
|
this.addListener("response", responseListener);
|
|
|
|
OutgoingMessage.prototype.finish.call(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function createIncomingMessageStream (connection, incoming_listener) {
|
2009-10-29 23:34:10 +01:00
|
|
|
var stream = new process.EventEmitter();
|
2009-07-14 18:31:50 +02:00
|
|
|
|
|
|
|
stream.addListener("incoming", incoming_listener);
|
|
|
|
|
|
|
|
var incoming;
|
2009-08-23 12:20:25 +02:00
|
|
|
var field = null, value = null;
|
2009-06-26 18:29:57 +02:00
|
|
|
|
2009-09-29 18:06:14 +02:00
|
|
|
connection.addListener("messageBegin", function () {
|
2009-07-14 18:31:50 +02:00
|
|
|
incoming = new IncomingMessage(connection);
|
2009-06-26 18:29:57 +02:00
|
|
|
});
|
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
// Only servers will get URI events.
|
2009-06-29 13:18:30 +02:00
|
|
|
connection.addListener("uri", function (data) {
|
2009-10-04 12:17:50 +02:00
|
|
|
incoming.uri.full += data;
|
|
|
|
});
|
|
|
|
|
|
|
|
connection.addListener("path", function (data) {
|
|
|
|
incoming.uri.path += data;
|
|
|
|
});
|
|
|
|
|
|
|
|
connection.addListener("fragment", function (data) {
|
|
|
|
incoming.uri.fragment += data;
|
|
|
|
});
|
|
|
|
|
|
|
|
connection.addListener("queryString", function (data) {
|
|
|
|
incoming.uri.queryString += data;
|
2009-06-26 18:29:57 +02:00
|
|
|
});
|
|
|
|
|
2009-09-29 18:06:14 +02:00
|
|
|
connection.addListener("headerField", function (data) {
|
2009-08-23 12:20:25 +02:00
|
|
|
if (value) {
|
|
|
|
incoming._addHeaderLine(field, value);
|
|
|
|
field = null;
|
|
|
|
value = null;
|
|
|
|
}
|
|
|
|
if (field) {
|
|
|
|
field += data;
|
2009-07-14 18:31:50 +02:00
|
|
|
} else {
|
2009-08-23 12:20:25 +02:00
|
|
|
field = data;
|
2009-07-14 18:31:50 +02:00
|
|
|
}
|
2009-06-26 18:29:57 +02:00
|
|
|
});
|
|
|
|
|
2009-09-29 18:06:14 +02:00
|
|
|
connection.addListener("headerValue", function (data) {
|
2009-08-23 12:20:25 +02:00
|
|
|
if (value) {
|
|
|
|
value += data;
|
|
|
|
} else {
|
|
|
|
value = data;
|
|
|
|
}
|
2009-06-26 18:29:57 +02:00
|
|
|
});
|
|
|
|
|
2009-09-29 18:06:14 +02:00
|
|
|
connection.addListener("headerComplete", function (info) {
|
2009-08-23 12:20:25 +02:00
|
|
|
if (field && value) {
|
|
|
|
incoming._addHeaderLine(field, value);
|
|
|
|
}
|
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
incoming.httpVersion = info.httpVersion;
|
2009-06-26 18:29:57 +02:00
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
if (info.method) {
|
|
|
|
// server only
|
2009-08-26 22:03:19 +02:00
|
|
|
incoming.method = info.method;
|
2009-10-05 14:51:41 +02:00
|
|
|
|
|
|
|
if (incoming.uri.queryString.length > 0) {
|
|
|
|
incoming._parseQueryString();
|
|
|
|
}
|
2009-07-14 18:31:50 +02:00
|
|
|
} else {
|
|
|
|
// client only
|
2009-08-26 22:03:19 +02:00
|
|
|
incoming.statusCode = info.statusCode;
|
2009-07-14 18:31:50 +02:00
|
|
|
}
|
2009-06-26 18:29:57 +02:00
|
|
|
|
2009-09-12 13:01:03 +02:00
|
|
|
stream.emit("incoming", incoming, info.should_keep_alive);
|
2009-06-26 18:29:57 +02:00
|
|
|
});
|
|
|
|
|
2009-06-29 13:18:30 +02:00
|
|
|
connection.addListener("body", function (chunk) {
|
2009-09-12 13:01:03 +02:00
|
|
|
incoming.emit("body", chunk);
|
2009-06-26 18:29:57 +02:00
|
|
|
});
|
|
|
|
|
2009-09-29 18:06:14 +02:00
|
|
|
connection.addListener("messageComplete", function () {
|
2009-07-14 18:31:50 +02:00
|
|
|
incoming.emit("complete");
|
2009-06-26 18:29:57 +02:00
|
|
|
});
|
2009-07-14 18:31:50 +02:00
|
|
|
|
|
|
|
return stream;
|
2009-07-01 00:49:56 +02:00
|
|
|
}
|
2009-06-26 18:29:57 +02:00
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
/* Returns true if the message queue is finished and the connection
|
|
|
|
* should be closed. */
|
|
|
|
function flushMessageQueue (connection, queue) {
|
|
|
|
while (queue[0]) {
|
|
|
|
var message = queue[0];
|
2009-05-18 19:33:05 +02:00
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
while (message.output.length > 0) {
|
2009-08-21 17:54:10 +02:00
|
|
|
if (connection.readyState !== "open" && connection.readyState !== "writeOnly") {
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-13 12:38:59 +02:00
|
|
|
|
|
|
|
var data = message.output.shift();
|
|
|
|
var encoding = message.outputEncodings.shift();
|
|
|
|
|
|
|
|
connection.send(data, encoding);
|
2009-05-18 19:33:05 +02:00
|
|
|
}
|
2009-08-26 22:03:19 +02:00
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
if (!message.finished) break;
|
2009-05-18 19:33:05 +02:00
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
message.emit("sent");
|
|
|
|
queue.shift();
|
2009-05-18 19:33:05 +02:00
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
if (message.closeOnFinish) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-18 19:33:05 +02:00
|
|
|
|
2009-07-16 10:59:40 +02:00
|
|
|
|
2009-09-28 12:36:36 +02:00
|
|
|
exports.createServer = function (requestListener, options) {
|
2009-10-29 23:34:10 +01:00
|
|
|
var server = new process.http.Server();
|
2009-07-14 18:31:50 +02:00
|
|
|
//server.setOptions(options);
|
|
|
|
server.addListener("request", requestListener);
|
|
|
|
server.addListener("connection", connectionListener);
|
|
|
|
return server;
|
|
|
|
};
|
2009-05-18 19:33:05 +02:00
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
function connectionListener (connection) {
|
|
|
|
// An array of responses for each connection. In pipelined connections
|
|
|
|
// we need to keep track of the order they were sent.
|
|
|
|
var responses = [];
|
2009-05-18 19:33:05 +02:00
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
// is this really needed?
|
|
|
|
connection.addListener("eof", function () {
|
|
|
|
if (responses.length == 0) {
|
|
|
|
connection.close();
|
|
|
|
} else {
|
|
|
|
responses[responses.length-1].closeOnFinish = true;
|
2009-07-01 00:49:56 +02:00
|
|
|
}
|
2009-07-14 18:31:50 +02:00
|
|
|
});
|
2009-05-18 19:33:05 +02:00
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
createIncomingMessageStream(connection, function (incoming, should_keep_alive) {
|
|
|
|
var req = incoming;
|
2009-06-26 12:51:27 +02:00
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
var res = new ServerResponse(connection);
|
|
|
|
res.should_keep_alive = should_keep_alive;
|
2009-07-31 18:34:27 +02:00
|
|
|
res.addListener("flush", function () {
|
|
|
|
if(flushMessageQueue(connection, responses)) {
|
2009-08-19 17:28:41 +02:00
|
|
|
connection.close();
|
2009-07-31 18:34:27 +02:00
|
|
|
}
|
|
|
|
});
|
2009-07-14 18:31:50 +02:00
|
|
|
responses.push(res);
|
2009-08-26 22:03:19 +02:00
|
|
|
|
2009-09-12 13:01:03 +02:00
|
|
|
connection.server.emit("request", req, res);
|
2009-07-14 18:31:50 +02:00
|
|
|
});
|
|
|
|
}
|
2009-06-26 12:51:27 +02:00
|
|
|
|
2009-06-26 18:30:55 +02:00
|
|
|
|
2009-09-28 12:36:36 +02:00
|
|
|
exports.createClient = function (port, host) {
|
2009-10-29 23:34:10 +01:00
|
|
|
var client = new process.http.Client();
|
2009-06-26 18:30:55 +02:00
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
var requests = [];
|
2009-06-26 12:51:27 +02:00
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
client._pushRequest = function (req) {
|
|
|
|
req.addListener("flush", function () {
|
|
|
|
if (client.readyState == "closed") {
|
2009-10-13 13:26:00 +02:00
|
|
|
//sys.debug("HTTP CLIENT request flush. reconnect. readyState = " + client.readyState);
|
2009-07-14 18:31:50 +02:00
|
|
|
client.connect(port, host); // reconnect
|
|
|
|
return;
|
|
|
|
}
|
2009-10-13 13:26:00 +02:00
|
|
|
//sys.debug("client flush readyState = " + client.readyState);
|
2009-10-15 19:18:18 +02:00
|
|
|
if (req == requests[0]) {
|
|
|
|
if(flushMessageQueue(client, [req])) client.close();
|
|
|
|
}
|
2009-07-14 18:31:50 +02:00
|
|
|
});
|
|
|
|
requests.push(req);
|
|
|
|
};
|
2009-06-26 12:51:27 +02:00
|
|
|
|
2009-06-29 13:18:30 +02:00
|
|
|
client.addListener("connect", function () {
|
2009-07-14 18:31:50 +02:00
|
|
|
requests[0].flush();
|
2009-06-26 18:29:57 +02:00
|
|
|
});
|
2009-05-06 14:54:28 +02:00
|
|
|
|
2009-06-29 13:18:30 +02:00
|
|
|
client.addListener("eof", function () {
|
2009-10-13 13:26:00 +02:00
|
|
|
//sys.debug("client got eof closing. readyState = " + client.readyState);
|
2009-06-26 18:29:57 +02:00
|
|
|
client.close();
|
|
|
|
});
|
2009-06-26 12:51:27 +02:00
|
|
|
|
2009-08-14 12:51:46 +02:00
|
|
|
client.addListener("close", function (had_error) {
|
2009-06-26 18:29:57 +02:00
|
|
|
if (had_error) {
|
2009-06-29 13:18:30 +02:00
|
|
|
client.emit("error");
|
2009-06-26 18:29:57 +02:00
|
|
|
return;
|
|
|
|
}
|
2009-08-26 22:03:19 +02:00
|
|
|
|
2009-10-13 13:26:00 +02:00
|
|
|
//sys.debug("HTTP CLIENT onClose. readyState = " + client.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.
|
2009-07-14 18:31:50 +02:00
|
|
|
if (requests.length > 0 && client.readyState != "opening") {
|
2009-10-13 13:26:00 +02:00
|
|
|
//sys.debug("HTTP CLIENT: reconnecting readyState = " + client.readyState);
|
2009-07-14 18:31:50 +02:00
|
|
|
client.connect(port, host); // reconnect
|
2009-06-26 18:29:57 +02:00
|
|
|
}
|
|
|
|
});
|
2009-06-26 12:51:27 +02:00
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
createIncomingMessageStream(client, function (res) {
|
2009-10-13 13:26:00 +02:00
|
|
|
//sys.debug("incoming response!");
|
2009-06-26 12:51:27 +02:00
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
res.addListener("complete", function ( ) {
|
2009-10-13 13:26:00 +02:00
|
|
|
//sys.debug("request complete disconnecting. readyState = " + client.readyState);
|
2009-07-14 18:31:50 +02:00
|
|
|
client.close();
|
|
|
|
});
|
2009-05-15 18:11:49 +02:00
|
|
|
|
2009-07-14 18:31:50 +02:00
|
|
|
var req = requests.shift();
|
2009-09-12 13:01:03 +02:00
|
|
|
req.emit("response", res);
|
2009-06-26 18:29:57 +02:00
|
|
|
});
|
2009-05-15 18:11:49 +02:00
|
|
|
|
2009-06-26 18:29:57 +02:00
|
|
|
return client;
|
|
|
|
};
|
2009-05-15 18:11:49 +02:00
|
|
|
|
2009-10-29 23:34:10 +01:00
|
|
|
process.http.Client.prototype.get = function (uri, headers) {
|
2009-07-14 18:31:50 +02:00
|
|
|
var req = new ClientRequest("GET", uri, headers);
|
|
|
|
this._pushRequest(req);
|
2009-06-26 18:30:55 +02:00
|
|
|
return req;
|
2009-06-26 18:29:57 +02:00
|
|
|
};
|
2009-05-15 18:11:49 +02:00
|
|
|
|
2009-10-29 23:34:10 +01:00
|
|
|
process.http.Client.prototype.head = function (uri, headers) {
|
2009-07-14 18:31:50 +02:00
|
|
|
var req = new ClientRequest("HEAD", uri, headers);
|
|
|
|
this._pushRequest(req);
|
2009-06-26 18:30:55 +02:00
|
|
|
return req;
|
2009-06-26 18:29:57 +02:00
|
|
|
};
|
2009-05-15 18:11:49 +02:00
|
|
|
|
2009-10-29 23:34:10 +01:00
|
|
|
process.http.Client.prototype.post = function (uri, headers) {
|
2009-07-14 18:31:50 +02:00
|
|
|
var req = new ClientRequest("POST", uri, headers);
|
|
|
|
this._pushRequest(req);
|
2009-06-26 18:30:55 +02:00
|
|
|
return req;
|
2009-06-26 18:29:57 +02:00
|
|
|
};
|
2009-05-15 18:11:49 +02:00
|
|
|
|
2009-10-29 23:34:10 +01:00
|
|
|
process.http.Client.prototype.del = function (uri, headers) {
|
2009-07-14 18:31:50 +02:00
|
|
|
var req = new ClientRequest("DELETE", uri, headers);
|
|
|
|
this._pushRequest(req);
|
2009-06-26 18:30:55 +02:00
|
|
|
return req;
|
2009-06-26 18:29:57 +02:00
|
|
|
};
|
2009-05-15 18:11:49 +02:00
|
|
|
|
2009-10-29 23:34:10 +01:00
|
|
|
process.http.Client.prototype.put = function (uri, headers) {
|
2009-07-14 18:31:50 +02:00
|
|
|
var req = new ClientRequest("PUT", uri, headers);
|
|
|
|
this._pushRequest(req);
|
2009-06-26 18:30:55 +02:00
|
|
|
return req;
|
2009-06-26 18:29:57 +02:00
|
|
|
};
|
2009-06-06 23:57:15 +02:00
|
|
|
|
2009-05-19 13:12:46 +02:00
|
|
|
|
2009-09-30 11:54:14 +02:00
|
|
|
exports.cat = function (url, encoding, headers) {
|
2009-10-29 23:34:10 +01:00
|
|
|
var promise = new process.Promise();
|
2009-06-28 19:05:58 +02:00
|
|
|
|
2009-09-15 22:41:25 +02:00
|
|
|
encoding = encoding || "utf8";
|
|
|
|
|
2009-09-28 12:36:36 +02:00
|
|
|
var uri = exports.parseUri(url);
|
2009-09-30 11:54:14 +02:00
|
|
|
headers = headers || {};
|
|
|
|
if (!headers["Host"] && uri.host) {
|
|
|
|
headers["Host"] = uri.host;
|
|
|
|
}
|
|
|
|
|
2009-09-28 12:36:36 +02:00
|
|
|
var client = exports.createClient(uri.port || 80, uri.host);
|
2009-09-30 11:54:14 +02:00
|
|
|
var req = client.get(uri.path || "/", headers);
|
2009-06-28 19:05:58 +02:00
|
|
|
|
2009-06-29 13:18:30 +02:00
|
|
|
client.addListener("error", function () {
|
2009-06-28 19:05:58 +02:00
|
|
|
promise.emitError();
|
|
|
|
});
|
|
|
|
|
|
|
|
var content = "";
|
|
|
|
|
2009-06-22 13:12:47 +02:00
|
|
|
req.finish(function (res) {
|
2009-06-28 19:05:58 +02:00
|
|
|
if (res.statusCode < 200 || res.statusCode >= 300) {
|
2009-09-12 13:01:03 +02:00
|
|
|
promise.emitError(res.statusCode);
|
2009-06-28 19:05:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2009-06-22 13:12:47 +02:00
|
|
|
res.setBodyEncoding(encoding);
|
2009-06-29 13:18:30 +02:00
|
|
|
res.addListener("body", function (chunk) { content += chunk; });
|
|
|
|
res.addListener("complete", function () {
|
2009-09-12 13:01:03 +02:00
|
|
|
promise.emitSuccess(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
|
|
|
|
|
|
|
return promise;
|
2009-06-22 13:12:47 +02:00
|
|
|
};
|