feat: basic http routing
This commit is contained in:
parent
8250a5b8b0
commit
7ece0eb4e5
4 changed files with 75 additions and 31 deletions
|
|
@ -25,24 +25,28 @@ bool http_loop_handle_request(event_loop_conn *conn) {
|
|||
|
||||
http_loop_ctx *ctx = conn->ctx;
|
||||
|
||||
http_parse_error res =
|
||||
http_parse_request(&ctx->req, (const char *)&conn->rbuf[conn->rbuf_read],
|
||||
conn->rbuf_size - conn->rbuf_read);
|
||||
// If route is defined, we're currently processing a request
|
||||
if (ctx->route == NULL) {
|
||||
http_parse_error res = http_parse_request(
|
||||
&ctx->req, (const char *)&conn->rbuf[conn->rbuf_read],
|
||||
conn->rbuf_size - conn->rbuf_read);
|
||||
|
||||
if (res == http_parse_error_ok) {
|
||||
// Perform the request
|
||||
/* http_route(conn); */
|
||||
}
|
||||
// Both in the case of an invalid HTTP request or one that's larger than the
|
||||
// read buffer, we cannot determine when the next, possibly valid, HTTP
|
||||
// request begins in the data stream. Therefore, we close the connection,
|
||||
// even if additional pipelined requests are incoming.
|
||||
else if (res == http_parse_error_invalid ||
|
||||
(res == http_parse_error_incomplete &&
|
||||
conn->rbuf_size == EVENT_LOOP_BUFFER_SIZE)) {
|
||||
conn->state = event_loop_conn_state_end;
|
||||
if (res == http_parse_error_invalid ||
|
||||
(res == http_parse_error_incomplete &&
|
||||
conn->rbuf_size == EVENT_LOOP_BUFFER_SIZE)) {
|
||||
conn->state = event_loop_conn_state_end;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// If the routing fails, we exit the handler
|
||||
if (!http_loop_route_request(conn)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
http_loop_process_request(conn);
|
||||
|
||||
// TODO in highly concurrent situations, it might actually be better to always
|
||||
// return false here, as this allows cycling better through all connections
|
||||
return conn->state == event_loop_conn_state_req;
|
||||
|
|
|
|||
|
|
@ -1,13 +1,48 @@
|
|||
#include <string.h>
|
||||
|
||||
#include "event_loop.h"
|
||||
#include "http.h"
|
||||
#include "http_loop.h"
|
||||
|
||||
void http_loop_process_request(event_loop_conn *conn) {}
|
||||
void http_loop_process_request(event_loop_conn *conn) {
|
||||
http_loop_ctx *ctx = conn->ctx;
|
||||
|
||||
/* void http_route(event_loop_conn *conn) { */
|
||||
/* // TODO routing */
|
||||
// The end of the list of steps is marked with a NULL. When this is reached,
|
||||
// we simply set the route to NULL, letting the http loop know a new request
|
||||
// can be handled.
|
||||
if (ctx->route->steps[ctx->current_step] == NULL) {
|
||||
ctx->route = NULL;
|
||||
ctx->current_step = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
/* // Fallthrough is to return a 404 */
|
||||
/* http_write_standard_response(conn, http_not_found); */
|
||||
/* } */
|
||||
// We execute the next step
|
||||
// NOTE can/should we execute more than one step at once here?
|
||||
if (ctx->route->steps[ctx->current_step](conn)) {
|
||||
ctx->current_step++;
|
||||
}
|
||||
}
|
||||
|
||||
bool http_loop_route_request(event_loop_conn *conn) {
|
||||
http_loop_ctx *ctx = conn->ctx;
|
||||
http_loop_gctx *gctx = ctx->g;
|
||||
|
||||
http_route *route;
|
||||
|
||||
for (size_t i = 0; i < gctx->route_count; i++) {
|
||||
route = &gctx->routes[i];
|
||||
|
||||
switch (route->type) {
|
||||
case http_route_literal:
|
||||
if (strncmp(route->path, ctx->req.method, ctx->req.method_len)) {
|
||||
ctx->route = route;
|
||||
return true;
|
||||
}
|
||||
// TODO
|
||||
case http_route_regex:;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallthrough is to write a 404
|
||||
http_write_standard_response(conn, http_not_found);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue