diff options
-rw-r--r-- | include/magi/file.h | 8 | ||||
-rw-r--r-- | src/loadfiles.c | 18 | ||||
-rw-r--r-- | src/multipart.c | 11 |
3 files changed, 21 insertions, 16 deletions
diff --git a/include/magi/file.h b/include/magi/file.h index cd3d675..d976194 100644 --- a/include/magi/file.h +++ b/include/magi/file.h @@ -32,11 +32,13 @@ const magi_file *magi_files_get(const magi_files *files, const char *name); * Callback to load files while analysing request. * addon_len is not null if something to add is in addon * and null if file_to_add_into is ended. + * newfile flag is setted up in the beginning of new file. * Files are passed sequentialy, one by one. */ -typedef void (*magi_file_callback_act)(void *userdata, +typedef void (*magi_file_callback_act)(void *userdata, + int newfile, magi_file *file_to_add_into, - char *addon, - int addon_len); + char *addon, + int addon_len); typedef struct magi_file_callback { magi_file_callback_act act; diff --git a/src/loadfiles.c b/src/loadfiles.c index b3efff7..6d68895 100644 --- a/src/loadfiles.c +++ b/src/loadfiles.c @@ -34,14 +34,15 @@ void magi_loadfiles_free(magi_loadfiles *table) table->count = 0; } -static void loadfiles(void *userdata, - magi_file *file, - char *addon, - int addon_len) +static void loadfiles_callback(void *userdata, + int newfile, + magi_file *file, + char *addon, + int addon_len) { magi_loadfiles *table = userdata; int pos; - if (!file->filename || !strcmp(file->filename, "")) { + if (!file->filename || !*file->filename) { return; } for (pos = 0; pos != table->count; ++pos) { @@ -49,7 +50,7 @@ static void loadfiles(void *userdata, static FILE *f = 0; static int unlimited; static int left; - if (!f) { + if (newfile) { const char *path = table->files[pos].path; f = fopen(path, "wb"); left = table->files[pos].max; @@ -62,9 +63,8 @@ static void loadfiles(void *userdata, fwrite(addon, 1, min, f); left -= min; } - if (!addon_len) { + if (!addon) { fclose(f); - f = 0; } return; } @@ -73,6 +73,6 @@ static void loadfiles(void *userdata, void magi_loadfiles_set(magi_request *request, magi_loadfiles *table) { - request->callback.act = loadfiles; + request->callback.act = loadfiles_callback; request->callback.userdata = table; } diff --git a/src/multipart.c b/src/multipart.c index a265184..8164bfd 100644 --- a/src/multipart.c +++ b/src/multipart.c @@ -48,6 +48,7 @@ typedef struct automata { int is_CR_readed; int is_quoted; int readed; + int newfile; } automata; typedef void *(*state)(automata *a, char c); @@ -120,9 +121,10 @@ static int param_end(automata *a) { if (a->file.filename) { a->request->callback.act(a->request->callback.userdata, - &a->file, a->buf, a->buf_size); + a->newfile, &a->file, a->buf, a->buf_size); a->request->callback.act(a->request->callback.userdata, - &a->file, 0, 0); + 0, &a->file, 0, 0); + a->newfile = 1; a->readed -= a->buf_size; a->buf_size = 0; magi_files_add(&a->request->files, &a->file); @@ -216,7 +218,8 @@ static void apply_callback(automata *a) int full = a->buf_size == a->request->callback.addon_max; if (a->file.filename && full) { a->request->callback.act(a->request->callback.userdata, - &a->file, a->buf, a->buf_size); + a->newfile, &a->file, a->buf, a->buf_size); + a->newfile = 0; a->readed -= a->buf_size; a->buf_size = 0; } @@ -412,7 +415,7 @@ void magi_parse_multipart(magi_request *request, void *next_userdata) { automata a = { - 0, { 0, 0, 0 }, { 0, 0 }, { 0, 0 }, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0 + 0, { 0, 0, 0 }, { 0, 0 }, { 0, 0 }, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 1 }; a.request = request; a.boundary = boundary; |