feat: rename some structs

Jef Roosens 2024-02-12 13:40:56 +01:00
parent 321e9c0b80
commit 5b612127b2
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 52 additions and 24 deletions

View File

@ -17,7 +17,7 @@
* Internal representation of a header in a request, defined using offsets * Internal representation of a header in a request, defined using offsets
* relative to the full buffer. * relative to the full buffer.
*/ */
typedef struct lnm_http_req_iheader { typedef struct lnm_http_req_ihdr {
struct { struct {
size_t o; size_t o;
size_t len; size_t len;
@ -26,21 +26,7 @@ typedef struct lnm_http_req_iheader {
size_t o; size_t o;
size_t len; size_t len;
} value; } value;
} lnm_http_req_iheader; } lnm_http_req_ihhr;
/**
* Represents an actual header value, with offsets already resolved.
*/
typedef struct lnm_http_req_header {
struct {
const char *s;
size_t len;
} name;
struct {
const char *s;
size_t len;
} value;
} lnm_http_req_header;
/** /**
* Represents the parsed HTTP request * Represents the parsed HTTP request
@ -63,7 +49,7 @@ typedef struct lnm_http_req {
size_t len; size_t len;
} query; } query;
struct { struct {
lnm_http_req_iheader arr[LNM_HTTP_MAX_REQ_HEADERS]; lnm_http_req_ihhr arr[LNM_HTTP_MAX_REQ_HEADERS];
size_t len; size_t len;
} headers; } headers;
struct { struct {
@ -98,6 +84,20 @@ lnm_http_parse_err lnm_http_req_parse(lnm_http_req *req, char *buf, size_t len);
*/ */
void lnm_http_req_reset(lnm_http_req *req); void lnm_http_req_reset(lnm_http_req *req);
/**
* Represents an actual header value, with offsets already resolved.
*/
typedef struct lnm_http_req_hdr {
struct {
const char *s;
size_t len;
} name;
struct {
const char *s;
size_t len;
} value;
} lnm_http_req_hdr;
/** /**
* Retrieve a known type header from the request. * Retrieve a known type header from the request.
* *
@ -108,7 +108,7 @@ void lnm_http_req_reset(lnm_http_req *req);
* @param req request to look for header in * @param req request to look for header in
* @param type type of header to look for * @param type type of header to look for
*/ */
lnm_err lnm_http_req_header_get(lnm_http_req_header *out, lnm_http_req *req, lnm_err lnm_http_req_header_get(lnm_http_req_hdr *out, lnm_http_req *req,
lnm_http_header type); lnm_http_header type);
/** /**
@ -121,7 +121,35 @@ lnm_err lnm_http_req_header_get(lnm_http_req_header *out, lnm_http_req *req,
* @param req request to look for header in * @param req request to look for header in
* @param name name of the header; matches case-insensitive * @param name name of the header; matches case-insensitive
*/ */
lnm_err lnm_http_req_header_get_s(lnm_http_req_header *out, lnm_http_req *req, lnm_err lnm_http_req_header_get_s(lnm_http_req_hdr *out, lnm_http_req *req,
const char *name); const char *name);
/**
* Represents a parameter in an HTTP header.
*/
typedef struct lnm_http_req_hdr_param {
struct {
const char *s;
size_t len;
} key;
struct {
const char *s;
size_t len;
} value;
} lnm_http_req_hdr_param;
/**
* Retrieve a specific key-value parameter from a header.
*
* Pointers retrieved from this function should never be used between step
* functions; simply request the header again if you need to.
*
* @param out output struct
* @param header header to look in
* @param key name of the parameter to return from the header
*/
lnm_err lnm_http_req_header_param(lnm_http_req_hdr_param *param,
const lnm_http_req_hdr *header,
const char *key);
#endif #endif

View File

@ -113,7 +113,7 @@ void lnm_http_loop_process_parse_headers(lnm_http_conn *conn) {
lnm_http_loop_ctx *ctx = conn->ctx; lnm_http_loop_ctx *ctx = conn->ctx;
lnm_http_req *req = &ctx->req; lnm_http_req *req = &ctx->req;
lnm_http_req_header header; lnm_http_req_hdr header;
if (lnm_http_req_header_get(&header, req, lnm_http_header_content_length) == if (lnm_http_req_header_get(&header, req, lnm_http_header_content_length) ==
lnm_err_ok) { lnm_err_ok) {
req->body.expected_len = lnm_atoi(header.value.s, header.value.len); req->body.expected_len = lnm_atoi(header.value.s, header.value.len);

View File

@ -30,7 +30,7 @@ lnm_http_step_err lnm_http_loop_step_auth(lnm_http_conn *conn) {
// If there's no API key, requests are always authorized // If there's no API key, requests are always authorized
bool authorized = ctx->g->api_key == NULL; bool authorized = ctx->g->api_key == NULL;
lnm_http_req_header header; lnm_http_req_hdr header;
if (!authorized && lnm_http_req_header_get_s(&header, &ctx->req, if (!authorized && lnm_http_req_header_get_s(&header, &ctx->req,
"X-Api-Key") == lnm_err_ok) { "X-Api-Key") == lnm_err_ok) {

View File

@ -88,17 +88,17 @@ void lnm_http_req_reset(lnm_http_req *req) {
memset(req, 0, sizeof(lnm_http_req)); memset(req, 0, sizeof(lnm_http_req));
} }
lnm_err lnm_http_req_header_get(lnm_http_req_header *out, lnm_http_req *req, lnm_err lnm_http_req_header_get(lnm_http_req_hdr *out, lnm_http_req *req,
lnm_http_header type) { lnm_http_header type) {
return lnm_http_req_header_get_s(out, req, lnm_http_header_names[type]); return lnm_http_req_header_get_s(out, req, lnm_http_header_names[type]);
} }
lnm_err lnm_http_req_header_get_s(lnm_http_req_header *out, lnm_http_req *req, lnm_err lnm_http_req_header_get_s(lnm_http_req_hdr *out, lnm_http_req *req,
const char *name) { const char *name) {
size_t name_len = strlen(name); size_t name_len = strlen(name);
for (size_t i = 0; i < req->headers.len; i++) { for (size_t i = 0; i < req->headers.len; i++) {
const lnm_http_req_iheader *header = &req->headers.arr[i]; const lnm_http_req_ihhr *header = &req->headers.arr[i];
if (lnm_strnieq(req->buf.s + header->name.o, header->name.len, name, if (lnm_strnieq(req->buf.s + header->name.o, header->name.len, name,
name_len)) { name_len)) {