Commit 90df11c01484440dc5f4014053ff1a32ed5462ec
1 parent
0a9bca48
now each HttpRequestParser initializes its own request queue and enqueus complet…
…ed requests there. The server now gets the queue and prints completed requests.
Showing
8 changed files
with
105 additions
and
31 deletions
| 1 | +2012-02-07 13:41:49 +0100 Georg Hopp | ||
| 2 | + | ||
| 3 | + * now each HttpRequestParser initializes its own request queue and enqueus completed requests there. The server now gets the queue and prints completed requests. (HEAD, master) | ||
| 4 | + | ||
| 5 | +2012-02-07 11:12:30 +0100 Georg Hopp | ||
| 6 | + | ||
| 7 | + * started filling out a request object with the parser | ||
| 8 | + | ||
| 9 | +2012-02-07 09:29:59 +0100 Georg Hopp | ||
| 10 | + | ||
| 11 | + * porformance improvement in parsing process (no longer do alloc and free on each line) | ||
| 12 | + | ||
| 13 | +2012-02-07 08:52:18 +0100 Georg Hopp | ||
| 14 | + | ||
| 15 | + * basic request parsing (line by line) implemented | ||
| 16 | + | ||
| 17 | +2012-02-06 16:08:13 +0100 Georg Hopp | ||
| 18 | + | ||
| 19 | + * split server implementation for readability | ||
| 20 | + | ||
| 21 | +2012-02-06 11:35:40 +0100 Georg Hopp | ||
| 22 | + | ||
| 23 | + * free reader (HttpRequestParser) when connection is closed | ||
| 24 | + | ||
| 1 | 2012-02-06 11:20:00 +0100 Georg Hopp | 25 | 2012-02-06 11:20:00 +0100 Georg Hopp |
| 2 | 26 | ||
| 3 | - * add StreamReader interface, modify HttpRequestParser and Server to use it (HEAD, master) | 27 | + * add StreamReader interface, modify HttpRequestParser and Server to use it |
| 4 | 28 | ||
| 5 | 2012-02-06 11:15:00 +0100 Georg Hopp | 29 | 2012-02-06 11:15:00 +0100 Georg Hopp |
| 6 | 30 |
| @@ -2,8 +2,7 @@ | @@ -2,8 +2,7 @@ | ||
| 2 | #define __HTTP_REQUEST_PARSER_H__ | 2 | #define __HTTP_REQUEST_PARSER_H__ |
| 3 | 3 | ||
| 4 | #include "class.h" | 4 | #include "class.h" |
| 5 | -//#include "http/request.h" | ||
| 6 | -//#include "http/request_queue.h" | 5 | +#include "http/request_queue.h" |
| 7 | 6 | ||
| 8 | #define HTTP_REQUEST_PARSER_READ_CHUNK 1024 | 7 | #define HTTP_REQUEST_PARSER_READ_CHUNK 1024 |
| 9 | 8 | ||
| @@ -21,7 +20,7 @@ CLASS(HttpRequestParser) { | @@ -21,7 +20,7 @@ CLASS(HttpRequestParser) { | ||
| 21 | size_t buffer_used; | 20 | size_t buffer_used; |
| 22 | size_t buffer_size; | 21 | size_t buffer_size; |
| 23 | 22 | ||
| 24 | - //HttpRequestQueue request_queue; | 23 | + HttpRequestQueue request_queue; |
| 25 | HttpRequestState state; | 24 | HttpRequestState state; |
| 26 | }; | 25 | }; |
| 27 | 26 |
| @@ -9,8 +9,8 @@ | @@ -9,8 +9,8 @@ | ||
| 9 | 9 | ||
| 10 | CLASS(HttpRequestQueue) { | 10 | CLASS(HttpRequestQueue) { |
| 11 | HttpRequest requests[HTTP_REQUEST_QUEUE_MAX]; | 11 | HttpRequest requests[HTTP_REQUEST_QUEUE_MAX]; |
| 12 | - size_t nrequests; | ||
| 13 | -} | 12 | + size_t nrequests; |
| 13 | +}; | ||
| 14 | 14 | ||
| 15 | #endif /* __HTTP_REQUEST_QUEUE_H__ */ | 15 | #endif /* __HTTP_REQUEST_QUEUE_H__ */ |
| 16 | 16 |
| @@ -5,7 +5,7 @@ CLASS = class.c interface.c interface/class.c | @@ -5,7 +5,7 @@ CLASS = class.c interface.c interface/class.c | ||
| 5 | SOCKET = socket.c socket/accept.c socket/connect.c socket/listen.c | 5 | SOCKET = socket.c socket/accept.c socket/connect.c socket/listen.c |
| 6 | SERVER = server.c server/run.c server/close_conn.c | 6 | SERVER = server.c server/run.c server/close_conn.c |
| 7 | LOGGER = logger.c logger/stderr.c logger/syslog.c interface/logger.c | 7 | LOGGER = logger.c logger/stderr.c logger/syslog.c interface/logger.c |
| 8 | -HTTP = interface/stream_reader.c http/request_parser.c http/request.c | 8 | +HTTP = interface/stream_reader.c http/request_parser.c http/request.c http/request_queue.c |
| 9 | 9 | ||
| 10 | AM_CFLAGS = -Wall -I ../include/ | 10 | AM_CFLAGS = -Wall -I ../include/ |
| 11 | 11 |
| @@ -2,9 +2,10 @@ | @@ -2,9 +2,10 @@ | ||
| 2 | #include <stdarg.h> | 2 | #include <stdarg.h> |
| 3 | 3 | ||
| 4 | #include "class.h" | 4 | #include "class.h" |
| 5 | -#include "http/request.h" | ||
| 6 | #include "interface/class.h" | 5 | #include "interface/class.h" |
| 7 | 6 | ||
| 7 | +#include "http/request.h" | ||
| 8 | + | ||
| 8 | static | 9 | static |
| 9 | void | 10 | void |
| 10 | _free(void ** data) | 11 | _free(void ** data) |
| @@ -11,7 +11,7 @@ | @@ -11,7 +11,7 @@ | ||
| 11 | #include "interface/class.h" | 11 | #include "interface/class.h" |
| 12 | #include "interface/stream_reader.h" | 12 | #include "interface/stream_reader.h" |
| 13 | #include "http/request.h" | 13 | #include "http/request.h" |
| 14 | -//#include "http/request_queue.h" | 14 | +#include "http/request_queue.h" |
| 15 | 15 | ||
| 16 | static | 16 | static |
| 17 | void | 17 | void |
| @@ -23,7 +23,7 @@ ctor(void * _this, va_list * params) | @@ -23,7 +23,7 @@ ctor(void * _this, va_list * params) | ||
| 23 | { | 23 | { |
| 24 | HttpRequestParser this = _this; | 24 | HttpRequestParser this = _this; |
| 25 | 25 | ||
| 26 | - //this->request_queue = va_arg(*params, HttpRequestQueue); | 26 | + this->request_queue = new(HttpRequestQueue); |
| 27 | 27 | ||
| 28 | this->buffer = malloc(HTTP_REQUEST_PARSER_READ_CHUNK); | 28 | this->buffer = malloc(HTTP_REQUEST_PARSER_READ_CHUNK); |
| 29 | this->buffer[0] = 0; | 29 | this->buffer[0] = 0; |
| @@ -36,6 +36,7 @@ dtor(void * _this) | @@ -36,6 +36,7 @@ dtor(void * _this) | ||
| 36 | HttpRequestParser this = _this; | 36 | HttpRequestParser this = _this; |
| 37 | 37 | ||
| 38 | free(this->buffer); | 38 | free(this->buffer); |
| 39 | + delete(&(this->request_queue)); | ||
| 39 | } | 40 | } |
| 40 | 41 | ||
| 41 | static | 42 | static |
| @@ -46,7 +47,10 @@ _clone(void * _this, void * _base) | @@ -46,7 +47,10 @@ _clone(void * _this, void * _base) | ||
| 46 | HttpRequestParser base = _base; | 47 | HttpRequestParser base = _base; |
| 47 | size_t chunks; | 48 | size_t chunks; |
| 48 | 49 | ||
| 49 | - //this->request_queue = base->request_queue; | 50 | + /** |
| 51 | + * every parser has its own queue... | ||
| 52 | + */ | ||
| 53 | + this->request_queue = new(HttpRequestQueue); | ||
| 50 | this->buffer_used = base->buffer_used; | 54 | this->buffer_used = base->buffer_used; |
| 51 | 55 | ||
| 52 | chunks = this->buffer_used / HTTP_REQUEST_PARSER_READ_CHUNK; | 56 | chunks = this->buffer_used / HTTP_REQUEST_PARSER_READ_CHUNK; |
| @@ -183,10 +187,6 @@ httpRequestParserParse(HttpRequestParser this) | @@ -183,10 +187,6 @@ httpRequestParserParse(HttpRequestParser this) | ||
| 183 | } | 187 | } |
| 184 | } | 188 | } |
| 185 | 189 | ||
| 186 | - printf("method: %s\n", request->method); | ||
| 187 | - printf("uri: %s\n", request->uri); | ||
| 188 | - printf("version: %s\n", request->http_version); | ||
| 189 | - | ||
| 190 | header_idx = 0; | 190 | header_idx = 0; |
| 191 | this->state = HTTP_REQUEST_REQEUST_LINE_DONE; | 191 | this->state = HTTP_REQUEST_REQEUST_LINE_DONE; |
| 192 | break; | 192 | break; |
| @@ -223,16 +223,6 @@ httpRequestParserParse(HttpRequestParser this) | @@ -223,16 +223,6 @@ httpRequestParserParse(HttpRequestParser this) | ||
| 223 | case HTTP_REQUEST_HEADERS_DONE: | 223 | case HTTP_REQUEST_HEADERS_DONE: |
| 224 | puts("==headers done=="); | 224 | puts("==headers done=="); |
| 225 | 225 | ||
| 226 | - { | ||
| 227 | - int i; | ||
| 228 | - | ||
| 229 | - for (i=0; i<header_idx; i++) { | ||
| 230 | - printf("header-name: %s\n", (request->header)[i].name); | ||
| 231 | - printf("header-value: %s\n", (request->header)[i].value); | ||
| 232 | - } | ||
| 233 | - } | ||
| 234 | - | ||
| 235 | - printf("%s\n", line); | ||
| 236 | this->state = HTTP_REQUEST_DONE; | 226 | this->state = HTTP_REQUEST_DONE; |
| 237 | break; | 227 | break; |
| 238 | 228 | ||
| @@ -249,7 +239,9 @@ httpRequestParserParse(HttpRequestParser this) | @@ -249,7 +239,9 @@ httpRequestParserParse(HttpRequestParser this) | ||
| 249 | cont = 0; | 239 | cont = 0; |
| 250 | } | 240 | } |
| 251 | 241 | ||
| 252 | - delete(&request); | 242 | + this->request_queue->requests[(this->request_queue->nrequests)++] = |
| 243 | + request; | ||
| 244 | + | ||
| 253 | this->state = HTTP_REQUEST_GARBAGE; | 245 | this->state = HTTP_REQUEST_GARBAGE; |
| 254 | 246 | ||
| 255 | break; | 247 | break; |
| @@ -9,8 +9,12 @@ serverHandleAccept(Server this) | @@ -9,8 +9,12 @@ serverHandleAccept(Server this) | ||
| 9 | acc = socketAccept(this->sock, remoteAddr); | 9 | acc = socketAccept(this->sock, remoteAddr); |
| 10 | 10 | ||
| 11 | if (-1 != acc->handle) { | 11 | if (-1 != acc->handle) { |
| 12 | - (this->conns)[this->nfds].sock = acc; // save the socket handle | ||
| 13 | - (this->conns)[this->nfds].reader = clone(this->reader); // clone reader | 12 | + //* save the socket handle |
| 13 | + (this->conns)[this->nfds].sock = acc; | ||
| 14 | + | ||
| 15 | + //* clone reader | ||
| 16 | + (this->conns)[this->nfds].reader = clone(this->reader); | ||
| 17 | + | ||
| 14 | (this->fds)[this->nfds].fd = acc->handle; | 18 | (this->fds)[this->nfds].fd = acc->handle; |
| 15 | (this->fds)[this->nfds].events = POLLIN; | 19 | (this->fds)[this->nfds].events = POLLIN; |
| 16 | this->nfds++; | 20 | this->nfds++; |
| @@ -10,6 +10,13 @@ | @@ -10,6 +10,13 @@ | ||
| 10 | #include "signalHandling.h" | 10 | #include "signalHandling.h" |
| 11 | #include "interface/class.h" | 11 | #include "interface/class.h" |
| 12 | #include "interface/stream_reader.h" | 12 | #include "interface/stream_reader.h" |
| 13 | +#include "interface/logger.h" | ||
| 14 | + | ||
| 15 | +//* @TODO: to be removed | ||
| 16 | +#include "http/request.h" | ||
| 17 | +#include "http/request_parser.h" | ||
| 18 | +#include "http/request_queue.h" | ||
| 19 | +//* until here | ||
| 13 | 20 | ||
| 14 | #undef MAX | 21 | #undef MAX |
| 15 | #define MAX(x,y) ((x) > (y) ? (x) : (y)) | 22 | #define MAX(x,y) ((x) > (y) ? (x) : (y)) |
| @@ -21,11 +28,18 @@ | @@ -21,11 +28,18 @@ | ||
| 21 | void | 28 | void |
| 22 | serverRun(Server this) | 29 | serverRun(Server this) |
| 23 | { | 30 | { |
| 24 | - /* | ||
| 25 | - * @TODO again...add verbosity to logger.... | ||
| 26 | - */ | ||
| 27 | loggerLog(this->logger, LOGGER_INFO, "service started"); | 31 | loggerLog(this->logger, LOGGER_INFO, "service started"); |
| 28 | 32 | ||
| 33 | + /** | ||
| 34 | + * @TODO: actually this is the main loop of my server. When | ||
| 35 | + * stuff becomes more complicated it might be feasabible to | ||
| 36 | + * split stuff into separate processes. This will definetly | ||
| 37 | + * involve some IPC and syncing. Right now as this is actually | ||
| 38 | + * only a simple HTTP server implementation we go on with | ||
| 39 | + * this single process. | ||
| 40 | + * What we can first do to get some processing between read/write | ||
| 41 | + * cicles is to use the poll timeout. | ||
| 42 | + */ | ||
| 29 | while (!doShutdown) /* until error or signal */ | 43 | while (!doShutdown) /* until error or signal */ |
| 30 | { | 44 | { |
| 31 | int events; | 45 | int events; |
| @@ -46,6 +60,46 @@ serverRun(Server this) | @@ -46,6 +60,46 @@ serverRun(Server this) | ||
| 46 | * handle reads | 60 | * handle reads |
| 47 | */ | 61 | */ |
| 48 | serverRead(this); | 62 | serverRead(this); |
| 63 | + | ||
| 64 | + /** | ||
| 65 | + * do some other processing | ||
| 66 | + * @TODO: actually this will hard assume that our stream reader | ||
| 67 | + * is a http parser and it has its queue...think about more | ||
| 68 | + * generalizing here. | ||
| 69 | + */ | ||
| 70 | + { | ||
| 71 | + int i; | ||
| 72 | + | ||
| 73 | + for (i=1; i<this->nfds; i++) { | ||
| 74 | + int j; | ||
| 75 | + HttpRequestQueue queue = | ||
| 76 | + ((HttpRequestParser)(this->conns)[i].reader)->request_queue; | ||
| 77 | + | ||
| 78 | + for (j=0; j<queue->nrequests; j++) { | ||
| 79 | + int k; | ||
| 80 | + HttpRequest request = queue->requests[j]; | ||
| 81 | + | ||
| 82 | + printf("method: %s\n", request->method); | ||
| 83 | + printf("uri: %s\n", request->uri); | ||
| 84 | + printf("version: %s\n", request->http_version); | ||
| 85 | + puts(""); | ||
| 86 | + | ||
| 87 | + for (k=0; k<128; k++) { | ||
| 88 | + if (NULL == (request->header)[k].name) break; | ||
| 89 | + printf("header-name: %s\n", (request->header)[k].name); | ||
| 90 | + printf("header-value: %s\n", (request->header)[k].value); | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + delete(&request); | ||
| 94 | + } | ||
| 95 | + queue->nrequests = 0; | ||
| 96 | + } | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + | ||
| 100 | + /** | ||
| 101 | + * handle writes | ||
| 102 | + */ | ||
| 49 | } | 103 | } |
| 50 | } | 104 | } |
| 51 | 105 |
Please
register
or
login
to post a comment