Browse code

to parsing fixed!

Bogdan-Andrei Iancu authored on 13/02/2002 17:00:31
Showing 2 changed files
... ...
@@ -262,7 +262,7 @@ char* get_hdr_field(char* buf, char* end, struct hdr_field* hdr)
262 262
 			}
263 263
 			hdr->parsed=to_b;
264 264
 			hdr->body.len=tmp-hdr->body.s;
265
-			DBG("get_hdr_field: to <%s>: <%s>=[%d] %c [%d]\n",
265
+			DBG("get_hdr_field: to <%s>: <%s>=[%d] <%c> [%d]\n",
266 266
 				hdr->name.s, to_b->body.s,to_b->body.len,*tmp,hdr->body.len);
267 267
 			break;
268 268
 		case HDR_FROM:
... ...
@@ -7,12 +7,15 @@
7 7
 #include "mem/mem.h"
8 8
 
9 9
 
10
-enum{ START_TO, IN_NAME_ADDR, E_NAME_ADDR , IN_ADDR_SPEC, E_ADDR_SPEC
11
-	, IN_ADDR_SPEC_ONLY, S_PARA_NAME, PARA_NAME, S_EQUAL, S_PARA_VALUE
12
-	, TAG1, TAG2, TAG3, PARA_VALUE_TOKEN , PARA_VALUE_QUOTED, E_PARA_VALUE
10
+enum{ START_TO, QUOTED, ENCLOSED, BODY
13 11
 	, F_CR, F_LF, F_CRLF
14 12
 	};
15 13
 
14
+enum{ S_PARA_NAME=20, PARA_NAME, S_EQUAL, S_PARA_VALUE, TAG1, TAG2, TAG3
15
+	, PARA_VALUE_TOKEN , PARA_VALUE_QUOTED, E_PARA_VALUE, PARA_START
16
+	};
17
+
18
+
16 19
 
17 20
 #define add_param( _param , _body ) \
18 21
 	do{\
... ...
@@ -26,10 +29,12 @@ enum{ START_TO, IN_NAME_ADDR, E_NAME_ADDR , IN_ADDR_SPEC, E_ADDR_SPEC
26 29
 
27 30
 
28 31
 
29
-char* parse_to(char* buffer, char *end, struct to_body *to_b)
32
+
33
+char* parse_to_param(char *buffer, char *end, struct to_body *to_b,
34
+								unsigned int *returned_status)
30 35
 {
31 36
 	struct to_param *param=0;
32
-	int status = START_TO;
37
+	int status =PARA_START;
33 38
 	int saved_status;
34 39
 	char  *tmp;
35 40
 
... ...
@@ -41,14 +46,6 @@ char* parse_to(char* buffer, char *end, struct to_body *to_b)
41 46
 			case '\t':
42 47
 				switch (status)
43 48
 				{
44
-					case IN_ADDR_SPEC_ONLY:
45
-						to_b->body.len=tmp-to_b->body.s;
46
-						*tmp=0;
47
-						status = E_ADDR_SPEC;
48
-						break;
49
-					case E_ADDR_SPEC:
50
-						*tmp =0;
51
-						break;
52 49
 					case TAG3:
53 50
 						param->type=TAG_PARAM;
54 51
 					case PARA_NAME:
... ...
@@ -75,15 +72,6 @@ char* parse_to(char* buffer, char *end, struct to_body *to_b)
75 72
 			case '\n':
76 73
 				switch (status)
77 74
 				{
78
-					case IN_ADDR_SPEC_ONLY:
79
-						to_b->body.len=tmp-to_b->body.s;
80
-						*tmp=0;
81
-						status = E_ADDR_SPEC;
82
-						break;
83
-					case E_ADDR_SPEC:
84
-						*tmp =0;
85
-					case START_TO:
86
-					case E_NAME_ADDR:
87 75
 					case S_PARA_NAME:
88 76
 					case S_EQUAL:
89 77
 					case S_PARA_VALUE:
... ...
@@ -114,22 +102,13 @@ char* parse_to(char* buffer, char *end, struct to_body *to_b)
114 102
 						status=saved_status;
115 103
 						goto endofheader;
116 104
 					default:
117
-						LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
118
-						"in status %d .\n",*tmp,status);
105
+						LOG( L_ERR , "ERROR: parse_to_param : "
106
+						"unexpected char [%c] in status %d .\n",*tmp,status);
119 107
 				}
120 108
 				break;
121 109
 			case '\r':
122 110
 				switch (status)
123 111
 				{
124
-					case IN_ADDR_SPEC_ONLY:
125
-						to_b->body.len=tmp-to_b->body.s;
126
-						*tmp=0;
127
-						status = E_ADDR_SPEC;
128
-						break;
129
-					case E_ADDR_SPEC:
130
-						*tmp =0;
131
-					case START_TO:
132
-					case E_NAME_ADDR:
133 112
 					case S_PARA_NAME:
134 113
 					case S_EQUAL:
135 114
 					case S_PARA_VALUE:
... ...
@@ -158,81 +137,32 @@ char* parse_to(char* buffer, char *end, struct to_body *to_b)
158 137
 						status=saved_status;
159 138
 						goto endofheader;
160 139
 					default:
161
-						LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
162
-						"in status %d .\n",*tmp,status);
140
+						LOG( L_ERR , "ERROR: parse_to_param : "
141
+						"unexpected char [%c] in status %d .\n",*tmp,status);
163 142
 						goto error;
164 143
 				}
165 144
 				break;
166 145
 			case '\\':
167 146
 				switch (status)
168 147
 				{
169
-					case IN_NAME_ADDR:
170 148
 					case PARA_VALUE_QUOTED:
171 149
 						switch (*(tmp+1))
172 150
 						{
173
-							case F_CR:
174
-							case F_LF:
151
+							case '\r':
152
+							case '\n':
175 153
 								break;
176 154
 							default:
177 155
 								tmp++;
178 156
 						}
179 157
 					default:
180
-						LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
181
-						"in status %d .\n",*tmp,status);
182
-						goto error;
183
-				}
184
-				break;
185
-			case '<':
186
-				switch (status)
187
-				{
188
-					case START_TO:
189
-					case E_NAME_ADDR:
190
-						if (!to_b->body.s) to_b->body.s=tmp;
191
-						status = IN_ADDR_SPEC;
192
-						break;
193
-					case IN_NAME_ADDR:
194
-						break;
195
-					case F_CRLF:
196
-					case F_LF:
197
-					case F_CR:
198
-						/*previous=crlf and now !=' '*/
199
-						goto endofheader;
200
-					default:
201
-						LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
202
-						"in status %d .\n",*tmp,status);
203
-						goto error;
204
-				}
205
-				break;
206
-			case '>':
207
-				switch (status)
208
-				{
209
-					case IN_ADDR_SPEC:
210
-						to_b->body.len=tmp-to_b->body.s;
211
-						status = E_ADDR_SPEC;
212
-						break;
213
-					case IN_NAME_ADDR:
214
-						break;
215
-					case F_CRLF:
216
-					case F_LF:
217
-					case F_CR:
218
-						/*previous=crlf and now !=' '*/
219
-						goto endofheader;
220
-					default:
221
-						LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
222
-						"in status %d .\n",*tmp,status);
158
+						LOG( L_ERR , "ERROR: parse_to_param : "
159
+						"unexpected char [%c] in status %d .\n",*tmp,status);
223 160
 						goto error;
224 161
 				}
225 162
 				break;
226 163
 			case '"':
227 164
 				switch (status)
228 165
 				{
229
-					case START_TO:
230
-						to_b->body.s=tmp;
231
-						status = IN_NAME_ADDR;
232
-						break;
233
-					case IN_NAME_ADDR:
234
-						status = E_NAME_ADDR;
235
-						break;
236 166
 					case S_PARA_VALUE:
237 167
 						param->value.s = tmp+1;
238 168
 						status = PARA_VALUE_QUOTED;
... ...
@@ -249,25 +179,23 @@ char* parse_to(char* buffer, char *end, struct to_body *to_b)
249 179
 						/*previous=crlf and now !=' '*/
250 180
 						goto endofheader;
251 181
 					default:
252
-						LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
253
-						"in status %d .\n",*tmp,status);
182
+						LOG( L_ERR , "ERROR: parse_to_param :"
183
+						"unexpected char [%c] in status %d .\n",*tmp,status);
254 184
 						goto error;
255 185
 				}
