diff --git a/http_request.h b/http_request.h new file mode 100644 index 0000000000..e611495067 --- /dev/null +++ b/http_request.h @@ -0,0 +1,41 @@ +#ifndef http_request_h +#define http_request_h +extern "C" { +#include +#include +} +#include + +using namespace std; + +class Connection { +public: + Connection ( void) + { + oi_socket_init (&socket, 30.0); + ebb_request_parser_init (&parser); + } + ebb_request_parser parser; + oi_socket socket; +}; + +class HttpRequest { + public: + HttpRequest (Connection &c) : connection_(c) { ebb_request_init(&parser_info); } + ~HttpRequest() { } + + const string& Path () { return path; } + const string& Referrer () { return referrer; } + const string& Host () { return host; } + const string& UserAgent () { return user_agent; } + + string path; + string referrer; + string host; + string user_agent; + + Connection &connection_; + ebb_request parser_info; +}; + +#endif // http_request_h diff --git a/js_http_request_processor.h b/js_http_request_processor.h index 32d89e260b..f2abb7321c 100644 --- a/js_http_request_processor.h +++ b/js_http_request_processor.h @@ -5,6 +5,8 @@ #include #include +#include "http_request.h" + using namespace std; using namespace v8; @@ -13,17 +15,6 @@ using namespace v8; // and then add scripting capabilities that allow you to interact with // the objects through JavaScript. -/** - * A simplified http request. - */ -class HttpRequest { - public: - virtual ~HttpRequest() { } - virtual const string& Path() = 0; - virtual const string& Referrer() = 0; - virtual const string& Host() = 0; - virtual const string& UserAgent() = 0; -}; /** * The abstract superclass of http request processors. diff --git a/server.cc b/server.cc index 2fc65753f8..60170be62d 100644 --- a/server.cc +++ b/server.cc @@ -1,14 +1,17 @@ -#include - extern "C" { #include #include } +#include "http_request.h" +#include "js_http_request_processor.h" + #include #include #include +#include + using namespace v8; using namespace std; @@ -17,37 +20,7 @@ using namespace std; static oi_server server; static struct ev_loop *loop; - -class Connection { -public: - Connection(void); - ebb_request_parser parser; - oi_socket socket; -}; - -class Request { -public: - Request(Connection &); - string path; - Connection &connection_; - ebb_request parser_info; -}; - -Request::Request - ( Connection &connection - ) - : connection_(connection) -{ - ebb_request_init(&parser_info); -} - -Connection::Connection - ( void - ) -{ - oi_socket_init (&socket, 30.0); - ebb_request_parser_init (&parser); -} +static JsHttpRequestProcessor *processor; void on_path ( ebb_request *req @@ -55,17 +28,23 @@ void on_path , size_t len ) { - Request *request = static_cast (req->data); + HttpRequest *request = static_cast (req->data); request->path.append(buf, len); } -void on_request_complete +void on_headers_complete ( ebb_request *req ) { - Request *request = static_cast (req->data); + HttpRequest *request = static_cast (req->data); - // dispatch to javascript + processor->Process(request); +} + +void on_request_complete + ( ebb_request *req + ) +{ } ebb_request * on_request @@ -74,7 +53,7 @@ ebb_request * on_request { Connection *connection = static_cast (data); - Request *request = new Request(*connection); + HttpRequest *request = new HttpRequest(*connection); request->parser_info.on_path = on_path; request->parser_info.on_query_string = NULL; @@ -82,7 +61,7 @@ ebb_request * on_request request->parser_info.on_fragment = NULL; request->parser_info.on_header_field = NULL; request->parser_info.on_header_value = NULL; - request->parser_info.on_headers_complete = NULL; + request->parser_info.on_headers_complete = on_headers_complete; request->parser_info.on_body = NULL; request->parser_info.on_complete = on_request_complete; request->parser_info.data = request; @@ -128,19 +107,16 @@ static oi_socket* new_connection ) { Connection *connection = new Connection(); + connection->socket.on_read = on_read; + connection->socket.on_error = NULL; + connection->socket.on_close = on_close; + connection->socket.on_timeout = NULL; + connection->socket.on_drain = on_drain; + connection->socket.data = connection; - /* initialize the components of Connection */ - oi_socket_init(&connection->socket, 30.0); - connection->socket.on_read = on_read; - connection->socket.on_error = NULL; - connection->socket.on_close = on_close; - connection->socket.on_timeout = NULL; - connection->socket.on_drain = on_drain; - connection->socket.data = connection; + connection->parser.new_request = on_request; + connection->parser.data = connection; - ebb_request_parser_init(&connection->parser); - connection->parser.new_request = on_request; - connection->parser.data = connection; return &connection->socket; } @@ -208,14 +184,14 @@ int main fprintf(stderr, "Error reading '%s'.\n", file.c_str()); return 1; } - /* - HttpRequestProcessor processor(source); + + processor = new JsHttpRequestProcessor(source); map output; - if (!processor.Initialize(&options, &output)) { + if (!processor->Initialize(&options, &output)) { fprintf(stderr, "Error initializing processor.\n"); return 1; } - */ + /////////////////////////////////////