From 40e6878a6e96084c2fa0c98b988020ee06b5f9c1 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Thu, 18 Jul 2019 18:08:15 -0400 Subject: account for final field not ending in &, decode '+' to ' ' --- post.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) (limited to 'post.c') diff --git a/post.c b/post.c index b0eed9e..67d3957 100644 --- a/post.c +++ b/post.c @@ -8,6 +8,22 @@ #define HSIZE 4 /* username, password, title, body */ +static void add_data(char *buf) +{ + char *value = strchr(buf, '='); + *value = '\0'; + value++; + + char *key = strdup(buf); + char *data = strdup(value); + + ENTRY e = { + .key = key, + .data = data + }; + hsearch(e, ENTER); +} + void read_post_data(void) { char *content_length = getenv("CONTENT_LENGTH"); @@ -33,19 +49,10 @@ void read_post_data(void) int c; while ((c = getchar()) != EOF) { if (c == '&') { - char *value = strchr(buf, '='); - *value = '\0'; - value++; - - char *key = strdup(buf); - char *data = strdup(value); - - ENTRY e = { - .key = key, - .data = data - }; - hsearch(e, ENTER); + add_data(buf); pos = 0; + } else if (c == '+') { + buf[++pos] = ' '; } else if (c == '%') { char hex[3] = { 0, 0, 0 }; hex[0] = getchar(); @@ -58,6 +65,10 @@ void read_post_data(void) } } + if (pos != 0) { + add_data(buf); + } + free(buf); } -- cgit v1.2.1