got it working ok
[citadel.git] / webcit / mime_parser.c
1 /*
2  * mime_parser.c
3  *
4  * This is a really bad attempt at writing a parser to handle multipart
5  * messages -- in the case of WebCit, a form containing uploaded files.
6  */
7
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <signal.h>
12 #include <sys/types.h>
13 #include <ctype.h>
14 #include <string.h>
15 #include "webcit.h"
16 #include "child.h"
17
18 /*
19  * Take a part, figure out its length, and do something with it
20  */
21 void process_part(char *content, int part_length) {
22         FILE *fp;
23         char filename[256];
24
25         fprintf(stderr, "MIME: process_part() called with a length o' %d\n",
26                 part_length);
27
28         
29         }
30
31
32 /*
33  * Main function of parser
34  */
35 void mime_parser(char *content, int ContentLength, char *ContentType) {
36         char boundary[256];
37         char endary[256];
38         int have_boundary = 0;
39         int a;
40         char *ptr;
41         char *beginning;
42         int bytes_processed = 0;
43         int part_length;
44
45         fprintf(stderr, "MIME: ContentLength: %d, ContentType: %s\n",
46                 ContentLength, ContentType);
47
48         /* Figure out what the boundary is */
49         strcpy(boundary, ContentType);
50         for (a=0; a<strlen(boundary); ++a) {
51                 if (!strncasecmp(&boundary[a], "boundary=", 9)) {
52                         strcpy(boundary, &boundary[a+10]);
53                         have_boundary = 1;
54                         }
55                 if ((boundary[a]==13) || (boundary[a]==10)) {
56                         boundary[a] = 0;
57                         }
58                 }
59
60         /* We can't process multipart messages without a boundary. */
61         if (have_boundary == 0) return;
62         strcpy(endary, boundary);
63         strcat(endary, "--");
64
65         ptr = content;
66
67         /* Seek to the beginning of the next boundary */
68         while ( (bytes_processed < ContentLength)
69               && (strncasecmp(ptr, boundary, strlen(boundary))) ) {
70                 ++ptr;
71                 ++bytes_processed;
72
73                 /* See if we're at the end */
74                 if (!strncasecmp(ptr, endary, strlen(endary))) {
75                         fprintf(stderr, "MIME: the end.\n");
76                         return;
77                         }
78
79                 /* Seek to the end of the boundary string */
80                 if (!strncasecmp(ptr, boundary, strlen(boundary))) {
81                         fprintf(stderr, "MIME: founda bounda\n");
82                         while ( (bytes_processed < ContentLength)
83                               && (strncasecmp(ptr, "\n", 1)) ) {
84                                 ++ptr;
85                                 ++bytes_processed;
86                                 }
87                         beginning = ptr;
88                         part_length = 0;
89                         while ( (bytes_processed < ContentLength)
90                           && (strncasecmp(ptr, boundary, strlen(boundary))) ) {
91                                 ++ptr;
92                                 ++bytes_processed;
93                                 ++part_length;
94                                 }
95                         process_part(beginning, part_length);
96                         /* Back off so we can see the next boundary */
97                         --ptr;
98                         --bytes_processed;
99                         }
100                 }
101         }