From 1e5de42a75e229660145cff754991fdf09ba0c26 Mon Sep 17 00:00:00 2001 From: Ryan Date: Fri, 13 Mar 2009 12:22:35 +0100 Subject: [PATCH] allow null host parameter to listen on localhost --- Makefile | 23 +++++++++-------------- node_http.cc | 10 ++++++---- node_tcp.cc | 8 ++++++-- tcp_example.js | 32 +++++++------------------------- test/test_http_server_echo.rb | 2 +- test/test_http_server_null_response.rb | 2 +- 6 files changed, 30 insertions(+), 47 deletions(-) diff --git a/Makefile b/Makefile index d4790a4..0168a18 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,13 @@ EVDIR=$(HOME)/local/libev + +OIINC = $(HOME)/projects/oi/ +OILIB = $(HOME)/projects/oi/liboi.a + V8INC = $(HOME)/src/v8/include -V8LIB = $(HOME)/src/v8/libv8_g.a -#V8LIB = $(HOME)/src/v8/libv8.a +#V8LIB = $(HOME)/src/v8/libv8_g.a +V8LIB = $(HOME)/src/v8/libv8.a -CFLAGS = -g -I$(V8INC) -Ideps/oi -DHAVE_GNUTLS=0 -Ideps/ebb +CFLAGS = -g -I$(V8INC) -I$(OIINC) -DHAVE_GNUTLS=0 -Ideps/ebb LDFLAGS = -lev -pthread # -lefence ifdef EVDIR @@ -11,8 +15,8 @@ ifdef EVDIR LDFLAGS += -L$(EVDIR)/lib endif -node: node.o node_tcp.o node_http.o node_timer.o oi_socket.o oi_async.o oi_buf.o ebb_request_parser.o - g++ -o node $^ $(LDFLAGS) $(V8LIB) +node: node.o node_tcp.o node_http.o node_timer.o ebb_request_parser.o + g++ -o node $^ $(LDFLAGS) $(V8LIB) $(OILIB) node.o: node.cc g++ $(CFLAGS) -c $< @@ -25,15 +29,6 @@ node_http.o: node_http.cc node_timer.o: node_timer.cc g++ $(CFLAGS) -c $< - -oi_socket.o: deps/oi/oi_socket.c deps/oi/oi_socket.h - gcc $(CFLAGS) -c $< - -oi_async.o: deps/oi/oi_async.c deps/oi/oi_async.h - gcc $(CFLAGS) -c $< - -oi_buf.o: deps/oi/oi_buf.c deps/oi/oi_buf.h - gcc $(CFLAGS) -c $< ebb_request_parser.o: ebb_request_parser.c deps/ebb/ebb_request_parser.h g++ $(CFLAGS) -c $< diff --git a/node_http.cc b/node_http.cc index fcc032b..82d4072 100644 --- a/node_http.cc +++ b/node_http.cc @@ -153,8 +153,6 @@ RespondCallback (const Arguments& args) void HttpRequest::Respond (Handle data) { - // TODO ByteArray ? - if(data == Null()) { done = true; } else { @@ -607,7 +605,11 @@ newHTTPServer (const Arguments& args) HandleScope scope; - String::AsciiValue host(args[0]->ToString()); + char *host = NULL; + String::AsciiValue host_v(args[0]->ToString()); + if(args[0]->IsString()) { + host = *host_v; + } String::AsciiValue port(args[1]->ToString()); Handle onrequest = Handle::Cast(args[2]); @@ -621,7 +623,7 @@ newHTTPServer (const Arguments& args) hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; // FIXME BLOCKING - int r = getaddrinfo(*host, *port, &hints, &servinfo); + int r = getaddrinfo(host, *port, &hints, &servinfo); if (r != 0) return Undefined(); // XXX raise error? diff --git a/node_tcp.cc b/node_tcp.cc index 89c74cd..8db5d64 100644 --- a/node_tcp.cc +++ b/node_tcp.cc @@ -84,14 +84,18 @@ static Handle newTCPClient HandleScope scope; - String::AsciiValue host(args[0]); + char *host = NULL; + String::AsciiValue host_v(args[0]->ToString()); + if(args[0]->IsString()) { + host = *host_v; + } String::AsciiValue port(args[1]); TCPClient *client = new TCPClient(args.This()); if(client == NULL) return Undefined(); // XXX raise error? - int r = client->Connect(*host, *port); + int r = client->Connect(host, *port); if (r != 0) return Undefined(); // XXX raise error? diff --git a/tcp_example.js b/tcp_example.js index cf1504f..37e56a0 100644 --- a/tcp_example.js +++ b/tcp_example.js @@ -1,33 +1,12 @@ -/* -[Constructor(in DOMString url)] -interface TCP.Client { - readonly attribute DOMString host; - readonly attribute DOMString port; - - // ready state - const unsigned short CONNECTING = 0; - const unsigned short OPEN = 1; - const unsigned short CLOSED = 2; - readonly attribute long readyState; - - // networking - attribute Function onopen; - attribute Function onrecv; - attribute Function onclose; - void send(in DOMString data); - void disconnect(); -}; -*/ - -client = new TCPClient("localhost", 11222); +client = new TCPClient("google.com", 80); log("readyState: " + client.readyState); //assertEqual(client.readystate, TCP.CONNECTING); client.onopen = function () { - log("connected to dynomite"); + log("connected to google"); log("readyState: " + client.readyState); - client.write("get 1 /\n"); + client.write("GET / HTTP/1.1\r\n\r\n"); }; client.onread = function (chunk) { @@ -38,8 +17,11 @@ client.onclose = function () { log("connection closed"); }; + + setTimeout(function () { client.disconnect(); -}, 1000); +}, 10 * 1000); +log("hello"); diff --git a/test/test_http_server_echo.rb b/test/test_http_server_echo.rb index b515c8f..e1d8585 100755 --- a/test/test_http_server_echo.rb +++ b/test/test_http_server_echo.rb @@ -32,7 +32,7 @@ function encode(data) { } var port = 8000; -var server = new HTTPServer("localhost", port, function (request) { +var server = new HTTPServer(null, port, function (request) { // onBody sends null on the last chunk. request.onbody = function (chunk) { diff --git a/test/test_http_server_null_response.rb b/test/test_http_server_null_response.rb index 794acae..5bdc01c 100755 --- a/test/test_http_server_null_response.rb +++ b/test/test_http_server_null_response.rb @@ -16,7 +16,7 @@ ensure end __END__ -var server = new HTTPServer("localhost", 8000, function (request) { +var server = new HTTPServer(null, 8000, function (request) { log("request"); request.respond("HTTP/1.1 200 OK\r\n"); request.respond("Content-Length: 0\r\n"); -- 2.7.4