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)
25 strcpy(target, source);
26 for (a = 0; a < strlen(target); ++a) {
27 if ((!strncasecmp(&target[a], key, strlen(key)))
28 && (target[a + strlen(key)] == '=')) {
29 strcpy(target, &target[a + strlen(key) + 1]);
31 strcpy(target, &target[1]);
32 for (b = 0; b < strlen(target); ++b)
44 * The very back end for the component handler
45 * (This function expects to be fed CONTENT ONLY, no headers)
47 void do_something_with_it(char *content,
50 char *content_disposition,
63 extract_key(name, content_disposition, " name");
64 extract_key(filename, content_disposition, "filename");
66 /* Nested multipart gets recursively fed back into the parser */
67 if (!strncasecmp(content_type, "multipart", 9)) {
68 mime_parser(content, length, content_type, CallBack);
70 /**** OTHERWISE, HERE'S WHERE WE HANDLE THE STUFF!! *****/
72 CallBack(name, filename, "", content, content_type, length);
74 /**** END OF STUFF-HANDLER ****/
80 * Take a part, figure out its length, and do something with it
81 * (This function expects to be fed HEADERS+CONTENT)
83 void handle_part(char *content,
85 char *supplied_content_type,
95 char content_type[256];
96 char content_disposition[256];
99 int crlf = 0; /* set to 1 for crlf-style newlines */
102 strcpy(content_type, supplied_content_type);
104 /* Strip off any leading blank lines. */
106 while ((!strncmp(start, "\r", 1)) || (!strncmp(start, "\n", 1))) {
111 /* At this point all we have left is the headers and the content. */
115 buf[strlen(buf) + 1] = 0;
116 if (strlen(buf) < ((sizeof buf) - 1)) {
117 strncpy(&buf[strlen(buf)], start, 1);
121 } while ((buf[strlen(buf) - 1] != 10) && (part_length > 0));
122 if (part_length <= 0)
124 buf[strlen(buf) - 1] = 0;
125 if (buf[strlen(buf) - 1] == 13) {
126 buf[strlen(buf) - 1] = 0;
129 if (!strncasecmp(buf, "Content-type: ", 14)) {
130 strcpy(content_type, &buf[14]);
132 if (!strncasecmp(buf, "Content-disposition: ", 21)) {
133 strcpy(content_disposition, &buf[21]);
135 } while (strlen(buf) > 0);
138 actual_length = part_length - 2;
140 actual_length = part_length - 1;
142 /* Now that we've got this component isolated, what to do with it? */
143 do_something_with_it(start, actual_length,
144 content_type, content_disposition, CallBack);
150 * Break out the components of a multipart message
151 * (This function expects to be fed CONTENT ONLY, no headers)
155 void mime_parser(char *content,
169 int have_boundary = 0;
173 int bytes_processed = 0;
176 /* If it's not multipart, don't process it as multipart */
177 if (strncasecmp(ContentType, "multipart", 9)) {
178 do_something_with_it(content, ContentLength,
179 ContentType, "", CallBack);
182 /* Figure out what the boundary is */
183 strcpy(boundary, ContentType);
184 for (a = 0; a < strlen(boundary); ++a) {
185 if (!strncasecmp(&boundary[a], "boundary=", 9)) {
188 strcpy(&boundary[2], &boundary[a + 9]);
192 if ((boundary[a] == 13) || (boundary[a] == 10)) {
197 /* We can't process multipart messages without a boundary. */
198 if (have_boundary == 0)
200 strcpy(endary, boundary);
201 strcat(endary, "--");
205 /* Seek to the beginning of the next boundary */
206 while (bytes_processed < ContentLength) {
207 /* && (strncasecmp(ptr, boundary, strlen(boundary))) ) { */
209 if (strncasecmp(ptr, boundary, strlen(boundary))) {
213 /* See if we're at the end */
214 if (!strncasecmp(ptr, endary, strlen(endary))) {
217 /* Seek to the end of the boundary string */
218 if (!strncasecmp(ptr, boundary, strlen(boundary))) {
219 while ((bytes_processed < ContentLength)
220 && (strncasecmp(ptr, "\n", 1))) {
226 while ((bytes_processed < ContentLength)
227 && (strncasecmp(ptr, boundary, strlen(boundary)))) {
232 handle_part(beginning, part_length, "", CallBack);
233 /* Back off so we can see the next boundary */