summary refs log tree commit diff
diff options
context:
space:
mode:
authorsternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org>2020-09-22 14:26:09 +0200
committersternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org>2020-09-22 14:26:09 +0200
commitbd9403c6e548ceb5d82105391a1d07eb389f4aca (patch)
treeed417129a97d243bd72a67783776691a3f63eb80
parent1761f0446bd34460fd1117f3a7fe88cd79647980 (diff)
fix(warteraum): match segments exactly, not just prefixes
Previously match_string_segment(n, queue, … ) would match everything
from "q" to "queue". This is now fixed by using HTTP_STRING_IS which
also matches the strings' lengths.
-rw-r--r--warteraum/main.c16
-rw-r--r--warteraum/routing.c12
-rw-r--r--warteraum/routing.h7
3 files changed, 13 insertions, 22 deletions
diff --git a/warteraum/main.c b/warteraum/main.c
index 3d64b5a..d8dbd7b 100644
--- a/warteraum/main.c
+++ b/warteraum/main.c
@@ -337,28 +337,28 @@ void handle_request(http_request_t *request) {
   if(count < 0) {
     status = WARTERAUM_INTERNAL_ERROR;
   } else {
-    if(segment_match(0, "api", segs, count)) {
-      if(segment_match(1, "v1", segs, count)) {
+    if(SEGMENT_MATCH(0, "api", segs, count)) {
+      if(SEGMENT_MATCH(1, "v1", segs, count)) {
         api_version = WARTERAUM_API_V1;
 
-        if(segment_match(2, "queue", segs, count)) {
+        if(SEGMENT_MATCH(2, "queue", segs, count)) {
           if(count == 3) {
             status = response_queue(api_version, request, response);
-          } else if(segment_match_last(3, "add", segs, count)) {
+          } else if(SEGMENT_MATCH_LAST(3, "add", segs, count)) {
             // this endpoint returns html in /api/v1
             v1_html_response = true;
             status = response_queue_add(api_version, request, response);
-          } else if(segment_match(3, "del", segs, count) && count == 5) {
+          } else if(SEGMENT_MATCH(3, "del", segs, count) && count == 5) {
             status = response_queue_del(segs[4], api_version, request, response);
           }
         }
-      } else if(segment_match(1, "v2", segs, count)) {
+      } else if(SEGMENT_MATCH(1, "v2", segs, count)) {
         api_version = WARTERAUM_API_V2;
 
-        if(segment_match(2, "queue", segs, count)) {
+        if(SEGMENT_MATCH(2, "queue", segs, count)) {
           if(count == 3) {
             status = response_queue(api_version, request, response);
-          } else if(segment_match_last(3, "add", segs, count)) {
+          } else if(SEGMENT_MATCH_LAST(3, "add", segs, count)) {
             status = response_queue_add(api_version, request, response);
           } else if(count == 4) {
             // /api/v2/queue/<id>
diff --git a/warteraum/routing.c b/warteraum/routing.c
index 103ab95..e181844 100644
--- a/warteraum/routing.c
+++ b/warteraum/routing.c
@@ -53,15 +53,3 @@ int split_segments(struct http_string_s path, struct http_string_s **segs) {
 
   return seg_len;
 }
-
-bool segment_match(int index, char *string, struct http_string_s *segs, int seg_count) {
-  if(index >= seg_count || segs == NULL || string == NULL) {
-    return false;
-  }
-
-  return (strncmp(string, segs[index].buf, segs[index].len) == 0);
-}
-
-bool segment_match_last(int index, char *string, struct http_string_s *segs, int seg_count) {
-  return (index + 1 == seg_count && segment_match(index, string, segs, seg_count));
-}
diff --git a/warteraum/routing.h b/warteraum/routing.h
index 77a3440..1bbeeb9 100644
--- a/warteraum/routing.h
+++ b/warteraum/routing.h
@@ -1,7 +1,10 @@
 #include "../third_party/httpserver.h/httpserver.h"
+#include "http_string.h"
 
 int split_segments(struct http_string_s, struct http_string_s **);
 
-bool segment_match(int, char *, struct http_string_s[], int);
+#define SEGMENT_MATCH(index, str, segs, count)             \
+ ((index < count) && HTTP_STRING_IS(segs[index], str))
 
-bool segment_match_last(int, char *, struct http_string_s[], int);
+#define SEGMENT_MATCH_LAST(index, str, segs, count) \
+  (index + 1 == count && SEGMENT_MATCH(index, str, segs, count))