Commit 0a9bca482e199374ee3d87110ebaee8df85e4b06

Authored by Georg Hopp
1 parent 81d98966

started filling out a request object with the parser

@@ -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 8 +HTTP = interface/stream_reader.c http/request_parser.c http/request.c
9 9
10 AM_CFLAGS = -Wall -I ../include/ 10 AM_CFLAGS = -Wall -I ../include/
11 11
1 #include <stdlib.h> 1 #include <stdlib.h>
  2 +#include <stdarg.h>
2 3
3 #include "class.h" 4 #include "class.h"
4 #include "http/request.h" 5 #include "http/request.h"
  6 +#include "interface/class.h"
5 7
6 static 8 static
7 void 9 void
@@ -14,23 +16,28 @@ _free(void ** data) @@ -14,23 +16,28 @@ _free(void ** data)
14 16
15 static 17 static
16 void 18 void
  19 +ctor(void * _this, va_list * params) {}
  20 +
  21 +static
  22 +void
17 dtor(void * _this) 23 dtor(void * _this)
18 { 24 {
19 - int i; 25 + HttpRequest this = _this;
  26 + int i;
20 27
21 - _free(&(this->http_version));  
22 - _free(&(this->uri));  
23 - _free(&(this->method)); 28 + _free((void **)&(this->http_version));
  29 + _free((void **)&(this->uri));
  30 + _free((void **)&(this->method));
24 31
25 for (i=0; i<128; i++) { 32 for (i=0; i<128; i++) {
26 - _free(&((this->header)[i].name));  
27 - _free(&((this->header)[i].value)); 33 + _free((void **)&((this->header)[i].name));
  34 + _free((void **)&((this->header)[i].value));
28 } 35 }
29 36
30 - _free(&(this->body)); 37 + _free((void **)&(this->body));
31 } 38 }
32 39
33 -INIT_IFACE(Class, NULL, dtor, NULL);  
34 -CREATE_CLASS(HttpRequest, IFACE(Class)); 40 +INIT_IFACE(Class, ctor, dtor, NULL);
  41 +CREATE_CLASS(HttpRequest, NULL, IFACE(Class));
35 42
36 // vim: set ts=4 sw=4: 43 // vim: set ts=4 sw=4:
@@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
10 #include "http/request_parser.h" 10 #include "http/request_parser.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 "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
@@ -122,27 +122,25 @@ inline @@ -122,27 +122,25 @@ inline
122 void 122 void
123 httpRequestSkip(char ** data) 123 httpRequestSkip(char ** data)
124 { 124 {
125 - for (; 0 != **data && ! isalpha(**data); *data++); 125 + for (; 0 != **data && ! isalpha(**data); (*data)++);
126 } 126 }
127 127
128 static 128 static
129 void 129 void
130 httpRequestParserParse(HttpRequestParser this) 130 httpRequestParserParse(HttpRequestParser this)
131 { 131 {
132 - //static HttpRequest request = NULL; 132 + static HttpRequest request = NULL;
133 char * data = this->buffer; 133 char * data = this->buffer;
134 char * line; 134 char * line;
135 int cont = 1; 135 int cont = 1;
136 -  
137 - //if(NULL == HttpRequest) {  
138 - // request = new(HttpRequest);  
139 - //} 136 + static int header_idx;
140 137
141 while(cont) { 138 while(cont) {
142 switch(this->state) { 139 switch(this->state) {
143 case HTTP_REQUEST_GARBAGE: 140 case HTTP_REQUEST_GARBAGE:
144 puts("==skip garbage=="); 141 puts("==skip garbage==");
145 httpRequestSkip(&data); 142 httpRequestSkip(&data);
  143 + request = new(HttpRequest);
146 144
147 this->state = HTTP_REQUEST_START; 145 this->state = HTTP_REQUEST_START;
148 break; 146 break;
@@ -153,8 +151,43 @@ httpRequestParserParse(HttpRequestParser this) @@ -153,8 +151,43 @@ httpRequestParserParse(HttpRequestParser this)
153 cont = 0; 151 cont = 0;
154 break; 152 break;
155 } 153 }
  154 +
  155 + {
  156 + char * delim = strchr(line, ' ');
  157 +
  158 + if (NULL != delim) {
  159 + *delim = 0;
  160 + request->method = malloc(strlen(line) + 1);
  161 + strcpy(request->method, line);
  162 + line = delim + 1;
  163 +
  164 + for (; *line == ' ' && *line != 0; line++);
  165 +
  166 + if (0 != *line) {
  167 + delim = strchr(line, ' ');
  168 +
  169 + if (NULL != delim) {
  170 + *delim = 0;
  171 + request->uri = malloc(strlen(line) + 1);
  172 + strcpy(request->uri, line);
  173 + line = delim + 1;
  174 +
  175 + for (; *line == ' ' && *line != 0; line++);
  176 +
  177 + if (0 != *line) {
  178 + request->http_version = malloc(strlen(line) + 1);
  179 + strcpy(request->http_version, line);
  180 + }
  181 + }
  182 + }
  183 + }
  184 + }
156 185
157 - printf("%s\n", line); 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;
158 this->state = HTTP_REQUEST_REQEUST_LINE_DONE; 191 this->state = HTTP_REQUEST_REQEUST_LINE_DONE;
159 break; 192 break;
160 193
@@ -170,11 +203,36 @@ httpRequestParserParse(HttpRequestParser this) @@ -170,11 +203,36 @@ httpRequestParserParse(HttpRequestParser this)
170 break; 203 break;
171 } 204 }
172 205
173 - printf("%s\n", line); 206 + {
  207 + char * delim = strchr(line, ':');
  208 +
  209 + *delim = 0;
  210 + (request->header)[header_idx].name = malloc(strlen(line) + 1);
  211 + strcpy((request->header)[header_idx].name, line);
  212 +
  213 + line = delim + 1;
  214 + for (; *line == ' ' && *line != 0; line++);
  215 +
  216 + (request->header)[header_idx].value = malloc(strlen(line) + 1);
  217 + strcpy((request->header)[header_idx].value, line);
  218 + }
  219 +
  220 + header_idx++;
174 break; 221 break;
175 222
176 case HTTP_REQUEST_HEADERS_DONE: 223 case HTTP_REQUEST_HEADERS_DONE:
177 puts("==headers done=="); 224 puts("==headers done==");
  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);
178 this->state = HTTP_REQUEST_DONE; 236 this->state = HTTP_REQUEST_DONE;
179 break; 237 break;
180 238
@@ -191,6 +249,7 @@ httpRequestParserParse(HttpRequestParser this) @@ -191,6 +249,7 @@ httpRequestParserParse(HttpRequestParser this)
191 cont = 0; 249 cont = 0;
192 } 250 }
193 251
  252 + delete(&request);
194 this->state = HTTP_REQUEST_GARBAGE; 253 this->state = HTTP_REQUEST_GARBAGE;
195 254
196 break; 255 break;
Please register or login to post a comment