From b9a0844fe25cf717cdd796018bea2ae6eff58896 Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Fri, 6 Dec 2019 15:52:55 +0300 Subject: [magi] change of cookies and tempfile callback --- examples/echo.c | 4 -- examples/upload.c | 2 +- src/cgi.c | 6 +-- src/cookie.c | 14 +++-- src/cookie.h | 10 ++-- src/cookies.c | 22 +++----- src/request.c | 150 ++++++++++++++++++++++++++---------------------------- src/request.h | 23 ++++++--- src/response.c | 10 ++++ src/response.h | 3 ++ 10 files changed, 127 insertions(+), 117 deletions(-) diff --git a/examples/echo.c b/examples/echo.c index e9807f0..ca30ff7 100644 --- a/examples/echo.c +++ b/examples/echo.c @@ -22,10 +22,6 @@ void proceed_cookies(struct magi_cookie_list * cookies, magi_response_add(response, "] for domain ["); magi_response_add(response, cookies->item.domain); } - if (cookies->item.port) { - magi_response_add(response, "] for port ["); - magi_response_add(response, cookies->item.port); - } if (cookies->item.path) { magi_response_add(response, "] for path ["); magi_response_add(response, cookies->item.path); diff --git a/examples/upload.c b/examples/upload.c index c61562a..a880c53 100644 --- a/examples/upload.c +++ b/examples/upload.c @@ -35,7 +35,7 @@ void response_request(struct magi_request * req, struct magi_response * res) int main(int argc, char const * argv[]) { struct magi_request request; - struct magi_tempfiles tmps = { 0, 0, 0, 0 }; + struct magi_tempfiles tmps = { 0, 0 }; magi_request_setup(&request); magi_tempfiles_add(&tmps, "data", "data", 0); magi_request_setup_tempfiles(&request, &tmps); diff --git a/src/cgi.c b/src/cgi.c index 59e2ef7..6e8c98e 100644 --- a/src/cgi.c +++ b/src/cgi.c @@ -259,9 +259,9 @@ void output_cookies(struct magi_cookie_list * list) fputs("; Domain=", stdout); fputs(list->item.domain, stdout); } - if (list->item.port) { - fputs("; Port=", stdout); - fputs(list->item.port, stdout); + if (list->item.max_age) { + fputs("; Max-Age=", stdout); + fputs(list->item.max_age, stdout); } fputs("\r\n", stdout); list = list->next; diff --git a/src/cookie.c b/src/cookie.c index 9d2a5ce..d1d82b3 100644 --- a/src/cookie.c +++ b/src/cookie.c @@ -18,13 +18,17 @@ int magi_cookie_list_add(struct magi_cookie_list ** list, char * magi_cookie_list_get(struct magi_cookie_list * list, const char * name) { + char * res = 0; if (!list || !name) { return 0; - } else if (!strcmp(list->item.name, name)) { - return list->item.data; - } else { - return magi_cookie_list_get(list->next, name); } + while (list) { + if (!strcmp(list->item.name, name)) { + res = list->item.data; + } + list = list->next; + } + return res; } void magi_cookie_list_destroy(struct magi_cookie_list * list) @@ -36,6 +40,6 @@ void magi_cookie_list_destroy(struct magi_cookie_list * list) free(list->item.data); free(list->item.path); free(list->item.domain); - free(list->item.port); + free(list->item.max_age); } } diff --git a/src/cookie.h b/src/cookie.h index 0430226..9419457 100644 --- a/src/cookie.h +++ b/src/cookie.h @@ -6,9 +6,9 @@ struct magi_cookie { /* All pointers must be valid as 'free' arguments. */ char * name; char * data; - char * path; - char * domain; - char * port; + char * path; /* Without '/' at the end. */ + char * domain; /* With dot at the begining. */ + char * max_age; /* In seconds until discard, used only in response. */ }; struct magi_cookie_list { @@ -21,7 +21,9 @@ struct magi_cookie_list { int magi_cookie_list_add(struct magi_cookie_list ** list, struct magi_cookie * item); -/* Data of the first node in list: node.name == name; else null. */ +/* Data of last cookie in list: cookie.name == name, returns null if no such; + * Last cookie in list is first from request, and from RFC 2109 we know that + * first cookie is the most accurate for request's domain and path. */ char * magi_cookie_list_get(struct magi_cookie_list * list, const char * name); /* Freeing and invalidation of list. */ diff --git a/src/cookies.c b/src/cookies.c index 4cbe659..0c5560b 100644 --- a/src/cookies.c +++ b/src/cookies.c @@ -16,7 +16,7 @@ enum st { st_post_data }; -enum data_type { dt_plain = 0, dt_version, dt_path, dt_domain, dt_port }; +enum data_type { dt_plain = 0, dt_version, dt_path, dt_domain }; struct automata { struct magi_cookie_list ** list; @@ -25,7 +25,7 @@ struct automata { int buf_len; int buf_size; int is_first; - int is_cookie2; + int is_advanced; int is_quoted; enum data_type data_t; }; @@ -33,11 +33,11 @@ struct automata { static void nulify_cookie(struct automata * a) { - a->cookie.name = 0; - a->cookie.data = 0; - a->cookie.path = 0; - a->cookie.domain = 0; - a->cookie.port = 0; + a->cookie.name = 0; + a->cookie.data = 0; + a->cookie.path = 0; + a->cookie.domain = 0; + a->cookie.max_age = 0; } static void buf_new(struct automata * a) @@ -53,13 +53,11 @@ static enum data_type what_is_name(const struct automata * a) enum data_type data_t = dt_plain; if (a->is_first && !strcmp(a->buf, "$Version")) { data_t = dt_version; - } else if (a->is_cookie2) { + } else if (a->is_advanced) { if (!strcmp(a->buf, "$Path")) { data_t = dt_path; } else if (!strcmp(a->buf, "$Domain")) { data_t = dt_domain; - } else if (!strcmp(a->buf, "$Port")) { - data_t = dt_port; } } return data_t; @@ -96,9 +94,6 @@ static int end_data(struct automata * a) case dt_domain: a->cookie.domain = a->buf; break; - case dt_port: - a->cookie.port = a->buf; - break; case dt_version: if (strcmp(a->buf, "1")) { ok = 0; @@ -291,6 +286,5 @@ void magi_cookies(struct magi_request * request, const char * data) free(a.cookie.data); free(a.cookie.path); free(a.cookie.domain); - free(a.cookie.port); free(a.buf); } diff --git a/src/request.c b/src/request.c index 0db1e70..d8e6d69 100644 --- a/src/request.c +++ b/src/request.c @@ -22,84 +22,6 @@ void magi_request_setup(struct magi_request * request) } -void magi_tempfiles_add(struct magi_tempfiles * tmps, - const char * name, - const char * path, - int max) -{ - if (!tmps) { - return; - } - if (tmps->count++) { - tmps->param_names = realloc(tmps->param_names, - sizeof(*tmps->param_names) * tmps->count); - tmps->locations = - realloc(tmps->locations, sizeof(*tmps->locations) * tmps->count); - tmps->maximums = - realloc(tmps->maximums, sizeof(*tmps->maximums) * tmps->count); - } else { - tmps->param_names = malloc(sizeof(*tmps->param_names)); - tmps->locations = malloc(sizeof(*tmps->locations)); - tmps->maximums = malloc(sizeof(*tmps->maximums)); - } - tmps->param_names[tmps->count - 1] = name; - tmps->locations[tmps->count - 1] = path; - tmps->maximums[tmps->count - 1] = max; -} - -void magi_tempfiles_destroy(struct magi_tempfiles * tmps) -{ - if (!tmps) { - return; - } - free(tmps->param_names); - free(tmps->locations); - free(tmps->maximums); -} - -static void tempfiles(struct magi_file * file, - char * addon, - int addon_len, - int is_addon_last, - void * userdata) -{ - struct magi_tempfiles * table = userdata; - int pos; - 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; - 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; - } - if (is_addon_last) { - fclose(f); - f = 0; - } - return; - } - } -} - -void magi_request_setup_tempfiles(struct magi_request * request, - struct magi_tempfiles * table) -{ - request->file_callback = tempfiles; - request->file_callback_userdata = table; -} - - static void request_free(struct magi_request * request) { free(request->cookies); @@ -158,3 +80,75 @@ void magi_request_destroy(struct magi_request * request) request_annul(request); } } + + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * Tempfiles Callback + */ +void magi_tempfiles_add(struct magi_tempfiles * tmps, + const char * name, + const char * path, + int max) +{ + if (!tmps) { + return; + } + if (tmps->count++) { + tmps->tmps = realloc(tmps->tmps, sizeof(*tmps->tmps) * tmps->count); + } else { + tmps->tmps = malloc(sizeof(*tmps->tmps)); + } + tmps->tmps[tmps->count - 1].param_name = name; + tmps->tmps[tmps->count - 1].location = path; + tmps->tmps[tmps->count - 1].maximum = max; +} + +void magi_tempfiles_destroy(struct magi_tempfiles * tmps) +{ + if (!tmps) { + return; + } + free(tmps->tmps); +} + +static void tempfiles(struct magi_file * file, + char * addon, + int addon_len, + int is_addon_last, + void * userdata) +{ + struct magi_tempfiles * table = userdata; + int pos; + for (pos = 0; pos != table->count; ++pos) { + if (!strcmp(table->tmps[pos].param_name, file->param_name)) { + static FILE * f = 0; + static int unlimited; + static int left; + if (!f) { + const char * loc = table->tmps[pos].location; + f = fopen(loc, "wb"); + left = table->tmps[pos].maximum; + 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; + } + if (is_addon_last) { + fclose(f); + f = 0; + } + return; + } + } +} + +void magi_request_setup_tempfiles(struct magi_request * request, + struct magi_tempfiles * table) +{ + request->file_callback = tempfiles; + request->file_callback_userdata = table; +} diff --git a/src/request.h b/src/request.h index c0ff2f6..df9a57c 100644 --- a/src/request.h +++ b/src/request.h @@ -83,11 +83,22 @@ struct magi_request { void magi_request_setup(struct magi_request * request); +/* Destroys request. */ +void magi_request_destroy(struct magi_request * request); + + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * Tempfiles Callback + */ +struct magi_tempfile { + const char * param_name; /* Form field name, in which file is expected. */ + const char * location; /* Location to load file in. */ + int maximum; /* Null <=> unlimited. */ +}; + struct magi_tempfiles { - int count; - const char ** param_names; - const char ** locations; - int * maximums; /* Null maximums[i] <=> unlimited tempfiles[i]. */ + int count; + struct magi_tempfile * tmps; }; void magi_tempfiles_add(struct magi_tempfiles * tmps, @@ -103,8 +114,4 @@ void magi_request_setup_tempfiles(struct magi_request * request, struct magi_tempfiles * table); -/* Destroys request. */ -void magi_request_destroy(struct magi_request * request); - - #endif diff --git a/src/response.c b/src/response.c index 7e2ec0b..5e2f326 100644 --- a/src/response.c +++ b/src/response.c @@ -82,6 +82,16 @@ void magi_response_cookie_easy(struct magi_response * response, magi_cookie_list_add(&response->cookies, &cookie); } +void magi_response_cookie_discard(struct magi_response * response, + const char * name) +{ + struct magi_cookie cookie = { 0, 0, 0, 0, 0 }; + cookie.name = magi_str_create_copy(name, name + strlen(name)); + cookie.max_age = magi_str_create(1); + cookie.max_age[0] = '0'; + magi_cookie_list_add(&response->cookies, &cookie); +} + void magi_response_http(struct magi_response * response, const char * name, const char * data) diff --git a/src/response.h b/src/response.h index 95f6537..7389637 100644 --- a/src/response.h +++ b/src/response.h @@ -35,6 +35,9 @@ void magi_response_cookie_easy(struct magi_response * response, const char * name, const char * value); +void magi_response_cookie_discard(struct magi_response * response, + const char * name); + void magi_response_http(struct magi_response * response, const char * name, const char * data); -- cgit v1.2.3