00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 #include "wap_push_pap_mime.h"
00065
00066
00067
00068
00069
00070
00071 static int is_cr(int c);
00072 static int is_lf(int c);
00073 static int islwspchar(int c);
00074 static long octstr_drop_leading_blanks(Octstr **header_value);
00075 static void drop_separator(Octstr **header_value, long *pos);
00076 static int parse_preamble(Octstr **mime_content, Octstr *boundary);
00077 static long parse_transport_padding(Octstr *mime_content, long pos);
00078 static long parse_terminator(Octstr *mime_content, long pos);
00079 static int parse_body_part(Octstr **multipart, Octstr *boundary,
00080 Octstr **body_part);
00081 static int parse_encapsulation(Octstr **mime_content, Octstr *boundary,
00082 Octstr **push_data, List **content_headers,
00083 Octstr **rdf_content);
00084 static int check_control_headers(Octstr **body_part);
00085 static int check_control_content_type_header(Octstr **body_part, Octstr *boundary);
00086 static int drop_optional_header(Octstr **body_part, char *name, Octstr *boundary);
00087 static int drop_header_true(Octstr **body_part, long content_pos);
00088 static int drop_extension_headers(Octstr **mime_content, Octstr *boundary);
00089 static long parse_field_value(Octstr *pap_content, long pos);
00090 static long parse_field_name(Octstr *pap_content, long pos);
00091 static void octstr_split_by_pos(Octstr **mime_content, Octstr **pap_content,
00092 long boundary_pos);
00093 static Octstr *make_close_delimiter(Octstr *boundary);
00094 static Octstr *make_part_delimiter(Octstr *boundary);
00095 static Octstr *make_start_delimiter(Octstr *dash_boundary);
00096 static int pass_data_headers(Octstr **body_part, List **data_headers);
00097 static int check_data_content_type_header(Octstr **body_part, List **data_headers, Octstr *boundary);
00098 static int pass_optional_header(Octstr **body_part, char *name, List **content_headers,
00099 Octstr *boundary);
00100 static int pass_extension_headers(Octstr **body_part, List **data_headers, Octstr *boundary);
00101 static long pass_field_name(Octstr **body_part, Octstr **content_header,
00102 long pos);
00103 static long pass_field_value(Octstr **body_part, Octstr **content_header,
00104 long pos);
00105 static int parse_epilogue(Octstr **mime_content);
00106 static int parse_tail(Octstr **multipart, Octstr *part_delimiter,
00107 long boundary_pos, long *next_part_pos);
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130 int mime_parse(Octstr *boundary, Octstr *mime_content, Octstr **pap_content,
00131 Octstr **push_data, List **content_headers,
00132 Octstr **rdf_content)
00133 {
00134 int ret;
00135
00136 *pap_content = NULL;
00137 *push_data = NULL;
00138 *content_headers = NULL;
00139 *rdf_content = NULL;
00140
00141 if (parse_preamble(&mime_content, boundary) < 0) {
00142 warning(0, "erroneous preamble");
00143 return 0;
00144 }
00145 if (parse_body_part(&mime_content, boundary, pap_content) <= 0) {
00146 warning(0, "erroneous control entity");
00147 return 0;
00148 }
00149 if (check_control_headers(pap_content) == 0) {
00150 warning(0, "erroneous control headers");
00151 return 0;
00152 }
00153
00154 ret = -1;
00155 if ((ret = parse_encapsulation(&mime_content, boundary, push_data,
00156 content_headers, rdf_content)) < 0) {
00157 warning(0, "erroneous content entity (push message)");
00158 return 0;
00159 } else if (ret == 0) {
00160 gw_assert(*rdf_content == NULL);
00161 if (octstr_len(mime_content) != 0)
00162 parse_epilogue(&mime_content);
00163 return 1;
00164 }
00165
00166 if (check_control_headers(rdf_content) == 0) {
00167 warning(0, "erroneous capacity (rdf) headers");
00168 return 0;
00169 }
00170
00171 if (octstr_len(mime_content) != 0)
00172 parse_epilogue(&mime_content);
00173 gw_assert(octstr_len(mime_content) == 0);
00174
00175 return 1;
00176 }
00177
00178
00179
00180
00181
00182
00183 static int is_cr(int c)
00184 {
00185 return c == '\r';
00186 }
00187
00188 static int is_lf(int c)
00189 {
00190 return c == '\n';
00191 }
00192
00193
00194
00195
00196 static int islwspchar(int c)
00197 {
00198 return c == '\t' || c == ' ';
00199 }
00200
00201
00202 static int parse_tail(Octstr **multipart, Octstr *delimiter,
00203 long boundary_pos, long *next_part_pos)
00204 {
00205 *next_part_pos = parse_transport_padding(*multipart,
00206 boundary_pos + octstr_len(delimiter));
00207
00208 if ((*next_part_pos = parse_terminator(*multipart, *next_part_pos)) < 0)
00209 return -1;
00210
00211 return 0;
00212 }
00213
00214
00215
00216
00217
00218
00219 static int parse_preamble(Octstr **mime_content, Octstr *boundary)
00220 {
00221 long boundary_pos,
00222 next_part_pos;
00223 Octstr *dash_boundary;
00224
00225 boundary_pos = next_part_pos = -1;
00226 dash_boundary = make_start_delimiter(boundary);
00227
00228 if ((boundary_pos = octstr_search(*mime_content, dash_boundary, 0)) < 0)
00229 goto error;
00230
00231 if (parse_tail(mime_content, dash_boundary, boundary_pos,
00232 &next_part_pos) < 0)
00233 goto error;
00234
00235 octstr_delete(*mime_content, 0, next_part_pos);
00236 octstr_destroy(dash_boundary);
00237
00238 return 0;
00239
00240 error:
00241 octstr_destroy(dash_boundary);
00242 return -1;
00243 }
00244
00245 static long parse_terminator(Octstr *mime_content, long pos)
00246 {
00247 if (is_cr(octstr_get_char(mime_content, pos)))
00248 ++pos;
00249 else
00250 return -1;
00251
00252 if (is_lf(octstr_get_char(mime_content, pos)))
00253 ++pos;
00254 else
00255 return -1;
00256
00257 return pos;
00258 }
00259
00260 static long parse_transport_padding(Octstr *mime_content, long pos)
00261 {
00262 while (islwspchar(octstr_get_char(mime_content, pos)))
00263 ++pos;
00264
00265 return pos;
00266 }
00267
00268 static long parse_close_delimiter(Octstr *close_delimiter, Octstr *mime_content,
00269 long pos)
00270 {
00271 if (octstr_ncompare(close_delimiter, mime_content,
00272 octstr_len(close_delimiter)) != 0)
00273 return -1;
00274 pos += octstr_len(close_delimiter);
00275
00276 return pos;
00277 }
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288 static int parse_body_part (Octstr **multipart, Octstr *boundary,
00289 Octstr **body_part)
00290 {
00291 Octstr *part_delimiter,
00292 *close_delimiter;
00293 long boundary_pos,
00294 close_delimiter_pos,
00295 next_part_pos,
00296 epilogue_pos;
00297
00298 part_delimiter = make_part_delimiter(boundary);
00299 close_delimiter = make_close_delimiter(boundary);
00300
00301 if ((close_delimiter_pos = octstr_search(*multipart,
00302 close_delimiter, 0)) < 0)
00303 goto error;
00304
00305 boundary_pos = octstr_search(*multipart, part_delimiter, 0);
00306 if (boundary_pos == close_delimiter_pos) {
00307 octstr_split_by_pos(multipart, body_part, close_delimiter_pos);
00308 if ((epilogue_pos =
00309 parse_close_delimiter(close_delimiter, *multipart, 0)) < 0)
00310 goto error;
00311 epilogue_pos = parse_transport_padding(*multipart, epilogue_pos);
00312 octstr_delete(*multipart, 0, epilogue_pos);
00313 goto last_part;
00314 }
00315
00316 octstr_split_by_pos(multipart, body_part, boundary_pos);
00317
00318 if (parse_tail(multipart, part_delimiter, 0, &next_part_pos) < 0) {
00319 goto error;
00320 }
00321
00322 octstr_delete(*multipart, 0, next_part_pos);
00323 octstr_destroy(part_delimiter);
00324 octstr_destroy(close_delimiter);
00325
00326 return 1;
00327
00328 error:
00329 octstr_destroy(part_delimiter);
00330 octstr_destroy(close_delimiter);
00331 return -1;
00332
00333 last_part:
00334 octstr_destroy(part_delimiter);
00335 octstr_destroy(close_delimiter);
00336 return 0;
00337
00338 }
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353 static int parse_encapsulation(Octstr **mime_content, Octstr *boundary,
00354 Octstr **push_data, List **content_headers,
00355 Octstr **rdf_content)
00356 {
00357 int ret;
00358
00359 ret = -1;
00360 if ((ret = parse_body_part(mime_content, boundary, push_data)) < 0)
00361 return -1;
00362 if (pass_data_headers(push_data, content_headers) == 0)
00363 return -1;
00364
00365 if (ret == 0) {
00366 *rdf_content = NULL;
00367 return 0;
00368 }
00369
00370 if ((ret = parse_body_part(mime_content, boundary, rdf_content)) < 0 ||
00371 ret > 0)
00372 return -1;
00373 else if (ret == 0)
00374 return 1;
00375
00376 return 1;
00377 }
00378
00379
00380
00381
00382 static void octstr_split_by_pos(Octstr **os1, Octstr **os2,
00383 long boundary_pos)
00384 {
00385 *os2 = octstr_copy(*os1, 0, boundary_pos);
00386 octstr_delete(*os1, 0, boundary_pos);
00387 }
00388
00389 static Octstr *make_close_delimiter(Octstr *boundary)
00390 {
00391 Octstr *close_delimiter;
00392
00393 close_delimiter = make_part_delimiter(boundary);
00394 octstr_format_append(close_delimiter, "%s", "--");
00395
00396 return close_delimiter;
00397 }
00398
00399 static Octstr *make_part_delimiter(Octstr *dash_boundary)
00400 {
00401 Octstr *part_delimiter;
00402
00403 part_delimiter = octstr_create("\r\n--");
00404 octstr_append(part_delimiter, dash_boundary);
00405
00406 return part_delimiter;
00407 }
00408
00409 static Octstr *make_start_delimiter(Octstr *dash_boundary)
00410 {
00411 Octstr *start_delimiter;
00412
00413 start_delimiter = octstr_create("--");
00414 octstr_append(start_delimiter, dash_boundary);
00415
00416 return start_delimiter;
00417 }
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427 static int check_control_headers(Octstr **body_part)
00428 {
00429 if (check_control_content_type_header(body_part, octstr_imm("\r\n\r\n")) == 0)
00430 return 0;
00431 if (drop_optional_header(body_part, "Content-Transfer-Encoding:",
00432 octstr_imm("\r\n\r\n")) == 0)
00433 return 0;
00434 if (drop_optional_header(body_part, "Content-ID:", octstr_imm("\r\n\r\n")) == 0)
00435 return 0;
00436 if (drop_optional_header(body_part, "Content-Description:", octstr_imm("\r\n\r\n")) == 0)
00437 return 0;
00438 if (drop_extension_headers(body_part, octstr_imm("\r\n\r\n")) == 0)
00439 return 0;
00440
00441 return 1;
00442 }
00443
00444 static int check_control_content_type_header(Octstr **body_part, Octstr *boundary)
00445 {
00446 long content_pos;
00447 long message_start_pos;
00448
00449 message_start_pos = octstr_search(*body_part, boundary, 0);
00450 if ((content_pos = octstr_case_nsearch(*body_part, octstr_imm("Content-Type:"), 0,
00451 message_start_pos)) < 0 ||
00452 octstr_case_search(*body_part, octstr_imm("application/xml"), 0) < 0) {
00453 return 0;
00454 }
00455
00456 if (drop_header_true(body_part, content_pos) < 0)
00457 return 0;
00458
00459 return 1;
00460 }
00461
00462
00463
00464
00465
00466
00467 static int drop_header_true(Octstr **body_part, long content_pos)
00468 {
00469 long next_header_pos;
00470
00471 next_header_pos = -1;
00472 if ((next_header_pos = parse_field_value(*body_part, content_pos)) == 0)
00473 return 0;
00474 if ((next_header_pos = parse_terminator(*body_part, next_header_pos)) == 0)
00475 return 0;
00476 octstr_delete(*body_part, 0, next_header_pos);
00477
00478 return 1;
00479 }
00480
00481 static int drop_optional_header(Octstr **body_part, char *name, Octstr *boundary)
00482 {
00483 long content_pos;
00484 long message_start_pos;
00485
00486 content_pos = -1;
00487 message_start_pos = octstr_search(*body_part, boundary, 0);
00488
00489 if ((content_pos = octstr_case_nsearch(*body_part, octstr_imm(name), 0, message_start_pos)) < 0)
00490 return 1;
00491
00492 if (drop_header_true(body_part, content_pos) < 0)
00493 return 0;
00494
00495 return 1;
00496 }
00497
00498
00499
00500
00501
00502
00503
00504 static int drop_extension_headers(Octstr **body_part, Octstr *boundary)
00505 {
00506 long content_pos,
00507 next_header_pos;
00508 long next_content_part_pos;
00509
00510 next_content_part_pos = octstr_case_search(*body_part, boundary, 0);
00511 do {
00512 if ((content_pos = octstr_case_nsearch(*body_part, octstr_imm("Content"), 0,
00513 next_content_part_pos)) < 0)
00514 return 1;
00515 if ((next_header_pos = parse_field_name(*body_part, content_pos)) < 0)
00516 return 0;
00517 if ((next_header_pos = parse_field_value(*body_part,
00518 next_header_pos)) < 0)
00519 return 0;
00520 if ((next_header_pos = parse_terminator(*body_part,
00521 next_header_pos)) == 0)
00522 return 0;
00523 } while (islwspchar(octstr_get_char(*body_part, next_header_pos)));
00524
00525 octstr_delete(*body_part, content_pos, next_header_pos - content_pos);
00526
00527 return 1;
00528 }
00529
00530 static long parse_field_value(Octstr *pap_content, long pos)
00531 {
00532 int c;
00533
00534 while (!is_cr(c = octstr_get_char(pap_content, pos)) &&
00535 pos < octstr_len(pap_content)) {
00536 ++pos;
00537 }
00538
00539 if (is_lf(c)) {
00540 if (is_lf(octstr_get_char(pap_content, pos))) {
00541 ++pos;
00542 } else {
00543 return -1;
00544 }
00545 }
00546
00547 if (pos == octstr_len(pap_content)) {
00548 return -1;
00549 }
00550
00551 return pos;
00552 }
00553
00554 static long parse_field_name(Octstr *content, long pos)
00555 {
00556 while (octstr_get_char(content, pos) != ':' &&
00557 pos < octstr_len(content))
00558 ++pos;
00559
00560 if (pos == octstr_len(content))
00561 return -1;
00562
00563 return pos;
00564 }
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575 static int pass_data_headers(Octstr **body_part, List **data_headers)
00576 {
00577 *data_headers = http_create_empty_headers();
00578
00579 if (check_data_content_type_header(body_part, data_headers, octstr_imm("\r\n\r\n")) == 0) {
00580 warning(0, "MIME: pass_data_headers: Content-Type header missing");
00581 return 0;
00582 }
00583
00584 if (pass_optional_header(body_part, "Content-Transfer-Encoding", data_headers,
00585 octstr_imm("\r\n\r\n")) < 0)
00586 goto operror;
00587 if (pass_optional_header(body_part, "Content-ID", data_headers, octstr_imm("\r\n\r\n")) < 0)
00588 goto operror;
00589 if (pass_optional_header(body_part, "Content-Description", data_headers,
00590 octstr_imm("\r\n\r\n")) < 0)
00591 goto operror;
00592 if (pass_extension_headers(body_part, data_headers, octstr_imm("\r\n\r\n")) == 0)
00593 goto operror;
00594
00595 octstr_delete(*body_part, 0, octstr_len(octstr_imm("\r\n")));
00596
00597 return 1;
00598
00599 operror:
00600 warning(0, "MIME: pass_data_headers: an unparsable optional header");
00601 return 0;
00602 }
00603
00604
00605
00606
00607
00608
00609 static int check_data_content_type_header(Octstr **body_part, List **content_headers,
00610 Octstr *boundary)
00611 {
00612 long header_pos,
00613 next_header_pos;
00614 Octstr *content_header;
00615 long message_start_pos;
00616
00617 header_pos = next_header_pos = -1;
00618 content_header = octstr_create("Content-Type");
00619 message_start_pos = octstr_search(*body_part, boundary, 0);
00620
00621 if ((header_pos = octstr_case_nsearch(*body_part, content_header, 0,
00622 message_start_pos)) < 0) {
00623 goto error;
00624 }
00625 if ((next_header_pos = pass_field_value(body_part, &content_header,
00626 header_pos + octstr_len(content_header))) < 0) {
00627 goto error;
00628 }
00629 if ((next_header_pos = parse_terminator(*body_part, next_header_pos)) < 0) {
00630 goto error;
00631 }
00632
00633 octstr_delete(*body_part, header_pos, next_header_pos - header_pos);
00634 gwlist_append(*content_headers, octstr_duplicate(content_header));
00635 octstr_destroy(content_header);
00636
00637 return 1;
00638
00639 error:
00640 octstr_destroy(content_header);
00641 return 0;
00642 }
00643
00644
00645
00646
00647
00648
00649 static int pass_optional_header(Octstr **body_part, char *name, List **content_headers,
00650 Octstr *boundary)
00651 {
00652 long content_pos,
00653 next_header_pos;
00654 Octstr *osname,
00655 *osvalue;
00656 long message_start_pos;
00657
00658 content_pos = next_header_pos = -1;
00659 osname = octstr_create(name);
00660 osvalue = octstr_create("");
00661 message_start_pos = octstr_search(*body_part, boundary, 0);
00662
00663 if ((content_pos = octstr_case_nsearch(*body_part, osname, 0, message_start_pos)) < 0)
00664 goto noheader;
00665 if ((next_header_pos = pass_field_value(body_part, &osvalue,
00666 content_pos + octstr_len(osname))) < 0)
00667 goto error;
00668 if ((next_header_pos =
00669 parse_terminator(*body_part, next_header_pos)) == 0)
00670 goto error;
00671
00672 drop_separator(&osvalue, &next_header_pos);
00673 http_header_add(*content_headers, name, octstr_get_cstr(osvalue));
00674 octstr_delete(*body_part, content_pos, next_header_pos - content_pos);
00675
00676 octstr_destroy(osname);
00677 octstr_destroy(osvalue);
00678 return 1;
00679
00680 error:
00681 octstr_destroy(osvalue);
00682 octstr_destroy(osname);
00683 return -1;
00684
00685 noheader:
00686 octstr_destroy(osvalue);
00687 octstr_destroy(osname);
00688 return 0;
00689 }
00690
00691
00692
00693
00694 static void drop_separator(Octstr **header_value, long *pos)
00695 {
00696 long count;
00697
00698 octstr_delete(*header_value, 0, 1);
00699 count = octstr_drop_leading_blanks(header_value);
00700 pos = pos - 1 - count;
00701 }
00702
00703
00704
00705
00706 static long octstr_drop_leading_blanks(Octstr **header_value)
00707 {
00708 long count;
00709
00710 count = 0;
00711 while (octstr_get_char(*header_value, 0) == ' ') {
00712 octstr_delete(*header_value, 0, 1);
00713 ++count;
00714 }
00715
00716 return count;
00717 }
00718
00719
00720
00721
00722
00723
00724
00725 static int pass_extension_headers(Octstr **body_part, List **content_headers, Octstr *boundary)
00726 {
00727 long next_field_part_pos,
00728 count;
00729 Octstr *header_name,
00730 *header_value;
00731 long next_content_part_pos;
00732
00733 header_name = octstr_create("");
00734 header_value = octstr_create("");
00735 count = 0;
00736 next_field_part_pos = 0;
00737 next_content_part_pos = octstr_search(*body_part, boundary, 0);
00738
00739 do {
00740 if ((octstr_case_nsearch(*body_part, octstr_imm("Content"), 0,
00741 next_content_part_pos)) < 0)
00742 goto end;
00743 if ((next_field_part_pos = pass_field_name(body_part, &header_name,
00744 next_field_part_pos)) < 0)
00745 goto error;
00746 if ((next_field_part_pos = pass_field_value(body_part, &header_value,
00747 next_field_part_pos)) < 0)
00748 goto error;
00749 if ((next_field_part_pos = parse_terminator(*body_part,
00750 next_field_part_pos)) == 0)
00751 goto error;
00752 drop_separator(&header_value, &next_field_part_pos);
00753 http_header_add(*content_headers, octstr_get_cstr(header_name),
00754 octstr_get_cstr(header_value));
00755 } while (islwspchar(octstr_get_char(*body_part, next_field_part_pos)));
00756
00757 octstr_delete(*body_part, 0, next_field_part_pos);
00758
00759
00760
00761
00762
00763 end:
00764 octstr_destroy(header_name);
00765 octstr_destroy(header_value);
00766 return 1;
00767
00768 error:
00769 octstr_destroy(header_name);
00770 octstr_destroy(header_value);
00771 return 0;
00772 }
00773
00774 static long pass_field_value(Octstr **body_part, Octstr **header,
00775 long pos)
00776 {
00777 int c;
00778 long start;
00779 Octstr *field = NULL;
00780
00781 start = pos;
00782 while (!is_cr(c = octstr_get_char(*body_part, pos)) &&
00783 pos < octstr_len(*body_part)) {
00784 ++pos;
00785 }
00786
00787 if (pos == octstr_len(*body_part)) {
00788 return -1;
00789 }
00790
00791 field = octstr_copy(*body_part, start, pos - start);
00792 octstr_append(*header, field);
00793
00794 octstr_destroy(field);
00795 return pos;
00796 }
00797
00798 static long pass_field_name(Octstr **body_part, Octstr **field_part,
00799 long pos)
00800 {
00801 int c;
00802 long start;
00803 Octstr *name = NULL;
00804
00805 start = pos;
00806 while (((c = octstr_get_char(*body_part, pos)) != ':') &&
00807 pos < octstr_len(*body_part)) {
00808 ++pos;
00809 }
00810
00811 if (pos == octstr_len(*body_part)) {
00812 return -1;
00813 }
00814
00815 name = octstr_copy(*body_part, start, pos - start);
00816 octstr_append(*field_part, name);
00817
00818 octstr_destroy(name);
00819 return pos;
00820 }
00821
00822
00823 static int parse_epilogue(Octstr **mime_content)
00824 {
00825 long pos;
00826
00827 if (octstr_len(*mime_content) == 0)
00828 return 0;
00829
00830 if ((pos = parse_terminator(*mime_content, 0)) < 0)
00831 return -1;
00832
00833 octstr_delete(*mime_content, 0, octstr_len(*mime_content));
00834 return 0;
00835 }
00836
See file LICENSE for details about the license agreement for using,
modifying, copying or deriving work from this software.