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)) {
56 mg_cry(conn,
"%s: Cannot decode filename", __func__);
68 mg_cry(conn,
"%s: Function \"Get\" not available", __func__);
74 mg_cry(conn,
"%s: Function \"Store\" not available", __func__);
93 char *value_dec = (
char *)
mg_malloc_ctx(value_len + 1, conn->ctx);
94 int value_dec_len, ret;
99 "%s: Not enough memory (required: %lu)",
101 (
unsigned long)(value_len + 1));
105 mg_url_decode(key, (
int)key_len, key_dec, (
int)
sizeof(key_dec), 1);
108 mg_url_decode(value, (
int)value_len, value_dec, (
int)value_len + 1, 1);
112 (
size_t)value_dec_len,
132 mg_url_decode(key, (
int)key_len, key_dec, (
int)
sizeof(key_dec), 1);
155 const char *boundary,
160 int clen = (int)buf_len - (
int)boundary_len - 4;
163 for (i = 0; i <= clen; i++) {
164 if (!memcmp(buf + i,
"\r\n--", 4)) {
165 if (!memcmp(buf + i + 4, boundary, boundary_len)) {
178 const char *content_type;
186 int64_t file_size = 0;
190 (conn->request_info.content_length > 0) || (conn->is_chunked);
203 if (!has_body_data) {
206 if (strcmp(conn->request_info.request_method,
"GET")) {
216 data = conn->request_info.query_string;
224 const char *val = strchr(data,
'=');
226 ptrdiff_t keylen, vallen;
244 memset(path, 0,
sizeof(path));
256 next = strchr(val,
'&');
261 vallen = (ptrdiff_t)strlen(val);
268 conn, data, (
size_t)keylen, val, (
size_t)vallen, fdh);
273 fstore.access.fp = NULL;
276 if (fstore.access.fp != NULL) {
278 fwrite(val, 1, (
size_t)vallen, fstore.access.fp);
279 if ((n != (
size_t)vallen) || (ferror(fstore.access.fp))) {
281 "%s: Cannot write file %s",
287 file_size += (int64_t)n;
289 if (fstore.access.fp) {
296 "%s: Error saving file %s",
301 fstore.access.fp = NULL;
305 mg_cry(conn,
"%s: Cannot create file %s", __func__, path);
323 == FORM_FIELD_STORAGE_ABORT) {
338 || !
mg_strcasecmp(content_type,
"APPLICATION/X-WWW-FORM-URLENCODED")
339 || !
mg_strcasecmp(content_type,
"APPLICATION/WWW-FORM-URLENCODED")) {
342 int all_data_read = 0;
352 ptrdiff_t keylen, vallen;
354 int end_of_key_value_pair_found = 0;
357 if ((
size_t)buf_fill < (
sizeof(buf) - 1)) {
359 size_t to_read =
sizeof(buf) - 1 - (
size_t)buf_fill;
360 r =
mg_read(conn, buf + (
size_t)buf_fill, to_read);
365 if (r != (
int)to_read) {
380 val = strchr(buf,
'=');
389 memset(path, 0,
sizeof(path));
401 == FORM_FIELD_STORAGE_ABORT) {
408 fstore.access.fp = NULL;
411 if (!fstore.access.fp) {
412 mg_cry(conn,
"%s: Cannot create file %s", __func__, path);
419 next = strchr(val,
'&');
423 end_of_key_value_pair_found = 1;
425 vallen = (ptrdiff_t)strlen(val);
431 if (!end_of_key_value_pair_found && !all_data_read) {
440 ((get_block > 0) ? NULL : buf),
448 if (fstore.access.fp) {
450 fwrite(val, 1, (
size_t)vallen, fstore.access.fp);
451 if ((n != (
size_t)vallen) || (ferror(fstore.access.fp))) {
453 "%s: Cannot write file %s",
459 file_size += (int64_t)n;
462 if (!end_of_key_value_pair_found) {
466 sizeof(buf) - (
size_t)used);
467 buf_fill -= (int)used;
468 if ((
size_t)buf_fill < (
sizeof(buf) - 1)) {
470 size_t to_read =
sizeof(buf) - 1 - (
size_t)buf_fill;
471 r =
mg_read(conn, buf + (
size_t)buf_fill, to_read);
476 if (r != (
int)to_read) {
493 }
while (!end_of_key_value_pair_found);
495 if (fstore.access.fp) {
501 mg_cry(conn,
"%s: Error saving file %s", __func__, path);
504 fstore.access.fp = NULL;
509 memmove(buf, buf + (
size_t)used,
sizeof(buf) - (
size_t)used);
510 buf_fill -= (int)used;
525 const char *content_disp, *hend, *fbeg, *fend, *nbeg, *nend;
529 memset(&part_header, 0,
sizeof(part_header));
533 while (content_type[bl] ==
' ') {
544 fbeg = content_type + bl + 9;
550 "%s: Cannot allocate memory for boundary [%lu]",
555 memcpy(boundary, fbeg, bl);
560 if (boundary[0] ==
'"') {
561 hbuf = strchr(boundary + 1,
'"');
562 if ((!hbuf) || (*hbuf !=
'"')) {
568 memmove(boundary, boundary + 1, bl);
569 bl = strlen(boundary);
605 for (part_no = 0;; part_no++) {
610 buf + (
size_t)buf_fill,
611 sizeof(buf) - 1 - (
size_t)buf_fill);
627 while ((d < buf_fill) && (buf[d] !=
'-')) {
630 if ((d > 0) && (buf[d] ==
'-')) {
631 memmove(buf, buf + d, (
unsigned)buf_fill - (
unsigned)d);
637 if (buf[0] !=
'-' || buf[1] !=
'-') {
642 if (strncmp(buf + 2, boundary, bl)) {
647 if (buf[bl + 2] !=
'\r' || buf[bl + 3] !=
'\n') {
650 if (((
size_t)buf_fill != (
size_t)(bl + 6))
651 || (strncmp(buf + bl + 2,
"--\r\n", 4))) {
662 hend = strstr(hbuf,
"\r\n\r\n");
671 if ((hend + 2) != hbuf) {
684 "Content-Disposition");
693 nbeg = strstr(content_disp,
"name=\"");
694 while ((nbeg != NULL) && (strcspn(nbeg - 1,
":,; \t") != 0)) {
696 nbeg = strstr(nbeg + 1,
"name=\"");
708 nend = strchr(nbeg,
'\"');
716 nbeg = strstr(content_disp,
"name=");
717 while ((nbeg != NULL) && (strcspn(nbeg - 1,
":,; \t") != 0)) {
719 nbeg = strstr(nbeg + 1,
"name=");
735 nend = nbeg + strcspn(nbeg,
",; \t");
740 fbeg = strstr(content_disp,
"filename=\"");
741 while ((fbeg != NULL) && (strcspn(fbeg - 1,
":,; \t") != 0)) {
743 fbeg = strstr(fbeg + 1,
"filename=\"");
753 fend = strchr(fbeg,
'\"');
767 fbeg = strstr(content_disp,
"filename=");
768 while ((fbeg != NULL) && (strcspn(fbeg - 1,
":,; \t") != 0)) {
770 fbeg = strstr(fbeg + 1,
"filename=");
774 fend = fbeg + strcspn(fbeg,
",; \t");
784 if (!(((ptrdiff_t)fbeg > (ptrdiff_t)nend)
785 || ((ptrdiff_t)nbeg > (ptrdiff_t)fend))) {
791 memset(path, 0,
sizeof(path));
795 (
size_t)(nend - nbeg),
797 (
size_t)(fend - fbeg),
805 (
size_t)((buf - hbuf) + buf_fill),
812 fstore.access.fp = NULL;
816 if (!fstore.access.fp) {
817 mg_cry(conn,
"%s: Cannot create file %s", __func__, path);
825 towrite = (size_t)(buf - hend + buf_fill);
833 ((get_block > 0) ? NULL : nbeg),
836 : (
size_t)(nend - nbeg)),
844 if (fstore.access.fp) {
847 n = (size_t)fwrite(hend, 1, towrite, fstore.access.fp);
848 if ((n != towrite) || (ferror(fstore.access.fp))) {
850 "%s: Cannot write file %s",
856 file_size += (int64_t)n;
860 memmove(buf, hend + towrite, bl + 4);
861 buf_fill = (int)(bl + 4);
866 buf + (
size_t)buf_fill,
867 sizeof(buf) - 1 - (
size_t)buf_fill);
885 towrite = (size_t)(next - hend);
890 ((get_block > 0) ? NULL : nbeg),
892 : (
size_t)(nend - nbeg)),
900 if (fstore.access.fp) {
901 n = (size_t)fwrite(hend, 1, towrite, fstore.access.fp);
902 if ((n != towrite) || (ferror(fstore.access.fp))) {
904 "%s: Cannot write file %s",
910 file_size += (int64_t)n;
917 "%s: Error saving file %s",
923 fstore.access.fp = NULL;
928 == FORM_FIELD_STORAGE_ABORT) {
934 used = next - buf + 2;
935 memmove(buf, buf + (
size_t)used,
sizeof(buf) - (
size_t)used);
936 buf_fill -= (int)used;
int mg_url_decode(const char *src, int src_len, char *dst, int dst_len, int is_form_url_encoded)
static int parse_http_headers(char **buf, struct mg_header hdr[MG_MAX_HEADERS])
static int mg_fopen(const struct mg_connection *conn, const char *path, int mode, struct mg_file *filep)
static const char * get_header(const struct mg_header *hdr, int num_hdr, const char *name)
int mg_read(struct mg_connection *conn, void *buf, size_t len)
void mg_cry(const struct mg_connection *conn, const char *fmt,...)
static void remove_bad_file(const struct mg_connection *conn, const char *path)
#define mg_malloc_ctx(a, c)
static __inline void mg_free(void *a)
struct mg_header http_headers[MG_MAX_HEADERS]
int mg_strcasecmp(const char *s1, const char *s2)
const char * mg_get_header(const struct mg_connection *conn, const char *name)
static int mg_fclose(struct mg_file_access *fileacc)
#define MG_FOPEN_MODE_WRITE
typedef void((*Func_t)())
#define STRUCT_FILE_INITIALIZER
static __inline void * mg_malloc(size_t a)
int mg_strncasecmp(const char *s1, const char *s2, size_t len)