1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-08-07 16:02:55 +03:00

Alternative to mg_upload (Step 49/?)

This commit is contained in:
bel
2016-02-14 21:07:22 +01:00
parent 03047808a0
commit 25193ba1e2
2 changed files with 23 additions and 13 deletions

View File

@@ -178,10 +178,11 @@ struct mg_form_data_handler {
const char *value, const char *value,
size_t valuelen, size_t valuelen,
void *user_data); void *user_data);
int (*field_stored)(const char *path, void *user_data); int (*field_stored)(const char *path, size_t file_size, void *user_data);
void *user_data; void *user_data;
}; };
extern int mg_handle_form_data(struct mg_connection *conn, extern int mg_handle_form_data(struct mg_connection *conn,
struct mg_form_data_handler *fdh); struct mg_form_data_handler *fdh);
@@ -226,11 +227,14 @@ field_get(const char *key, const char *value, size_t valuelen, void *user_data)
int int
field_stored(const char *path, void *user_data) field_stored(const char *path, size_t file_size, void *user_data)
{ {
struct mg_connection *conn = (struct mg_connection *)user_data; struct mg_connection *conn = (struct mg_connection *)user_data;
mg_printf(conn, "stored as %s\r\n\r\n", path); mg_printf(conn,
"stored as %s (%lu bytes)\r\n\r\n",
path,
(unsigned long)file_size);
return 0; return 0;
} }

View File

