184 const char *content_type;
192 int64_t file_size = 0;
213 if (!has_body_data) {
234 const char *val = strchr(
data,
'=');
236 ptrdiff_t keylen, vallen;
256 memset(path, 0,
sizeof(path));
268 next = strchr(val,
'&');
272 vallen = (ptrdiff_t)strlen(val);
278 conn,
data, (
size_t)keylen, val, (
size_t *)&vallen, fdh);
296#if !defined(NO_FILESYSTEMS)
305 fwrite(val, 1, (
size_t)vallen, fstore.
access.
fp);
306 if ((
n != (
size_t)vallen) || (ferror(fstore.
access.
fp))) {
308 "%s: Cannot write file %s",
314 file_size += (int64_t)
n;
328 "%s: Error saving file %s",
338 "%s: Cannot create file %s",
375 "APPLICATION/X-WWW-FORM-URLENCODED",
378 "APPLICATION/WWW-FORM-URLENCODED",
382 int all_data_read = 0;
392 ptrdiff_t keylen, vallen;
394 int end_of_key_value_pair_found = 0;
397 if ((
size_t)buf_fill < (
sizeof(buf) - 1)) {
399 size_t to_read =
sizeof(buf) - 1 - (
size_t)buf_fill;
400 r =
mg_read(conn, buf + (
size_t)buf_fill, to_read);
401 if ((
r < 0) || ((
r == 0) && all_data_read)) {
411 all_data_read = (buf_fill == 0);
420 val = strchr(buf,
'=');
429 memset(path, 0,
sizeof(path));
446#if !defined(NO_FILESYSTEMS)
454 "%s: Cannot create file %s",
464 next = strchr(val,
'&');
467 end_of_key_value_pair_found = 1;
469 vallen = (ptrdiff_t)strlen(val);
470 end_of_key_value_pair_found = all_data_read;
475 if (!end_of_key_value_pair_found && !all_data_read) {
482 ((get_block > 0) ? NULL : buf),
507#if !defined(NO_FILESYSTEMS)
510 fwrite(val, 1, (
size_t)vallen, fstore.
access.
fp);
511 if ((
n != (
size_t)vallen) || (ferror(fstore.
access.
fp))) {
513 "%s: Cannot write file %s",
519 file_size += (int64_t)
n;
523 if (!end_of_key_value_pair_found) {
527 sizeof(buf) - (
size_t)used);
529 buf_fill -= (
int)used;
530 if ((
size_t)buf_fill < (
sizeof(buf) - 1)) {
532 size_t to_read =
sizeof(buf) - 1 - (
size_t)buf_fill;
533 r =
mg_read(conn, buf + (
size_t)buf_fill, to_read);
534 if ((
r < 0) || ((
r == 0) && all_data_read)) {
535#if !defined(NO_FILESYSTEMS)
550 all_data_read = (buf_fill == 0);
561 }
while (!end_of_key_value_pair_found);
563#if !defined(NO_FILESYSTEMS)
575 "%s: Error saving file %s",
584 if (all_data_read && (buf_fill == 0)) {
591 memmove(buf, buf + (
size_t)used,
sizeof(buf) - (
size_t)used);
592 buf_fill -= (
int)used;
607 const char *content_disp, *hend, *fbeg, *fend, *nbeg, *nend;
610 int all_data_read = 0;
612 memset(&part_header, 0,
sizeof(part_header));
616 while (content_type[bl] ==
' ') {
627 fbeg = content_type + bl + 9;
633 "%s: Cannot allocate memory for boundary [%lu]",
638 memcpy(boundary, fbeg, bl);
643 if (boundary[0] ==
'"') {
644 hbuf = strchr(boundary + 1,
'"');
645 if ((!hbuf) || (*hbuf !=
'"')) {
651 memmove(boundary, boundary + 1, bl);
652 bl = strlen(boundary);
678 for (part_no = 0;; part_no++) {
679 size_t towrite, fnlen,
n;
681 size_t to_read =
sizeof(buf) - 1 - (
size_t)buf_fill;
686 r =
mg_read(conn, buf + (
size_t)buf_fill, to_read);
687 if ((
r < 0) || ((
r == 0) && all_data_read)) {
693 all_data_read = (buf_fill == 0);
706 while ((
d < buf_fill) && (buf[
d] !=
'-')) {
709 if ((
d > 0) && (buf[
d] ==
'-')) {
710 memmove(buf, buf +
d, (
unsigned)buf_fill - (
unsigned)
d);
716 if (buf[0] !=
'-' || buf[1] !=
'-') {
721 if (0 != strncmp(buf + 2, boundary, bl)) {
726 if (buf[bl + 2] !=
'\r' || buf[bl + 3] !=
'\n') {
729 if (((
size_t)buf_fill != (
size_t)(bl + 6))
730 || (strncmp(buf + bl + 2,
"--\r\n", 4))) {
741 hend = strstr(hbuf,
"\r\n\r\n");
750 if ((hend + 2) != hbuf) {
763 "Content-Disposition");
772 nbeg = strstr(content_disp,
"name=\"");
773 while ((nbeg != NULL) && (strcspn(nbeg - 1,
":,; \t") != 0)) {
775 nbeg = strstr(nbeg + 1,
"name=\"");
787 nend = strchr(nbeg,
'\"');
795 nbeg = strstr(content_disp,
"name=");
796 while ((nbeg != NULL) && (strcspn(nbeg - 1,
":,; \t") != 0)) {
798 nbeg = strstr(nbeg + 1,
"name=");
814 nend = nbeg + strcspn(nbeg,
",; \t");
819 fbeg = strstr(content_disp,
"filename=\"");
820 while ((fbeg != NULL) && (strcspn(fbeg - 1,
":,; \t") != 0)) {
822 fbeg = strstr(fbeg + 1,
"filename=\"");
832 fend = strchr(fbeg,
'\"');
846 fbeg = strstr(content_disp,
"filename=");
847 while ((fbeg != NULL) && (strcspn(fbeg - 1,
":,; \t") != 0)) {
849 fbeg = strstr(fbeg + 1,
"filename=");
853 fend = fbeg + strcspn(fbeg,
",; \t");
857 if (!fbeg || !fend) {
862 fnlen = (size_t)(fend - fbeg);
868 if (!(((ptrdiff_t)fbeg > (ptrdiff_t)nend)
869 || ((ptrdiff_t)nbeg > (ptrdiff_t)fend))) {
875 memset(path, 0,
sizeof(path));
879 (
size_t)(nend - nbeg),
880 ((fnlen > 0) ? fbeg : NULL),
889 (
size_t)((buf - hbuf) + buf_fill),
893#if !defined(NO_FILESYSTEMS)
903 "%s: Cannot create file %s",
914 towrite = (size_t)(buf - hend + buf_fill);
916 if (towrite < bl + 4) {
930 ((get_block > 0) ? NULL : nbeg),
933 : (
size_t)(nend - nbeg)),
948#if !defined(NO_FILESYSTEMS)
953 n = (size_t)fwrite(hend, 1, towrite, fstore.
access.
fp);
954 if ((
n != towrite) || (ferror(fstore.
access.
fp))) {
956 "%s: Cannot write file %s",
962 file_size += (int64_t)
n;
967 memmove(buf, hend + towrite, bl + 4);
968 buf_fill = (
int)(bl + 4);
972 to_read =
sizeof(buf) - 1 - (
size_t)buf_fill;
973 r =
mg_read(conn, buf + (
size_t)buf_fill, to_read);
974 if ((
r < 0) || ((
r == 0) && all_data_read)) {
975#if !defined(NO_FILESYSTEMS)
994 if (!next && (
r == 0)) {
1000 towrite = (next ? (size_t)(next - hend) : 0);
1005 ((get_block > 0) ? NULL : nbeg),
1008 : (
size_t)(nend - nbeg)),
1022#if !defined(NO_FILESYSTEMS)
1026 n = (size_t)fwrite(hend, 1, towrite, fstore.
access.
fp);
1027 if ((
n != towrite) || (ferror(fstore.
access.
fp))) {
1029 "%s: Cannot write file %s",
1035 file_size += (int64_t)
n;
1046 "%s: Error saving file %s",
1065 used = next - buf + 2;
1066 memmove(buf, buf + (
size_t)used,
sizeof(buf) - (
size_t)used);
1067 buf_fill -= (
int)used;