diff --git a/lnm/include/lnm/http/loop.h b/lnm/include/lnm/http/loop.h index f84a1ea..2a253d3 100644 --- a/lnm/include/lnm/http/loop.h +++ b/lnm/include/lnm/http/loop.h @@ -1,19 +1,26 @@ #ifndef LNM_HTTP_LOOP #define LNM_HTTP_LOOP -#include "lnm/common.h" +#include -#define LNM_HTTP_MAX_REQ_HEADERS 32 +#include "lnm/common.h" +#include "lnm/http/req.h" typedef struct lnm_loop lnm_http_loop; -typedef struct lnm_conn lnm_http_conn; +typedef struct lnm_loop_conn lnm_http_conn; typedef struct lnm_http_step lnm_http_step; typedef struct lnm_http_route lnm_http_route; -typedef lnm_err (*lnm_http_step_fn)(lnm_http_conn *conn); +typedef enum lnm_step_err { + lnm_step_err_done = 0, + lnm_step_err_io_needed, + lnm_step_err_close, +} lnm_step_err; + +typedef lnm_step_err (*lnm_http_step_fn)(lnm_http_conn *conn); /** * Initialize a new `lnm_http_loop`. @@ -66,10 +73,23 @@ void lnm_http_loop_route_add(lnm_http_loop *hl, lnm_http_route *route); */ typedef enum lnm_http_loop_state { lnm_http_loop_state_parse_req = 0, + lnm_http_loop_state_route, + lnm_http_loop_state_steps, } lnm_http_loop_state; +typedef struct lnm_http_loop_gctx { + struct { + lnm_http_route **arr; + size_t len; + } routes; + void *c; +} lnm_http_loop_gctx; + typedef struct lnm_http_loop_ctx { lnm_http_loop_state state; + lnm_http_req req; + lnm_http_loop_gctx *g; + void *c; } lnm_http_loop_ctx; #endif diff --git a/lnm/include/lnm/http/req.h b/lnm/include/lnm/http/req.h index 4741f4d..35bd21a 100644 --- a/lnm/include/lnm/http/req.h +++ b/lnm/include/lnm/http/req.h @@ -6,7 +6,8 @@ #include "picohttpparser.h" #include "lnm/http/consts.h" -#include "lnm/http/loop.h" + +#define LNM_HTTP_MAX_REQ_HEADERS 32 /** * Represents the parsed HTTP request diff --git a/lnm/include/lnm/loop.h b/lnm/include/lnm/loop.h index a84055a..8493170 100644 --- a/lnm/include/lnm/loop.h +++ b/lnm/include/lnm/loop.h @@ -15,7 +15,7 @@ typedef enum { lnm_loop_state_end, } lnm_loop_state; -typedef struct { +typedef struct lnm_loop_conn { int fd; lnm_loop_state state; void *ctx; diff --git a/lnm/src/http/loop_ctx.c b/lnm/src/http/loop_ctx.c new file mode 100644 index 0000000..e69de29 diff --git a/lnm/src/http/loop_process.c b/lnm/src/http/loop_process.c new file mode 100644 index 0000000..e93aca7 --- /dev/null +++ b/lnm/src/http/loop_process.c @@ -0,0 +1,60 @@ +#include "lnm/http/loop.h" +#include "lnm/http/loop_internal.h" +#include "lnm/http/req.h" +#include "lnm/loop.h" +#include "lnm/loop_internal.h" + +void lnm_http_loop_process_parse_req(lnm_http_conn *conn) { + lnm_http_loop_ctx *ctx = conn->ctx; + + lnm_http_parse_err res = lnm_http_req_parse( + &ctx->req, &conn->r.buf[conn->r.read], conn->r.size - conn->r.read); + + switch (res) { + case lnm_http_parse_err_ok: + ctx->state = lnm_http_loop_state_steps; + break; + case lnm_http_parse_err_incomplete: + // If the request is already the size of the read buffer, we close the + // request. Otherwise, we wait for anything read + if (conn->r.size - conn->r.read == LNM_LOOP_BUF_SIZE) { + conn->state = lnm_loop_state_end; + } + break; + case lnm_http_parse_err_invalid: + conn->state = lnm_loop_state_end; + break; + case lnm_http_parse_err_unknown_method: + // TODO set status code here + conn->state = lnm_loop_state_end; + break; + } +} + +void lnm_http_loop_process_route(lnm_http_conn *conn) {} + +void lnm_http_loop_process_steps(lnm_http_conn *conn) { + /* lnm_http_loop_ctx *ctx = conn->ctx; */ + + /* while () */ +} + +void (*process_fns[])(lnm_http_conn *conn) = { + lnm_http_loop_process_parse_req, + lnm_http_loop_process_route, + lnm_http_loop_process_steps, +}; + +void lnm_http_loop_process(lnm_http_conn *conn) { + lnm_http_loop_ctx *ctx = conn->ctx; + + lnm_http_loop_state cur_state; + + // As long as the processing is able to advance to a next state, we keep + // progressing + do { + cur_state = ctx->state; + + process_fns[cur_state](conn); + } while (conn->state == lnm_loop_state_req && cur_state != ctx->state); +}