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 =
mg_malloc(value_len + 1);
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,
134 const char *boundary,
139 int clen = (int)buf_len - (
int)boundary_len - 4;
142 for (i = 0; i <= clen; i++) {
143 if (!memcmp(buf + i,
"\r\n--", 4)) {
144 if (!memcmp(buf + i + 4, boundary, boundary_len)) {
157 const char *content_type;
165 int64_t file_size = 0;
169 (conn->request_info.content_length > 0) || (conn->is_chunked);
182 if (!has_body_data) {
185 if (strcmp(conn->request_info.request_method,
"GET")) {
195 data = conn->request_info.query_string;
203 const char *val = strchr(data,
'=');
205 ptrdiff_t keylen, vallen;
223 memset(path, 0,
sizeof(path));
235 next = strchr(val,
'&');
240 vallen = (ptrdiff_t)strlen(val);
247 conn, data, (
size_t)keylen, val, (
size_t)vallen, fdh);
251 if (
mg_fopen(conn, path,
"wb", &fstore) == 0) {
255 if (fstore.fp !=
NULL) {
257 (size_t)fwrite(val, 1, (
size_t)vallen, fstore.fp);
258 if ((n != (
size_t)vallen) || (ferror(fstore.fp))) {
260 "%s: Cannot write file %s",
267 file_size += (int64_t)n;
270 r = fclose(fstore.fp);
276 "%s: Error saving file %s",
285 mg_cry(conn,
"%s: Cannot create file %s", __func__, path);
303 == FORM_FIELD_STORAGE_ABORT) {
318 || !
mg_strcasecmp(content_type,
"APPLICATION/X-WWW-FORM-URLENCODED")
319 || !
mg_strcasecmp(content_type,
"APPLICATION/WWW-FORM-URLENCODED")) {
322 int all_data_read = 0;
332 ptrdiff_t keylen, vallen;
334 int end_of_key_value_pair_found = 0;
337 if ((
size_t)buf_fill < (
sizeof(buf) - 1)) {
339 size_t to_read =
sizeof(buf) - 1 - (
size_t)buf_fill;
340 r =
mg_read(conn, buf + (
size_t)buf_fill, to_read);
345 if (r != (
int)to_read) {
360 val = strchr(buf,
'=');
369 memset(path, 0,
sizeof(path));
381 == FORM_FIELD_STORAGE_ABORT) {
387 if (
mg_fopen(conn, path,
"wb", &fstore) == 0) {
392 mg_cry(conn,
"%s: Cannot create file %s", __func__, path);
399 next = strchr(val,
'&');
403 end_of_key_value_pair_found = 1;
405 vallen = (ptrdiff_t)strlen(val);
411 if (!end_of_key_value_pair_found && !all_data_read) {
420 ((get_block > 0) ?
NULL : buf),
430 (size_t)fwrite(val, 1, (
size_t)vallen, fstore.fp);
431 if ((n != (
size_t)vallen) || (ferror(fstore.fp))) {
433 "%s: Cannot write file %s",
440 file_size += (int64_t)n;
443 if (!end_of_key_value_pair_found) {
447 sizeof(buf) - (
size_t)used);
448 buf_fill -= (int)used;
449 if ((
size_t)buf_fill < (
sizeof(buf) - 1)) {
451 size_t to_read =
sizeof(buf) - 1 - (
size_t)buf_fill;
452 r =
mg_read(conn, buf + (
size_t)buf_fill, to_read);
457 if (r != (
int)to_read) {
474 }
while (!end_of_key_value_pair_found);
477 r = fclose(fstore.fp);
482 mg_cry(conn,
"%s: Error saving file %s", __func__, path);
490 memmove(buf, buf + (
size_t)used,
sizeof(buf) - (
size_t)used);
491 buf_fill -= (int)used;
501 const char *boundary;
505 char *hbuf, *hend, *fbeg, *fend, *nbeg, *nend;
506 const char *content_disp;
509 memset(&part_header, 0,
sizeof(part_header));
513 while (content_type[bl] ==
' ') {
523 boundary = content_type + bl + 9;
524 bl = strlen(boundary);
526 if (bl + 800 >
sizeof(buf)) {
543 buf + (
size_t)buf_fill,
544 sizeof(buf) - 1 - (
size_t)buf_fill);
556 if (buf[0] !=
'-' || buf[1] !=
'-') {
560 if (strncmp(buf + 2, boundary, bl)) {
564 if (buf[bl + 2] !=
'\r' || buf[bl + 3] !=
'\n') {
567 if (((
size_t)buf_fill != (
size_t)(bl + 6))
568 || (strncmp(buf + bl + 2,
"--\r\n", 4))) {
578 hend = strstr(hbuf,
"\r\n\r\n");
585 if ((hend + 2) != hbuf) {
595 content_disp =
get_header(&part_header,
"Content-Disposition");
603 nbeg = strstr(content_disp,
"name=\"");
609 nend = strchr(nbeg,
'\"');
617 fbeg = strstr(content_disp,
"filename=\"");
620 fend = strchr(fbeg,
'\"');
634 memset(path, 0,
sizeof(path));
638 (
size_t)(nend - nbeg),
640 (
size_t)(fend - fbeg),
648 (
size_t)((buf - hbuf) + buf_fill),
654 if (
mg_fopen(conn, path,
"wb", &fstore) == 0) {
660 mg_cry(conn,
"%s: Cannot create file %s", __func__, path);
668 towrite = (size_t)(buf - hend + buf_fill);
676 ((get_block > 0) ?
NULL : nbeg),
679 : (
size_t)(nend - nbeg)),
690 n = (size_t)fwrite(hend, 1, towrite, fstore.fp);
691 if ((n != towrite) || (ferror(fstore.fp))) {
693 "%s: Cannot write file %s",
700 file_size += (int64_t)n;
704 memmove(buf, hend + towrite, bl + 4);
705 buf_fill = (int)(bl + 4);
710 buf + (
size_t)buf_fill,
711 sizeof(buf) - 1 - (
size_t)buf_fill);
727 towrite = (size_t)(next - hend);
732 ((get_block > 0) ?
NULL : nbeg),
734 : (
size_t)(nend - nbeg)),
743 n = (size_t)fwrite(hend, 1, towrite, fstore.fp);
744 if ((n != towrite) || (ferror(fstore.fp))) {
746 "%s: Cannot write file %s",
753 file_size += (int64_t)n;
760 r = fclose(fstore.fp);
766 "%s: Error saving file %s",
776 == FORM_FIELD_STORAGE_ABORT) {
782 used = next - buf + 2;
783 memmove(buf, buf + (
size_t)used,
sizeof(buf) - (
size_t)used);
784 buf_fill -= (int)used;
int mg_url_decode(const char *src, int src_len, char *dst, int dst_len, int is_form_url_encoded)
int mg_read(struct mg_connection *conn, void *buf, size_t len)
void mg_cry(const struct mg_connection *conn, const char *fmt,...)
static const char * get_header(const struct mg_request_info *ri, const char *name)
static void remove_bad_file(const struct mg_connection *conn, const char *path)
static void parse_http_headers(char **buf, struct mg_request_info *ri)
static int mg_fopen(const struct mg_connection *conn, const char *path, const char *mode, struct file *filep)
int mg_strcasecmp(const char *s1, const char *s2)
const char * mg_get_header(const struct mg_connection *conn, const char *name)
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)