117 struct MHD_PostProcessor
140 const char *encoding;
145 const char *boundary;
150 char *nested_boundary;
165 char *content_filename;
170 char *content_transfer_encoding;
196 uint64_t value_offset;
271 struct MHD_PostProcessor *
276 struct MHD_PostProcessor *ret;
277 const char *encoding;
278 const char *boundary;
281 if ((buffer_size < 256) || (connection ==
NULL) || (iter ==
NULL))
286 if (encoding ==
NULL)
299 boundary = strstr (boundary,
"boundary=");
300 if (
NULL == boundary)
302 boundary += strlen (
"boundary=");
303 blen = strlen (boundary);
304 if ((blen == 0) || (blen * 2 + 2 > buffer_size))
306 if ( (boundary[0] ==
'"') && (boundary[blen - 1] ==
'"') )
318 if (
NULL == (ret = malloc (
sizeof (
struct MHD_PostProcessor) + buffer_size + 1)))
320 memset (ret, 0,
sizeof (
struct MHD_PostProcessor) + buffer_size + 1);
321 ret->connection = connection;
324 ret->encoding = encoding;
325 ret->buffer_size = buffer_size;
328 ret->boundary = boundary;
344 const char *post_data,
345 size_t post_data_len)
352 int end_of_value_found;
356 buf = (
char *) &pp[1];
358 while (poff < post_data_len)
370 while ((equals + poff < post_data_len) &&
371 (post_data[equals + poff] !=
'='))
373 if (equals + pp->buffer_pos > pp->buffer_size)
378 memcpy (&buf[pp->buffer_pos], &post_data[poff], equals);
379 pp->buffer_pos += equals;
380 if (equals + poff == post_data_len)
382 buf[pp->buffer_pos] =
'\0';
387 pp->value_offset = 0;
391 memcpy (xbuf, pp->xbuf, pp->xbuf_pos);
397 while ((amper + poff < post_data_len) &&
399 (post_data[amper + poff] !=
'&') &&
400 (post_data[amper + poff] !=
'\n') &&
401 (post_data[amper + poff] !=
'\r'))
403 end_of_value_found = ((amper + poff < post_data_len) &&
404 ((post_data[amper + poff] ==
'&') ||
405 (post_data[amper + poff] ==
'\n') ||
406 (post_data[amper + poff] ==
'\r')));
414 memcpy (&xbuf[xoff], &post_data[poff], delta);
422 if ((delta > 0) && (xbuf[delta - 1] ==
'%'))
424 else if ((delta > 1) && (xbuf[delta - 2] ==
'%'))
431 memcpy (pp->xbuf, &xbuf[delta], xoff - delta);
432 pp->xbuf_pos = xoff - delta;
439 if ((xoff == 0) && (poff == post_data_len))
454 pp->value_offset += xoff;
457 if (end_of_value_found)
460 if ((post_data[poff] ==
'\n') || (post_data[poff] ==
'\r'))
464 else if (post_data[poff] ==
'&')
472 if ((post_data[poff] ==
'\n') || (post_data[poff] ==
'\r'))
504 if (0 == strncasecmp (prefix, line, strlen (prefix)))
506 *suffix = strdup (&line[strlen (prefix)]);
530 const char *boundary,
535 char *buf = (
char *) &pp[1];
538 if (pp->buffer_pos < 2 + blen)
540 if (pp->buffer_pos == pp->buffer_size)
545 if ((0 != memcmp (
"--", buf, 2)) || (0 != memcmp (&buf[2], boundary, blen)))
555 dash = memchr (buf,
'-', pp->buffer_pos);
557 (*ioffptr) += pp->buffer_pos;
562 (*ioffptr) += dash - buf;
567 (*ioffptr) += 2 + blen;
570 pp->state = next_state;
571 pp->dash_state = next_dash_state;
593 if (
NULL != *destination)
597 while (
NULL != (spos = strstr (bpos, key)))
599 if ((spos[klen] !=
'=') || ((spos != buf) && (spos[-1] !=
' ')))
605 if (spos[klen + 1] !=
'"')
607 if (
NULL == (endv = strchr (&spos[klen + 2],
'\"')))
609 vlen = endv - spos - klen - 1;
610 *destination = malloc (vlen);
611 if (
NULL == *destination)
613 (*destination)[vlen - 1] =
'\0';
614 memcpy (*destination, &spos[klen + 2], vlen - 1);
637 size_t *ioffptr,
enum PP_State next_state)
639 char *buf = (
char *) &pp[1];
643 while ((newline < pp->buffer_pos) &&
644 (buf[newline] !=
'\r') && (buf[newline] !=
'\n'))
646 if (newline == pp->buffer_size)
651 if (newline == pp->buffer_pos)
657 pp->state = next_state;
661 if (buf[newline] ==
'\r')
664 if (0 == strncasecmp (
"Content-disposition: ",
665 buf, strlen (
"Content-disposition: ")))
668 "name", &pp->content_name);
670 "filename", &pp->content_filename);
676 buf, &pp->content_transfer_encoding);
678 (*ioffptr) += newline + 1;
702 const char *boundary,
707 char *buf = (
char *) &pp[1];
716 while (newline + 4 < pp->buffer_pos)
718 r = memchr (&buf[newline],
'\r', pp->buffer_pos - newline - 4);
721 newline = pp->buffer_pos - 4;
725 if (0 == memcmp (
"\r\n--", &buf[newline], 4))
729 if (newline + pp->blen + 4 <= pp->buffer_pos)
732 if (0 != memcmp (&buf[newline + 4], boundary, pp->blen))
743 pp->state = next_state;
744 pp->dash_state = next_dash_state;
745 (*ioffptr) += pp->blen + 4;
755 if ((0 == newline) && (pp->buffer_pos == pp->buffer_size))
766 if ( ( (
MHD_YES == pp->must_ikvi) ||
768 (
MHD_NO == pp->ikvi (pp->cls,
771 pp->content_filename,
773 pp->content_transfer_encoding,
774 buf, pp->value_offset, newline)) )
780 pp->value_offset += newline;
781 (*ioffptr) += newline;
795 free (pp->content_name);
796 pp->content_name =
NULL;
800 free (pp->content_type);
801 pp->content_type =
NULL;
803 if ((
NULL != pp->content_filename) &&
806 free (pp->content_filename);
807 pp->content_filename =
NULL;
809 if ((
NULL != pp->content_transfer_encoding) &&
812 free (pp->content_transfer_encoding);
813 pp->content_transfer_encoding =
NULL;
828 const char *post_data,
829 size_t post_data_len)
837 buf = (
char *) &pp[1];
841 while ((poff < post_data_len) ||
842 ((pp->buffer_pos > 0) && (state_changed != 0)))
846 max = pp->buffer_size - pp->buffer_pos;
847 if (max > post_data_len - poff)
848 max = post_data_len - poff;
849 memcpy (&buf[pp->buffer_pos], &post_data[poff], max);
851 pp->buffer_pos += max;
852 if ((max == 0) && (state_changed == 0) && (poff < post_data_len))
884 if ((pp->buffer_pos > 1) && (buf[1] ==
'\n'))
910 pp->state = pp->dash_state;
969 if ((pp->content_type !=
NULL) &&
970 (0 == strncasecmp (pp->content_type,
972 strlen (
"multipart/mixed"))))
974 pp->nested_boundary = strstr (pp->content_type,
"boundary=");
975 if (pp->nested_boundary ==
NULL)
980 pp->nested_boundary =
981 strdup (&pp->nested_boundary[strlen (
"boundary=")]);
982 if (pp->nested_boundary ==
NULL)
990 free (pp->content_type);
991 pp->content_type =
NULL;
992 pp->nlen = strlen (pp->nested_boundary);
998 pp->value_offset = 0;
1018 if (pp->nested_boundary !=
NULL)
1020 free (pp->nested_boundary);
1021 pp->nested_boundary =
NULL;
1027 if (pp->nested_boundary ==
NULL)
1033 pp->nested_boundary,
1048 if (pp->content_name !=
NULL)
1050 if (pp->content_type !=
NULL)
1052 if (pp->content_filename !=
NULL)
1054 if (pp->content_transfer_encoding !=
NULL)
1060 pp->value_offset = 0;
1075 pp->nested_boundary,
1096 memmove (buf, &buf[ioff], pp->buffer_pos - ioff);
1097 pp->buffer_pos -= ioff;
1105 memmove (buf, &buf[ioff], pp->buffer_pos - ioff);
1106 pp->buffer_pos -= ioff;
1108 if (poff < post_data_len)
1132 const char *post_data,
size_t post_data_len)
1134 if (0 == post_data_len)
1177 if ((pp->xbuf_pos > 0) ||
1185 if (pp->nested_boundary !=
NULL)
1186 free (pp->nested_boundary);