feat(lnm): start of processing code
ci/woodpecker/push/build Pipeline was successful Details

lnm
Jef Roosens 2023-11-27 19:54:53 +01:00
parent 4c2b85d436
commit e04f6e170e
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
5 changed files with 87 additions and 6 deletions

View File

@ -1,19 +1,26 @@
#ifndef LNM_HTTP_LOOP #ifndef LNM_HTTP_LOOP
#define LNM_HTTP_LOOP #define LNM_HTTP_LOOP
#include "lnm/common.h" #include <stdlib.h>
#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_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_step lnm_http_step;
typedef struct lnm_http_route lnm_http_route; 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`. * 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 { typedef enum lnm_http_loop_state {
lnm_http_loop_state_parse_req = 0, lnm_http_loop_state_parse_req = 0,
lnm_http_loop_state_route,
lnm_http_loop_state_steps,
} lnm_http_loop_state; } 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 { typedef struct lnm_http_loop_ctx {
lnm_http_loop_state state; lnm_http_loop_state state;
lnm_http_req req;
lnm_http_loop_gctx *g;
void *c;
} lnm_http_loop_ctx; } lnm_http_loop_ctx;
#endif #endif

View File

@ -6,7 +6,8 @@
#include "picohttpparser.h" #include "picohttpparser.h"
#include "lnm/http/consts.h" #include "lnm/http/consts.h"
#include "lnm/http/loop.h"
#define LNM_HTTP_MAX_REQ_HEADERS 32
/** /**
* Represents the parsed HTTP request * Represents the parsed HTTP request

View File

@ -15,7 +15,7 @@ typedef enum {
lnm_loop_state_end, lnm_loop_state_end,
} lnm_loop_state; } lnm_loop_state;
typedef struct { typedef struct lnm_loop_conn {
int fd; int fd;
lnm_loop_state state; lnm_loop_state state;
void *ctx; void *ctx;

View File

View File

@ -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);
}