Key files reader

FossilOrigin-Name: 4e39822fc1dc1232fb338f1b3495cdb9f1303aa35faf7978c196e0d4df8bd4af
This commit is contained in:
me@ow.nekobit.net 2022-04-10 06:44:21 +00:00
parent 3b2ef7aaa9
commit 365f4eeddf
5 changed files with 66 additions and 13 deletions

View file

@ -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);

View file

@ -18,11 +18,13 @@
#ifndef KEY_H
#define KEY_H
#include <stddef.h>
struct form_props
{
char* filename;
char* filetype;
size_t data_size;
};
struct key_value_refs

View file

@ -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;
}

View file

@ -16,12 +16,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <fcgi_stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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);
}

View file

@ -20,6 +20,7 @@
#define QUERY_H
#include <fcgi_stdio.h>
#include <stddef.h>
#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