187 const char *content_type;
195 int64_t file_size = 0;
216 if (!has_body_data) {
237 const char *val = strchr(data,
'=');
239 ptrdiff_t keylen, vallen;
259 memset(path, 0,
sizeof(path));
271 next = strchr(val,
'&');
275 vallen = (ptrdiff_t)strlen(val);
281 conn, data, (
size_t)keylen, val, (
size_t *)&vallen, fdh);
299#if !defined(NO_FILESYSTEMS)
308 fwrite(val, 1, (
size_t)vallen, fstore.
access.
fp);
309 if ((
n != (
size_t)vallen) || (ferror(fstore.
access.
fp))) {
311 "%s: Cannot write file %s",
317 file_size += (int64_t)
n;
331 "%s: Error saving file %s",
341 "%s: Cannot create file %s",
378 "APPLICATION/X-WWW-FORM-URLENCODED",
381 "APPLICATION/WWW-FORM-URLENCODED",
385 int all_data_read = 0;
395 ptrdiff_t keylen, vallen;
397 int end_of_key_value_pair_found = 0;
400 if (buf_fill < (
sizeof(buf) - 1)) {
402 size_t to_read =
sizeof(buf) - 1 - buf_fill;
403 r =
mg_read(conn, buf + buf_fill, to_read);
404 if ((
r < 0) || ((
r == 0) && all_data_read)) {
414 all_data_read = (buf_fill == 0);
423 val = strchr(buf,
'=');
432 memset(path, 0,
sizeof(path));
449#if !defined(NO_FILESYSTEMS)
457 "%s: Cannot create file %s",
467 next = strchr(val,
'&');
470 end_of_key_value_pair_found = 1;
472 vallen = (ptrdiff_t)strlen(val);
473 end_of_key_value_pair_found = all_data_read;
478 if (!end_of_key_value_pair_found && !all_data_read) {
485 ((get_block > 0) ? NULL : buf),
510#if !defined(NO_FILESYSTEMS)
513 fwrite(val, 1, (
size_t)vallen, fstore.
access.
fp);
514 if ((
n != (
size_t)vallen) || (ferror(fstore.
access.
fp))) {
516 "%s: Cannot write file %s",
522 file_size += (int64_t)
n;
526 if (!end_of_key_value_pair_found) {
530 sizeof(buf) - (
size_t)used);
533 if (buf_fill < (
sizeof(buf) - 1)) {
535 size_t to_read =
sizeof(buf) - 1 - buf_fill;
536 r =
mg_read(conn, buf + buf_fill, to_read);
537 if ((
r < 0) || ((
r == 0) && all_data_read)) {
538#if !defined(NO_FILESYSTEMS)
553 all_data_read = (buf_fill == 0);
564 }
while (!end_of_key_value_pair_found);
566#if !defined(NO_FILESYSTEMS)
578 "%s: Error saving file %s",
587 if (all_data_read && (buf_fill == 0)) {
594 memmove(buf, buf + (
size_t)used,
sizeof(buf) - (
size_t)used);
610 const char *content_disp, *hend, *fbeg, *fend, *nbeg, *nend;
613 int all_data_read = 0;
615 memset(&part_header, 0,
sizeof(part_header));
619 while (content_type[bl] ==
' ') {
631 fbeg = content_type + bl + 9;
637 "%s: Cannot allocate memory for boundary [%lu]",
642 memcpy(boundary, fbeg, bl);
647 if (boundary[0] ==
'"') {
648 hbuf = strchr(boundary + 1,
'"');
649 if ((!hbuf) || (*hbuf !=
'"')) {
655 memmove(boundary, boundary + 1, bl);
656 bl = strlen(boundary);
682 for (part_no = 0;; part_no++) {
683 size_t towrite, fnlen,
n;
685 size_t to_read =
sizeof(buf) - 1 - buf_fill;
690 r =
mg_read(conn, buf + buf_fill, to_read);
691 if ((
r < 0) || ((
r == 0) && all_data_read)) {
697 all_data_read = (buf_fill == 0);
718 size_t preamble_length = 0;
722 while (preamble_length < 1024
723 && (preamble_length < buf_fill - bl)
724 && strncmp(buf + preamble_length + 2, boundary, bl)) {
728 if (0 == strncmp(buf + preamble_length + 2, boundary, bl)) {
730 buf + preamble_length,
731 (
unsigned)buf_fill - (
unsigned)preamble_length);
732 buf_fill -= preamble_length;
742 if (buf_fill < (bl + 2) || strncmp(buf,
"--", 2)
743 || strncmp(buf + 2, boundary, bl)) {
750 char *boundary_start = buf + 2;
751 size_t transport_padding = 0;
752 while (boundary_start[bl + transport_padding] ==
' '
753 || boundary_start[bl + transport_padding] ==
'\t') {
756 char *boundary_end = boundary_start + bl + transport_padding;
760 if (strncmp(boundary_end,
"\r\n", 2)) {
764 if (strncmp(boundary_end,
"--", 2)) {
774 hbuf = boundary_end + 2;
776 hend = strstr(hbuf,
"\r\n\r\n");
785 if ((hend + 2) != hbuf) {
798 "Content-Disposition");
807 nbeg = strstr(content_disp,
"name=\"");
808 while ((nbeg != NULL) && (strcspn(nbeg - 1,
":,; \t") != 0)) {
810 nbeg = strstr(nbeg + 1,
"name=\"");
822 nend = strchr(nbeg,
'\"');
830 nbeg = strstr(content_disp,
"name=");
831 while ((nbeg != NULL) && (strcspn(nbeg - 1,
":,; \t") != 0)) {
833 nbeg = strstr(nbeg + 1,
"name=");
849 nend = nbeg + strcspn(nbeg,
",; \t");
854 fbeg = strstr(content_disp,
"filename=\"");
855 while ((fbeg != NULL) && (strcspn(fbeg - 1,
":,; \t") != 0)) {
857 fbeg = strstr(fbeg + 1,
"filename=\"");
867 fend = strchr(fbeg,
'\"');
881 fbeg = strstr(content_disp,
"filename=");
882 while ((fbeg != NULL) && (strcspn(fbeg - 1,
":,; \t") != 0)) {
884 fbeg = strstr(fbeg + 1,
"filename=");
888 fend = fbeg + strcspn(fbeg,
",; \t");
892 if (!fbeg || !fend) {
897 fnlen = (size_t)(fend - fbeg);
903 if (!(((ptrdiff_t)fbeg > (ptrdiff_t)nend)
904 || ((ptrdiff_t)nbeg > (ptrdiff_t)fend))) {
910 memset(path, 0,
sizeof(path));
914 (
size_t)(nend - nbeg),
915 ((fnlen > 0) ? fbeg : NULL),
924 (
size_t)((buf - hbuf) + buf_fill),
928#if !defined(NO_FILESYSTEMS)
938 "%s: Cannot create file %s",
949 towrite = (size_t)(buf - hend + buf_fill);
951 if (towrite < bl + 4) {
965 ((get_block > 0) ? NULL : nbeg),
968 : (
size_t)(nend - nbeg)),
983#if !defined(NO_FILESYSTEMS)
988 n = (size_t)fwrite(hend, 1, towrite, fstore.
access.
fp);
989 if ((
n != towrite) || (ferror(fstore.
access.
fp))) {
991 "%s: Cannot write file %s",
997 file_size += (int64_t)
n;
1002 memmove(buf, hend + towrite, bl + 4);
1007 to_read =
sizeof(buf) - 1 - buf_fill;
1008 r =
mg_read(conn, buf + buf_fill, to_read);
1009 if ((
r < 0) || ((
r == 0) && all_data_read)) {
1010#if !defined(NO_FILESYSTEMS)
1029 if (!next && (
r == 0)) {
1035 towrite = (next ? (size_t)(next - hend) : 0);
1040 ((get_block > 0) ? NULL : nbeg),
1043 : (
size_t)(nend - nbeg)),
1057#if !defined(NO_FILESYSTEMS)
1061 n = (size_t)fwrite(hend, 1, towrite, fstore.
access.
fp);
1062 if ((
n != towrite) || (ferror(fstore.
access.
fp))) {
1064 "%s: Cannot write file %s",
1070 file_size += (int64_t)
n;
1081 "%s: Error saving file %s",
1100 used = next - buf + 2;
1101 memmove(buf, buf + (
size_t)used,
sizeof(buf) - (
size_t)used);