validate js
This commit is contained in:
parent
0640517a91
commit
4b9f26c51a
140
src/http.js
140
src/http.js
@ -1,43 +1,44 @@
|
|||||||
(function () {
|
(function () {
|
||||||
CRLF = "\r\n";
|
CRLF = "\r\n";
|
||||||
node.http.STATUS_CODES = { 100 : 'Continue'
|
node.http.STATUS_CODES = {
|
||||||
, 101 : 'Switching Protocols'
|
100 : 'Continue',
|
||||||
, 200 : 'OK'
|
101 : 'Switching Protocols',
|
||||||
, 201 : 'Created'
|
200 : 'OK',
|
||||||
, 202 : 'Accepted'
|
201 : 'Created',
|
||||||
, 203 : 'Non-Authoritative Information'
|
202 : 'Accepted',
|
||||||
, 204 : 'No Content'
|
203 : 'Non-Authoritative Information',
|
||||||
, 205 : 'Reset Content'
|
204 : 'No Content',
|
||||||
, 206 : 'Partial Content'
|
205 : 'Reset Content',
|
||||||
, 300 : 'Multiple Choices'
|
206 : 'Partial Content',
|
||||||
, 301 : 'Moved Permanently'
|
300 : 'Multiple Choices',
|
||||||
, 302 : 'Moved Temporarily'
|
301 : 'Moved Permanently',
|
||||||
, 303 : 'See Other'
|
302 : 'Moved Temporarily',
|
||||||
, 304 : 'Not Modified'
|
303 : 'See Other',
|
||||||
, 305 : 'Use Proxy'
|
304 : 'Not Modified',
|
||||||
, 400 : 'Bad Request'
|
305 : 'Use Proxy',
|
||||||
, 401 : 'Unauthorized'
|
400 : 'Bad Request',
|
||||||
, 402 : 'Payment Required'
|
401 : 'Unauthorized',
|
||||||
, 403 : 'Forbidden'
|
402 : 'Payment Required',
|
||||||
, 404 : 'Not Found'
|
403 : 'Forbidden',
|
||||||
, 405 : 'Method Not Allowed'
|
404 : 'Not Found',
|
||||||
, 406 : 'Not Acceptable'
|
405 : 'Method Not Allowed',
|
||||||
, 407 : 'Proxy Authentication Required'
|
406 : 'Not Acceptable',
|
||||||
, 408 : 'Request Time-out'
|
407 : 'Proxy Authentication Required',
|
||||||
, 409 : 'Conflict'
|
408 : 'Request Time-out',
|
||||||
, 410 : 'Gone'
|
409 : 'Conflict',
|
||||||
, 411 : 'Length Required'
|
410 : 'Gone',
|
||||||
, 412 : 'Precondition Failed'
|
411 : 'Length Required',
|
||||||
, 413 : 'Request Entity Too Large'
|
412 : 'Precondition Failed',
|
||||||
, 414 : 'Request-URI Too Large'
|
413 : 'Request Entity Too Large',
|
||||||
, 415 : 'Unsupported Media Type'
|
414 : 'Request-URI Too Large',
|
||||||
, 500 : 'Internal Server Error'
|
415 : 'Unsupported Media Type',
|
||||||
, 501 : 'Not Implemented'
|
500 : 'Internal Server Error',
|
||||||
, 502 : 'Bad Gateway'
|
501 : 'Not Implemented',
|
||||||
, 503 : 'Service Unavailable'
|
502 : 'Bad Gateway',
|
||||||
, 504 : 'Gateway Time-out'
|
503 : 'Service Unavailable',
|
||||||
, 505 : 'HTTP Version not supported'
|
504 : 'Gateway Time-out',
|
||||||
};
|
505 : 'HTTP Version not supported'
|
||||||
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
parseUri 1.2.1
|
parseUri 1.2.1
|
||||||
@ -67,30 +68,31 @@ node.http.parseUri = function (str) {
|
|||||||
});
|
});
|
||||||
uri.toString = function () { return str; };
|
uri.toString = function () { return str; };
|
||||||
|
|
||||||
for (var i = o.key.length - 1; i >= 0; i--){
|
for (i = o.key.length - 1; i >= 0; i--){
|
||||||
if (uri[o.key[i]] == "") delete uri[o.key[i]];
|
if (uri[o.key[i]] == "") delete uri[o.key[i]];
|
||||||
};
|
}
|
||||||
|
|
||||||
return uri;
|
return uri;
|
||||||
};
|
};
|
||||||
|
|
||||||
node.http.parseUri.options = {
|
node.http.parseUri.options = {
|
||||||
strictMode: false,
|
strictMode: false,
|
||||||
key: [ "source"
|
key: [
|
||||||
, "protocol"
|
"source",
|
||||||
, "authority"
|
"protocol",
|
||||||
, "userInfo"
|
"authority",
|
||||||
, "user"
|
"userInfo",
|
||||||
, "password"
|
"user",
|
||||||
, "host"
|
"password",
|
||||||
, "port"
|
"host",
|
||||||
, "relative"
|
"port",
|
||||||
, "path"
|
"relative",
|
||||||
, "directory"
|
"path",
|
||||||
, "file"
|
"directory",
|
||||||
, "query"
|
"file",
|
||||||
, "anchor"
|
"query",
|
||||||
],
|
"anchor"
|
||||||
|
],
|
||||||
q: {
|
q: {
|
||||||
name: "params",
|
name: "params",
|
||||||
parser: /(?:^|&)([^&=]*)=?([^&]*)/g
|
parser: /(?:^|&)([^&=]*)=?([^&]*)/g
|
||||||
@ -121,7 +123,7 @@ function send (output, data, encoding) {
|
|||||||
encoding = "raw";
|
encoding = "raw";
|
||||||
|
|
||||||
output.push([data, encoding]);
|
output.push([data, encoding]);
|
||||||
};
|
}
|
||||||
|
|
||||||
/* This is a wrapper around the LowLevelServer interface. It provides
|
/* This is a wrapper around the LowLevelServer interface. It provides
|
||||||
* connection handling, overflow checking, and some data buffering.
|
* connection handling, overflow checking, and some data buffering.
|
||||||
@ -232,7 +234,7 @@ function connectionListener (connection) {
|
|||||||
connection.addListener("message_complete", function () {
|
connection.addListener("message_complete", function () {
|
||||||
req.emit("complete");
|
req.emit("complete");
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
node.http.ServerResponse = function (connection) {
|
node.http.ServerResponse = function (connection) {
|
||||||
var responses = connection.responses;
|
var responses = connection.responses;
|
||||||
@ -249,12 +251,7 @@ node.http.ServerResponse = function (connection) {
|
|||||||
var sent_content_length_header = false;
|
var sent_content_length_header = false;
|
||||||
|
|
||||||
var reason = node.http.STATUS_CODES[statusCode] || "unknown";
|
var reason = node.http.STATUS_CODES[statusCode] || "unknown";
|
||||||
var header = "HTTP/1.1 "
|
var header = "HTTP/1.1 " + statusCode.toString() + " " + reason + CRLF;
|
||||||
+ statusCode.toString()
|
|
||||||
+ " "
|
|
||||||
+ reason
|
|
||||||
+ CRLF
|
|
||||||
;
|
|
||||||
|
|
||||||
for (var i = 0; i < headers.length; i++) {
|
for (var i = 0; i < headers.length; i++) {
|
||||||
var field = headers[i][0];
|
var field = headers[i][0];
|
||||||
@ -283,10 +280,7 @@ node.http.ServerResponse = function (connection) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( sent_content_length_header == false
|
if ( sent_content_length_header == false && sent_transfer_encoding_header == false ) {
|
||||||
&& sent_transfer_encoding_header == false
|
|
||||||
)
|
|
||||||
{
|
|
||||||
header += "Transfer-Encoding: chunked\r\n";
|
header += "Transfer-Encoding: chunked\r\n";
|
||||||
chunked_encoding = true;
|
chunked_encoding = true;
|
||||||
}
|
}
|
||||||
@ -315,11 +309,12 @@ node.http.ServerResponse = function (connection) {
|
|||||||
responses = [];
|
responses = [];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (responses.length > 0 && responses[0] === this)
|
if (responses.length > 0 && responses[0] === this) {
|
||||||
while (output.length > 0) {
|
while (output.length > 0) {
|
||||||
var out = output.shift();
|
var out = output.shift();
|
||||||
connection.send(out[0], out[1]);
|
connection.send(out[0], out[1]);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.finished = false;
|
this.finished = false;
|
||||||
@ -348,11 +343,7 @@ node.http.Client.prototype.flush = function (request) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//node.debug("HTTP CLIENT flush. readyState = " + connection.readyState);
|
//node.debug("HTTP CLIENT flush. readyState = " + connection.readyState);
|
||||||
while ( request === this.requests[0]
|
while ( request === this.requests[0] && request.output.length > 0 && this.readyState == "open" ) {
|
||||||
&& request.output.length > 0
|
|
||||||
&& this.readyState == "open"
|
|
||||||
)
|
|
||||||
{
|
|
||||||
var out = request.output.shift();
|
var out = request.output.shift();
|
||||||
this.send(out[0], out[1]);
|
this.send(out[0], out[1]);
|
||||||
}
|
}
|
||||||
@ -363,7 +354,7 @@ node.http.createClient = function (port, host) {
|
|||||||
|
|
||||||
client.requests = [];
|
client.requests = [];
|
||||||
|
|
||||||
client.reconnect = function () { return client.connect(port, host) };
|
client.reconnect = function () { return client.connect(port, host); };
|
||||||
|
|
||||||
client.addListener("connect", function () {
|
client.addListener("connect", function () {
|
||||||
//node.debug("HTTP CLIENT onConnect. readyState = " + client.readyState);
|
//node.debug("HTTP CLIENT onConnect. readyState = " + client.readyState);
|
||||||
@ -508,7 +499,6 @@ function createClientRequest (connection, method, uri, header_lines) {
|
|||||||
req.sendBody = function (chunk, encoding) {
|
req.sendBody = function (chunk, encoding) {
|
||||||
if (sent_content_length_header == false && chunked_encoding == false) {
|
if (sent_content_length_header == false && chunked_encoding == false) {
|
||||||
throw "Content-Length header (or Transfer-Encoding:chunked) not set";
|
throw "Content-Length header (or Transfer-Encoding:chunked) not set";
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (chunked_encoding) {
|
if (chunked_encoding) {
|
||||||
|
30
src/node.js
30
src/node.js
@ -60,8 +60,7 @@ node.path = new function () {
|
|||||||
} else if (i === arguments.length - 1) {
|
} else if (i === arguments.length - 1) {
|
||||||
part = part.replace(/^\/*/, "");
|
part = part.replace(/^\/*/, "");
|
||||||
} else {
|
} else {
|
||||||
part = part.replace(/^\/*/, "")
|
part = part.replace(/^\/*/, "").replace(/\/*$/, "/");
|
||||||
.replace(/\/*$/, "/");
|
|
||||||
}
|
}
|
||||||
joined += part;
|
joined += part;
|
||||||
}
|
}
|
||||||
@ -129,18 +128,17 @@ node.Module.prototype.load = function (callback) {
|
|||||||
self.target.__include = function (path) { self.newChild(path, self.target); };
|
self.target.__include = function (path) { self.newChild(path, self.target); };
|
||||||
|
|
||||||
// create wrapper function
|
// create wrapper function
|
||||||
var wrapper = "function (__filename) {\n"
|
var wrapper = "function (__filename) {\n"+
|
||||||
+ " var onLoad;\n"
|
" var onLoad;\n"+
|
||||||
+ " var onExit;\n"
|
" var onExit;\n"+
|
||||||
+ " var exports = this;\n"
|
" var exports = this;\n"+
|
||||||
+ " var require = this.__require;\n"
|
" var require = this.__require;\n"+
|
||||||
+ " var include = this.__include;\n"
|
" var include = this.__include;\n"+
|
||||||
+ content
|
content+
|
||||||
+ "\n"
|
"\n"+
|
||||||
+ " this.__onLoad = onLoad;\n"
|
" this.__onLoad = onLoad;\n"+
|
||||||
+ " this.__onExit = onExit;\n"
|
" this.__onExit = onExit;\n"+
|
||||||
+ "};\n"
|
"};\n";
|
||||||
;
|
|
||||||
var compiled_wrapper = node.compile(wrapper, self.filename);
|
var compiled_wrapper = node.compile(wrapper, self.filename);
|
||||||
compiled_wrapper.apply(self.target, [self.filename]);
|
compiled_wrapper.apply(self.target, [self.filename]);
|
||||||
self.onLoad = self.target.__onLoad;
|
self.onLoad = self.target.__onLoad;
|
||||||
@ -200,7 +198,7 @@ node.Module.prototype.exit = function (callback) {
|
|||||||
this.exitChildren(function () {
|
this.exitChildren(function () {
|
||||||
if (self.onExit) self.onExit();
|
if (self.onExit) self.onExit();
|
||||||
self.exited = true;
|
self.exited = true;
|
||||||
if (callback) callback()
|
if (callback) callback();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -218,4 +216,4 @@ node.Module.prototype.exit = function (callback) {
|
|||||||
node.reallyExit(code);
|
node.reallyExit(code);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}())
|
}());
|
||||||
|
@ -10,10 +10,10 @@ function onLoad () {
|
|||||||
var x = node.path.join(fixtures, "x.txt");
|
var x = node.path.join(fixtures, "x.txt");
|
||||||
|
|
||||||
file = new node.fs.File;
|
file = new node.fs.File;
|
||||||
file.addListener("error", function () { got_error = true });
|
file.addListener("error", function () { got_error = true; });
|
||||||
|
|
||||||
file.open(x, "r").addCallback(function () {
|
file.open(x, "r").addCallback(function () {
|
||||||
opened = true
|
opened = true;
|
||||||
file.close().addCallback(function () {
|
file.close().addCallback(function () {
|
||||||
closed = true;
|
closed = true;
|
||||||
});
|
});
|
||||||
|
@ -16,16 +16,14 @@ var got_good_server_content = false;
|
|||||||
var bad_server_got_error = false;
|
var bad_server_got_error = false;
|
||||||
|
|
||||||
function onLoad () {
|
function onLoad () {
|
||||||
node.http.cat("http://localhost:"+PORT+"/", "utf8")
|
node.http.cat("http://localhost:"+PORT+"/", "utf8").addCallback(function (content) {
|
||||||
.addCallback(function (content) {
|
|
||||||
puts("got response");
|
puts("got response");
|
||||||
got_good_server_content = true;
|
got_good_server_content = true;
|
||||||
assertEquals(body, content);
|
assertEquals(body, content);
|
||||||
server.close();
|
server.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
node.http.cat("http://localhost:12312/", "utf8")
|
node.http.cat("http://localhost:12312/", "utf8").addErrback(function () {
|
||||||
.addErrback(function () {
|
|
||||||
puts("got error (this should happen)");
|
puts("got error (this should happen)");
|
||||||
bad_server_got_error = true;
|
bad_server_got_error = true;
|
||||||
});
|
});
|
||||||
|
@ -20,7 +20,7 @@ function onLoad () {
|
|||||||
|
|
||||||
promise.addCallback(function (content) {
|
promise.addCallback(function (content) {
|
||||||
assertEquals(body, content);
|
assertEquals(body, content);
|
||||||
server.close()
|
server.close();
|
||||||
successes += 1;
|
successes += 1;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -35,7 +35,7 @@ function onLoad () {
|
|||||||
promise = node.cat(x, "utf8");
|
promise = node.cat(x, "utf8");
|
||||||
|
|
||||||
promise.addCallback(function (content) {
|
promise.addCallback(function (content) {
|
||||||
assertEquals("xyz", content.replace(/[\r\n]/, ''))
|
assertEquals("xyz", content.replace(/[\r\n]/, ''));
|
||||||
successes += 1;
|
successes += 1;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user