summary refs log tree commit diff
diff options
context:
space:
mode:
authorsternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org>2020-11-24 21:48:09 +0100
committersternenseemann <git@lukasepple.de>2020-11-24 21:50:48 +0100
commita8d02b58f401f26cb026bd8cad9169ab16569a10 (patch)
tree4f775b5802c385972adc926d143e305c53479255
parent0d57f52c70f401186d9f6e3d2860e32409469653 (diff)
feat(warteraum): trim trailing and leading whitespace of text input
-rw-r--r--warteraum/main.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/warteraum/main.c b/warteraum/main.c
index f8ff23e..345efe7 100644
--- a/warteraum/main.c
+++ b/warteraum/main.c
@@ -1,6 +1,7 @@
 #define HTTPSERVER_IMPL
 #include "../third_party/httpserver.h/httpserver.h"
 
+#include <ctype.h>
 #include <signal.h>
 #include <stdbool.h>
 #include <stdio.h>
@@ -47,6 +48,33 @@ void cleanup(int signum) {
   }
 }
 
+// adjust pointer and length so it points to a string that
+// has no leading nor trailing whitespace
+void trim_whitespace(char **str, int *len) {
+  if(*len > 0) {
+    int new_len = *len;
+    int new_start = 0;
+
+    int pos = 0;
+    while(isspace(*(*str + pos)) && pos < *len) {
+      new_start++;
+      new_len--;
+      pos++;
+    }
+
+    if(new_len > 0) {
+      pos = *len - 1;
+      while(isspace(*(*str + pos)) && pos > new_start) {
+        new_len--;
+        pos--;
+      }
+    }
+
+    *len = new_len;
+    *str = *str + new_start;
+  }
+}
+
 // authentication
 
 bool authenticate(http_string_t token) {
@@ -240,6 +268,8 @@ enum warteraum_result response_queue_add(enum warteraum_version version, http_re
 
   int decoded_len = urldecode(text, decoded, sizeof(char) * text.len);
 
+  trim_whitespace(&decoded, &decoded_len);
+
   if(decoded_len <= 0) {
     free(decoded_mem);
     return WARTERAUM_BAD_REQUEST;