test_mime.c
上传用户:gzpyjq
上传日期:2013-01-31
资源大小:1852k
文件大小:9k
源码类别:

手机WAP编程

开发平台:

WINDOWS

  1. /*
  2.  * test_mime.c: A simple program for testing the mime parser
  3.  *
  4.  * We add carriage returs to the source, so that output of some editors are 
  5.  * acceptable and remove them from component files, in case someone likes to 
  6.  * compile them (come on, this is only a test program).
  7.  * We panic on errors, so that this function can be used by a shell script
  8.  * for testing purposes.
  9.  *
  10.  * By Aarno Syv鋘en for Wiral Ltd
  11.  */
  12. #include <stdio.h>
  13. #include "gwlib/gwlib.h"
  14. #include "gw/wap_push_pap_mime.h"
  15. static void prepend_crlf(Octstr **os)
  16. {
  17.     octstr_insert(*os, octstr_imm("rn"), 0);
  18. }
  19. static void append_crlf(Octstr *os)
  20. {
  21.     octstr_append(os, octstr_imm("rn"));
  22. }
  23. static void add_crs(Octstr *os)
  24. {
  25.     long i;
  26.     Octstr *nos;
  27.     if (os == NULL)
  28.         return;
  29.     nos = octstr_format("%c", 'r');
  30.     i = 0;
  31.     while (i < octstr_len(os)) {
  32.         if (octstr_get_char(os, i) == 'n') {
  33.     octstr_insert(os, nos, i);
  34.             ++i;
  35.         }
  36.         ++i;
  37.     }
  38.     octstr_destroy(nos);
  39. }
  40. static void remove_crs(Octstr *os)
  41. {
  42.     long i;
  43.     if (os == NULL)
  44.         return;
  45.     i = 0;
  46.     while (i < octstr_len(os)) {
  47.         if (octstr_get_char(os, i) == 'r') {
  48.     octstr_delete(os, i, 1);
  49.             --i;
  50.         }
  51.         ++i;
  52.     }
  53. }
  54. static int skip_tail(Octstr **os, int delimiter)
  55. {
  56.     long delimiter_pos;
  57.     if ((delimiter_pos = octstr_search_char(*os, delimiter, 0)) == -1)
  58.         return 0;
  59.     octstr_delete(*os, delimiter_pos, octstr_len(*os) - delimiter_pos);
  60.     return 1;
  61. }
  62. static void help(void)
  63. {
  64.     info(0, "Usage: test_mime [options] source_file");
  65.     info(0, "Parse source file into component parts.");
  66.     info(0, "Source file has the following format:");
  67.     info(0, "    boundary=<mime boundary>;");
  68.     info(0, "    content=<mime content>;");
  69.     info(0, "Content headers are added into the content_file. This file has");
  70.     info(0, "following format:");
  71.     info(0, "    headers=<content_headers>;");
  72.     info(0, "    content=<push-content>;");
  73.     info(0, "And options are");
  74.     info(0, "    -h");
  75.     info(0, "print this info");
  76.     info(0, "    -d filename");
  77.     info(0, "store push data to file filename. Default test/data.txt");
  78.     info(0, "    -c filename");
  79.     info(0, "store push control message to file filename. Default");
  80.     info(0, " test/pap.txt");
  81.     info(0, "    -s");
  82.     info(0, "write push control message and push data to standard output");
  83.     info(0, "Default write it to the file.");
  84. }
  85. int main(int argc, char **argv)
  86. {
  87.     Octstr *mime_content,
  88.            *pap_content,
  89.            *push_data,
  90.            *rdf_content,
  91.            *boundary,
  92.            *push_content_file = NULL,
  93.            *this_header,
  94.            *pap_osname,
  95.            *data_osname;
  96.     List *content_headers,
  97.          *source_parts;
  98.     char *pap_content_file,
  99.          *push_data_file,
  100.          *rdf_content_file;
  101.     int ret,
  102.         std_out,
  103.         opt,
  104.         d_file,
  105.         c_file;
  106.     FILE *fp1,
  107.          *fp2,
  108.          *fp3;
  109.     gwlib_init();
  110.     std_out = 0;
  111.     d_file = 0;
  112.     c_file = 0;
  113.     data_osname = NULL;
  114.     pap_osname = NULL;
  115.     while ((opt = getopt(argc, argv, "hd:sc:")) != EOF) {
  116.         switch(opt) {
  117.             case 'h':
  118.                 help();
  119.                 exit(1);
  120.             break;
  121.             case 'd':
  122.         d_file = 1;
  123.                 data_osname = octstr_create(optarg);
  124.             break;
  125.             case 'c':
  126.         c_file = 1;
  127.                 pap_osname = octstr_create(optarg);
  128.             break;
  129.             case 's':
  130.                 std_out = 1;
  131.             break;
  132.             case '?':
  133.             default:
  134.                 error(0, "Invalid option %c", opt);
  135.                 help();
  136.                 panic(0, "Stopping");
  137.             break;
  138.         }
  139.     }    
  140.     if (optind >= argc) {
  141.         help();
  142.         panic(0, "missing arguments, stopping");
  143.     }
  144.     if (!c_file)
  145.         pap_content_file = "test/pap.txt";
  146.     else
  147.         pap_content_file = octstr_get_cstr(pap_osname);
  148.     if (!d_file)
  149.         push_data_file = "test/data.txt";
  150.     else
  151.         push_data_file = octstr_get_cstr(data_osname);
  152.     rdf_content_file = "test/rdf.txt";
  153.     mime_content = octstr_read_file(argv[optind]);
  154.     if (mime_content == NULL) {
  155.         octstr_destroy(mime_content);
  156.         error(0, "No MIME source");
  157.         panic(0, "Stopping");
  158.     }
  159.     source_parts = octstr_split(mime_content, octstr_imm("content="));
  160.     if (list_len(source_parts) == 1) {     /* a hack to circumvent a bug */
  161.         error(0, "Badly formatted source:");
  162.         octstr_destroy(mime_content);
  163.         list_destroy(source_parts, octstr_destroy_item);
  164.         panic(0, "Stopping");
  165.     }
  166.     boundary = list_extract_first(source_parts);
  167.     octstr_delete(boundary, 0, octstr_len(octstr_imm("boundary=")));
  168.     if (skip_tail(&boundary, ';') == 0) {
  169.         error(0, "Cannot determine boundary, no delimiter; possible");
  170.         octstr_dump(boundary, 0);
  171.         goto no_parse;
  172.     }
  173.     
  174.     octstr_destroy(mime_content);
  175.     mime_content = list_extract_first(source_parts);
  176.     if (skip_tail(&mime_content, ';') == 0){
  177.         error(0, "Cannot determine mime content, no delimiter");
  178.         octstr_dump(mime_content, 0);
  179.         goto no_parse;
  180.     }
  181.     prepend_crlf(&mime_content);
  182.     add_crs(mime_content);
  183.     append_crlf(mime_content);
  184.     
  185.     ret = mime_parse(boundary, mime_content, &pap_content, &push_data, 
  186.                      &content_headers, &rdf_content);
  187.     if (ret == 0) {
  188.         error(0, "Mime_parse returned 0, cannot continue");
  189.         goto error;
  190.     }
  191.     remove_crs(pap_content);
  192.     if (!std_out) {
  193.         fp1 = fopen(pap_content_file, "a");
  194.         if (fp1 == NULL) {
  195.             error(0, "Cannot open the file for pap control message");
  196.             goto error;
  197.         }
  198.         octstr_print(fp1, pap_content);
  199.         debug("test.mime", 0, "pap control message appended to the file");
  200.         fclose(fp1);
  201.     } else {
  202.         debug("test.mime", 0, "pap control message was");
  203.         octstr_dump(pap_content, 0);
  204.     }
  205.     remove_crs(push_data);
  206.     if (!std_out) {
  207.         fp2 = fopen(push_data_file, "a");
  208.         if (fp2 == NULL) {
  209.             error(0, "Cannot open the push data file");
  210.             goto error;
  211.         }
  212.         push_content_file = octstr_create("");
  213.         octstr_append(push_content_file, octstr_imm("headers="));
  214.         while (list_len(content_headers) > 0) {
  215.             octstr_append(push_content_file, 
  216.                           this_header = list_extract_first(content_headers));
  217.             octstr_format_append(push_content_file, "%c", ' ');
  218.             octstr_destroy(this_header);
  219.         }
  220.         octstr_append(push_content_file, octstr_imm(";n"));
  221.         octstr_append(push_content_file, octstr_imm("content="));
  222.         octstr_append(push_content_file, push_data);
  223.         octstr_append(push_content_file, octstr_imm(";n"));
  224.         octstr_print(fp2, push_content_file);
  225.         debug("test.mime", 0, "push content appended to the file");
  226.         fclose(fp2);
  227.     } else {
  228.         debug("test.mime", 0, "Content headers were");
  229.         http_header_dump(content_headers);
  230.         debug("test.mime", 0, "And push content itself");
  231.         octstr_dump(push_data, 0);
  232.     }
  233.     if (rdf_content != NULL)
  234.         remove_crs(rdf_content);
  235.     if (!std_out && rdf_content != NULL) {
  236.         fp3 = NULL;
  237.         if (rdf_content != NULL) {
  238.             fp3 = fopen(rdf_content_file, "a");
  239.             if (fp3 == NULL) {
  240.                 error(0, "Cannot open the rdf file");
  241.                 goto cerror;
  242.              }
  243.             octstr_print(fp3, rdf_content);
  244.             debug("test.mime", 0, "push caps message appended to the file");
  245.             fclose(fp3);
  246.         }
  247.     } else {
  248.         if (rdf_content != NULL) {
  249.             debug("test.mime", 0, "push caps message was");
  250.             octstr_dump(rdf_content, 0);
  251.         }
  252.     }
  253.     
  254.     octstr_destroy(boundary);
  255.     octstr_destroy(mime_content);
  256.     octstr_destroy(pap_content);
  257.     octstr_destroy(push_data);
  258.     octstr_destroy(rdf_content);
  259.     octstr_destroy(pap_osname);
  260.     octstr_destroy(data_osname);
  261.     http_destroy_headers(content_headers);
  262.     list_destroy(source_parts, octstr_destroy_item);
  263.     octstr_destroy(push_content_file);
  264.     gwlib_shutdown();
  265.     info(0, "MIME data parsed successfully");
  266.     return 0;
  267. no_parse:
  268.     octstr_destroy(mime_content);
  269.     octstr_destroy(pap_osname);
  270.     octstr_destroy(data_osname);
  271.     list_destroy(source_parts, octstr_destroy_item);
  272.     octstr_destroy(boundary);
  273.     gwlib_shutdown();
  274.     panic(0, "Stopping");
  275. error:
  276.     octstr_destroy(mime_content);
  277.     list_destroy(source_parts, octstr_destroy_item);
  278.     octstr_destroy(boundary);
  279.     octstr_destroy(pap_content);
  280.     octstr_destroy(push_data);
  281.     octstr_destroy(pap_osname);
  282.     octstr_destroy(data_osname);
  283.     http_destroy_headers(content_headers);
  284.     octstr_destroy(rdf_content);
  285.     gwlib_shutdown();
  286.     panic(0, "Stopping");
  287. cerror:
  288.     octstr_destroy(mime_content);
  289.     list_destroy(source_parts, octstr_destroy_item);
  290.     octstr_destroy(boundary);
  291.     octstr_destroy(pap_content);
  292.     octstr_destroy(push_data);
  293.     octstr_destroy(push_content_file);
  294.     octstr_destroy(pap_osname);
  295.     octstr_destroy(data_osname);
  296.     http_destroy_headers(content_headers);
  297.     octstr_destroy(rdf_content);
  298.     gwlib_shutdown();
  299.     panic(0, "Stopping");
  300. /* return after panic allways required by gcc */
  301.     return 1;
  302. }