From 676f6520bf76867135f1af4fab5d69b3b212d198 Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Fri, 29 Nov 2019 23:33:09 +0300 Subject: [magi] Finally done. (Probably. =) ) --- src/cgi.c | 12 +++++++++--- src/multipart.c | 22 ++++++++++++++++------ src/request.c | 19 ++++++++++++------- 3 files changed, 37 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/cgi.c b/src/cgi.c index d410873..7563613 100644 --- a/src/cgi.c +++ b/src/cgi.c @@ -185,15 +185,21 @@ static int next(void * any) /* Interfacial CGI Request Handling */ int magi_request_cgi(struct magi_request * request) { - enum magi_error * e = &request->error; - request->files = 0; - request->params = 0; request->url_params = 0; request->http_params = 0; request->error = magi_error_none; cgi_env(request); cgi_cookies(request); cgi_url(request); + return !request->error; +} + +int magi_request_resume_cgi(struct magi_request * request) +{ + enum magi_error * e = &request->error; + request->files = 0; + request->params = 0; + request->error = magi_error_none; if (request->method && !strcmp(request->method, "post")) { const char * t = getenv("CONTENT_TYPE"); if (!t) { diff --git a/src/multipart.c b/src/multipart.c index 5d7f261..7b829d8 100644 --- a/src/multipart.c +++ b/src/multipart.c @@ -58,6 +58,7 @@ struct automata { int is_end_suspected; int is_CR_readed; int is_quoted; + int readed; }; @@ -102,6 +103,7 @@ static int content_disposition(struct automata * a) if (a->file.file_name) { a->file.param_name = a->param.name; a->file.params = 0; + a->param.name = 0; } free(a->subparam.name); free(a->subparam.data); @@ -127,13 +129,10 @@ static int subparam_end(struct automata * a) static int param_end(struct automata * a) { - if (!a->param.name) { - a->request->error = magi_error_multipart; - return 0; - } if (a->file.file_name) { a->request->file_callback(&a->file, a->buf, a->buf_size, 1, a->request->file_callback_userdata); + a->readed -= a->buf_size; a->buf_size = 0; if (!magi_file_list_add(&a->request->files, &a->file)) { free(a->file.file_name); @@ -150,6 +149,10 @@ static int param_end(struct automata * a) a->len = 0; return 1; } + if (!a->param.name) { + a->request->error = magi_error_multipart; + return 0; + } if (!magi_param_list_add(&a->request->params, &a->param)) { free(a->param.name); free(a->param.data); @@ -238,6 +241,7 @@ static void apply_callback(struct automata * a) if (a->file.file_name && full) { a->request->file_callback(&a->file, a->buf, a->buf_size, 0, a->request->file_callback_userdata); + a->readed -= a->buf_size; a->buf_size = 0; } } @@ -409,7 +413,8 @@ static void run_automata(struct automata * a, { enum st state = st_begin; int c = next(next_userdata); - while (state && state != st_end && c != EOF) { + while (state && state != st_end && c != EOF && + (!a->request->params_max || a->readed != a->request->params_max)) { switch (state) { case st_begin: state = parse_begin(a, c); @@ -435,6 +440,11 @@ static void run_automata(struct automata * a, break; } c = next(next_userdata); + ++a->readed; + } + if (a->request->params_max && a->readed == a->request->params_max) { + a->request->error = magi_error_limit; + return; } if (state == st_data && is_semiend(a)) { state = st_end; @@ -457,7 +467,7 @@ void magi_multipart(struct magi_request * request, void * next_userdata) { struct automata a = { 0, { 0, 0, 0 }, { 0, 0 }, { 0, 0 }, 0, 0, 1, - 0, 0, 2, 0, 0, 0 }; + 0, 0, 2, 0, 0, 0, 0 }; a.request = request; a.boundary = boundary; a.boundary_len = strlen(boundary); diff --git a/src/request.c b/src/request.c index 99f3a2a..e905e53 100644 --- a/src/request.c +++ b/src/request.c @@ -41,9 +41,9 @@ void magi_tempfiles_add(struct magi_tempfiles * tmps, tmps->locations = malloc(sizeof(*tmps->locations)); tmps->maximums = malloc(sizeof(*tmps->maximums)); } - tmps->param_names[tmps->count] = name; - tmps->locations[tmps->count] = path; - tmps->maximums[tmps->count] = max; + tmps->param_names[tmps->count - 1] = name; + tmps->locations[tmps->count - 1] = path; + tmps->maximums[tmps->count - 1] = max; } static void tempfiles(struct magi_file * file, @@ -57,16 +57,21 @@ static void tempfiles(struct magi_file * file, for (pos = 0; pos != table->count; ++pos) { if (!strcmp(table->param_names[pos], file->param_name)) { static FILE * f = 0; + static int unlimited; static int left; - int min; if (!f) { const char * loc = table->locations[pos]; f = fopen(loc, "wb"); left = table->maximums[pos]; + unlimited = !left; + } + if (unlimited) { + fwrite(addon, 1, addon_len, f); + } else { + int min = left < addon_len ? left : addon_len; + fwrite(addon, 1, min, f); + left -= min; } - min = left < addon_len ? left : addon_len; - fwrite(addon, 1, min, f); - left -= min; if (is_addon_last) { fclose(f); f = 0; -- cgit v1.2.3