@@ -87,7 +87,7 @@ struct mg_form_data_handler {
* Return value: * Return value:
* TODO: Needs to be defined. * TODO: Needs to be defined.
*/ */
int (*field_stored)(const char *path, void *user_data); int (*field_stored)(const char *path, size_t file_size, void *user_data);
/* User supplied argument, passed to all callback functions. */ /* User supplied argument, passed to all callback functions. */
void *user_data; void *user_data;
@@ -205,13 +205,14 @@ url_encoded_field_get(const struct mg_connection *conn,
static int static int
field_stored(const struct mg_connection *conn, field_stored(const struct mg_connection *conn,
const char *path, const char *path,
size_t file_size,
struct mg_form_data_handler *fdh) struct mg_form_data_handler *fdh)
{ {
/* Equivalent to "upload" callback of "mg_upload". */ /* Equivalent to "upload" callback of "mg_upload". */
(void)conn; /* we do not need mg_cry here, so conn is currently unused */ (void)conn; /* we do not need mg_cry here, so conn is currently unused */
return fdh->field_stored(path, fdh->user_data); return fdh->field_stored(path, file_size, fdh->user_data);
} }
@@ -258,6 +259,7 @@ mg_handle_form_data(struct mg_connection *conn,
int buf_fill = 0; int buf_fill = 0;
int r; int r;
FILE *fstore = NULL; FILE *fstore = NULL;
size_t file_size;
int field_count = 0; int field_count = 0;
int has_body_data = int has_body_data =
@@ -344,6 +346,7 @@ mg_handle_form_data(struct mg_connection *conn,
if (field_storage == FORM_FIELD_STORAGE_STORE) { if (field_storage == FORM_FIELD_STORAGE_STORE) {
/* Store the content to a file */ /* Store the content to a file */
fstore = fopen(path, "wb"); fstore = fopen(path, "wb");
file_size = 0;
if (fstore != NULL) { if (fstore != NULL) {
size_t n = (size_t)fwrite(val, 1, (size_t)vallen, fstore); size_t n = (size_t)fwrite(val, 1, (size_t)vallen, fstore);
if ((n != (size_t)vallen) || (ferror(fstore))) { if ((n != (size_t)vallen) || (ferror(fstore))) {
@@ -355,12 +358,13 @@ mg_handle_form_data(struct mg_connection *conn,
fstore = NULL; fstore = NULL;
remove_bad_file(conn, path); remove_bad_file(conn, path);
} }
file_size += (size_t)n;
if (fstore) { if (fstore) {
r = fclose(fstore); r = fclose(fstore);
if (r == 0) { if (r == 0) {
/* stored successfully */ /* stored successfully */
field_stored(conn, path, fdh); field_stored(conn, path, file_size, fdh);
} else { } else {
mg_cry(conn, mg_cry(conn,
"%s: Error saving file %s", "%s: Error saving file %s",
@@ -472,6 +476,7 @@ mg_handle_form_data(struct mg_connection *conn,
if (field_storage == FORM_FIELD_STORAGE_STORE) { if (field_storage == FORM_FIELD_STORAGE_STORE) {
fstore = fopen(path, "wb"); fstore = fopen(path, "wb");
file_size = 0;
if (!fstore) { if (!fstore) {
mg_cry(conn, "%s: Cannot create file %s", __func__, path); mg_cry(conn, "%s: Cannot create file %s", __func__, path);
} }
@@ -500,6 +505,7 @@ mg_handle_form_data(struct mg_connection *conn,
fstore = NULL; fstore = NULL;
remove_bad_file(conn, path); remove_bad_file(conn, path);
} }
file_size += (size_t)n;
} }
if (field_storage == FORM_FIELD_STORAGE_GET) { if (field_storage == FORM_FIELD_STORAGE_GET) {
if (!end_of_key_value_pair_found && !all_data_read) { if (!end_of_key_value_pair_found && !all_data_read) {
@@ -525,7 +531,7 @@ mg_handle_form_data(struct mg_connection *conn,
r = fclose(fstore); r = fclose(fstore);
if (r == 0) { if (r == 0) {
/* stored successfully */ /* stored successfully */
field_stored(conn, path, fdh); field_stored(conn, path, file_size, fdh);
} else { } else {
mg_cry(conn, "%s: Error saving file %s", __func__, path); mg_cry(conn, "%s: Error saving file %s", __func__, path);
remove_bad_file(conn, path); remove_bad_file(conn, path);
@@ -536,7 +542,7 @@ mg_handle_form_data(struct mg_connection *conn,
/* Proceed to next entry */ /* Proceed to next entry */
used = next - buf; used = next - buf;
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 -= (int)used;
} }
return field_count; return field_count;
@@ -708,8 +714,8 @@ mg_handle_form_data(struct mg_connection *conn,
if (field_storage == FORM_FIELD_STORAGE_STORE) { if (field_storage == FORM_FIELD_STORAGE_STORE) {
/* Store the content to a file */ /* Store the content to a file */
size_t towrite, n; size_t towrite, n;
size_t flen = 0;
fstore = fopen(path, "wb"); fstore = fopen(path, "wb");
file_size = 0;
if (!fstore) { if (!fstore) {
mg_cry(conn, "%s: Cannot create file %s", __func__, path); mg_cry(conn, "%s: Cannot create file %s", __func__, path);
@@ -737,7 +743,7 @@ mg_handle_form_data(struct mg_connection *conn,
fstore = NULL; fstore = NULL;
remove_bad_file(conn, path); remove_bad_file(conn, path);
} }
flen += n; file_size += (size_t)n;
} }
memmove(buf, hend + towrite, bl + 4); memmove(buf, hend + towrite, bl + 4);
@@ -775,14 +781,14 @@ mg_handle_form_data(struct mg_connection *conn,
fstore = NULL; fstore = NULL;
remove_bad_file(conn, path); remove_bad_file(conn, path);
} }
flen += n; file_size += (size_t)n;
} }
if (fstore) { if (fstore) {
r = fclose(fstore); r = fclose(fstore);
if (r == 0) { if (r == 0) {
/* stored successfully */ /* stored successfully */
field_stored(conn, path, fdh); field_stored(conn, path, file_size, fdh);
} else { } else {
mg_cry(conn, mg_cry(conn,
"%s: Error saving file %s", "%s: Error saving file %s",
@@ -803,7 +809,7 @@ mg_handle_form_data(struct mg_connection *conn,
/* Remove from the buffer */ /* Remove from the buffer */
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 -= (int)used;
} }
/* All parts handled */ /* All parts handled */