Browse code

- tcp bug fixes

Andrei Pelinescu-Onciul authored on 12/12/2002 23:59:26
Showing 3 changed files
... ...
@@ -143,7 +143,7 @@ DEFS+= -DNAME='"$(NAME)"' -DVERSION='"$(RELEASE)"' -DARCH='"$(ARCH)"' \
143 143
 	 -DDNS_IP_HACK \
144 144
 	 -DUSE_IPV6 \
145 145
 	 -DDBG_QM_MALLOC \
146
-	 #-DUSE_TCP \
146
+	 # -DUSE_TCP \
147 147
 	 #-DF_MALLOC \
148 148
 	 #-DNO_DEBUG \
149 149
 	 #-DNO_LOG
... ...
@@ -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 99
 #define init_tcp_req( r) \
100 100
 	do{ \
101 101
 		memset( (r), 0, sizeof(struct tcp_req)); \
102
-		(r)->parsed=(r)->pos=(r)->buf; \
102
+		(r)->parsed=(r)->pos=(r)->start=(r)->buf; \
103 103
 		(r)->error=TCP_REQ_OK;\
104
-		(r)->state=H_STARTWS; \
104
+		(r)->state=H_SKIP_EMPTY; \
105 105
 	}while(0)
106 106
 
107 107
 
... ...
@@ -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 277
 					case '6':
278 278
 					case '7':
279 279
 					case '8':
280
+					case '9':
280 281
 						r->content_len=r->content_len*10+(*p-'0');
281 282
 						break;
282 283
 					case '\r':
... ...
@@ -364,24 +383,25 @@ int tcp_read_req(struct tcp_connection* con)
364 364
 			resp=CONN_RELEASE;
365 365
 			/* just for debugging use sendipv4 as receiving socket */
366 366
 			DBG("calling receive_msg(%p, %d, )\n",
367
-					req->buf, (int)(req->parsed-req->buf));
367
+					req->buf, (int)(req->parsed-req->start));
368 368
 			bind_address=sendipv4; /*&tcp_info[con->sock_idx];*/
369 369
 			con->rcv.proto_reserved1=con->id; /* copy the id */
370
-			if (receive_msg(req->buf, req->parsed-req->buf, &con->rcv)<0){
370
+			if (receive_msg(req->start, req->parsed-req->start, &con->rcv)<0){
371 371
 				resp=CONN_ERROR;
372 372
 				goto end_req;
373 373
 			}
374 374
 			
375 375
 			/* prepare for next request */
376
-			size=req->pos-req->body;
377
-			if (size) memmove(req->buf, req->body, size);
376
+			size=req->pos-req->parsed;
377
+			if (size) memmove(req->buf, req->parsed, size);
378 378
 			DBG("tcp_read_req: preparing for new request, kept %ld bytes\n",
379 379
 					size);
380 380
 			req->pos=req->buf+size;
381 381
 			req->parsed=req->buf;
382
+			req->start=req->buf;
382 383
 			req->body=0;
383 384
 			req->error=TCP_REQ_OK;
384
-			req->state=H_STARTWS;
385
+			req->state=H_SKIP_EMPTY;
385 386
 			req->complete=req->content_len=req->has_content_len=0;
386 387
 			req->bytes_to_go=0;
387 388