feat: routes now specify a method
This commit is contained in:
parent
9b66223a57
commit
18d67fb69f
8 changed files with 40 additions and 17 deletions
|
|
@ -35,6 +35,7 @@ void http_loop_ctx_reset(http_loop_ctx *ctx) {
|
|||
|
||||
ctx->res.body = NULL;
|
||||
|
||||
ctx->res.type = 0;
|
||||
ctx->res.head_len = 0;
|
||||
ctx->res.head_written = 0;
|
||||
ctx->res.body_len = 0;
|
||||
|
|
|
|||
|
|
@ -79,6 +79,7 @@ void http_loop_route_request(event_loop_conn *conn) {
|
|||
ctx->req.path);
|
||||
|
||||
http_route *route;
|
||||
bool path_matched = false;
|
||||
|
||||
for (size_t i = 0; i < gctx->route_count; i++) {
|
||||
route = &gctx->routes[i];
|
||||
|
|
@ -86,16 +87,21 @@ void http_loop_route_request(event_loop_conn *conn) {
|
|||
switch (route->type) {
|
||||
case http_route_literal:
|
||||
if (strncmp(route->path, ctx->req.path, ctx->req.path_len) == 0) {
|
||||
ctx->route = route;
|
||||
return;
|
||||
path_matched = true;
|
||||
|
||||
if (ctx->req.method == route->method) {
|
||||
ctx->route = route;
|
||||
return;
|
||||
}
|
||||
}
|
||||
break;
|
||||
// TODO
|
||||
case http_route_regex:;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallthrough is to write a 404
|
||||
ctx->res.type = http_not_found;
|
||||
// Fallthrough case
|
||||
ctx->res.type = path_matched ? http_method_not_allowed : http_not_found;
|
||||
conn->state = event_loop_conn_state_res;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,10 @@ static const char *http_response_format = "HTTP/1.1 %i %s\n"
|
|||
"Content-Length: %lu\n\n";
|
||||
|
||||
void http_loop_init_header(http_response *res) {
|
||||
if (res->type == 0) {
|
||||
res->type = http_ok;
|
||||
}
|
||||
|
||||
const char *response_type_name =
|
||||
http_response_type_names[res->type / 100 - 1][res->type % 100];
|
||||
|
||||
|
|
@ -61,3 +65,10 @@ void http_loop_write_response(event_loop_conn *conn) {
|
|||
res->body_written += bytes_to_write;
|
||||
}
|
||||
}
|
||||
|
||||
void http_loop_res_set_body(http_loop_ctx *ctx, const char *body,
|
||||
size_t body_len, bool owned) {
|
||||
ctx->res.body = body;
|
||||
ctx->res.body_len = body_len;
|
||||
ctx->res.owns_body = owned;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue