mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-20 11:19:56 +00:00
mg_file_upload_handler: Support multiple files
curl -F file1 -F file2 ... Add a unit test and fix a minor memory leak when returning an error. CL: mg_file_upload_handler: Support multiple files PUBLISHED_FROM=5c4bf2be676346fb782e80f50f79df6a6721ac88
This commit is contained in:
1 parent
9ba6eb716d
commit
339bbee0df
4 files changed
+122
-20
No files matched your search
+15
-8
@@ -2713,8 +2713,7 @@ void mg_file_upload_handler(struct mg_connection *nc, int ev, void *ev_data,
|
||||
case MG_EV_HTTP_PART_BEGIN: {
|
||||
struct mg_http_multipart_part *mp =
|
||||
(struct mg_http_multipart_part *) ev_data;
|
||||
struct file_upload_state *fus =
|
||||
(struct file_upload_state *) MG_CALLOC(1, sizeof(*fus));
|
||||
struct file_upload_state *fus;
|
||||
struct mg_str lfn = local_name_fn(nc, mg_mk_str(mp->file_name));
|
||||
mp->user_data = NULL;
|
||||
if (lfn.p == NULL || lfn.len == 0) {
|
||||
@@ -2728,6 +2727,11 @@ void mg_file_upload_handler(struct mg_connection *nc, int ev, void *ev_data,
|
||||
nc->flags |= MG_F_SEND_AND_CLOSE;
|
||||
return;
|
||||
}
|
||||
fus = (struct file_upload_state *) MG_CALLOC(1, sizeof(*fus));
|
||||
if (fus == NULL) {
|
||||
nc->flags |= MG_F_CLOSE_IMMEDIATELY;
|
||||
return;
|
||||
}
|
||||
fus->lfn = (char *) MG_MALLOC(lfn.len + 1);
|
||||
memcpy(fus->lfn, lfn.p, lfn.len);
|
||||
fus->lfn[lfn.len] = '\0';
|
||||
@@ -2799,12 +2803,6 @@ void mg_file_upload_handler(struct mg_connection *nc, int ev, void *ev_data,
|
||||
if (mp->status >= 0 && fus->fp != NULL) {
|
||||
LOG(LL_DEBUG, ("%p Uploaded %s (%s), %d bytes", nc, mp->file_name,
|
||||
fus->lfn, (int) fus->num_recd));
|
||||
mg_printf(nc,
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Content-Type: text/plain\r\n"
|
||||
"Connection: close\r\n\r\n"
|
||||
"Ok, %s - %d bytes.\r\n",
|
||||
mp->file_name, (int) fus->num_recd);
|
||||
} else {
|
||||
LOG(LL_ERROR, ("Failed to store %s (%s)", mp->file_name, fus->lfn));
|
||||
/*
|
||||
@@ -2816,6 +2814,15 @@ void mg_file_upload_handler(struct mg_connection *nc, int ev, void *ev_data,
|
||||
MG_FREE(fus->lfn);
|
||||
MG_FREE(fus);
|
||||
mp->user_data = NULL;
|
||||
/* Don't close the connection yet, there may be more files to come. */
|
||||
break;
|
||||
}
|
||||
case MG_EV_HTTP_MULTIPART_REQUEST_END: {
|
||||
mg_printf(nc,
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Content-Type: text/plain\r\n"
|
||||
"Connection: close\r\n\r\n"
|
||||
"Ok.\r\n");
|
||||
nc->flags |= MG_F_SEND_AND_CLOSE;
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user