256 186
 				break;
257 187
 			case ';' :
258 188
 				switch (status)
259 189
 				{
260
-					case IN_NAME_ADDR:
261 190
 					case PARA_VALUE_QUOTED:
262 191
 						break;
263
-					case IN_ADDR_SPEC_ONLY:
264
-						to_b->body.len=tmp-to_b->body.s;
192
+					case PARA_START:
265 193
 						*tmp=0;
266
-					case E_ADDR_SPEC:
267 194
 					case E_PARA_VALUE:
268 195
 						param = (struct to_param*)pkg_malloc(sizeof(struct to_param));
269 196
 						if (!param){
270
-							LOG( L_ERR , "ERROR: parse_to - out of memory\n" );
197
+							LOG( L_ERR , "ERROR: parse_to_param"
198
+							" - out of memory\n" );
271 199
 							goto error;
272 200
 						}
273 201
 						memset(param,0,sizeof(struct to_param));
... ...
@@ -280,8 +208,8 @@ char* parse_to(char* buffer, char *end, struct to_body *to_b)
280 208
 						/*previous=crlf and now !=' '*/
281 209
 						goto endofheader;
282 210
 					default:
283
-						LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
284
-						"in status %d .\n",*tmp,status);
211
+						LOG( L_ERR , "ERROR: parse_to_param :"
212
+						"unexpected char [%c] in status %d .\n",*tmp,status);
285 213
 						goto error;
