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.
12 #include <sys/types.h>
15 #include "mime_parser.h"
21 void extract_key(char *target, char *source, char *key) {
24 strcpy(target, source);
25 for (a=0; a<strlen(target); ++a) {
26 if ((!strncasecmp(&target[a], key, strlen(key)))
27 && (target[a+strlen(key)]=='=')) {
28 strcpy(target, &target[a+strlen(key)+1]);
29 if (target[0]==34) strcpy(target, &target[1]);
30 for (b=0; b<strlen(target); ++b)
31 if (target[b]==34) target[b]=0;
41 * The very back end for the component handler
42 * (This function expects to be fed CONTENT ONLY, no headers)
44 void do_something_with_it(char *content,
47 char *content_disposition,
58 extract_key(name, content_disposition, " name");
59 extract_key(filename, content_disposition, "filename");
61 /* Nested multipart gets recursively fed back into the parser */
62 if (!strncasecmp(content_type, "multipart", 9)) {
63 mime_parser(content, length, content_type, CallBack);
66 /**** OTHERWISE, HERE'S WHERE WE HANDLE THE STUFF!! *****/
68 CallBack(name, filename, "", content, length);
70 /**** END OF STUFF-HANDLER ****/
76 * Take a part, figure out its length, and do something with it
77 * (This function expects to be fed HEADERS+CONTENT)
79 void handle_part(char *content,
81 char *supplied_content_type,
89 char content_type[256];
90 char content_disposition[256];
93 int crlf = 0; /* set to 1 for crlf-style newlines */
96 strcpy(content_type, supplied_content_type);
98 /* Strip off any leading blank lines. */
100 while ((!strncmp(start, "\r", 1)) || (!strncmp(start, "\n", 1))) {
105 /* At this point all we have left is the headers and the content. */
109 buf[strlen(buf)+1] = 0;
110 if (strlen(buf)<((sizeof buf)-1)) {
111 strncpy(&buf[strlen(buf)], start, 1);
115 } while((buf[strlen(buf)-1] != 10) && (part_length>0));
116 if (part_length <= 0) return;
117 buf[strlen(buf)-1] = 0;
118 if (buf[strlen(buf)-1]==13) {
119 buf[strlen(buf)-1] = 0;
122 if (!strncasecmp(buf, "Content-type: ", 14)) {
123 strcpy(content_type, &buf[14]);
125 if (!strncasecmp(buf, "Content-disposition: ", 21)) {
126 strcpy(content_disposition, &buf[21]);
128 } while (strlen(buf)>0);
130 if (crlf) actual_length = part_length - 2;
131 else actual_length = part_length - 1;
133 /* Now that we've got this component isolated, what to do with it? */
134 do_something_with_it(start, actual_length,
135 content_type, content_disposition, CallBack);
141 * Break out the components of a multipart message
142 * (This function expects to be fed CONTENT ONLY, no headers)
146 void mime_parser(char *content,
158 int have_boundary = 0;
162 int bytes_processed = 0;
165 /* If it's not multipart, don't process it as multipart */
166 if (strncasecmp(ContentType, "multipart", 9)) {
167 do_something_with_it(content, ContentLength,
168 ContentType, "", CallBack);
172 /* Figure out what the boundary is */
173 strcpy(boundary, ContentType);
174 for (a=0; a<strlen(boundary); ++a) {
175 if (!strncasecmp(&boundary[a], "boundary=", 9)) {
178 strcpy(&boundary[2], &boundary[a+9]);
182 if ((boundary[a]==13) || (boundary[a]==10)) {
187 /* We can't process multipart messages without a boundary. */
188 if (have_boundary == 0) return;
189 strcpy(endary, boundary);
190 strcat(endary, "--");
194 /* Seek to the beginning of the next boundary */
195 while (bytes_processed < ContentLength) {
196 /* && (strncasecmp(ptr, boundary, strlen(boundary))) ) { */
198 if (strncasecmp(ptr, boundary, strlen(boundary))) {
203 /* See if we're at the end */
204 if (!strncasecmp(ptr, endary, strlen(endary))) {
208 /* Seek to the end of the boundary string */
209 if (!strncasecmp(ptr, boundary, strlen(boundary))) {
210 while ( (bytes_processed < ContentLength)
211 && (strncasecmp(ptr, "\n", 1)) ) {
217 while ( (bytes_processed < ContentLength)
218 && (strncasecmp(ptr, boundary, strlen(boundary))) ) {
223 handle_part(beginning, part_length, "", CallBack);
224 /* Back off so we can see the next boundary */