2009-05-18 12:44:01 +02:00
|
|
|
(function () {
|
2009-05-19 13:12:46 +02:00
|
|
|
CRLF = "\r\n";
|
2009-05-11 19:08:29 +02:00
|
|
|
node.http.STATUS_CODES = { 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-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-05-19 00:01:11 +02:00
|
|
|
node.http.parseUri = function (str) {
|
|
|
|
var o = node.http.parseUri.options,
|
2009-05-18 19:33:05 +02:00
|
|
|
m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
|
|
|
|
uri = {},
|
|
|
|
i = 14;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
var key = decode($1);
|
|
|
|
var val = decode($2);
|
|
|
|
uri[o.q.name][key] = val;
|
|
|
|
}
|
2009-05-18 19:33:05 +02:00
|
|
|
});
|
|
|
|
uri.toString = function () { return str; };
|
2009-06-21 16:28:23 +02:00
|
|
|
|
|
|
|
for (var i = o.key.length - 1; i >= 0; i--){
|
|
|
|
if (uri[o.key[i]] == "") delete uri[o.key[i]];
|
|
|
|
};
|
|
|
|
|
2009-05-18 19:33:05 +02:00
|
|
|
return uri;
|
|
|
|
};
|
|
|
|
|
2009-05-19 00:01:11 +02:00
|
|
|
node.http.parseUri.options = {
|
2009-05-18 19:33:05 +02:00
|
|
|
strictMode: false,
|
|
|
|
key: [ "source"
|
|
|
|
, "protocol"
|
|
|
|
, "authority"
|
|
|
|
, "userInfo"
|
|
|
|
, "user"
|
|
|
|
, "password"
|
|
|
|
, "host"
|
|
|
|
, "port"
|
|
|
|
, "relative"
|
|
|
|
, "path"
|
|
|
|
, "directory"
|
|
|
|
, "file"
|
|
|
|
, "query"
|
|
|
|
, "anchor"
|
|
|
|
],
|
|
|
|
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-05-11 18:54:52 +02:00
|
|
|
var connection_expression = /Connection/i;
|
|
|
|
var transfer_encoding_expression = /Transfer-Encoding/i;
|
|
|
|
var close_expression = /close/i;
|
|
|
|
var chunk_expression = /chunk/i;
|
|
|
|
var content_length_expression = /Content-Length/i;
|
|
|
|
|
2009-05-18 12:44:01 +02:00
|
|
|
function toRaw(string) {
|
|
|
|
var a = [];
|
|
|
|
for (var i = 0; i < string.length; i++)
|
|
|
|
a.push(string.charCodeAt(i));
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2009-05-26 17:46:56 +02:00
|
|
|
function send (output, data, encoding) {
|
|
|
|
if (data.constructor === String)
|
|
|
|
encoding = encoding || "ascii";
|
|
|
|
else
|
|
|
|
encoding = "raw";
|
|
|
|
|
|
|
|
output.push([data, encoding]);
|
|
|
|
};
|
2009-05-18 19:33:05 +02:00
|
|
|
|
2009-05-26 17:46:56 +02:00
|
|
|
node.http.ServerResponse = function (connection, responses) {
|
|
|
|
responses.push(this);
|
|
|
|
this.connection = connection;
|
|
|
|
this.closeOnFinish = false;
|
|
|
|
var output = [];
|
2009-05-18 19:33:05 +02:00
|
|
|
|
|
|
|
var chunked_encoding = false;
|
|
|
|
|
2009-05-20 16:05:31 +02:00
|
|
|
this.sendHeader = function (statusCode, headers) {
|
2009-05-18 19:33:05 +02:00
|
|
|
var sent_connection_header = false;
|
|
|
|
var sent_transfer_encoding_header = false;
|
|
|
|
var sent_content_length_header = false;
|
|
|
|
|
2009-05-20 16:05:31 +02:00
|
|
|
var reason = node.http.STATUS_CODES[statusCode] || "unknown";
|
2009-05-18 19:33:05 +02:00
|
|
|
var header = "HTTP/1.1 "
|
2009-05-20 16:05:31 +02:00
|
|
|
+ statusCode.toString()
|
2009-05-18 19:33:05 +02:00
|
|
|
+ " "
|
|
|
|
+ reason
|
2009-05-19 13:12:46 +02:00
|
|
|
+ CRLF
|
2009-05-18 19:33:05 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
for (var i = 0; i < headers.length; i++) {
|
|
|
|
var field = headers[i][0];
|
|
|
|
var value = headers[i][1];
|
|
|
|
|
2009-05-19 13:12:46 +02:00
|
|
|
header += field + ": " + value + CRLF;
|
2009-05-18 19:33:05 +02:00
|
|
|
|
|
|
|
if (connection_expression.exec(field)) {
|
|
|
|
sent_connection_header = true;
|
|
|
|
if (close_expression.exec(value))
|
2009-05-20 10:28:10 +02:00
|
|
|
this.closeOnFinish = true;
|
2009-05-18 19:33:05 +02:00
|
|
|
} else if (transfer_encoding_expression.exec(field)) {
|
|
|
|
sent_transfer_encoding_header = true;
|
|
|
|
if (chunk_expression.exec(value))
|
|
|
|
chunked_encoding = true;
|
|
|
|
} else if (content_length_expression.exec(field)) {
|
|
|
|
sent_content_length_header = true;
|
2009-05-18 12:44:01 +02:00
|
|
|
}
|
2009-05-18 19:33:05 +02:00
|
|
|
}
|
2009-05-11 18:54:52 +02:00
|
|
|
|
2009-05-18 19:33:05 +02:00
|
|
|
// keep-alive logic
|
|
|
|
if (sent_connection_header == false) {
|
|
|
|
if (this.should_keep_alive) {
|
|
|
|
header += "Connection: keep-alive\r\n";
|
2009-05-18 12:44:01 +02:00
|
|
|
} else {
|
2009-05-20 10:28:10 +02:00
|
|
|
this.closeOnFinish = true;
|
2009-05-18 19:33:05 +02:00
|
|
|
header += "Connection: close\r\n";
|
2009-05-18 12:44:01 +02:00
|
|
|
}
|
2009-05-18 19:33:05 +02:00
|
|
|
}
|
|
|
|
|
2009-05-20 13:00:20 +02:00
|
|
|
if ( sent_content_length_header == false
|
|
|
|
&& sent_transfer_encoding_header == false
|
|
|
|
)
|
|
|
|
{
|
2009-05-18 19:33:05 +02:00
|
|
|
header += "Transfer-Encoding: chunked\r\n";
|
|
|
|
chunked_encoding = true;
|
|
|
|
}
|
|
|
|
|
2009-05-19 13:12:46 +02:00
|
|
|
header += CRLF;
|
2009-05-18 19:33:05 +02:00
|
|
|
|
2009-05-26 17:46:56 +02:00
|
|
|
send(output, header);
|
2009-05-18 19:33:05 +02:00
|
|
|
};
|
|
|
|
|
2009-05-26 17:46:56 +02:00
|
|
|
this.sendBody = function (chunk, encoding) {
|
2009-05-18 19:33:05 +02:00
|
|
|
if (chunked_encoding) {
|
2009-05-26 17:46:56 +02:00
|
|
|
send(output, chunk.length.toString(16));
|
|
|
|
send(output, CRLF);
|
|
|
|
send(output, chunk, encoding);
|
|
|
|
send(output, CRLF);
|
2009-05-18 19:33:05 +02:00
|
|
|
} else {
|
2009-05-26 17:46:56 +02:00
|
|
|
send(output, chunk, encoding);
|
2009-05-18 19:33:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.flush();
|
|
|
|
};
|
|
|
|
|
2009-05-19 20:24:37 +02:00
|
|
|
this.flush = function () {
|
2009-05-26 17:46:56 +02:00
|
|
|
if (connection.readyState === "closed" || connection.readyState === "readOnly")
|
|
|
|
{
|
|
|
|
responses = [];
|
|
|
|
return;
|
|
|
|
}
|
2009-05-19 20:24:37 +02:00
|
|
|
if (responses.length > 0 && responses[0] === this)
|
|
|
|
while (output.length > 0) {
|
|
|
|
var out = output.shift();
|
2009-05-26 17:46:56 +02:00
|
|
|
connection.send(out[0], out[1]);
|
2009-05-19 20:24:37 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-05-18 19:33:05 +02:00
|
|
|
this.finished = false;
|
|
|
|
this.finish = function () {
|
|
|
|
if (chunked_encoding)
|
2009-05-26 17:46:56 +02:00
|
|
|
send(output, "0\r\n\r\n"); // last chunk
|
2009-05-18 19:33:05 +02:00
|
|
|
|
|
|
|
this.finished = true;
|
|
|
|
|
|
|
|
while (responses.length > 0 && responses[0].finished) {
|
|
|
|
var res = responses[0];
|
|
|
|
res.flush();
|
2009-05-19 20:24:37 +02:00
|
|
|
if (res.closeOnFinish)
|
|
|
|
connection.fullClose();
|
2009-05-18 19:33:05 +02:00
|
|
|
responses.shift();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2009-05-13 23:35:36 +02:00
|
|
|
|
2009-05-18 19:33:05 +02:00
|
|
|
/* This is a wrapper around the LowLevelServer interface. It provides
|
|
|
|
* connection handling, overflow checking, and some data buffering.
|
|
|
|
*/
|
|
|
|
node.http.Server = function (RequestHandler, options) {
|
|
|
|
if (!(this instanceof node.http.Server))
|
|
|
|
throw Error("Constructor called as a function");
|
2009-05-19 14:49:28 +02:00
|
|
|
var server = this;
|
2009-05-06 14:54:28 +02:00
|
|
|
|
2009-05-18 12:44:01 +02:00
|
|
|
function ConnectionHandler (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-12 03:46:04 +02:00
|
|
|
|
2009-05-18 12:44:01 +02:00
|
|
|
connection.onMessage = function ( ) {
|
2009-06-12 17:37:43 +02:00
|
|
|
var interrupted = false;
|
2009-05-18 12:44:01 +02:00
|
|
|
// filled in ...
|
|
|
|
var req = { method : null // at onHeadersComplete
|
|
|
|
, uri : "" // at onURI
|
2009-06-12 17:37:43 +02:00
|
|
|
, httpVersion : null // at onHeadersComplete
|
2009-05-18 12:44:01 +02:00
|
|
|
, headers : [] // at onHeaderField, onHeaderValue
|
|
|
|
, onBody : null // by user
|
|
|
|
, onBodyComplete : null // by user
|
2009-06-12 17:37:43 +02:00
|
|
|
, interrupt : function ( ) { interrupted = true; }
|
2009-05-20 10:17:07 +02:00
|
|
|
, setBodyEncoding : function (enc) {
|
|
|
|
connection.setEncoding(enc);
|
|
|
|
}
|
2009-06-12 17:37:43 +02:00
|
|
|
};
|
2009-05-18 19:33:05 +02:00
|
|
|
var res = new node.http.ServerResponse(connection, responses);
|
2009-05-18 12:44:01 +02:00
|
|
|
|
|
|
|
this.onURI = function (data) {
|
|
|
|
req.uri += data;
|
2009-06-12 17:37:43 +02:00
|
|
|
return !interrupted;
|
2009-05-18 12:44:01 +02:00
|
|
|
};
|
2009-05-12 03:46:04 +02:00
|
|
|
|
|
|
|
var last_was_value = false;
|
2009-05-18 12:44:01 +02:00
|
|
|
var headers = req.headers;
|
2009-05-06 14:54:28 +02:00
|
|
|
|
|
|
|
this.onHeaderField = function (data) {
|
2009-05-12 03:46:04 +02:00
|
|
|
if (headers.length > 0 && last_was_value == false)
|
|
|
|
headers[headers.length-1][0] += data;
|
|
|
|
else
|
|
|
|
headers.push([data]);
|
|
|
|
last_was_value = false;
|
2009-06-12 17:37:43 +02:00
|
|
|
return !interrupted;
|
2009-05-06 14:54:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this.onHeaderValue = function (data) {
|
2009-05-12 03:46:04 +02:00
|
|
|
var last_pair = headers[headers.length-1];
|
2009-05-06 14:54:28 +02:00
|
|
|
if (last_pair.length == 1)
|
|
|
|
last_pair[1] = data;
|
|
|
|
else
|
2009-05-12 03:46:04 +02:00
|
|
|
last_pair[1] += data;
|
|
|
|
last_was_value = true;
|
2009-06-12 17:37:43 +02:00
|
|
|
return !interrupted;
|
2009-05-06 14:54:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this.onHeadersComplete = function () {
|
2009-05-20 16:05:31 +02:00
|
|
|
req.httpVersion = this.httpVersion;
|
2009-06-12 17:37:43 +02:00
|
|
|
req.method = this.method;
|
|
|
|
// TODO parse the URI lazily?
|
|
|
|
req.uri = node.http.parseUri(req.uri);
|
2009-05-18 12:44:01 +02:00
|
|
|
res.should_keep_alive = this.should_keep_alive;
|
|
|
|
|
2009-06-12 17:37:43 +02:00
|
|
|
RequestHandler.apply(server, [req, res]);
|
|
|
|
|
|
|
|
return !interrupted;
|
2009-05-06 14:54:28 +02:00
|
|
|
};
|
|
|
|
|
2009-05-12 03:46:04 +02:00
|
|
|
this.onBody = function (chunk) {
|
2009-06-12 17:37:43 +02:00
|
|
|
if (req.onBody) req.onBody(chunk);
|
|
|
|
return !interrupted;
|
2009-05-12 03:46:04 +02:00
|
|
|
};
|
2009-05-06 14:54:28 +02:00
|
|
|
|
2009-06-12 15:24:57 +02:00
|
|
|
this.onMessageComplete = function () {
|
2009-06-12 17:37:43 +02:00
|
|
|
if (req.onBodyComplete) req.onBodyComplete();
|
|
|
|
return !interrupted;
|
2009-05-12 03:46:04 +02:00
|
|
|
};
|
2009-05-06 14:54:28 +02:00
|
|
|
};
|
2009-05-15 18:11:49 +02:00
|
|
|
|
|
|
|
// is this really needed?
|
|
|
|
connection.onEOF = function () {
|
2009-05-19 20:24:37 +02:00
|
|
|
if (responses.length == 0)
|
|
|
|
connection.close();
|
|
|
|
else
|
|
|
|
responses[responses.length-1].closeOnFinish = true;
|
2009-05-15 18:11:49 +02:00
|
|
|
};
|
2009-05-03 21:06:20 +02:00
|
|
|
}
|
2009-05-06 14:54:28 +02:00
|
|
|
|
2009-06-09 14:10:53 +02:00
|
|
|
this.__proto__ = new node.http.LowLevelServer(ConnectionHandler, options);
|
2009-05-03 21:06:20 +02:00
|
|
|
};
|
2009-05-15 18:11:49 +02:00
|
|
|
|
2009-05-19 14:49:28 +02:00
|
|
|
node.http.Client = function (port, host) {
|
|
|
|
var connection = new node.http.LowLevelClient();
|
2009-05-19 13:12:46 +02:00
|
|
|
var requests = [];
|
2009-06-04 12:36:08 +02:00
|
|
|
var self = this;
|
2009-05-15 18:11:49 +02:00
|
|
|
|
2009-05-19 13:12:46 +02:00
|
|
|
function ClientRequest (method, uri, header_lines) {
|
2009-06-04 15:41:40 +02:00
|
|
|
this.uri = uri;
|
2009-05-15 18:11:49 +02:00
|
|
|
|
|
|
|
var chunked_encoding = false;
|
2009-05-20 13:00:20 +02:00
|
|
|
this.closeOnFinish = false;
|
2009-05-15 18:11:49 +02:00
|
|
|
|
|
|
|
var sent_connection_header = false;
|
|
|
|
var sent_transfer_encoding_header = false;
|
|
|
|
var sent_content_length_header = false;
|
|
|
|
|
|
|
|
var header = method + " " + uri + " HTTP/1.1\r\n";
|
|
|
|
|
2009-05-19 13:12:46 +02:00
|
|
|
header_lines = header_lines || [];
|
2009-05-15 18:11:49 +02:00
|
|
|
for (var i = 0; i < header_lines.length; i++) {
|
|
|
|
var field = header_lines[i][0];
|
|
|
|
var value = header_lines[i][1];
|
|
|
|
|
2009-05-19 13:12:46 +02:00
|
|
|
header += field + ": " + value + CRLF;
|
2009-05-15 18:11:49 +02:00
|
|
|
|
|
|
|
if (connection_expression.exec(field)) {
|
|
|
|
sent_connection_header = true;
|
|
|
|
if (close_expression.exec(value))
|
2009-05-19 20:24:37 +02:00
|
|
|
this.closeOnFinish = true;
|
2009-05-15 18:11:49 +02:00
|
|
|
} else if (transfer_encoding_expression.exec(field)) {
|
|
|
|
sent_transfer_encoding_header = true;
|
|
|
|
if (chunk_expression.exec(value))
|
|
|
|
chunked_encoding = true;
|
|
|
|
} else if (content_length_expression.exec(field)) {
|
|
|
|
sent_content_length_header = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sent_connection_header == false) {
|
|
|
|
header += "Connection: keep-alive\r\n";
|
|
|
|
}
|
|
|
|
|
2009-05-19 13:12:46 +02:00
|
|
|
header += CRLF;
|
2009-05-20 13:00:20 +02:00
|
|
|
|
2009-06-04 15:41:40 +02:00
|
|
|
var output = [];
|
|
|
|
send(output, header);
|
2009-05-15 18:11:49 +02:00
|
|
|
|
2009-05-26 17:46:56 +02:00
|
|
|
this.sendBody = function (chunk, encoding) {
|
2009-06-06 23:57:15 +02:00
|
|
|
if (sent_content_length_header == false && chunked_encoding == false) {
|
|
|
|
throw "Content-Length header (or Transfer-Encoding:chunked) not set";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-19 13:12:46 +02:00
|
|
|
if (chunked_encoding) {
|
2009-05-26 17:46:56 +02:00
|
|
|
send(output, chunk.length.toString(16));
|
|
|
|
send(output, CRLF);
|
|
|
|
send(output, chunk, encoding);
|
|
|
|
send(output, CRLF);
|
2009-05-19 13:12:46 +02:00
|
|
|
} else {
|
2009-05-26 17:46:56 +02:00
|
|
|
send(output, chunk, encoding);
|
2009-05-19 13:12:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.flush();
|
2009-05-15 18:11:49 +02:00
|
|
|
};
|
|
|
|
|
2009-05-19 13:12:46 +02:00
|
|
|
this.flush = function ( ) {
|
2009-06-04 15:41:40 +02:00
|
|
|
if (connection.readyState == "closed") {
|
2009-05-19 13:12:46 +02:00
|
|
|
connection.connect(port, host);
|
|
|
|
return;
|
|
|
|
}
|
2009-06-16 20:53:15 +02:00
|
|
|
//node.debug("HTTP CLIENT flush. readyState = " + connection.readyState);
|
|
|
|
while ( this === requests[0]
|
|
|
|
&& output.length > 0
|
|
|
|
&& connection.readyState == "open"
|
|
|
|
)
|
|
|
|
{
|
2009-06-04 15:41:40 +02:00
|
|
|
var out = output.shift();
|
|
|
|
connection.send(out[0], out[1]);
|
2009-05-19 13:12:46 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.finished = false;
|
|
|
|
this.finish = function (responseHandler) {
|
|
|
|
this.responseHandler = responseHandler;
|
|
|
|
if (chunked_encoding)
|
2009-05-26 17:46:56 +02:00
|
|
|
send(output, "0\r\n\r\n"); // last chunk
|
2009-05-19 13:12:46 +02:00
|
|
|
|
|
|
|
this.flush();
|
2009-05-15 18:11:49 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-05-19 13:12:46 +02:00
|
|
|
connection.onConnect = function () {
|
2009-06-04 15:41:40 +02:00
|
|
|
//node.debug("HTTP CLIENT onConnect. readyState = " + connection.readyState);
|
2009-06-16 20:53:15 +02:00
|
|
|
//node.debug("requests[0].uri = '" + requests[0].uri + "'");
|
2009-05-19 13:12:46 +02:00
|
|
|
requests[0].flush();
|
|
|
|
};
|
|
|
|
|
2009-06-04 15:41:40 +02:00
|
|
|
connection.onEOF = function () {
|
|
|
|
connection.close();
|
|
|
|
};
|
|
|
|
|
2009-06-04 12:36:08 +02:00
|
|
|
connection.onDisconnect = function (had_error) {
|
|
|
|
if (had_error) {
|
|
|
|
if (self.onError) self.onError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-06-04 15:41:40 +02:00
|
|
|
//node.debug("HTTP CLIENT onDisconnect. readyState = " + connection.readyState);
|
2009-05-19 13:12:46 +02:00
|
|
|
// If there are more requests to handle, reconnect.
|
|
|
|
if (requests.length > 0) {
|
2009-05-19 14:49:28 +02:00
|
|
|
//node.debug("HTTP CLIENT: reconnecting");
|
|
|
|
this.connect(port, host);
|
2009-05-19 13:12:46 +02:00
|
|
|
}
|
|
|
|
};
|
2009-05-15 18:11:49 +02:00
|
|
|
|
2009-05-19 13:12:46 +02:00
|
|
|
// On response
|
|
|
|
connection.onMessage = function () {
|
|
|
|
var req = requests.shift();
|
2009-05-20 16:05:31 +02:00
|
|
|
var res = { statusCode : null // set in onHeadersComplete
|
|
|
|
, httpVersion : null // set in onHeadersComplete
|
|
|
|
, headers : [] // set in onHeaderField/Value
|
2009-05-20 13:00:20 +02:00
|
|
|
, setBodyEncoding : function (enc) {
|
|
|
|
connection.setEncoding(enc);
|
|
|
|
}
|
2009-05-19 13:12:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
var headers = res.headers;
|
|
|
|
var last_was_value = false;
|
|
|
|
|
|
|
|
this.onHeaderField = function (data) {
|
|
|
|
if (headers.length > 0 && last_was_value == false)
|
|
|
|
headers[headers.length-1][0] += data;
|
|
|
|
else
|
|
|
|
headers.push([data]);
|
|
|
|
last_was_value = false;
|
|
|
|
return true;
|
2009-05-15 18:11:49 +02:00
|
|
|
};
|
|
|
|
|
2009-05-19 13:12:46 +02:00
|
|
|
this.onHeaderValue = function (data) {
|
|
|
|
var last_pair = headers[headers.length-1];
|
|
|
|
if (last_pair.length == 1)
|
|
|
|
last_pair[1] = data;
|
|
|
|
else
|
|
|
|
last_pair[1] += data;
|
|
|
|
last_was_value = true;
|
|
|
|
return true;
|
2009-05-15 18:11:49 +02:00
|
|
|
};
|
2009-05-19 13:12:46 +02:00
|
|
|
|
|
|
|
this.onHeadersComplete = function () {
|
2009-05-20 16:05:31 +02:00
|
|
|
res.statusCode = this.statusCode;
|
|
|
|
res.httpVersion = this.httpVersion;
|
2009-05-19 13:12:46 +02:00
|
|
|
res.headers = headers;
|
|
|
|
|
|
|
|
req.responseHandler(res);
|
2009-06-12 15:24:57 +02:00
|
|
|
return true;
|
2009-05-19 13:12:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this.onBody = function (chunk) {
|
|
|
|
if (res.onBody)
|
|
|
|
return res.onBody(chunk);
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.onMessageComplete = function () {
|
|
|
|
connection.close();
|
|
|
|
|
|
|
|
if (res.onBodyComplete)
|
|
|
|
return res.onBodyComplete();
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
function newRequest (method, uri, headers) {
|
|
|
|
var req = new ClientRequest(method, uri, headers);
|
|
|
|
requests.push(req);
|
|
|
|
return req;
|
2009-05-15 18:11:49 +02:00
|
|
|
}
|
|
|
|
|
2009-05-19 13:12:46 +02:00
|
|
|
this.get = function (uri, headers) {
|
|
|
|
return newRequest("GET", uri, headers);
|
2009-05-15 18:11:49 +02:00
|
|
|
};
|
|
|
|
|
2009-05-19 13:12:46 +02:00
|
|
|
this.head = function (uri, headers) {
|
|
|
|
return newRequest("HEAD", uri, headers);
|
2009-05-15 18:11:49 +02:00
|
|
|
};
|
|
|
|
|
2009-05-19 13:12:46 +02:00
|
|
|
this.post = function (uri, headers) {
|
|
|
|
return newRequest("POST", uri, headers);
|
2009-05-15 18:11:49 +02:00
|
|
|
};
|
|
|
|
|
2009-05-19 13:12:46 +02:00
|
|
|
this.del = function (uri, headers) {
|
|
|
|
return newRequest("DELETE", uri, headers);
|
2009-05-15 18:11:49 +02:00
|
|
|
};
|
|
|
|
|
2009-05-19 13:12:46 +02:00
|
|
|
this.put = function (uri, headers) {
|
|
|
|
return newRequest("PUT", uri, headers);
|
2009-05-15 18:11:49 +02:00
|
|
|
};
|
|
|
|
};
|
2009-05-18 12:44:01 +02:00
|
|
|
|
2009-06-17 08:52:47 +02:00
|
|
|
node.http.cat = function(url, encoding, callback) {
|
|
|
|
var uri = node.http.parseUri(url)
|
2009-06-21 16:28:58 +02:00
|
|
|
var req = new node.http.Client(uri.port || 80, uri.host).get(uri.path || "/")
|
2009-06-17 08:52:47 +02:00
|
|
|
req.finish(function(res) {
|
2009-06-21 16:59:11 +02:00
|
|
|
var status = res.statusCode == 200 ? 0 : -1;
|
2009-06-17 08:52:47 +02:00
|
|
|
res.setBodyEncoding(encoding)
|
|
|
|
var content = ""
|
|
|
|
res.onBody = function(chunk) {
|
|
|
|
content += chunk;
|
|
|
|
}
|
|
|
|
res.onBodyComplete = function() {
|
|
|
|
callback(status, content);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2009-05-18 12:44:01 +02:00
|
|
|
})(); // anonymous namespace
|
2009-06-17 08:52:47 +02:00
|
|
|
|