286 214
 				}
287 215
 				break;
... ...
@@ -289,15 +217,8 @@ char* parse_to(char* buffer, char *end, struct to_body *to_b)
289 217
 			case 't' :
290 218
 				switch (status)
291 219
 				{
292
-					case START_TO:
293
-						to_b->body.s=tmp;
294
-						status = IN_ADDR_SPEC_ONLY;
295
-						break;
296
-					case IN_NAME_ADDR:
297 220
 					case PARA_VALUE_QUOTED:
298 221
 					case PARA_VALUE_TOKEN:
299
-					case IN_ADDR_SPEC:
300
-					case IN_ADDR_SPEC_ONLY:
301 222
 					case PARA_NAME:
302 223
 						break;
303 224
 					case S_PARA_NAME:
... ...
@@ -319,8 +240,8 @@ char* parse_to(char* buffer, char *end, struct to_body *to_b)
319 240
 						/*previous=crlf and now !=' '*/
320 241
 						goto endofheader;
321 242
 					default:
322
-						LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
323
-						"in status %d .\n",*tmp,status);
243
+						LOG( L_ERR , "ERROR: parse_to_param :"
244
+						" unexpected char [%c] in status %d .\n",*tmp,status);
324 245
 						goto error;
325 246
 				}
326 247
 				break;
... ...
@@ -328,15 +249,8 @@ char* parse_to(char* buffer, char *end, struct to_body *to_b)
328 249
 			case 'a' :
329 250
 				switch (status)
330 251
 				{
331
-					case START_TO:
332
-						to_b->body.s=tmp;
333
-						status = IN_ADDR_SPEC_ONLY;
334
-						break;
335
-					case IN_NAME_ADDR:
336 252
 					case PARA_VALUE_QUOTED:
337 253
 					case PARA_VALUE_TOKEN:
338
-					case IN_ADDR_SPEC:
339
-					case IN_ADDR_SPEC_ONLY:
340 254
 					case PARA_NAME:
341 255
 						break;
342 256
 					case S_PARA_NAME:
... ...
@@ -360,8 +274,8 @@ char* parse_to(char* buffer, char *end, struct to_body *to_b)
360 274
 						/*previous=crlf and now !=' '*/
361 275
 						goto endofheader;
362 276
 					default:
363
-						LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
364
-						"in status %d .\n",*tmp,status);
277
+						LOG( L_ERR , "ERROR: parse_to_param : "
278
+						"unexpected char [%c] in status %d .\n",*tmp,status);
365 279
 						goto error;
366 280
 				}
367 281
 				break;
... ...
@@ -369,15 +283,8 @@ char* parse_to(char* buffer, char *end, struct to_body *to_b)
369 283
 			case 'g' :
370 284
 				switch (status)
371 285
 				{
372
-					case START_TO:
373
-						to_b->body.s=tmp;
374
-						status = IN_ADDR_SPEC_ONLY;
375
-						break;
376
-					case IN_NAME_ADDR:
377 286
 					case PARA_VALUE_QUOTED:
378 287
 					case PARA_VALUE_TOKEN:
379
-					case IN_ADDR_SPEC:
380
-					case IN_ADDR_SPEC_ONLY:
381 288
 					case PARA_NAME:
382 289
 						break;
383 290
 					case S_PARA_NAME:
... ...
@@ -401,15 +308,14 @@ char* parse_to(char* buffer, char *end, struct to_body *to_b)
401 308
 						/*previous=crlf and now !=' '*/
402 309
 						goto endofheader;
403 310
 					default:
404
-						LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
405
-						"in status %d .\n",*tmp,status);
311
+						LOG( L_ERR , "ERROR: parse_to_param : "
312
+						"unexpected char [%c] in status %d .\n",*tmp,status);
406 313
 						goto error;
