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. =) ) --- examples/append.c | 2 +- examples/cookie.c | 2 +- examples/echo.c | 2 +- examples/fcgi.c | 3 ++- examples/upload.c | 4 ++-- src/cgi.c | 12 +++++++++--- src/multipart.c | 22 ++++++++++++++++------ src/request.c | 19 ++++++++++++------- 8 files changed, 44 insertions(+), 22 deletions(-) diff --git a/examples/append.c b/examples/append.c index e6e4a8a..cb49e50 100644 --- a/examples/append.c +++ b/examples/append.c @@ -34,7 +34,7 @@ int main(int argc, char const * argv[]) { struct magi_request request; magi_request_setup(&request); - if (magi_request_cgi(&request)) { + if (magi_request_cgi(&request) && magi_request_resume_cgi(&request)) { struct magi_response response; magi_response_setup(&response); response_request(&request, &response); diff --git a/examples/cookie.c b/examples/cookie.c index f21cb4a..bb1e1d1 100644 --- a/examples/cookie.c +++ b/examples/cookie.c @@ -32,7 +32,7 @@ int main(int argc, char const * argv[]) { struct magi_request request; magi_request_setup(&request); - if (magi_request_cgi(&request)) { + if (magi_request_cgi(&request) && magi_request_resume_cgi(&request)) { struct magi_response response; magi_response_setup(&response); response_request(&request, &response); diff --git a/examples/echo.c b/examples/echo.c index 3f72f8a..e9807f0 100644 --- a/examples/echo.c +++ b/examples/echo.c @@ -113,7 +113,7 @@ int main(int argc, char const * argv[]) { struct magi_request request; magi_request_setup(&request); - if (magi_request_cgi(&request)) { + if (magi_request_cgi(&request) && magi_request_resume_cgi(&request)) { struct magi_response response; magi_response_setup(&response); response_request(&request, &response); diff --git a/examples/fcgi.c b/examples/fcgi.c index 06384c4..2790f9d 100644 --- a/examples/fcgi.c +++ b/examples/fcgi.c @@ -27,7 +27,8 @@ int main(int argc, char const * argv[]) if (magi_session_fcgi(&session, sock)) { struct magi_request request; magi_request_setup(&request); - while (magi_request_fcgi(&request, &session)) { + while (magi_request_fcgi(&request, &session) && + magi_request_resume_fcgi(&request, &session)) { if (!request.error) { struct magi_response response; magi_response_setup(&response); diff --git a/examples/upload.c b/examples/upload.c index f4350ee..1947f93 100644 --- a/examples/upload.c +++ b/examples/upload.c @@ -22,7 +22,7 @@ void response_request(struct magi_request * req, struct magi_response * res) "" "Upload File" "" - "
" "" "" @@ -39,7 +39,7 @@ int main(int argc, char const * argv[]) magi_request_setup(&request); magi_tempfiles_add(&tmps, "data", "data", 0); magi_request_setup_tempfiles(&request, &tmps); - if (magi_request_cgi(&request)) { + if (magi_request_cgi(&request) && magi_request_resume_cgi(&request)) { struct magi_response response; magi_response_setup(&response); response_request(&request, &response); 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