... | ... |
@@ -43,7 +43,7 @@ |
43 | 43 |
|
44 | 44 |
enum tcp_req_errors { TCP_REQ_INIT, TCP_REQ_OK, TCP_READ_ERROR, |
45 | 45 |
TCP_REQ_OVERRUN, TCP_REQ_BAD_LEN }; |
46 |
-enum tcp_req_states { H_SKIP, H_LF, H_LFCR, H_BODY, H_STARTWS, |
|
46 |
+enum tcp_req_states { H_SKIP_EMPTY, H_SKIP, H_LF, H_LFCR, H_BODY, H_STARTWS, |
|
47 | 47 |
H_CONT_LEN1, H_CONT_LEN2, H_CONT_LEN3, H_CONT_LEN4, H_CONT_LEN5, |
48 | 48 |
H_CONT_LEN6, H_CONT_LEN7, H_CONT_LEN8, H_CONT_LEN9, H_CONT_LEN10, |
49 | 49 |
H_CONT_LEN11, H_CONT_LEN12, H_CONT_LEN13, H_L_COLON, |
... | ... |
@@ -58,6 +58,8 @@ struct tcp_req{ |
58 | 58 |
struct tcp_req* next; |
59 | 59 |
/* sockaddr ? */ |
60 | 60 |
char buf[TCP_BUF_SIZE]; /* bytes read so far*/ |
61 |
+ char* start; /* where the message starts, after alll the empty lines are |
|
62 |
+ skipped*/ |
|
61 | 63 |
char* pos; /* current position in buf */ |
62 | 64 |
char* parsed; /* last parsed position */ |
63 | 65 |
char* body; /* body position */ |
... | ... |
@@ -99,9 +101,9 @@ struct tcp_connection{ |
99 | 101 |
#define init_tcp_req( r) \ |
100 | 102 |
do{ \ |
101 | 103 |
memset( (r), 0, sizeof(struct tcp_req)); \ |
102 |
- (r)->parsed=(r)->pos=(r)->buf; \ |
|
104 |
+ (r)->parsed=(r)->pos=(r)->start=(r)->buf; \ |
|
103 | 105 |
(r)->error=TCP_REQ_OK;\ |
104 |
- (r)->state=H_STARTWS; \ |
|
106 |
+ (r)->state=H_SKIP_EMPTY; \ |
|
105 | 107 |
}while(0) |
106 | 108 |
|
107 | 109 |
|
... | ... |
@@ -139,7 +139,7 @@ int tcp_read_headers(struct tcp_req *r, int fd) |
139 | 139 |
p=r->parsed; |
140 | 140 |
|
141 | 141 |
while(p<r->pos && r->error==TCP_REQ_OK){ |
142 |
- switch(r->state){ |
|
142 |
+ switch((unsigned char)r->state){ |
|
143 | 143 |
case H_BODY: /* read the body*/ |
144 | 144 |
remaining=r->pos-p; |
145 | 145 |
if (remaining>r->bytes_to_go) remaining=r->bytes_to_go; |
... | ... |
@@ -214,7 +214,25 @@ int tcp_read_headers(struct tcp_req *r, int fd) |
214 | 214 |
} |
215 | 215 |
p++; |
216 | 216 |
break; |
217 |
- |
|
217 |
+ case H_SKIP_EMPTY: |
|
218 |
+ switch (*p){ |
|
219 |
+ case '\n': |
|
220 |
+ case '\r': |
|
221 |
+ case ' ': |
|
222 |
+ case '\t': |
|
223 |
+ /* skip empty lines */ |
|
224 |
+ break; |
|
225 |
+ case 'C': |
|
226 |
+ case 'c': |
|
227 |
+ r->state=H_CONT_LEN1; |
|
228 |
+ r->start=p; |
|
229 |
+ break; |
|
230 |
+ default: |
|
231 |
+ r->state=H_SKIP; |
|
232 |
+ r->start=p; |
|
233 |
+ }; |
|
234 |
+ p++; |
|
235 |
+ break; |
|
218 | 236 |
change_state_case(H_CONT_LEN1, 'O', 'o', H_CONT_LEN2); |
219 | 237 |
change_state_case(H_CONT_LEN2, 'N', 'n', H_CONT_LEN3); |
220 | 238 |
change_state_case(H_CONT_LEN3, 'T', 't', H_CONT_LEN4); |
... | ... |
@@ -277,6 +295,7 @@ int tcp_read_headers(struct tcp_req *r, int fd) |
277 | 295 |
case '6': |
278 | 296 |
case '7': |
279 | 297 |
case '8': |
298 |
+ case '9': |
|
280 | 299 |
r->content_len=r->content_len*10+(*p-'0'); |
281 | 300 |
break; |
282 | 301 |
case '\r': |
... | ... |
@@ -364,24 +383,25 @@ int tcp_read_req(struct tcp_connection* con) |
364 | 383 |
resp=CONN_RELEASE; |
365 | 384 |
/* just for debugging use sendipv4 as receiving socket */ |
366 | 385 |
DBG("calling receive_msg(%p, %d, )\n", |
367 |
- req->buf, (int)(req->parsed-req->buf)); |
|
386 |
+ req->buf, (int)(req->parsed-req->start)); |
|
368 | 387 |
bind_address=sendipv4; /*&tcp_info[con->sock_idx];*/ |
369 | 388 |
con->rcv.proto_reserved1=con->id; /* copy the id */ |
370 |
- if (receive_msg(req->buf, req->parsed-req->buf, &con->rcv)<0){ |
|
389 |
+ if (receive_msg(req->start, req->parsed-req->start, &con->rcv)<0){ |
|
371 | 390 |
resp=CONN_ERROR; |
372 | 391 |
goto end_req; |
373 | 392 |
} |
374 | 393 |
|
375 | 394 |
/* prepare for next request */ |
376 |
- size=req->pos-req->body; |
|
377 |
- if (size) memmove(req->buf, req->body, size); |
|
395 |
+ size=req->pos-req->parsed; |
|
396 |
+ if (size) memmove(req->buf, req->parsed, size); |
|
378 | 397 |
DBG("tcp_read_req: preparing for new request, kept %ld bytes\n", |
379 | 398 |
size); |
380 | 399 |
req->pos=req->buf+size; |
381 | 400 |
req->parsed=req->buf; |
401 |
+ req->start=req->buf; |
|
382 | 402 |
req->body=0; |
383 | 403 |
req->error=TCP_REQ_OK; |
384 |
- req->state=H_STARTWS; |
|
404 |
+ req->state=H_SKIP_EMPTY; |
|
385 | 405 |
req->complete=req->content_len=req->has_content_len=0; |
386 | 406 |
req->bytes_to_go=0; |
387 | 407 |
|