407 314
 				}
408 315
 				break;
409 316
 			case '=':
410 317
 				switch (status)
411 318
 				{
412
-					case IN_NAME_ADDR:
413 319
 					case PARA_VALUE_QUOTED:
414 320
 						break;
415 321
 					case TAG3:
... ...
@@ -424,24 +330,22 @@ char* parse_to(char* buffer, char *end, struct to_body *to_b)
424 330
 					case S_EQUAL:
425 331
 						status = S_PARA_VALUE;
426 332
 						break;
333
+					case F_CRLF:
334
+					case F_LF:
335
+					case F_CR:
336
+						/*previous=crlf and now !=' '*/
337
+						goto endofheader;
427 338
 					default:
428
-						LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
429
-						"in status %d .\n",*tmp,status);
339
+						LOG( L_ERR , "ERROR: parse_to_param : "
340
+						"unexpected char [%c] in status %d .\n",*tmp,status);
430 341
 						goto error;
431 342
 				}
432 343
 				break;
433 344
 			default:
434 345
 				switch (status)
435 346
 				{
436
-					case START_TO:
437
-						to_b->body.s=tmp;
438
-						status = IN_ADDR_SPEC_ONLY;
439
-						break;
440 347
 					case PARA_VALUE_TOKEN:
441 348
 					case PARA_NAME:
442
-					case IN_ADDR_SPEC:
443
-					case IN_ADDR_SPEC_ONLY:
444
-					case IN_NAME_ADDR:
445 349
 					case PARA_VALUE_QUOTED:
446 350
 						break;
447 351
 					case S_PARA_NAME:
... ...
@@ -454,6 +358,215 @@ char* parse_to(char* buffer, char *end, struct to_body *to_b)
454 358
 						break;
455 359
 					case F_CRLF:
456 360
 					case F_LF:
361
+					case F_CR:
362
+						/*previous=crlf and now !=' '*/
363
+						goto endofheader;
364
+					default:
365
+						DBG("DEBUG: parse_to_param: "
366
+						"spitting out [%c] in status %d\n",*tmp,status );
367
+						goto error;
368
+				}
369
+		}/*switch*/
370
+	}/*for*/
371
+
372
+
373
+endofheader:
374
+	*returned_status=saved_status;
375
+	return tmp;
376
+
377
+error:
378
+	LOG(L_ERR, "to_param parse error\n");
379
+	if (param) pkg_free(param);
380
+	to_b->error=PARSE_ERROR;
381
+	return tmp;
382
+}
383
+
384
+
385
+
386
+
387
+char* parse_to(char* buffer, char *end, struct to_body *to_b)
388
+{
389
+	struct to_param *param=0;
390
+	int status = START_TO;
391
+	int saved_status;
392
+	char  *tmp,*posible_end;
393
+
394
+	posible_end = 0;
395
+	for( tmp=buffer; tmp<end; tmp++)
396
+	{
397
+		switch(*tmp)
398
+		{
399
+			case ' ':
400
+			case '\t':
401
+				switch (status)
402
+				{
403
+					case F_CRLF:
404
+					case F_LF:
405
+					case F_CR:
406
+						/*previous=crlf and now =' '*/
407
+						status=saved_status;
408
+						break;
409
+				}
410
+				break;
411
+			case '\n':
412
+				switch (status)
413
+				{
414
+					case BODY:
415
+						saved_status=status;
416
+						status=F_LF;
417
+						break;
418
+					case F_CR:
419
+						status=F_CRLF;
420
+						break;
421
+					case F_CRLF:
422
+					case F_LF:
423
+						status=saved_status;
424
+						goto endofheader;
425
+					default:
426
+						LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
427
+						"in status %d .\n",*tmp,status);
428
+				}
429
+				break;
430
+			case '\r':
431
+				switch (status)
432
+				{
433
+					case BODY:
434
+						saved_status=status;
435
+						status=F_CR;
436
+						break;
437
+					case F_CRLF:
438
+					case F_CR:
439
+					case F_LF:
440
+						status=saved_status;
441
+						goto endofheader;
442
+					default:
443
+						LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
444
+						"in status %d .\n",*tmp,status);
445
+						goto error;
446
+				}
447
+				break;
448
+			case '\\':
449
+				posible_end = 0;
450
+				switch (status)
451
+				{
452
+					case QUOTED:
453
+						switch (*(tmp+1))
454
+						{
455
+							case '\n':
456
+							case '\r':
457
+								break;
458
+							default:
459
+								tmp++;
460
+						}
461
+					default:
462
+						LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
463
+						"in status %d .\n",*tmp,status);
464
+						goto error;
465
+				}
466
+				break;
467
+			case '<':
468
+				posible_end = 0;
469
+				switch (status)
470
+				{
471
+					case QUOTED:
472
+						break;
473
+					case START_TO:
474
+						to_b->body.s = tmp;
475
+					case BODY:
476
+						status = ENCLOSED;
477
+						break;
478
+					case F_CRLF:
479
+					case F_LF:
480
+					case F_CR:
481
+						/*previous=crlf and now !=' '*/
482
+						goto endofheader;
483
+					default:
484
+						LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
485
+						"in status %d .\n",*tmp,status);
486
+						goto error;
487
+				}
488
+				break;
489
+			case '>':
490
+				switch (status)
491
+				{
492
+					case QUOTED:
493
+						posible_end = tmp;
494
+						break;
495
+					case ENCLOSED:
496
+						posible_end = tmp;
497
+						status = BODY;
498
+						break;
499
+					case F_CRLF:
500
+					case F_LF:
501
+					case F_CR:
502
+						/*previous=crlf and now !=' '*/
503
+						goto endofheader;
504
+					default:
505
+						LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
506
+						"in status %d .\n",*tmp,status);
507
+						goto error;
508
+				}
509
+				break;
510
+			case '"':
511
+				posible_end = 0;
512
+				switch (status)
513
+				{
514
+					case START_TO:
515
+						to_b->body.s = tmp;
516
+					case BODY:
517
+						status = QUOTED;
518
+						break;
519
+					case QUOTED:
520
+						status = BODY;
521
+						break;
522
+					case F_CRLF:
523
+					case F_LF:
524
+					case F_CR:
525
+						/*previous=crlf and now !=' '*/
526
+						goto endofheader;
527
+					default:
528
+						LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
529
+						"in status %d .\n",*tmp,status);
530
+						goto error;
531
+				}
532
+				break;
533
+			case ';' :
534
+				switch (status)
535
+				{
536
+					case QUOTED:
537
+					case ENCLOSED:
538
+						posible_end = 0;
539
+						break;
540
+					case BODY:
541
+						tmp = parse_to_param(tmp,end,to_b,&saved_status);
542
+						goto endofheader;
543
+					case F_CRLF:
544
+					case F_LF:
545
+					case F_CR:
546
+						posible_end = 0;
547
+						/*previous=crlf and now !=' '*/
548
+						goto endofheader;
549
+					default:
550
+						LOG( L_ERR , "ERROR: parse_to : unexpected char [%c] "
551
+						"in status %d .\n",*tmp,status);
552
+						goto error;
553
+				}
554
+				break;
555
+			default:
556
+				switch (status)
557
+				{
558
+					case START_TO:
559
+						to_b->body.s=tmp;
560
+						posible_end = tmp;
561
+						status = BODY;
562
+						break;
563
+					case QUOTED:
564
+					case ENCLOSED:
565
+					case BODY:
566
+						posible_end = tmp;
567
+						break;
568
+					case F_CRLF:
569
+					case F_LF:
457 570
 					case F_CR:
458 571
 						/*previous=crlf and now !=' '*/
459 572
 						goto endofheader;
... ...
@@ -470,19 +583,26 @@ endofheader:
470 583
 	DBG("end of header reached, state=%d\n", status);
471 584
 	/* check if error*/
472 585
 	switch(status){
473
-		case E_ADDR_SPEC:
586
+		case BODY:
474 587
 		case E_PARA_VALUE:
588
+			if (posible_end){
589
+				*(posible_end+1) = 0;
590
+				to_b->body.len=(posible_end+1)-to_b->body.s;
591
+			}else{
592
+				LOG(L_ERR, "ERROR: parse_to: invalid To - unexpected "
593
+					"end of header in %d status\n", status);
594
+				goto error;
595
+			}
475 596
 			break;
476 597
 		default:
477
-			LOG(L_ERR, "ERROR: parse_to: invalid To - end of header in"
478
-					" state %d\n", status);
598
+			LOG(L_ERR, "ERROR: parse_to: invalid To -  unexpected "
599
+					"end of header in state %d\n", status);
479 600
 			goto error;
480 601
 	}
481 602
 	return tmp;
482 603
 
483 604
 error:
484 605
 	LOG(L_ERR, "to parse error\n");
485
-	if (param) pkg_free(param);
486 606
 	to_b->error=PARSE_ERROR;
487 607
 	return tmp;
488 608