From f6657c3c9dd3e41df7147adc86ebc6469538e1ed Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 28 Sep 2009 12:36:36 +0200 Subject: [PATCH] Move http library to /http.js --- benchmark/http_simple.js | 8 ++- benchmark/process_loop.js | 3 + benchmark/run.js | 3 + benchmark/static_http_server.js | 7 ++- doc/api.html | 53 ++++++++-------- doc/api.txt | 52 ++++++++------- doc/api.xml | 63 ++++++++++--------- doc/index.html | 10 +-- doc/node.1 | 49 ++++++++------- {src => lib}/http.js | 43 +++++++------ src/node.cc | 1 - src/node.js | 8 +++ src/util.js | 8 ++- .../test-cat.js} | 3 +- test/mjsunit/test-http-cat.js | 8 ++- test/mjsunit/test-http-client-race.js | 5 +- test/mjsunit/test-http-client-upload.js | 5 +- test/mjsunit/test-http-malformed-request.js | 3 +- test/mjsunit/test-http-proxy.js | 9 +-- test/mjsunit/test-http-server.js | 3 +- test/mjsunit/test-http.js | 5 +- test/mjsunit/test-multipart.js | 3 +- wscript | 1 - 23 files changed, 200 insertions(+), 153 deletions(-) rename {src => lib}/http.js (92%) rename test/mjsunit/{test-node-cat.js => disabled/test-cat.js} (92%) diff --git a/benchmark/http_simple.js b/benchmark/http_simple.js index d1801e7fd5..f334c01fd5 100644 --- a/benchmark/http_simple.js +++ b/benchmark/http_simple.js @@ -1,9 +1,15 @@ +libDir = node.path.join(node.path.dirname(__filename), "../lib"); +node.libraryPaths.unshift(libDir); + +include("/utils.js"); +http = require("/http.js"); + fixed = "" for (var i = 0; i < 20*1024; i++) { fixed += "C"; } stored = {}; -node.http.createServer(function (req, res) { +http.createServer(function (req, res) { var commands = req.uri.path.split("/"); var command = commands[1]; var body = ""; diff --git a/benchmark/process_loop.js b/benchmark/process_loop.js index 67b9edd74b..ec971f0959 100644 --- a/benchmark/process_loop.js +++ b/benchmark/process_loop.js @@ -1,3 +1,6 @@ +libDir = node.path.join(node.path.dirname(__filename), "../lib"); +node.libraryPaths.unshift(libDir); +include("/utils.js"); function next (i) { if (i <= 0) return; diff --git a/benchmark/run.js b/benchmark/run.js index a8a4d1715e..d9c2668687 100644 --- a/benchmark/run.js +++ b/benchmark/run.js @@ -1,3 +1,6 @@ +libDir = node.path.join(node.path.dirname(__filename), "../lib"); +node.libraryPaths.unshift(libDir); +include("/utils.js"); var benchmarks = [ "static_http_server.js" , "timers.js" , "process_loop.js" diff --git a/benchmark/static_http_server.js b/benchmark/static_http_server.js index ef465a8462..3ce04997f0 100644 --- a/benchmark/static_http_server.js +++ b/benchmark/static_http_server.js @@ -1,3 +1,6 @@ +libDir = node.path.join(node.path.dirname(__filename), "../lib"); +node.libraryPaths.unshift(libDir); +http = require("/http.js"); var concurrency = 30; var port = 8000; var n = 700; @@ -11,7 +14,7 @@ for (var i = 0; i < bytes; i++) { body += "C"; } -var server = node.http.createServer(function (req, res) { +var server = http.createServer(function (req, res) { res.sendHeader(200, { "Content-Type": "text/plain", "Content-Length": body.length @@ -35,7 +38,7 @@ function responseListener (res) { } for (var i = 0; i < concurrency; i++) { - var client = node.http.createClient(port); + var client = http.createClient(port); client.id = i; client.get("/").finish(responseListener); requests++; diff --git a/doc/api.html b/doc/api.html index fe7d4c1b16..d564939739 100644 --- a/doc/api.html +++ b/doc/api.html @@ -37,7 +37,8 @@ World":

include("/utils.js");
-node.http.createServer(function (request, response) {
+include("/http.js");
+createServer(function (request, response) {
   response.sendHeader(200, {"Content-Type": "text/plain"});
   response.sendBody("Hello World\n");
   response.finish();
@@ -940,6 +941,8 @@ on error: no parameters.
 
 

HTTP

+

To use the HTTP server and client one must require("/http.js") or +include("/http.js").

The HTTP interfaces in Node are designed to support many features of the protocol which have been traditionally difficult to use. In particular, large, possibly chunk-encoded, messages. The interface is @@ -948,11 +951,11 @@ user is able to stream data.

HTTP message headers are represented by an object like this

-
  { "Content-Length": "123"
-  , "Content-Type": "text/plain"
-  , "Connection": "keep-alive"
-  , "Accept": "*/*"
-  }
+
{ "Content-Length": "123"
+, "Content-Type": "text/plain"
+, "Connection": "keep-alive"
+, "Accept": "*/*"
+}

In order to support the full spectrum of possible HTTP applications, Node’s HTTP API is very low-level. It deals with connection handling and message @@ -960,7 +963,7 @@ parsing only. It parses a message into headers and body but it does not parse the actual headers or the body. That means, for example, that Node does not, and will never, provide API to access or manipulate Cookies or multi-part bodies. This is left to the user.

-

node.http.Server

+

http.Server

- + @@ -1004,7 +1007,7 @@ want to access this event. The connection can also be accessed at
-node.http.createServer(request_listener, options); +http.createServer(request_listener, options);

@@ -1035,7 +1038,7 @@ Stops the server from accepting new connections.

-

node.http.ServerRequest

+

http.ServerRequest

This object is created internally by a HTTP server—not by the user—and passed as the first argument to a "request" listener.

@@ -1156,11 +1159,11 @@ Resumes a paused request.

-The node.http.Connection object. +The http.Connection object.

-

node.http.ServerResponse

+

http.ServerResponse

This object is created internally by a HTTP server—not by the user. It is passed as the second parameter to the "request" event.

@@ -1216,7 +1219,7 @@ response.

-

node.http.Client

+

http.Client

An HTTP client is constructed with a server address as its argument, the returned handle is then used to issue one or more requests. Depending on the server connected to, the client might @@ -1225,7 +1228,7 @@ connection. Currently the implementation does not pipeline requests.

Example of connecting to google.com

-
var google = node.http.createClient(80, "google.com");
+
var google = http.createClient(80, "google.com");
 var request = google.get("/");
 request.finish(function (response) {
   puts("STATUS: " + response.statusCode);
@@ -1238,7 +1241,7 @@ request.finish(function (response) {
 
-node.http.createClient(port, host) +http.createClient(port, host)

@@ -1252,7 +1255,7 @@ connection is not established until a request is issued.

-Issues a request; if necessary establishes connection. Returns a node.http.ClientRequest instance. +Issues a request; if necessary establishes connection. Returns a http.ClientRequest instance.

request_headers is optional. Additional request headers might be added internally @@ -1275,9 +1278,9 @@ for the user to stream a body to the server with

-

node.http.ClientRequest

+

http.ClientRequest

This object is created internally and returned from the request methods of a -node.http.Client. It represents an in-progress request whose header has +http.Client. It represents an in-progress request whose header has already been sent.

"request"

request, response

request is an instance of node.http.ServerRequest
-response is an instance of node.http.ServerResponse

request is an instance of http.ServerRequest
+response is an instance of http.ServerResponse

"connection"

connection

When a new TCP connection is established. -connection is an object of type node.http.Connection. Usually users will not +connection is an object of type http.Connection. Usually users will not want to access this event. The connection can also be accessed at request.connection.

+The response argument will be an instance of http.ClientResponse.

Emitted when a response is received to this request. Typically the user will set a listener to this via the request.finish() method.
This event is emitted only once.
-The response argument will be an instance of node.http.ClientResponse.

@@ -1338,7 +1341,7 @@ chunked, this will send the terminating "0\r\n\r\n".

The parameter responseListener is a callback which will be executed when the response headers have been received. The responseListener callback is executed with one -argument which is an instance of node.http.ClientResponse.

+argument which is an instance of http.ClientResponse.

In the responseListener callback, one can add more listeners to the response, in particular listening for the "body" event. Note that the responseListener is called before any part of the body is receieved, @@ -1365,7 +1368,7 @@ request.finish(function (response) {

-

node.http.ClientResponse

+

http.ClientResponse

This object is created internally and passed to the "response" event.

- A reference to the node.http.Client that this response belongs to. + A reference to the http.Client that this response belongs to.

@@ -1943,7 +1946,7 @@ init (Handle<Object> target) diff --git a/doc/api.txt b/doc/api.txt index 3f858c4002..cd7f178ff6 100644 --- a/doc/api.txt +++ b/doc/api.txt @@ -17,7 +17,8 @@ World": ---------------------------------------- include("/utils.js"); -node.http.createServer(function (request, response) { +include("/http.js"); +createServer(function (request, response) { response.sendHeader(200, {"Content-Type": "text/plain"}); response.sendBody("Hello World\n"); response.finish(); @@ -568,6 +569,9 @@ Objects returned from +node.fs.stat()+ are of this type. === HTTP +To use the HTTP server and client one must +require("/http.js")+ or ++include("/http.js")+. + The HTTP interfaces in Node are designed to support many features of the protocol which have been traditionally difficult to use. In particular, large, possibly chunk-encoded, messages. The interface is @@ -577,11 +581,11 @@ user is able to stream data. HTTP message headers are represented by an object like this ---------------------------------------- - { "Content-Length": "123" - , "Content-Type": "text/plain" - , "Connection": "keep-alive" - , "Accept": "*/*" - } +{ "Content-Length": "123" +, "Content-Type": "text/plain" +, "Connection": "keep-alive" +, "Accept": "*/*" +} ---------------------------------------- In order to support the full spectrum of possible HTTP applications, Node's @@ -592,20 +596,20 @@ does not, and will never, provide API to access or manipulate Cookies or multi-part bodies. _This is left to the user._ -==== +node.http.Server+ +==== +http.Server+ [cols="1,2,10",options="header"] |========================================================= |Event | Parameters | Notes |+"request"+ | +request, response+ | -+request+ is an instance of +node.http.ServerRequest+ ++request+ is an instance of +http.ServerRequest+ + -+response+ is an instance of +node.http.ServerResponse+ ++response+ is an instance of +http.ServerResponse+ |+"connection"+ | +connection+ | When a new TCP connection is established. -+connection+ is an object of type +node.http.Connection+. Usually users will not ++connection+ is an object of type +http.Connection+. Usually users will not want to access this event. The +connection+ can also be accessed at +request.connection+. @@ -616,7 +620,7 @@ want to access this event. The +connection+ can also be accessed at |========================================================= -+node.http.createServer(request_listener, options);+ :: ++http.createServer(request_listener, options);+ :: Returns a new web server object. + The +options+ argument is optional. The @@ -636,7 +640,7 @@ Stops the server from accepting new connections. -==== +node.http.ServerRequest+ +==== +http.ServerRequest+ This object is created internally by a HTTP server--not by the user--and passed as the first argument to a +"request"+ listener. @@ -712,10 +716,10 @@ Resumes a paused request. +request.connection+ :: -The +node.http.Connection+ object. +The +http.Connection+ object. -==== +node.http.ServerResponse+ +==== +http.ServerResponse+ This object is created internally by a HTTP server--not by the user. It is passed as the second parameter to the +"request"+ event. @@ -766,7 +770,7 @@ response. -==== +node.http.Client+ +==== +http.Client+ An HTTP client is constructed with a server address as its argument, the returned handle is then used to issue one or more @@ -777,7 +781,7 @@ connection. _Currently the implementation does not pipeline requests._ Example of connecting to +google.com+ ---------------------------------------- -var google = node.http.createClient(80, "google.com"); +var google = http.createClient(80, "google.com"); var request = google.get("/"); request.finish(function (response) { puts("STATUS: " + response.statusCode); @@ -789,7 +793,7 @@ request.finish(function (response) { }); ---------------------------------------- -+node.http.createClient(port, host)+ :: ++http.createClient(port, host)+ :: Constructs a new HTTP client. +port+ and +host+ refer to the server to be connected to. A @@ -797,7 +801,7 @@ connection is not established until a request is issued. +client.get(path, request_headers)+, +client.head(path, request_headers)+, +client.post(path, request_headers)+, +client.del(path, request_headers)+, +client.put(path, request_headers)+ :: -Issues a request; if necessary establishes connection. Returns a +node.http.ClientRequest+ instance. +Issues a request; if necessary establishes connection. Returns a +http.ClientRequest+ instance. + +request_headers+ is optional. Additional request headers might be added internally @@ -815,10 +819,10 @@ for the user to stream a body to the server with +request.sendBody()+.) -==== +node.http.ClientRequest+ +==== +http.ClientRequest+ This object is created internally and returned from the request methods of a -+node.http.Client+. It represents an _in-progress_ request whose header has ++http.Client+. It represents an _in-progress_ request whose header has already been sent. [cols="1,2,10",options="header"] @@ -830,7 +834,7 @@ set a listener to this via the +request.finish()+ method. + This event is emitted only once. + -The +response+ argument will be an instance of +node.http.ClientResponse+. +The +response+ argument will be an instance of +http.ClientResponse+. |========================================================= @@ -861,7 +865,7 @@ chunked, this will send the terminating +"0\r\n\r\n"+. The parameter +responseListener+ is a callback which will be executed when the response headers have been received. The +responseListener+ callback is executed with one -argument which is an instance of +node.http.ClientResponse+. +argument which is an instance of +http.ClientResponse+. + In the +responseListener+ callback, one can add more listeners to the response, in particular listening for the +"body"+ event. Note that @@ -891,7 +895,7 @@ request.finish(function (response) { -==== +node.http.ClientResponse+ +==== +http.ClientResponse+ This object is created internally and passed to the +"response"+ event. @@ -932,7 +936,7 @@ After emitted no other events will be emitted on the response. Resumes a paused response. +response.client+ :: - A reference to the +node.http.Client+ that this response belongs to. + A reference to the +http.Client+ that this response belongs to. diff --git a/doc/api.xml b/doc/api.xml index 470c079245..c6ccf14c12 100644 --- a/doc/api.xml +++ b/doc/api.xml @@ -13,7 +13,8 @@ An example of a web server written with Node which responds with "Hello World":include("/utils.js"); -node.http.createServer(function (request, response) { +include("/http.js"); +createServer(function (request, response) { response.sendHeader(200, {"Content-Type": "text/plain"}); response.sendBody("Hello World\n"); response.finish(); @@ -988,25 +989,27 @@ on error: no parameters. HTTP +To use the HTTP server and client one must require("/http.js") or +include("/http.js"). The HTTP interfaces in Node are designed to support many features of the protocol which have been traditionally difficult to use. In particular, large, possibly chunk-encoded, messages. The interface is careful to never buffer entire requests or responses—the user is able to stream data. HTTP message headers are represented by an object like this - { "Content-Length": "123" - , "Content-Type": "text/plain" - , "Connection": "keep-alive" - , "Accept": "*/*" - } +{ "Content-Length": "123" +, "Content-Type": "text/plain" +, "Connection": "keep-alive" +, "Accept": "*/*" +} In order to support the full spectrum of possible HTTP applications, Node’s HTTP API is very low-level. It deals with connection handling and message parsing only. It parses a message into headers and body but it does not parse the actual headers or the body. That means, for example, that Node does not, and will never, provide API to access or manipulate Cookies or multi-part bodies. This is left to the user. - -<literal>node.http.Server</literal> + +<literal>http.Server</literal> "request" request, response -request is an instance of node.http.ServerRequest -response is an instance of node.http.ServerResponse +request is an instance of http.ServerRequest +response is an instance of http.ServerResponse "connection" connection When a new TCP connection is established. -connection is an object of type node.http.Connection. Usually users will not +connection is an object of type http.Connection. Usually users will not want to access this event. The connection can also be accessed at request.connection. @@ -1051,7 +1054,7 @@ want to access this event. The connection can also be accesse -node.http.createServer(request_listener, options); +http.createServer(request_listener, options); @@ -1088,8 +1091,8 @@ Stops the server from accepting new connections. - -<literal>node.http.ServerRequest</literal> + +<literal>http.ServerRequest</literal> This object is created internally by a HTTP server—not by the user—and passed as the first argument to a "request" listener. -The node.http.Connection object. +The http.Connection object. - -<literal>node.http.ServerResponse</literal> + +<literal>http.ServerResponse</literal> This object is created internally by a HTTP server—not by the user. It is passed as the second parameter to the "request" event. @@ -1286,15 +1289,15 @@ response. - -<literal>node.http.Client</literal> + +<literal>http.Client</literal> An HTTP client is constructed with a server address as its argument, the returned handle is then used to issue one or more requests. Depending on the server connected to, the client might pipeline the requests or reestablish the connection after each connection. Currently the implementation does not pipeline requests. Example of connecting to google.com -var google = node.http.createClient(80, "google.com"); +var google = http.createClient(80, "google.com"); var request = google.get("/"); request.finish(function (response) { puts("STATUS: " + response.statusCode); @@ -1307,7 +1310,7 @@ request.finish(function (response) { -node.http.createClient(port, host) +http.createClient(port, host) @@ -1323,7 +1326,7 @@ connection is not established until a request is issued. -Issues a request; if necessary establishes connection. Returns a node.http.ClientRequest instance. +Issues a request; if necessary establishes connection. Returns a http.ClientRequest instance. request_headers is optional. Additional request headers might be added internally @@ -1341,10 +1344,10 @@ for the user to stream a body to the server with - -<literal>node.http.ClientRequest</literal> + +<literal>http.ClientRequest</literal> This object is created internally and returned from the request methods of a -node.http.Client. It represents an in-progress request whose header has +http.Client. It represents an in-progress request whose header has already been sent. Emitted when a response is received to this request. Typically the user will set a listener to this via the request.finish() method. This event is emitted only once. -The response argument will be an instance of node.http.ClientResponse. +The response argument will be an instance of http.ClientResponse. @@ -1408,7 +1411,7 @@ chunked, this will send the terminating "0\r\n\r\n". The parameter responseListener is a callback which will be executed when the response headers have been received. The responseListener callback is executed with one -argument which is an instance of node.http.ClientResponse. +argument which is an instance of http.ClientResponse. In the responseListener callback, one can add more listeners to the response, in particular listening for the "body" event. Note that the responseListener is called before any part of the body is receieved, @@ -1434,8 +1437,8 @@ request.finish(function (response) { - -<literal>node.http.ClientResponse</literal> + +<literal>http.ClientResponse</literal> This object is created internally and passed to the "response" event. - A reference to the node.http.Client that this response belongs to. + A reference to the http.Client that this response belongs to. diff --git a/doc/index.html b/doc/index.html index dbce7afc15..88094f3b05 100644 --- a/doc/index.html +++ b/doc/index.html @@ -41,16 +41,16 @@

-utils = require("/utils.js");
-server = node.http.createServer(function (req, res) {
+include("/utils.js");
+include("/http.js");
+createServer(function (req, res) {
   setTimeout(function () {
     res.sendHeader(200, {"Content-Type": "text/plain"});
     res.sendBody("Hello World");
     res.finish();
   }, 2000);
-});
-server.listen(8000);
-utils.puts("Server running at http://127.0.0.1:8000/");
+}).listen(8000); +puts("Server running at http://127.0.0.1:8000/");

diff --git a/doc/node.1 b/doc/node.1 index b36944316f..75b9811620 100644 --- a/doc/node.1 +++ b/doc/node.1 @@ -19,7 +19,8 @@ An example of a web server written with Node which responds with "Hello World": .RS 4 .nf include("/utils\.js"); -node\.http\.createServer(function (request, response) { +include("/http\.js"); +createServer(function (request, response) { response\.sendHeader(200, {"Content\-Type": "text/plain"}); response\.sendBody("Hello World\en"); response\.finish(); @@ -819,6 +820,8 @@ stats\.isFile(), stats\.isDirectory(), stats\.isBlockDevice(), stats\.isCharacte .RE .RE .SS "HTTP" +To use the HTTP server and client one must require("/http\.js") or include("/http\.js")\. +.sp The HTTP interfaces in Node are designed to support many features of the protocol which have been traditionally difficult to use\. In particular, large, possibly chunk\-encoded, messages\. The interface is careful to never buffer entire requests or responses\(emthe user is able to stream data\. .sp HTTP message headers are represented by an object like this @@ -826,11 +829,11 @@ HTTP message headers are represented by an object like this .sp .RS 4 .nf - { "Content\-Length": "123" - , "Content\-Type": "text/plain" - , "Connection": "keep\-alive" - , "Accept": "*/*" - } +{ "Content\-Length": "123" +, "Content\-Type": "text/plain" +, "Connection": "keep\-alive" +, "Accept": "*/*" +} .fi .RE In order to support the full spectrum of possible HTTP applications, Node\(cqs HTTP API is very low\-level\. It deals with connection handling and message parsing only\. It parses a message into headers and body but it does not parse the actual headers or the body\. That means, for example, that Node does not, and will never, provide API to access or manipulate Cookies or multi\-part bodies\. \fIThis is left to the user\.\fR @@ -840,7 +843,7 @@ In order to support the full spectrum of possible HTTP applications, Node\(cqs H .nr an-no-space-flag 1 .nr an-break-flag 1 .br -node.http.Server +http.Server .RS .TS allbox tab(:); @@ -863,7 +866,7 @@ T}:T{ request, response .sp T}:T{ -request is an instance of node\.http\.ServerRequest response is an instance of node\.http\.ServerResponse +request is an instance of http\.ServerRequest response is an instance of http\.ServerResponse .sp T} T{ @@ -873,7 +876,7 @@ T}:T{ connection .sp T}:T{ -When a new TCP connection is established\. connection is an object of type node\.http\.Connection\. Usually users will not want to access this event\. The connection can also be accessed at request\.connection\. +When a new TCP connection is established\. connection is an object of type http\.Connection\. Usually users will not want to access this event\. The connection can also be accessed at request\.connection\. .sp T} T{ @@ -888,7 +891,7 @@ Emitted when the server closes\. errorno is an integer which indicates what, if T} .TE .PP -node\.http\.createServer(request_listener, options); +http\.createServer(request_listener, options); .RS 4 Returns a new web server object\. .sp @@ -922,7 +925,7 @@ Stops the server from accepting new connections\. .nr an-no-space-flag 1 .nr an-break-flag 1 .br -node.http.ServerRequest +http.ServerRequest .RS This object is created internally by a HTTP server\(emnot by the user\(emand passed as the first argument to a "request" listener\. .sp @@ -1047,7 +1050,7 @@ Resumes a paused request\. request\.connection .RS 4 The -node\.http\.Connection +http\.Connection object\. .RE .RE @@ -1056,7 +1059,7 @@ object\. .nr an-no-space-flag 1 .nr an-break-flag 1 .br -node.http.ServerResponse +http.ServerResponse .RS This object is created internally by a HTTP server\(emnot by the user\. It is passed as the second parameter to the "request" event\. .PP @@ -1116,7 +1119,7 @@ response\.finish(), MUST be called on each response\. .nr an-no-space-flag 1 .nr an-break-flag 1 .br -node.http.Client +http.Client .RS An HTTP client is constructed with a server address as its argument, the returned handle is then used to issue one or more requests\. Depending on the server connected to, the client might pipeline the requests or reestablish the connection after each connection\. \fICurrently the implementation does not pipeline requests\.\fR .sp @@ -1125,7 +1128,7 @@ Example of connecting to google\.com .sp .RS 4 .nf -var google = node\.http\.createClient(80, "google\.com"); +var google = http\.createClient(80, "google\.com"); var request = google\.get("/"); request\.finish(function (response) { puts("STATUS: " + response\.statusCode); @@ -1138,7 +1141,7 @@ request\.finish(function (response) { .fi .RE .PP -node\.http\.createClient(port, host) +http\.createClient(port, host) .RS 4 Constructs a new HTTP client\. port @@ -1150,7 +1153,7 @@ refer to the server to be connected to\. A connection is not established until a client\.get(path, request_headers), client\.head(path, request_headers), client\.post(path, request_headers), client\.del(path, request_headers), client\.put(path, request_headers) .RS 4 Issues a request; if necessary establishes connection\. Returns a -node\.http\.ClientRequest +http\.ClientRequest instance\. .sp request_headers @@ -1179,9 +1182,9 @@ request\.sendBody()\.) .nr an-no-space-flag 1 .nr an-break-flag 1 .br -node.http.ClientRequest +http.ClientRequest .RS -This object is created internally and returned from the request methods of a node\.http\.Client\. It represents an \fIin\-progress\fR request whose header has already been sent\. +This object is created internally and returned from the request methods of a http\.Client\. It represents an \fIin\-progress\fR request whose header has already been sent\. .sp .TS allbox tab(:); @@ -1202,7 +1205,7 @@ T}:T{ response .sp T}:T{ -Emitted when a response is received to this request\. Typically the user will set a listener to this via the request\.finish() method\. This event is emitted only once\. The response argument will be an instance of node\.http\.ClientResponse\. +Emitted when a response is received to this request\. Typically the user will set a listener to this via the request\.finish() method\. This event is emitted only once\. The response argument will be an instance of http\.ClientResponse\. .sp T} .TE @@ -1237,7 +1240,7 @@ responseListener is a callback which will be executed when the response headers have been received\. The responseListener callback is executed with one argument which is an instance of -node\.http\.ClientResponse\. +http\.ClientResponse\. .sp In the responseListener @@ -1277,7 +1280,7 @@ request\.finish(function (response) { .nr an-no-space-flag 1 .nr an-break-flag 1 .br -node.http.ClientResponse +http.ClientResponse .RS This object is created internally and passed to the "response" event\. .sp @@ -1356,7 +1359,7 @@ Resumes a paused response\. response\.client .RS 4 A reference to the -node\.http\.Client +http\.Client that this response belongs to\. .RE .RE diff --git a/src/http.js b/lib/http.js similarity index 92% rename from src/http.js rename to lib/http.js index 736fb1b756..78a224aa78 100644 --- a/src/http.js +++ b/lib/http.js @@ -1,6 +1,7 @@ -(function () { -CRLF = "\r\n"; -node.http.STATUS_CODES = { +var utils = require("/utils.js"); + +var CRLF = "\r\n"; +var STATUS_CODES = { 100 : 'Continue', 101 : 'Switching Protocols', 200 : 'OK', @@ -50,8 +51,8 @@ function decode (s) { return decodeURIComponent(s.replace(/\+/g, ' ')); } -node.http.parseUri = function (str) { - var o = node.http.parseUri.options, +exports.parseUri = function (str) { + var o = exports.parseUri.options, m = o.parser[o.strictMode ? "strict" : "loose"].exec(str), uri = {}, i = 14; @@ -79,7 +80,7 @@ node.http.parseUri = function (str) { return uri; }; -node.http.parseUri.options = { +exports.parseUri.options = { strictMode: false, key: [ "source", @@ -296,7 +297,7 @@ function ServerResponse () { node.inherits(ServerResponse, OutgoingMessage); ServerResponse.prototype.sendHeader = function (statusCode, headers) { - var reason = node.http.STATUS_CODES[statusCode] || "unknown"; + var reason = STATUS_CODES[statusCode] || "unknown"; var status_line = "HTTP/1.1 " + statusCode.toString() + " " + reason + CRLF; this.sendHeaderLines(status_line, headers); }; @@ -371,7 +372,7 @@ function createIncomingMessageStream (connection, incoming_listener) { if (info.method) { // server only incoming.method = info.method; - incoming.uri = node.http.parseUri(incoming.uri); // TODO parse the URI lazily? + incoming.uri = exports.parseUri(incoming.uri); // TODO parse the URI lazily? } else { // client only incoming.statusCode = info.statusCode; @@ -419,7 +420,7 @@ function flushMessageQueue (connection, queue) { } -node.http.createServer = function (requestListener, options) { +exports.createServer = function (requestListener, options) { var server = new node.http.Server(); //server.setOptions(options); server.addListener("request", requestListener); @@ -459,7 +460,7 @@ function connectionListener (connection) { } -node.http.createClient = function (port, host) { +exports.createClient = function (port, host) { var client = new node.http.Client(); var requests = []; @@ -467,11 +468,11 @@ node.http.createClient = function (port, host) { client._pushRequest = function (req) { req.addListener("flush", function () { if (client.readyState == "closed") { - //node.debug("HTTP CLIENT request flush. reconnect. readyState = " + client.readyState); + //utils.debug("HTTP CLIENT request flush. reconnect. readyState = " + client.readyState); client.connect(port, host); // reconnect return; } - //node.debug("client flush readyState = " + client.readyState); + //utils.debug("client flush readyState = " + client.readyState); if (req == requests[0]) flushMessageQueue(client, [req]); }); requests.push(req); @@ -482,7 +483,7 @@ node.http.createClient = function (port, host) { }); client.addListener("eof", function () { - //node.debug("client got eof closing. readyState = " + client.readyState); + //utils.debug("client got eof closing. readyState = " + client.readyState); client.close(); }); @@ -492,20 +493,20 @@ node.http.createClient = function (port, host) { return; } - //node.debug("HTTP CLIENT onClose. readyState = " + client.readyState); + //utils.debug("HTTP CLIENT onClose. readyState = " + client.readyState); // If there are more requests to handle, reconnect. if (requests.length > 0 && client.readyState != "opening") { - //node.debug("HTTP CLIENT: reconnecting readyState = " + client.readyState); + //utils.debug("HTTP CLIENT: reconnecting readyState = " + client.readyState); client.connect(port, host); // reconnect } }); createIncomingMessageStream(client, function (res) { - //node.debug("incoming response!"); + //utils.debug("incoming response!"); res.addListener("complete", function ( ) { - //node.debug("request complete disconnecting. readyState = " + client.readyState); + //utils.debug("request complete disconnecting. readyState = " + client.readyState); client.close(); }); @@ -547,13 +548,13 @@ node.http.Client.prototype.put = function (uri, headers) { }; -node.http.cat = function (url, encoding) { +exports.cat = function (url, encoding) { var promise = new node.Promise(); encoding = encoding || "utf8"; - var uri = node.http.parseUri(url); - var client = node.http.createClient(uri.port || 80, uri.host); + var uri = exports.parseUri(url); + var client = exports.createClient(uri.port || 80, uri.host); var req = client.get(uri.path || "/"); client.addListener("error", function () { @@ -576,5 +577,3 @@ node.http.cat = function (url, encoding) { return promise; }; - -})(); // anonymous namespace diff --git a/src/node.cc b/src/node.cc index 5432447b01..e1eabe4213 100644 --- a/src/node.cc +++ b/src/node.cc @@ -402,7 +402,6 @@ static Local Load(int argc, char *argv[]) { ExecuteNativeJS("util.js", native_util); ExecuteNativeJS("events.js", native_events); - ExecuteNativeJS("http.js", native_http); ExecuteNativeJS("file.js", native_file); ExecuteNativeJS("node.js", native_node); diff --git a/src/node.js b/src/node.js index 1f5dfee8a5..f78de6b86f 100644 --- a/src/node.js +++ b/src/node.js @@ -19,6 +19,14 @@ node.exec = function () { throw new Error("node.exec() has moved. Use include('/utils.js') to bring it back."); } +node.http.createServer = function () { + throw new Error("node.http.createServer() has moved. Use require('/http.js') to access it."); +} + +node.http.createClient = function () { + throw new Error("node.http.createClient() has moved. Use require('/http.js') to access it."); +} + node.tcp.createConnection = function (port, host) { var connection = new node.tcp.Connection(); connection.connect(port, host); diff --git a/src/util.js b/src/util.js index 930e65bccb..0a477bbc0b 100644 --- a/src/util.js +++ b/src/util.js @@ -25,8 +25,12 @@ node.assert = function (x, msg) { node.cat = function(location, encoding) { var url_re = new RegExp("^http:\/\/"); - var f = url_re.exec(location) ? node.http.cat : node.fs.cat; - return f(location, encoding); + if (url_re.exec(location)) { + throw new Error("node.cat for http urls is temporarally disabled."); + } + //var f = url_re.exec(location) ? node.http.cat : node.fs.cat; + //return f(location, encoding); + return node.fs.cat(location, encoding); }; node.path = new function () { diff --git a/test/mjsunit/test-node-cat.js b/test/mjsunit/disabled/test-cat.js similarity index 92% rename from test/mjsunit/test-node-cat.js rename to test/mjsunit/disabled/test-cat.js index 384cfe2f0e..c3bc3fb496 100644 --- a/test/mjsunit/test-node-cat.js +++ b/test/mjsunit/disabled/test-cat.js @@ -1,10 +1,11 @@ include("common.js"); +http = require("/http.js"); PORT = 8888; puts("hello world"); var body = "exports.A = function() { return 'A';}"; -var server = node.http.createServer(function (req, res) { +var server = http.createServer(function (req, res) { puts("req?"); res.sendHeader(200, { "Content-Length": body.length, diff --git a/test/mjsunit/test-http-cat.js b/test/mjsunit/test-http-cat.js index c0c0b62893..4e122655c0 100644 --- a/test/mjsunit/test-http-cat.js +++ b/test/mjsunit/test-http-cat.js @@ -1,8 +1,9 @@ include("common.js"); +http = require("/http.js"); PORT = 8888; var body = "exports.A = function() { return 'A';}"; -var server = node.http.createServer(function (req, res) { +var server = http.createServer(function (req, res) { puts("got request"); res.sendHeader(200, [ ["Content-Length", body.length], @@ -16,19 +17,20 @@ server.listen(PORT); var got_good_server_content = false; var bad_server_got_error = false; -node.http.cat("http://localhost:"+PORT+"/", "utf8").addCallback(function (content) { +http.cat("http://localhost:"+PORT+"/", "utf8").addCallback(function (content) { puts("got response"); got_good_server_content = true; assertEquals(body, content); server.close(); }); -node.http.cat("http://localhost:12312/", "utf8").addErrback(function () { +http.cat("http://localhost:12312/", "utf8").addErrback(function () { puts("got error (this should happen)"); bad_server_got_error = true; }); process.addListener("exit", function () { + puts("exit"); assertTrue(got_good_server_content); assertTrue(bad_server_got_error); }); diff --git a/test/mjsunit/test-http-client-race.js b/test/mjsunit/test-http-client-race.js index 59361581c7..8087d7e3db 100644 --- a/test/mjsunit/test-http-client-race.js +++ b/test/mjsunit/test-http-client-race.js @@ -1,10 +1,11 @@ include("common.js"); +http = require("/http.js"); PORT = 8888; var body1_s = "1111111111111111"; var body2_s = "22222"; -var server = node.http.createServer(function (req, res) { +var server = http.createServer(function (req, res) { var body = req.uri.path === "/1" ? body1_s : body2_s; res.sendHeader(200, { "Content-Type": "text/plain" , "Content-Length": body.length @@ -14,7 +15,7 @@ var server = node.http.createServer(function (req, res) { }); server.listen(PORT); -var client = node.http.createClient(PORT); +var client = http.createClient(PORT); var body1 = ""; var body2 = ""; diff --git a/test/mjsunit/test-http-client-upload.js b/test/mjsunit/test-http-client-upload.js index bfd716cdd5..6e95f11d84 100644 --- a/test/mjsunit/test-http-client-upload.js +++ b/test/mjsunit/test-http-client-upload.js @@ -1,11 +1,12 @@ include("common.js"); +http = require("/http.js"); var PORT = 18032; var sent_body = ""; var server_req_complete = false; var client_res_complete = false; -var server = node.http.createServer(function(req, res) { +var server = http.createServer(function(req, res) { assertEquals("POST", req.method); req.setBodyEncoding("utf8"); @@ -24,7 +25,7 @@ var server = node.http.createServer(function(req, res) { }); server.listen(PORT); -var client = node.http.createClient(PORT); +var client = http.createClient(PORT); var req = client.post('/'); req.sendBody('1\n'); diff --git a/test/mjsunit/test-http-malformed-request.js b/test/mjsunit/test-http-malformed-request.js index 68a6ea1ff6..158ee3b90f 100644 --- a/test/mjsunit/test-http-malformed-request.js +++ b/test/mjsunit/test-http-malformed-request.js @@ -1,4 +1,5 @@ include("common.js"); +http = require("/http.js"); // Make sure no exceptions are thrown when receiving malformed HTTP // requests. @@ -7,7 +8,7 @@ port = 9999; nrequests_completed = 0; nrequests_expected = 1; -var s = node.http.createServer(function (req, res) { +var s = http.createServer(function (req, res) { puts("req: " + JSON.stringify(req.uri)); res.sendHeader(200, {"Content-Type": "text/plain"}); diff --git a/test/mjsunit/test-http-proxy.js b/test/mjsunit/test-http-proxy.js index 04743d934b..f902661b59 100644 --- a/test/mjsunit/test-http-proxy.js +++ b/test/mjsunit/test-http-proxy.js @@ -1,9 +1,10 @@ include("common.js"); +http = require("/http.js"); var PROXY_PORT = 8869; var BACKEND_PORT = 8870; -var backend = node.http.createServer(function (req, res) { +var backend = http.createServer(function (req, res) { // debug("backend"); res.sendHeader(200, {"content-type": "text/plain"}); res.sendBody("hello world\n"); @@ -12,8 +13,8 @@ var backend = node.http.createServer(function (req, res) { // debug("listen backend") backend.listen(BACKEND_PORT); -var proxy_client = node.http.createClient(BACKEND_PORT); -var proxy = node.http.createServer(function (req, res) { +var proxy_client = http.createClient(BACKEND_PORT); +var proxy = http.createServer(function (req, res) { debug("proxy req headers: " + JSON.stringify(req.headers)); var proxy_req = proxy_client.get(req.uri.path); proxy_req.finish(function(proxy_res) { @@ -32,7 +33,7 @@ proxy.listen(PROXY_PORT); var body = ""; -var client = node.http.createClient(PROXY_PORT); +var client = http.createClient(PROXY_PORT); var req = client.get("/test"); // debug("client req") req.finish(function (res) { diff --git a/test/mjsunit/test-http-server.js b/test/mjsunit/test-http-server.js index 251f083c37..82bb217da8 100644 --- a/test/mjsunit/test-http-server.js +++ b/test/mjsunit/test-http-server.js @@ -1,4 +1,5 @@ include("common.js"); +http = require("/http.js"); var port = 8222; @@ -7,7 +8,7 @@ var requests_sent = 0; var server_response = ""; var client_got_eof = false; -node.http.createServer(function (req, res) { +http.createServer(function (req, res) { res.id = request_number; req.id = request_number++; diff --git a/test/mjsunit/test-http.js b/test/mjsunit/test-http.js index a2e339edb2..c8d5b2772c 100644 --- a/test/mjsunit/test-http.js +++ b/test/mjsunit/test-http.js @@ -1,4 +1,5 @@ include("common.js"); +http = require("/http.js"); PORT = 8888; var responses_sent = 0; @@ -6,7 +7,7 @@ var responses_recvd = 0; var body0 = ""; var body1 = ""; -node.http.createServer(function (req, res) { +http.createServer(function (req, res) { if (responses_sent == 0) { assertEquals("GET", req.method); assertEquals("/hello", req.uri.path); @@ -35,7 +36,7 @@ node.http.createServer(function (req, res) { //assertEquals("127.0.0.1", res.connection.remoteAddress); }).listen(PORT); -var client = node.http.createClient(PORT); +var client = http.createClient(PORT); var req = client.get("/hello", {"Accept": "*/*", "Foo": "bar"}); req.finish(function (res) { assertEquals(200, res.statusCode); diff --git a/test/mjsunit/test-multipart.js b/test/mjsunit/test-multipart.js index 33a74f7662..c8ba90cac7 100644 --- a/test/mjsunit/test-multipart.js +++ b/test/mjsunit/test-multipart.js @@ -1,4 +1,5 @@ include("common.js"); +http = require("/http.js"); var multipart = require('/multipart.js'); var port = 8222; @@ -6,7 +7,7 @@ var parts_reveived = 0; var parts_complete = 0; var parts = {}; -var server = node.http.createServer(function(req, res) { +var server = http.createServer(function(req, res) { var stream = new multipart.Stream(req); stream.addListener('part', function(part) { diff --git a/wscript b/wscript index 4129088a64..4740b225d5 100644 --- a/wscript +++ b/wscript @@ -241,7 +241,6 @@ def build(bld): source = """ src/util.js src/events.js - src/http.js src/file.js src/node.js """,