diff options
| author | spl3g <spleefer6@yandex.ru> | 2025-03-18 11:39:44 +0300 |
|---|---|---|
| committer | spl3g <spleefer6@yandex.ru> | 2025-03-18 11:43:31 +0300 |
| commit | 1e12d57401b01d269d5d4a62d050876fcf03fd81 (patch) | |
| tree | 36dbc7f1554d5a2ef9735dca5f923155f54372da | |
| parent | ea3e7bc2c7c115161918e352e68c1c7e67dfdae3 (diff) | |
Add path parameters support
| -rw-r--r-- | lib/http.c | 33 |
1 files changed, 31 insertions, 2 deletions
@@ -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; } } |
