1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-12-22 04:02:04 +03:00

Alternative to mg_upload (Step 34/?)

This commit is contained in:
bel
2016-01-25 00:04:20 +01:00
parent dd461c7724
commit 02b65a29b4

View File

@@ -416,7 +416,7 @@ mg_handle_form_data(struct mg_connection *conn,
boundary = content_type + 30; boundary = content_type + 30;
bl = strlen(boundary); bl = strlen(boundary);
do { for (;;) {
r = mg_read(conn, r = mg_read(conn,
buf + (size_t)buf_fill, buf + (size_t)buf_fill,
@@ -441,8 +441,14 @@ mg_handle_form_data(struct mg_connection *conn,
return 0; return 0;
} }
if (buf[bl + 2] != '\r' || buf[bl + 3] != '\n') { if (buf[bl + 2] != '\r' || buf[bl + 3] != '\n') {
/* Malformed request */ /* Every part must end with \r\n, if there is another part.
return 0; * The end of the request has an extra -- */
if (strncmp(buf + 2, "--\r\n", 4)) {
/* Malformed request */
return 0;
}
/* End of the request */
break;
} }
/* Next, we need to get the part header: Read until \r\n\r\n */ /* Next, we need to get the part header: Read until \r\n\r\n */
@@ -534,11 +540,29 @@ mg_handle_form_data(struct mg_connection *conn,
if (disposition == FORM_DISPOSITION_STORE) { if (disposition == FORM_DISPOSITION_STORE) {
/* Store the content to a file */ /* Store the content to a file */
FILE *f = fopen(path, "wb"); FILE *fstore = fopen(path, "wb");
if (f != NULL) { if (fstore != NULL) {
/* TODO: store from part_header to next boundary */ /* TODO: store all data until the next boundary */
fclose(f); /* while (!next) {
} */
if (fstore) {
size_t towrite = (size_t)(next - hend - 4);
size_t n = (size_t)fwrite(hend + 4, 1, towrite, fstore);
if ((n != towrite) || (ferror(fstore))) {
mg_cry(conn,
"%s: Cannot write file %s",
__func__,
path);
fclose(fstore);
fstore = NULL;
}
}
if (fstore) {
fclose(fstore);
}
} else { } else {
mg_cry(conn, "%s: Cannot create file %s", __func__, path); mg_cry(conn, "%s: Cannot create file %s", __func__, path);
} }
@@ -553,10 +577,9 @@ mg_handle_form_data(struct mg_connection *conn,
used = next - buf + 2; used = next - buf + 2;
memmove(buf, buf + (size_t)used, sizeof(buf) - (size_t)used); memmove(buf, buf + (size_t)used, sizeof(buf) - (size_t)used);
buf_fill -= used; buf_fill -= used;
}
} while (1 /* TODO */); /* All parts handled */
/* TODO: handle multipart request */
return 0; return 0;
} }