Commit f1bf7c49f78db168c321af825feeb513b574394a

Authored by Georg Hopp
1 parent 19724f0d

basic request parsing (line by line) implemented

... ... @@ -8,7 +8,8 @@
8 8 #define HTTP_REQUEST_PARSER_READ_CHUNK 1024
9 9
10 10 typedef enum e_HttpRequestState {
11   - HTTP_REQUEST_START=0,
  11 + HTTP_REQUEST_GARBAGE=0,
  12 + HTTP_REQUEST_START,
12 13 HTTP_REQUEST_REQEUST_LINE_DONE,
13 14 HTTP_REQUEST_HEADERS_DONE,
14 15 HTTP_REQUEST_DONE
... ... @@ -18,6 +19,7 @@ typedef enum e_HttpRequestState {
18 19 CLASS(HttpRequestParser) {
19 20 char * buffer;
20 21 size_t buffer_used;
  22 + size_t buffer_size;
21 23
22 24 //HttpRequestQueue request_queue;
23 25 HttpRequestState state;
... ...
1 1 #include <stdlib.h>
2 2
3   -#include "cclass.h"
  3 +#include "class.h"
4 4 #include "http/request.h"
5 5
6   -INIT_CLASS(HTTP_REQUEST);
7   -
8   -static void
  6 +static
  7 +void
9 8 _free(void ** data)
10 9 {
11 10 if (NULL != *data) {
... ... @@ -13,9 +12,9 @@ _free(void ** data)
13 12 }
14 13 }
15 14
16   -__construct(LOGGER) {}
17   -
18   -__destruct(LOGGER)
  15 +static
  16 +void
  17 +dtor(void * _this)
19 18 {
20 19 int i;
21 20
... ... @@ -31,8 +30,7 @@ __destruct(LOGGER)
31 30 _free(&(this->body));
32 31 }
33 32
34   -__jsonConst(LOGGER) {}
35   -__toJson(LOGGER) {}
36   -__clear(LOGGER) {}
  33 +INIT_IFACE(Class, NULL, dtor, NULL);
  34 +CREATE_CLASS(HttpRequest, IFACE(Class));
37 35
38 36 // vim: set ts=4 sw=4:
... ...
... ... @@ -3,6 +3,7 @@
3 3 #include <stdlib.h>
4 4 #include <stdio.h>
5 5 #include <unistd.h>
  6 +#include <ctype.h>
6 7 #include <sys/types.h>
7 8
8 9 #include "class.h"
... ... @@ -14,7 +15,7 @@
14 15
15 16 static
16 17 void
17   -httpRequestParserParse(char * data, size_t * size);
  18 +httpRequestParserParse(HttpRequestParser);
18 19
19 20 static
20 21 void
... ... @@ -25,6 +26,7 @@ ctor(void * _this, va_list * params)
25 26 //this->request_queue = va_arg(*params, HttpRequestQueue);
26 27
27 28 this->buffer = malloc(HTTP_REQUEST_PARSER_READ_CHUNK);
  29 + this->buffer[0] = 0;
28 30 }
29 31
30 32 static
... ... @@ -78,15 +80,16 @@ get_data(void * _this, int fd)
78 80 */
79 81 chunks++;
80 82
81   - if (size > remaining) {
  83 + if (size >= remaining) {
82 84 this->buffer =
83 85 realloc(this->buffer, chunks * HTTP_REQUEST_PARSER_READ_CHUNK);
84 86 }
85 87
86 88 memcpy(this->buffer + this->buffer_used, buffer, size);
87 89 this->buffer_used += size;
  90 + this->buffer[this->buffer_used] = 0;
88 91
89   - httpRequestParserParse(this->buffer, &this->buffer_used);
  92 + httpRequestParserParse(this);
90 93 }
91 94
92 95 return size;
... ... @@ -97,12 +100,121 @@ INIT_IFACE(StreamReader, get_data);
97 100 CREATE_CLASS(HttpRequestParser, NULL, IFACE(Class), IFACE(StreamReader));
98 101
99 102 static
  103 +inline
  104 +int
  105 +httpRequestLineGet(HttpRequestParser this, char ** line)
  106 +{
  107 + char * line_end = strstr(this->buffer, "\r\n");
  108 +
  109 + if (NULL == line_end) {
  110 + *line = NULL;
  111 + return -1;
  112 + }
  113 +
  114 + *line_end = 0;
  115 + *line = malloc(strlen(this->buffer) + 1);
  116 + strcpy(*line, this->buffer);
  117 +
  118 + line_end+=2;
  119 +
  120 + memmove(this->buffer,
  121 + line_end,
  122 + this->buffer_used - (line_end-this->buffer) + 1);
  123 +
  124 + this->buffer_used -= line_end-this->buffer;
  125 +
  126 + return line_end-this->buffer-2;
  127 +}
  128 +
  129 +static
  130 +inline
100 131 void
101   -httpRequestParserParse(char * data, size_t * size)
  132 +httpRequestSkip(HttpRequestParser this)
102 133 {
103   - data[*size] = 0;
104   - printf("%s", data);
105   - *size = 0;
  134 + size_t skip;
  135 +
  136 + for (skip = 0;
  137 + 0 != this->buffer[skip]
  138 + && ! isalpha(this->buffer[skip])
  139 + && this->buffer_used > skip;
  140 + skip++);
  141 +
  142 + memmove(this->buffer,
  143 + &this->buffer[skip],
  144 + this->buffer_used - skip + 1);
  145 +
  146 + this->buffer_used -= skip;
  147 +}
  148 +
  149 +static
  150 +void
  151 +httpRequestParserParse(HttpRequestParser this)
  152 +{
  153 + //static HttpRequest request = NULL;
  154 + char * line;
  155 + int cont = 1;
  156 + int length;
  157 +
  158 + //if(NULL == HttpRequest) {
  159 + // request = new(HttpRequest);
  160 + //}
  161 +
  162 + while(cont) {
  163 + switch(this->state) {
  164 + case HTTP_REQUEST_GARBAGE:
  165 + puts("==skip garbage==");
  166 + httpRequestSkip(this);
  167 +
  168 + this->state = HTTP_REQUEST_START;
  169 + break;
  170 +
  171 + case HTTP_REQUEST_START:
  172 + puts("==request line==");
  173 + if (-1 == httpRequestLineGet(this, &line)) {
  174 + cont = 0;
  175 + break;
  176 + }
  177 +
  178 + printf("%s\n", line);
  179 + free(line);
  180 + this->state = HTTP_REQUEST_REQEUST_LINE_DONE;
  181 + break;
  182 +
  183 + case HTTP_REQUEST_REQEUST_LINE_DONE:
  184 + puts("==read header==");
  185 + if (-1 == (length = httpRequestLineGet(this, &line))) {
  186 + cont = 0;
  187 + break;
  188 + }
  189 +
  190 + if (0 == length) {
  191 + free(line);
  192 + this->state = HTTP_REQUEST_HEADERS_DONE;
  193 + break;
  194 + }
  195 +
  196 + printf("%s\n", line);
  197 + free(line);
  198 + break;
  199 +
  200 + case HTTP_REQUEST_HEADERS_DONE:
  201 + puts("==headers done==");
  202 + this->state = HTTP_REQUEST_DONE;
  203 + break;
  204 +
  205 + case HTTP_REQUEST_DONE:
  206 + puts("==request done==");
  207 +
  208 + if (0 == this->buffer_used) {
  209 + cont = 0;
  210 + }
  211 +
  212 + break;
  213 +
  214 + default:
  215 + break;
  216 + }
  217 + }
106 218 }
107 219
108 220 // vim: set ts=4 sw=4:
... ...
Please register or login to post a comment