31 #define XBUF_SIZE 1024
111 struct MHD_PostProcessor
134 const char *encoding;
139 const char *boundary;
144 char *nested_boundary;
159 char *content_filename;
164 char *content_transfer_encoding;
190 uint64_t value_offset;
248 struct MHD_PostProcessor *
253 struct MHD_PostProcessor *ret;
254 const char *encoding;
255 const char *boundary;
258 if ((buffer_size < 256) || (connection ==
NULL) || (ikvi ==
NULL))
263 if (encoding ==
NULL)
276 boundary = strstr (boundary,
"boundary=");
277 if (
NULL == boundary)
279 boundary += strlen (
"boundary=");
280 blen = strlen (boundary);
281 if ((blen == 0) || (blen * 2 + 2 > buffer_size))
286 ret = malloc (
sizeof (
struct MHD_PostProcessor) + buffer_size + 1);
289 memset (ret, 0,
sizeof (
struct MHD_PostProcessor) + buffer_size + 1);
290 ret->connection = connection;
293 ret->encoding = encoding;
294 ret->buffer_size = buffer_size;
297 ret->boundary = boundary;
307 const char *post_data,
308 size_t post_data_len)
315 int end_of_value_found;
319 buf = (
char *) &pp[1];
321 while (poff < post_data_len)
333 while ((equals + poff < post_data_len) &&
334 (post_data[equals + poff] !=
'='))
336 if (equals + pp->buffer_pos > pp->buffer_size)
341 memcpy (&buf[pp->buffer_pos], &post_data[poff], equals);
342 pp->buffer_pos += equals;
343 if (equals + poff == post_data_len)
345 buf[pp->buffer_pos] =
'\0';
350 pp->value_offset = 0;
354 memcpy (xbuf, pp->xbuf, pp->xbuf_pos);
360 while ((amper + poff < post_data_len) &&
362 (post_data[amper + poff] !=
'&') &&
363 (post_data[amper + poff] !=
'\n') &&
364 (post_data[amper + poff] !=
'\r'))
366 end_of_value_found = ((amper + poff < post_data_len) &&
367 ((post_data[amper + poff] ==
'&') ||
368 (post_data[amper + poff] ==
'\n') ||
369 (post_data[amper + poff] ==
'\r')));
377 memcpy (&xbuf[xoff], &post_data[poff], delta);
385 if ((delta > 0) && (xbuf[delta - 1] ==
'%'))
387 else if ((delta > 1) && (xbuf[delta - 2] ==
'%'))
394 memcpy (pp->xbuf, &xbuf[delta], xoff - delta);
395 pp->xbuf_pos = xoff - delta;
402 if ((xoff == 0) && (poff == post_data_len))
416 pp->value_offset += xoff;
419 if (end_of_value_found)
422 if ((post_data[poff] ==
'\n') || (post_data[poff] ==
'\r'))
434 if ((post_data[poff] ==
'\n') || (post_data[poff] ==
'\r'))
462 if (0 == strncasecmp (prefix, line, strlen (prefix)))
464 *suffix = strdup (&line[strlen (prefix)]);
474 const char *boundary,
479 char *buf = (
char *) &pp[1];
481 if (pp->buffer_pos < 2 + blen)
483 if (pp->buffer_pos == pp->buffer_size)
487 if ((0 != memcmp (
"--", buf, 2)) || (0 != memcmp (&buf[2], boundary, blen)))
493 (*ioffptr) += 2 + blen;
496 pp->state = next_state;
497 pp->dash_state = next_dash_state;
518 if (
NULL != *destination)
522 while (
NULL != (spos = strstr (bpos, key)))
524 if ((spos[klen] !=
'=') || ((spos != buf) && (spos[-1] !=
' ')))
530 if (spos[klen + 1] !=
'"')
532 if (
NULL == (endv = strstr (&spos[klen + 2],
"\"")))
534 vlen = endv - spos - klen - 1;
535 *destination = malloc (vlen);
536 if (
NULL == *destination)
538 (*destination)[vlen - 1] =
'\0';
539 memcpy (*destination, &spos[klen + 2], vlen - 1);
558 size_t *ioffptr,
enum PP_State next_state)
560 char *buf = (
char *) &pp[1];
564 while ((newline < pp->buffer_pos) &&
565 (buf[newline] !=
'\r') && (buf[newline] !=
'\n'))
567 if (newline == pp->buffer_size)
572 if (newline == pp->buffer_pos)
578 pp->state = next_state;
582 if (buf[newline] ==
'\r')
585 if (0 == strncasecmp (
"Content-disposition: ",
586 buf, strlen (
"Content-disposition: ")))
589 "name", &pp->content_name);
591 "filename", &pp->content_filename);
597 buf, &pp->content_transfer_encoding);
599 (*ioffptr) += newline + 1;
620 const char *boundary,
625 char *buf = (
char *) &pp[1];
633 while ((newline + 4 < pp->buffer_pos) &&
634 (0 != memcmp (
"\r\n--", &buf[newline], 4)))
636 if (newline + pp->blen + 4 <= pp->buffer_pos)
639 if (0 != memcmp (&buf[newline + 4], boundary, pp->blen))
650 pp->state = next_state;
651 pp->dash_state = next_dash_state;
652 (*ioffptr) += pp->blen + 4;
661 if ((newline == 0) && (pp->buffer_pos == pp->buffer_size))
672 if (
MHD_NO == pp->ikvi (pp->cls,
675 pp->content_filename,
677 pp->content_transfer_encoding,
678 buf, pp->value_offset, newline))
683 pp->value_offset += newline;
684 (*ioffptr) += newline;
693 free (pp->content_name);
694 pp->content_name =
NULL;
698 free (pp->content_type);
699 pp->content_type =
NULL;
701 if ((pp->content_filename !=
NULL) &&
704 free (pp->content_filename);
705 pp->content_filename =
NULL;
707 if ((pp->content_transfer_encoding !=
NULL) &&
710 free (pp->content_transfer_encoding);
711 pp->content_transfer_encoding =
NULL;
720 const char *post_data,
721 size_t post_data_len)
729 buf = (
char *) &pp[1];
733 while ((poff < post_data_len) ||
734 ((pp->buffer_pos > 0) && (state_changed != 0)))
738 max = pp->buffer_size - pp->buffer_pos;
739 if (max > post_data_len - poff)
740 max = post_data_len - poff;
741 memcpy (&buf[pp->buffer_pos], &post_data[poff], max);
743 pp->buffer_pos += max;
744 if ((max == 0) && (state_changed == 0) && (poff < post_data_len))
776 if ((pp->buffer_pos > 1) && (buf[1] ==
'\n'))
802 pp->state = pp->dash_state;
842 if ((pp->content_type !=
NULL) &&
843 (0 == strncasecmp (pp->content_type,
845 strlen (
"multipart/mixed"))))
847 pp->nested_boundary = strstr (pp->content_type,
"boundary=");
848 if (pp->nested_boundary ==
NULL)
853 pp->nested_boundary =
854 strdup (&pp->nested_boundary[strlen (
"boundary=")]);
855 if (pp->nested_boundary ==
NULL)
863 free (pp->content_type);
864 pp->content_type =
NULL;
865 pp->nlen = strlen (pp->nested_boundary);
871 pp->value_offset = 0;
891 if (pp->nested_boundary !=
NULL)
893 free (pp->nested_boundary);
894 pp->nested_boundary =
NULL;
900 if (pp->nested_boundary ==
NULL)
921 if (pp->content_name !=
NULL)
923 if (pp->content_type !=
NULL)
925 if (pp->content_filename !=
NULL)
927 if (pp->content_transfer_encoding !=
NULL)
933 pp->value_offset = 0;
969 memmove (buf, &buf[ioff], pp->buffer_pos - ioff);
970 pp->buffer_pos -= ioff;
978 memmove (buf, &buf[ioff], pp->buffer_pos - ioff);
979 pp->buffer_pos -= ioff;
981 if (poff < post_data_len)
1005 const char *post_data,
size_t post_data_len)
1007 if (post_data_len == 0)
1033 if ((pp->xbuf_pos > 0) || (pp->state !=
PP_Done))
1039 if (pp->nested_boundary !=
NULL)
1040 free (pp->nested_boundary);