34 char filename_dec[1024];
40 mg_url_decode(key, (
int)key_len, key_dec, (
int)
sizeof(key_dec), 1);
42 if (((
size_t)key_dec_len >= (
size_t)
sizeof(key_dec)) || (key_dec_len < 0)) {
50 (
int)
sizeof(filename_dec),
53 if (((
size_t)filename_dec_len >= (
size_t)
sizeof(filename_dec))
54 || (filename_dec_len < 0)) {
69 "%s: Function \"Get\" not available",
77 "%s: Function \"Store\" not available",
97 char *value_dec = (
char *)
mg_malloc_ctx(value_len + 1, conn->phys_ctx);
98 int value_dec_len, ret;
103 "%s: Not enough memory (required: %lu)",
105 (
unsigned long)(value_len + 1));
109 mg_url_decode(key, (
int)key_len, key_dec, (
int)
sizeof(key_dec), 1);
112 mg_url_decode(value, (
int)value_len, value_dec, (
int)value_len + 1, 1);
116 (
size_t)value_dec_len,
136 mg_url_decode(key, (
int)key_len, key_dec, (
int)
sizeof(key_dec), 1);
159 const char *boundary,
164 int clen = (int)buf_len - (
int)boundary_len - 4;
167 for (i = 0; i <= clen; i++) {
168 if (!memcmp(buf + i,
"\r\n--", 4)) {
169 if (!memcmp(buf + i + 4, boundary, boundary_len)) {
182 const char *content_type;
190 int64_t file_size = 0;
194 (conn->request_info.content_length > 0) || (conn->is_chunked);
207 if (!has_body_data) {
210 if (0 != strcmp(conn->request_info.request_method,
"GET")) {
220 data = conn->request_info.query_string;
228 const char *val = strchr(data,
'=');
230 ptrdiff_t keylen, vallen;
250 memset(path, 0,
sizeof(path));
262 next = strchr(val,
'&');
267 vallen = (ptrdiff_t)strlen(val);
274 conn, data, (
size_t)keylen, val, (
size_t)vallen, fdh);
287 fstore.access.fp = NULL;
290 if (fstore.access.fp != NULL) {
292 fwrite(val, 1, (
size_t)vallen, fstore.access.fp);
293 if ((
n != (
size_t)vallen) || (ferror(fstore.access.fp))) {
295 "%s: Cannot write file %s",
301 file_size += (int64_t)
n;
303 if (fstore.access.fp) {
315 "%s: Error saving file %s",
320 fstore.access.fp = NULL;
325 "%s: Cannot create file %s",
361 "APPLICATION/X-WWW-FORM-URLENCODED",
364 "APPLICATION/WWW-FORM-URLENCODED",
368 int all_data_read = 0;
378 ptrdiff_t keylen, vallen;
380 int end_of_key_value_pair_found = 0;
383 if ((
size_t)buf_fill < (
sizeof(buf) - 1)) {
385 size_t to_read =
sizeof(buf) - 1 - (
size_t)buf_fill;
386 r =
mg_read(conn, buf + (
size_t)buf_fill, to_read);
391 if (
r != (
int)to_read) {
406 val = strchr(buf,
'=');
415 memset(path, 0,
sizeof(path));
434 fstore.access.fp = NULL;
437 if (!fstore.access.fp) {
439 "%s: Cannot create file %s",
448 next = strchr(val,
'&');
452 end_of_key_value_pair_found = 1;
454 vallen = (ptrdiff_t)strlen(val);
456 end_of_key_value_pair_found = all_data_read;
461 if (!end_of_key_value_pair_found && !all_data_read) {
468 ((get_block > 0) ? NULL : buf),
485 if (fstore.access.fp) {
487 fwrite(val, 1, (
size_t)vallen, fstore.access.fp);
488 if ((
n != (
size_t)vallen) || (ferror(fstore.access.fp))) {
490 "%s: Cannot write file %s",
496 file_size += (int64_t)
n;
499 if (!end_of_key_value_pair_found) {
503 sizeof(buf) - (
size_t)used);
505 buf_fill -= (int)used;
506 if ((
size_t)buf_fill < (
sizeof(buf) - 1)) {
508 size_t to_read =
sizeof(buf) - 1 - (
size_t)buf_fill;
509 r =
mg_read(conn, buf + (
size_t)buf_fill, to_read);
512 if (fstore.access.fp) {
518 if (
r != (
int)to_read) {
535 }
while (!end_of_key_value_pair_found);
537 if (fstore.access.fp) {
548 "%s: Error saving file %s",
553 fstore.access.fp = NULL;
556 if (all_data_read && (buf_fill == 0)) {
563 memmove(buf, buf + (
size_t)used,
sizeof(buf) - (
size_t)used);
564 buf_fill -= (int)used;
579 const char *content_disp, *hend, *fbeg, *fend, *nbeg, *nend;
583 memset(&part_header, 0,
sizeof(part_header));
587 while (content_type[bl] ==
' ') {
598 fbeg = content_type + bl + 9;
604 "%s: Cannot allocate memory for boundary [%lu]",
609 memcpy(boundary, fbeg, bl);
614 if (boundary[0] ==
'"') {
615 hbuf = strchr(boundary + 1,
'"');
616 if ((!hbuf) || (*hbuf !=
'"')) {
622 memmove(boundary, boundary + 1, bl);
623 bl = strlen(boundary);
649 for (part_no = 0;; part_no++) {
650 size_t towrite, fnlen,
n;
654 buf + (
size_t)buf_fill,
655 sizeof(buf) - 1 - (
size_t)buf_fill);
671 while ((buf[
d] !=
'-') && (
d < buf_fill)) {
674 if ((
d > 0) && (buf[
d] ==
'-')) {
675 memmove(buf, buf +
d, (
unsigned)buf_fill - (
unsigned)
d);
681 if (buf[0] !=
'-' || buf[1] !=
'-') {
686 if (0 != strncmp(buf + 2, boundary, bl)) {
691 if (buf[bl + 2] !=
'\r' || buf[bl + 3] !=
'\n') {
694 if (((
size_t)buf_fill != (
size_t)(bl + 6))
695 || (strncmp(buf + bl + 2,
"--\r\n", 4))) {
706 hend = strstr(hbuf,
"\r\n\r\n");
715 if ((hend + 2) != hbuf) {
728 "Content-Disposition");
737 nbeg = strstr(content_disp,
"name=\"");
738 while ((nbeg != NULL) && (strcspn(nbeg - 1,
":,; \t") != 0)) {
740 nbeg = strstr(nbeg + 1,
"name=\"");
752 nend = strchr(nbeg,
'\"');
760 nbeg = strstr(content_disp,
"name=");
761 while ((nbeg != NULL) && (strcspn(nbeg - 1,
":,; \t") != 0)) {
763 nbeg = strstr(nbeg + 1,
"name=");
779 nend = nbeg + strcspn(nbeg,
",; \t");
784 fbeg = strstr(content_disp,
"filename=\"");
785 while ((fbeg != NULL) && (strcspn(fbeg - 1,
":,; \t") != 0)) {
787 fbeg = strstr(fbeg + 1,
"filename=\"");
797 fend = strchr(fbeg,
'\"');
811 fbeg = strstr(content_disp,
"filename=");
812 while ((fbeg != NULL) && (strcspn(fbeg - 1,
":,; \t") != 0)) {
814 fbeg = strstr(fbeg + 1,
"filename=");
818 fend = fbeg + strcspn(fbeg,
",; \t");
822 if (!fbeg || !fend) {
827 fnlen = (size_t)(fend - fbeg);
833 if (!(((ptrdiff_t)fbeg > (ptrdiff_t)nend)
834 || ((ptrdiff_t)nbeg > (ptrdiff_t)fend))) {
840 memset(path, 0,
sizeof(path));
844 (
size_t)(nend - nbeg),
845 ((fnlen > 0) ? fbeg : NULL),
854 (
size_t)((buf - hbuf) + buf_fill),
861 fstore.access.fp = NULL;
865 if (!fstore.access.fp) {
867 "%s: Cannot create file %s",
877 towrite = (size_t)(buf - hend + buf_fill);
885 ((get_block > 0) ? NULL : nbeg),
888 : (
size_t)(nend - nbeg)),
904 if (fstore.access.fp) {
907 n = (size_t)fwrite(hend, 1, towrite, fstore.access.fp);
908 if ((
n != towrite) || (ferror(fstore.access.fp))) {
910 "%s: Cannot write file %s",
916 file_size += (int64_t)
n;
920 memmove(buf, hend + towrite, bl + 4);
921 buf_fill = (int)(bl + 4);
926 buf + (
size_t)buf_fill,
927 sizeof(buf) - 1 - (
size_t)buf_fill);
930 if (fstore.access.fp) {
945 towrite = (size_t)(next - hend);
950 ((get_block > 0) ? NULL : nbeg),
953 : (
size_t)(nend - nbeg)),
969 if (fstore.access.fp) {
970 n = (size_t)fwrite(hend, 1, towrite, fstore.access.fp);
971 if ((
n != towrite) || (ferror(fstore.access.fp))) {
973 "%s: Cannot write file %s",
979 file_size += (int64_t)
n;
990 "%s: Error saving file %s",
996 fstore.access.fp = NULL;
1007 used = next - buf + 2;
1008 memmove(buf, buf + (
size_t)used,
sizeof(buf) - (
size_t)used);
1009 buf_fill -= (int)used;
typedef void((*Func_t)())
int mg_strncasecmp(const char *s1, const char *s2, size_t len)
#define mg_malloc_ctx(a, c)
static void remove_bad_file(const struct mg_connection *conn, const char *path)
static __inline void * mg_malloc(size_t a)
static int mg_fopen(const struct mg_connection *conn, const char *path, int mode, struct mg_file *filep)
static int parse_http_headers(char **buf, struct mg_header hdr[MG_MAX_HEADERS])
static const char * get_header(const struct mg_header *hdr, int num_hdr, const char *name)
const char * mg_get_header(const struct mg_connection *conn, const char *name)
#define mg_cry_internal(conn, fmt,...)
int mg_url_decode(const char *src, int src_len, char *dst, int dst_len, int is_form_url_encoded)
#define MG_FOPEN_MODE_WRITE
int mg_read(struct mg_connection *conn, void *buf, size_t len)
static int mg_fclose(struct mg_file_access *fileacc)
static __inline void mg_free(void *a)
#define STRUCT_FILE_INITIALIZER
@ MG_FORM_FIELD_STORAGE_GET
@ MG_FORM_FIELD_STORAGE_ABORT
@ MG_FORM_FIELD_STORAGE_STORE
@ MG_FORM_FIELD_STORAGE_SKIP
@ MG_FORM_FIELD_HANDLE_NEXT
@ MG_FORM_FIELD_HANDLE_ABORT
struct mg_header http_headers[MG_MAX_HEADERS]