From 365f4eeddf91f670e4113dd6a3125a2b8e96e31e Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Sun, 10 Apr 2022 06:44:21 +0000 Subject: [PATCH] Key files reader FossilOrigin-Name: 4e39822fc1dc1232fb338f1b3495cdb9f1303aa35faf7978c196e0d4df8bd4af --- src/cookie.c | 10 +++++----- src/key.h | 2 ++ src/main.c | 1 + src/query.c | 49 ++++++++++++++++++++++++++++++++++++++++++++----- src/query.h | 17 ++++++++++++++--- 5 files changed, 66 insertions(+), 13 deletions(-) diff --git a/src/cookie.c b/src/cookie.c index c1e02c6..538e6b5 100644 --- a/src/cookie.c +++ b/src/cookie.c @@ -50,10 +50,10 @@ char* read_cookies_env(struct cookie_values* cookies) // Will loop through these struct key_value_refs refs[] = { - { "access_token", &(cookies->access_token) }, - { "logged_in", &(cookies->logged_in) }, - { "theme", &(cookies->theme) }, - { "instance_url", &(cookies->instance_url) } + { "access_token", &(cookies->access_token), key_string }, + { "logged_in", &(cookies->logged_in), key_string }, + { "theme", &(cookies->theme), key_string }, + { "instance_url", &(cookies->instance_url), key_string } }; do @@ -64,7 +64,7 @@ char* read_cookies_env(struct cookie_values* cookies) for (int i = 0; i < (sizeof(refs)/sizeof(refs[0])); ++i) { if (strcmp(info.key, refs[i].key) == 0) - *(refs[i].val) = info.val; + refs[i].func(info.val, NULL, refs[i].val); } } while (cookies_read); diff --git a/src/key.h b/src/key.h index 0e26e46..e5f70de 100644 --- a/src/key.h +++ b/src/key.h @@ -18,11 +18,13 @@ #ifndef KEY_H #define KEY_H +#include struct form_props { char* filename; char* filetype; + size_t data_size; }; struct key_value_refs diff --git a/src/main.c b/src/main.c index 20ebef7..6b560ac 100644 --- a/src/main.c +++ b/src/main.c @@ -105,6 +105,7 @@ int main(void) if (post_str) free(post_str); if (get_str) free(get_str); mastodont_free(&api); + free_files(&(ssn.post.files)); ++run_count; } diff --git a/src/query.c b/src/query.c index 6e91327..4a09914 100644 --- a/src/query.c +++ b/src/query.c @@ -16,12 +16,12 @@ * along with this program. If not, see . */ +#include #include #include #include #include #include "query.h" -#include "key.h" #include "mime.h" struct query_values post = { 0 }; @@ -35,8 +35,8 @@ char* read_query_data(struct get_values* query) // BEGIN Query references struct key_value_refs refs[] = { - { "offset", &(query->offset), { 0 }, key_string }, - { "q", &(query->q), { 0 }, key_string }, + { "offset", &(query->offset), key_string }, + { "q", &(query->q), key_string }, }; // END Query references @@ -67,10 +67,35 @@ char* read_query_data(struct get_values* query) return get_query; } +void key_files(char* val, struct form_props* form, void* arg) +{ + struct file_array* arr = arg; + char* ptr; + + arr->content = realloc(arr->content, + sizeof(struct file_content) * ++(arr->array_size)); + if (!(arr->content)) + return; + + ptr = malloc(form->data_size+1); + if (!ptr) + return; + + memcpy(ptr, val, form->data_size+1); + + // Store + arr->content[arr->array_size-1].content = ptr; + arr->content[arr->array_size-1].content_size = form->data_size; + + return; + +} + char* read_post_data(struct query_values* post) { struct http_query_info query_info; struct http_form_info form_info; + struct form_props form_props; char* request_method = getenv("REQUEST_METHOD"); char* post_query = NULL, *p_query_read; @@ -90,7 +115,7 @@ char* read_post_data(struct query_values* post) { "min_id", &(post->min_id), key_string }, { "max_id", &(post->max_id), key_string }, { "start_id", &(post->start_id), key_string }, - { "file", &(post->file), key_form } + { "file", &(post->files), key_files } }; // END Query references @@ -121,10 +146,13 @@ char* read_post_data(struct query_values* post) p_query_read = read_form_data(mime_boundary, p_query_read, &form_info); + form_props.filename = form_info.filename; + form_props.filetype = form_info.content_type; + form_props.data_size = form_info.value_size; if (!p_query_read) break; for (size_t i = 0; i < (sizeof(refs)/sizeof(refs[0])); ++i) if (strcmp(form_info.name, refs[i].key) == 0) - refs[i].func(form_info.value, &(refs[i].form), refs[i].val); + refs[i].func(form_info.value, &form_props, refs[i].val); } else { // Mime value not set @@ -201,3 +229,14 @@ char* try_handle_post(void (*call)(struct http_query_info*, void*), void* arg) return post_query; } + +void free_files(struct file_array* files) +{ + if (!files) return; + struct file_content* content = files->content; + for (size_t i = 0; i < files->array_size; ++i) + { + free(content[i].content); + } + free(content); +} diff --git a/src/query.h b/src/query.h index d539630..f89a6b5 100644 --- a/src/query.h +++ b/src/query.h @@ -20,6 +20,7 @@ #define QUERY_H #include #include +#include "key.h" struct http_query_info { @@ -27,11 +28,17 @@ struct http_query_info char* val; }; -struct http_array +struct file_content { char* content; + size_t content_size; +}; + +struct file_array +{ + struct file_content* content; size_t array_size; -} +}; struct query_values { @@ -52,7 +59,7 @@ struct query_values char* max_id; char* start_id; - struct http_array file; + struct file_array files; }; struct get_values @@ -61,10 +68,14 @@ struct get_values char* q; }; +void key_files(char* val, struct form_props* form, void* arg); + char* read_query_data(struct get_values* query); char* read_post_data(struct query_values* post); /* A stupidly quick query parser */ char* parse_query(char* begin, struct http_query_info* info); char* try_handle_post(void (*call)(struct http_query_info*, void*), void* arg); +void free_files(struct file_array* files); + #endif // QUERY_H