feat(routing): make path segments easily accessible from request struct

This commit is contained in:
Jef Roosens 2024-02-25 21:50:51 +01:00
parent fbd41f7e4e
commit 3ae1b62dd0
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
6 changed files with 105 additions and 65 deletions

View file

@ -58,10 +58,11 @@ void lnm_http_loop_process_route(lnm_http_conn *conn) {
lnm_http_loop_ctx *ctx = conn->ctx;
const lnm_http_loop_gctx *gctx = ctx->g;
switch (lnm_http_router_route(&ctx->match, gctx->router, ctx->req.method,
switch (lnm_http_router_route(&ctx->req.route_match, gctx->router,
ctx->req.method,
ctx->req.buf.s + ctx->req.path.o)) {
case lnm_http_route_err_match:
ctx->cur_step = ctx->match.route->step;
ctx->cur_step = ctx->req.route_match.route->step;
ctx->state = lnm_http_loop_state_parse_headers;
break;
case lnm_http_route_err_unknown_method:

View file

@ -112,3 +112,19 @@ lnm_err lnm_http_req_header_get_s(const char **out, size_t *out_len,
return lnm_err_not_found;
}
size_t lnm_http_req_route_segment(const char **out, lnm_http_req *req,
const char *key) {
const lnm_http_route_match_segment *segment =
lnm_http_route_match_get(&req->route_match, key);
if (segment == NULL) {
return 0;
}
if (out != NULL) {
*out = req->buf.s + req->path.o + segment->start;
}
return segment->len;
}