aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/http.c33
1 files changed, 31 insertions, 2 deletions
diff --git a/lib/http.c b/lib/http.c
index 0c2efa9..a0374a3 100644
--- a/lib/http.c
+++ b/lib/http.c
@@ -333,6 +333,35 @@ int parse_request(arena *arena, size_t inc_fd, struct request *req) {
return 0;
}
+bool compare_paths(const_string handler_path, const_string req_path, arena* arena, context *ctx) {
+ const_string path;
+ bool entered = false;
+
+ while (cs_try_chop_delim(&handler_path, ':', &path)) {
+ entered = true;
+
+ const_string req_byte = {req_path.data, path.len};
+ if (!cs_eq(path, req_byte)) {
+ return false;
+ }
+
+ req_path.data += path.len;
+ req_path.len -= path.len;
+
+ const_string name = cs_chop_delim(&handler_path, '/');
+ const_string tmp = cs_chop_delim(&req_path, '/');
+ const_string *value = arena_alloc(arena, sizeof(const_string));
+ *value = tmp;
+ set_context_value(arena, ctx, (context_value){name, (void *)value});
+ }
+
+ if (!entered && !cs_eq(handler_path, req_path)) {
+ return false;
+ }
+
+ return true;
+}
+
void process_request(struct server *serv, size_t inc_fd) {
arena req_arena = {0};
@@ -353,8 +382,8 @@ void process_request(struct server *serv, size_t inc_fd) {
for (size_t i = 0; i < serv->handlers.len; i++) {
struct handler handler = serv->handlers.data[i];
if ((cs_eq(handler.method, req.method) || (cs_eq(handler.method, CS("GET")) && cs_eq(req.method, CS("HEAD"))) )
- && cs_eq(handler.path, req.path)) {
- handler.func(req, &resp);
+ && compare_paths(handler.path, req.path, &req_arena, &req_context)) {
+ handler.func(&req_context, req, &resp);
break;
}
}