tmove query parsing into separate function - vote - simple cgi voting system fo… | |
git clone git://src.adamsgaard.dk/vote | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit 74b58729c204508d0e3fbe2162a3325d2f3d5aeb | |
parent c7654831e7c7bc274ca47f940c93392a55a74cb2 | |
Author: Anders Damsgaard <[email protected]> | |
Date: Sun, 27 Sep 2020 01:39:22 +0200 | |
move query parsing into separate function | |
Diffstat: | |
M vote.c | 28 +++++++++++++++++++++------- | |
1 file changed, 21 insertions(+), 7 deletions(-) | |
--- | |
diff --git a/vote.c b/vote.c | |
t@@ -8,6 +8,8 @@ | |
#define OUT(s) (fputs((s), stdout)) | |
#define POLLS_DIR "polls" | |
+static char poll[1024]; | |
+ | |
void | |
print_html_head() { | |
OUT("Content-type: text/html; charset=utf-8\r\n\r\n"); | |
t@@ -36,10 +38,24 @@ getparam(const char *query, const char *s) { | |
return (char *)last; | |
} | |
+void | |
+parse_query() { | |
+ char *query, *p; | |
+ size_t len; | |
+ | |
+ if (!(query = getenv("QUERY_STRING"))) | |
+ query = ""; | |
+ | |
+ if ((p = getparam(query, "poll"))) { | |
+ if ((len = strcspn(p, "&")) && len + 1 < sizeof(poll)) { | |
+ memcpy(poll, p, len); | |
+ poll[len] = '\0'; | |
+ } | |
+ } | |
+} | |
+ | |
int | |
main() { | |
- char *query; | |
- char *q; | |
struct stat sb; | |
if (pledge("stdio cpath rpath", NULL) == -1) { | |
t@@ -47,6 +63,7 @@ main() { | |
OUT("Status: 500 Internal Server Error\r\n\r\n"); | |
exit(1); | |
} | |
+ | |
if (stat(POLLS_DIR, &sb) == -1) { | |
if (mkdir(POLLS_DIR, 0755) == -1) { | |
fprintf(stderr, "mkdir polls/ failed: %s\n", strerror(… | |
t@@ -55,13 +72,10 @@ main() { | |
} | |
} | |
- if (!(query = getenv("QUERY_STRING"))) | |
- query = ""; | |
- | |
- q = getparam(query, "q"); | |
+ parse_query(); | |
print_html_head(); | |
- printf("<p>query string: '%s', q: '%s'</p>\n", query, q); | |
+ printf("<p>poll: '%s'</p>\n", poll); | |
print_html_foot(); | |
return 0; |