From 19478ed4b14263c489e872156ca55ff16a07ebe0 Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 3 Mar 2009 01:56:15 +0100 Subject: [PATCH] Major refactoring: program name now "node" Trying to make a more moduler design. Two libraries currently "TCP" and "HTTP" each have their own file. Other major feature added here is multiple web servers! excitement. --- Makefile | 25 +-- http_api.js | 30 ++++ node.cc | 154 +++++++++++++++++++ server.cc => node_http.cc | 309 ++++++++++++++++---------------------- node_http.h | 11 ++ tcp.cc => node_tcp.cc | 12 +- node_tcp.h | 11 ++ tcp.h | 11 -- 8 files changed, 355 insertions(+), 208 deletions(-) create mode 100644 http_api.js create mode 100644 node.cc rename server.cc => node_http.cc (66%) create mode 100644 node_http.h rename tcp.cc => node_tcp.cc (94%) create mode 100644 node_tcp.h delete mode 100644 tcp.h diff --git a/Makefile b/Makefile index 6d7086c1af..5b6082f995 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ EVDIR=$(HOME)/local/libev V8INC = $(HOME)/src/v8/include -V8LIB = $(HOME)/src/v8/libv8.a +V8LIB = $(HOME)/src/v8/libv8_g.a CFLAGS = -g -I$(V8INC) -Ideps/oi -DHAVE_GNUTLS=0 -Ideps/ebb LDFLAGS = -lev -pthread # -lefence @@ -10,20 +10,17 @@ ifdef EVDIR LDFLAGS += -L$(EVDIR)/lib endif -server: server.o tcp.o oi_socket.o oi_async.o ebb_request_parser.o oi_buf.o - g++ -o server $^ $(LDFLAGS) $(V8LIB) +node: node.o node_tcp.o node_http.o oi_socket.o oi_async.o oi_buf.o ebb_request_parser.o + g++ -o node $^ $(LDFLAGS) $(V8LIB) -server.o: server.cc +node.o: node.cc g++ $(CFLAGS) -c $< -tcp.o: tcp.cc - g++ $(CFLAGS) -c $< - -ebb_request_parser.o: ebb_request_parser.c deps/ebb/ebb_request_parser.h +node_tcp.o: node_tcp.cc g++ $(CFLAGS) -c $< -ebb_request_parser.c: deps/ebb/ebb_request_parser.rl - ragel -s -G2 $< -o $@ +node_http.o: node_http.cc + g++ $(CFLAGS) -c $< oi_socket.o: deps/oi/oi_socket.c deps/oi/oi_socket.h gcc $(CFLAGS) -c $< @@ -33,10 +30,16 @@ oi_async.o: deps/oi/oi_async.c deps/oi/oi_async.h 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 $< + +ebb_request_parser.c: deps/ebb/ebb_request_parser.rl + ragel -s -G2 $< -o $@ clean: rm -f ebb_request_parser.c rm -f *.o - rm -f server + rm -f node .PHONY: clean test diff --git a/http_api.js b/http_api.js new file mode 100644 index 0000000000..9fb80860f2 --- /dev/null +++ b/http_api.js @@ -0,0 +1,30 @@ +function encode(data) { + var chunk = data.toString(); + return chunk.length.toString(16) + "\r\n" + chunk + "\r\n"; +} + +var server = new HTTP.Server("localhost", 8000); + +server.onRequest = function (request) { + log( "path: " + request.path ); + log( "query string: " + request.query_string ); + + // onBody sends null on the last chunk. + request.onBody = function (chunk) { + if(chunk) { + this.respond(encode(chunk)); + } else { + this.respond(encode("\n")); + this.respond("0\r\n\r\n"); + this.respond(null); // signals end-of-request + } + } + request.respond("HTTP/1.0 200 OK\r\n"); + request.respond("Content-Type: text/plain\r\n"); + request.respond("Transfer-Encoding: chunked\r\n"); + request.respond("\r\n"); + +}; +/* +server.close(); +*/ diff --git a/node.cc b/node.cc new file mode 100644 index 0000000000..13d40ca214 --- /dev/null +++ b/node.cc @@ -0,0 +1,154 @@ +#include + +#include "node_tcp.h" +#include "node_http.h" + +#include +#include +#include +#include +#include + +#include + +using namespace v8; +using namespace std; + +static struct ev_loop *loop; + +static Persistent context; +static Persistent process_; + +// Reads a file into a v8 string. +static Handle ReadFile + ( const string& name + ) +{ + FILE* file = fopen(name.c_str(), "rb"); + if (file == NULL) return Handle(); + + fseek(file, 0, SEEK_END); + int size = ftell(file); + rewind(file); + + char* chars = new char[size + 1]; + chars[size] = '\0'; + for (int i = 0; i < size;) { + int read = fread(&chars[i], 1, size - i, file); + i += read; + } + fclose(file); + Handle result = String::New(chars, size); + delete[] chars; + return result; +} + +static void ParseOptions + ( int argc + , char* argv[] + , map& options + , string* file + ) +{ + for (int i = 1; i < argc; i++) { + string arg = argv[i]; + int index = arg.find('=', 0); + if (index == string::npos) { + *file = arg; + } else { + string key = arg.substr(0, index); + string value = arg.substr(index+1); + options[key] = value; + } + } +} + +static bool compile + ( Handle script + ) +{ + HandleScope handle_scope; + + // We're just about to compile the script; set up an error handler to + // catch any exceptions the script might throw. + TryCatch try_catch; + + // Compile the script and check for errors. + Handle