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,
59 extract_key(name, content_disposition, " name");
60 extract_key(filename, content_disposition, "filename");
62 /* Nested multipart gets recursively fed back into the parser */
63 if (!strncasecmp(content_type, "multipart", 9)) {
64 mime_parser(content, length, content_type, CallBack);
67 /**** OTHERWISE, HERE'S WHERE WE HANDLE THE STUFF!! *****/
69 CallBack(name, filename, "", content, content_type, length);
71 /**** END OF STUFF-HANDLER ****/
77 * Take a part, figure out its length, and do something with it
78 * (This function expects to be fed HEADERS+CONTENT)
80 void handle_part(char *content,
82 char *supplied_content_type,
91 char content_type[256];
92 char content_disposition[256];
95 int crlf = 0; /* set to 1 for crlf-style newlines */
98 strcpy(content_type, supplied_content_type);
100 /* Strip off any leading blank lines. */
102 while ((!strncmp(start, "\r", 1)) || (!strncmp(start, "\n", 1))) {
107 /* At this point all we have left is the headers and the content. */
111 buf[strlen(buf)+1] = 0;
112 if (strlen(buf)<((sizeof buf)-1)) {
113 strncpy(&buf[strlen(buf)], start, 1);
117 } while((buf[strlen(buf)-1] != 10) && (part_length>0));
118 if (part_length <= 0) return;
119 buf[strlen(buf)-1] = 0;
120 if (buf[strlen(buf)-1]==13) {
121 buf[strlen(buf)-1] = 0;
124 if (!strncasecmp(buf, "Content-type: ", 14)) {
125 strcpy(content_type, &buf[14]);
127 if (!strncasecmp(buf, "Content-disposition: ", 21)) {
128 strcpy(content_disposition, &buf[21]);
130 } while (strlen(buf)>0);
132 if (crlf) actual_length = part_length - 2;
133 else actual_length = part_length - 1;
135 /* Now that we've got this component isolated, what to do with it? */
136 do_something_with_it(start, actual_length,
137 content_type, content_disposition, CallBack);
143 * Break out the components of a multipart message
144 * (This function expects to be fed CONTENT ONLY, no headers)
148 void mime_parser(char *content,
161 int have_boundary = 0;
165 int bytes_processed = 0;
168 /* If it's not multipart, don't process it as multipart */
169 if (strncasecmp(ContentType, "multipart", 9)) {
170 do_something_with_it(content, ContentLength,
171 ContentType, "", CallBack);
175 /* Figure out what the boundary is */
176 strcpy(boundary, ContentType);
177 for (a=0; a<strlen(boundary); ++a) {
178 if (!strncasecmp(&boundary[a], "boundary=", 9)) {
181 strcpy(&boundary[2], &boundary[a+9]);
185 if ((boundary[a]==13) || (boundary[a]==10)) {
190 /* We can't process multipart messages without a boundary. */
191 if (have_boundary == 0) return;
192 strcpy(endary, boundary);
193 strcat(endary, "--");
197 /* Seek to the beginning of the next boundary */
198 while (bytes_processed < ContentLength) {
199 /* && (strncasecmp(ptr, boundary, strlen(boundary))) ) { */
201 if (strncasecmp(ptr, boundary, strlen(boundary))) {
206 /* See if we're at the end */
207 if (!strncasecmp(ptr, endary, strlen(endary))) {
211 /* Seek to the end of the boundary string */
212 if (!strncasecmp(ptr, boundary, strlen(boundary))) {
213 while ( (bytes_processed < ContentLength)
214 && (strncasecmp(ptr, "\n", 1)) ) {
220 while ( (bytes_processed < ContentLength)
221 && (strncasecmp(ptr, boundary, strlen(boundary))) ) {
226 handle_part(beginning, part_length, "", CallBack);
227 /* Back off so we can see the next boundary */