From 4a88dcb68d00466286f455fe973e1b588305dc70 Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Mon, 28 Mar 2022 18:37:35 +0000 Subject: [PATCH] Handle get queries FossilOrigin-Name: e125357cd01e8393307cae6fccf3e7b79317b60293d479934b51742f2eb22a3e --- src/main.c | 4 +++- src/query.c | 41 +++++++++++++++++++++++++++++++++++++++++ src/query.h | 10 +++++++++- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index 1da4f8b..d083535 100644 --- a/src/main.c +++ b/src/main.c @@ -53,7 +53,8 @@ int main(void) char* cookies_str = read_cookies_env(); api.token = cookies.access_token; // Load token now char* post_str = read_post_data(); - + char* get_str = read_query_data(); + // Config defaults g_config.theme = "treebird20"; @@ -81,6 +82,7 @@ int main(void) // Cleanup if (cookies_str) free(cookies_str); if (post_str) free(post_str); + if (get_str) free(get_str); mastodont_free(&api); // Obliterate all global values, so the next client diff --git a/src/query.c b/src/query.c index 34cce4c..3558c08 100644 --- a/src/query.c +++ b/src/query.c @@ -24,6 +24,47 @@ #include "key.h" struct query_values post = { 0 }; +struct get_values query = { 0 }; + +char* read_query_data() +{ + struct http_query_info info; + char* query_string = getenv("QUERY_STRING"); + char* get_query = NULL, *g_query_read; + + // BEGIN Query references + struct key_value_refs refs[] = { + { "offset", &(query.offset) }, + { "id", &(query.id) }, + }; + // END Query references + + if (query_string) + { + get_query = malloc(strlen(query_string) + 1); + if (!get_query) + { + perror("malloc"); + return NULL; + } + strcpy(get_query, query_string); + + // For shifting through + g_query_read = get_query; + + do + { + g_query_read = parse_query(g_query_read, &info); + if (!(info.key && info.val)) break; + for (size_t i = 0; i < (sizeof(refs)/sizeof(refs[0])); ++i) + if (strcmp(info.key, refs[i].key) == 0) + *(refs[i].val) = info.val; + } + while (g_query_read); + } + + return get_query; +} char* read_post_data() { diff --git a/src/query.h b/src/query.h index 074faef..c7fa89e 100644 --- a/src/query.h +++ b/src/query.h @@ -38,8 +38,16 @@ struct query_values char* replyid; }; -extern struct query_values post; +struct get_values +{ + char* offset; + char* id; +}; +extern struct query_values post; +extern struct get_values query; + +char* read_query_data(); char* read_post_data(); /* A stupidly quick query parser */ char* parse_query(char* begin, struct http_query_info* info);