From 31efb281a48b9a122e92e010914a4ca209c8ed47 Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Tue, 1 Feb 2022 03:10:09 +0000 Subject: [PATCH] Query parser FossilOrigin-Name: fb3dec10d90e0beac45bcef20f4f0b7ba9fcc1a446d602e4484a0d0fa5a9e856 --- src/cookie.h | 22 ++++++++++++++++++++++ src/page_config.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- src/path.c | 5 +++-- src/query.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/query.h | 33 +++++++++++++++++++++++++++++++++ static/config.html | 6 ++---- static/index.html | 7 ++++++- 7 files changed, 150 insertions(+), 9 deletions(-) create mode 100644 src/cookie.h create mode 100644 src/query.c create mode 100644 src/query.h diff --git a/src/cookie.h b/src/cookie.h new file mode 100644 index 0000000..b4960a6 --- /dev/null +++ b/src/cookie.h @@ -0,0 +1,22 @@ +/* + * RatFE - Lightweight frontend for Pleroma + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef COOKIE_H +#define COOKIE_H + +#endif // COOKIE_H diff --git a/src/page_config.c b/src/page_config.c index bc949c1..dc25aa9 100644 --- a/src/page_config.c +++ b/src/page_config.c @@ -17,8 +17,12 @@ */ #include +#include +#include +#include #include "../config.h" #include "page_config.h" +#include "query.h" // Pages #include "../static/index.chtml" @@ -27,9 +31,45 @@ void content_config(mastodont_t* api) { (void)api; // No need to use this - - /* Output */ + char* request_method = getenv("REQUEST_METHOD"); + char* post_query, * p_query_read; + struct http_query_info info; + + // Output printf("Content-Length: %ld\r\n\r\n", data_index_html_size + data_config_html_size); + + + // Handle POST + if (request_method && (strcmp("POST", request_method) == 0)) + { + int content_length = atoi(getenv("CONTENT_LENGTH")); + post_query = malloc(content_length + 1); + if (!post_query) + { + puts("Malloc error!"); + return; + } + read(STDIN_FILENO, post_query, content_length); + post_query[content_length] = '\0'; + + // For parse_query to shift through, so we can still free the original + p_query_read = post_query; + + // Parse + while (1) + { + p_query_read = parse_query(p_query_read, &info); + + printf("%.*s,", (int)info.val_len, info.val); + + if (p_query_read == NULL) + break; + } + + // Cleanup + free(post_query); + } + printf(data_index_html, config_canonical_name, data_config_html); } diff --git a/src/path.c b/src/path.c index 58e5ebd..ab23951 100644 --- a/src/path.c +++ b/src/path.c @@ -19,6 +19,7 @@ #include #include #include "path.h" +#include "index.h" void handle_paths(mastodont_t* api, struct path_info* paths, size_t paths_len) { @@ -28,11 +29,11 @@ void handle_paths(mastodont_t* api, struct path_info* paths, size_t paths_len) { content_index(api); } - else if (path && path[1] == '@') + else if (path[1] == '@') { // Account path content_index(api); } - else if (path) + else { // Generic path for (size_t i = 0; i < paths_len; ++i) { diff --git a/src/query.c b/src/query.c new file mode 100644 index 0000000..da30ef8 --- /dev/null +++ b/src/query.c @@ -0,0 +1,42 @@ +/* + * RatFE - Lightweight frontend for Pleroma + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "query.h" + +char* parse_query(char* begin, struct http_query_info* info) +{ + int end = 0; + char* val_begin; + info->key = begin; + for (; *begin != '&' && *begin != '\0'; ++begin) + { + if (*begin == '=') + { + val_begin = begin+1; + *begin = '\0'; + } + } + + if (*begin == '\0') end = 1; + if (*begin == '&') *begin = '\0'; + info->val = val_begin; + // The val length may be large, so strlen can waste time + info->val_len = (size_t)(begin - val_begin); + + return end ? NULL : begin+1; +} diff --git a/src/query.h b/src/query.h new file mode 100644 index 0000000..2fe5881 --- /dev/null +++ b/src/query.h @@ -0,0 +1,33 @@ +/* + * RatFE - Lightweight frontend for Pleroma + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef QUERY_H +#define QUERY_H +#include + +struct http_query_info +{ + char* key; + char* val; + size_t val_len; // Val may be large, like CSS property +}; + +/* A stupidly quick query parser */ +char* parse_query(char* begin, struct http_query_info* info); + +#endif // QUERY_H diff --git a/static/config.html b/static/config.html index e28c475..9ba836b 100644 --- a/static/config.html +++ b/static/config.html @@ -15,10 +15,6 @@ -
  • - - -
  • @@ -44,5 +40,7 @@ + + diff --git a/static/index.html b/static/index.html index 8e5a6f0..bba2775 100644 --- a/static/index.html +++ b/static/index.html @@ -26,10 +26,15 @@