feat(lnm): add request parser
All checks were successful
ci/woodpecker/push/build Pipeline was successful

This commit is contained in:
Jef Roosens 2023-11-27 13:50:08 +01:00
parent e59ee38ae2
commit 4c2b85d436
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
5 changed files with 134 additions and 14 deletions

View file

@ -12,7 +12,7 @@ typedef enum lnm_http_method {
http_method_put,
http_method_patch,
http_method_delete
} http_method;
} lnm_http_method;
extern const char *lnm_http_status_names[][32];

View file

@ -3,9 +3,11 @@
#include "lnm/common.h"
#define LNM_HTTP_MAX_REQ_HEADERS 32
typedef struct lnm_loop lnm_http_loop;
typedef struct lnm_http_conn lnm_http_conn;
typedef struct lnm_conn lnm_http_conn;
typedef struct lnm_http_step lnm_http_step;
@ -20,14 +22,6 @@ typedef lnm_err (*lnm_http_step_fn)(lnm_http_conn *conn);
*/
lnm_err lnm_http_loop_init(lnm_http_loop **out);
/**
* Initialize a first step
*
* @param out where to store pointer to new `lnm_http_step`
* @param fn step function associated with the step
*/
lnm_err lnm_http_step_init(lnm_http_step **out, lnm_http_step_fn fn);
/**
* Append the given step fn to the step.
*
@ -67,4 +61,15 @@ lnm_err lnm_http_route_init_regex(lnm_http_route **out, const char *pattern,
*/
void lnm_http_loop_route_add(lnm_http_loop *hl, lnm_http_route *route);
/**
* Represents what state an HTTP loop request is currently in.
*/
typedef enum lnm_http_loop_state {
lnm_http_loop_state_parse_req = 0,
} lnm_http_loop_state;
typedef struct lnm_http_loop_ctx {
lnm_http_loop_state state;
} lnm_http_loop_ctx;
#endif

View file

@ -0,0 +1,48 @@
#ifndef LNM_HTTP_REQ
#define LNM_HTTP_REQ
#include <stdlib.h>
#include "picohttpparser.h"
#include "lnm/http/consts.h"
#include "lnm/http/loop.h"
/**
* Represents the parsed HTTP request
*/
typedef struct lnm_http_req {
size_t len;
int minor_version;
lnm_http_method method;
struct {
const char *s;
size_t len;
} path;
struct {
const char *s;
size_t len;
} query;
struct {
struct phr_header arr[LNM_HTTP_MAX_REQ_HEADERS];
size_t len;
} headers;
} lnm_http_req;
typedef enum lnm_http_parse_err {
lnm_http_parse_err_ok = 0,
lnm_http_parse_err_incomplete,
lnm_http_parse_err_invalid,
lnm_http_parse_err_unknown_method,
} lnm_http_parse_err;
/**
* Try to parse the given buffer into an HTTP request.
*
* @param req request to store parsed data in
* @param buf buffer to parse; might be modified
* @param len length of buf
*/
lnm_http_parse_err lnm_http_req_parse(lnm_http_req *req, char *buf, size_t len);
#endif