Commit 0a9bca482e199374ee3d87110ebaee8df85e4b06
1 parent
81d98966
started filling out a request object with the parser
Showing
3 changed files
with
85 additions
and
19 deletions
... | ... | @@ -5,7 +5,7 @@ CLASS = class.c interface.c interface/class.c |
5 | 5 | SOCKET = socket.c socket/accept.c socket/connect.c socket/listen.c |
6 | 6 | SERVER = server.c server/run.c server/close_conn.c |
7 | 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 | 10 | AM_CFLAGS = -Wall -I ../include/ |
11 | 11 | ... | ... |
1 | 1 | #include <stdlib.h> |
2 | +#include <stdarg.h> | |
2 | 3 | |
3 | 4 | #include "class.h" |
4 | 5 | #include "http/request.h" |
6 | +#include "interface/class.h" | |
5 | 7 | |
6 | 8 | static |
7 | 9 | void |
... | ... | @@ -14,23 +16,28 @@ _free(void ** data) |
14 | 16 | |
15 | 17 | static |
16 | 18 | void |
19 | +ctor(void * _this, va_list * params) {} | |
20 | + | |
21 | +static | |
22 | +void | |
17 | 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 | 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 | 43 | // vim: set ts=4 sw=4: | ... | ... |
... | ... | @@ -10,7 +10,7 @@ |
10 | 10 | #include "http/request_parser.h" |
11 | 11 | #include "interface/class.h" |
12 | 12 | #include "interface/stream_reader.h" |
13 | -//#include "http/request.h" | |
13 | +#include "http/request.h" | |
14 | 14 | //#include "http/request_queue.h" |
15 | 15 | |
16 | 16 | static |
... | ... | @@ -122,27 +122,25 @@ inline |
122 | 122 | void |
123 | 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 | 128 | static |
129 | 129 | void |
130 | 130 | httpRequestParserParse(HttpRequestParser this) |
131 | 131 | { |
132 | - //static HttpRequest request = NULL; | |
132 | + static HttpRequest request = NULL; | |
133 | 133 | char * data = this->buffer; |
134 | 134 | char * line; |
135 | 135 | int cont = 1; |
136 | - | |
137 | - //if(NULL == HttpRequest) { | |
138 | - // request = new(HttpRequest); | |
139 | - //} | |
136 | + static int header_idx; | |
140 | 137 | |
141 | 138 | while(cont) { |
142 | 139 | switch(this->state) { |
143 | 140 | case HTTP_REQUEST_GARBAGE: |
144 | 141 | puts("==skip garbage=="); |
145 | 142 | httpRequestSkip(&data); |
143 | + request = new(HttpRequest); | |
146 | 144 | |
147 | 145 | this->state = HTTP_REQUEST_START; |
148 | 146 | break; |
... | ... | @@ -153,8 +151,43 @@ httpRequestParserParse(HttpRequestParser this) |
153 | 151 | cont = 0; |
154 | 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 | 191 | this->state = HTTP_REQUEST_REQEUST_LINE_DONE; |
159 | 192 | break; |
160 | 193 | |
... | ... | @@ -170,11 +203,36 @@ httpRequestParserParse(HttpRequestParser this) |
170 | 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 | 221 | break; |
175 | 222 | |
176 | 223 | case HTTP_REQUEST_HEADERS_DONE: |
177 | 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 | 236 | this->state = HTTP_REQUEST_DONE; |
179 | 237 | break; |
180 | 238 | |
... | ... | @@ -191,6 +249,7 @@ httpRequestParserParse(HttpRequestParser this) |
191 | 249 | cont = 0; |
192 | 250 | } |
193 | 251 | |
252 | + delete(&request); | |
194 | 253 | this->state = HTTP_REQUEST_GARBAGE; |
195 | 254 | |
196 | 255 | break; | ... | ... |
Please
register
or
login
to post a comment