feat(lnm): implement content-length header parsing
All checks were successful
ci/woodpecker/push/build Pipeline was successful

This commit is contained in:
Jef Roosens 2023-12-02 16:28:52 +01:00
parent 3c1e62330c
commit 13ccfef94d
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
5 changed files with 150 additions and 3 deletions

View file

@ -1,6 +1,10 @@
#ifndef LNM_COMMON
#define LNM_COMMON
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#define LNM_RES(x) \
{ \
lnm_err res = x; \
@ -20,13 +24,14 @@
#define LNM_MIN(x, y) ((x) < (y) ? (x) : (y))
#define LNM_MAX(x, y) ((x) > (y) ? (x) : (y))
typedef enum {
typedef enum lnm_err {
lnm_err_ok = 0,
lnm_err_failed_alloc,
lnm_err_failed_network,
lnm_err_failed_poll,
lnm_err_not_setup,
lnm_err_bad_regex
lnm_err_bad_regex,
lnm_err_not_found,
} lnm_err;
typedef struct lnm_loop lnm_http_loop;
@ -37,4 +42,41 @@ typedef struct lnm_http_step lnm_http_step;
typedef struct lnm_http_route lnm_http_route;
/**
* Returns whether the two strings are equal.
*
* @param s1 first string to compare
* @param s1_len length of `s1`
* @param s2 second string to compare
* @param s2_len length of `s2`
*/
bool lnm_strneq(const char *s1, size_t s1_len, const char *s2, size_t s2_len);
/**
* Returns whether the two strings are equal, ignoring capitalisation.
*
* @param s1 first string to compare
* @param s1_len length of `s1`
* @param s2 second string to compare
* @param s2_len length of `s2`
*/
bool lnm_strnieq(const char *s1, size_t s1_len, const char *s2, size_t s2_len);
/**
* Calculate integer exponentation.
*
* @param base
* @param power
*/
uint64_t lnm_ipow(uint64_t base, uint64_t power);
/**
* Parse the given string into a number.
*
* @param s string to parse
* @param len length of s
* @return the parsed number, or 0 if the number is invalid
*/
uint64_t lnm_atoi(const char *s, size_t len);
#endif

View file

@ -2,10 +2,12 @@
#define LNM_HTTP_REQ
#include <regex.h>
#include <stdint.h>
#include <stdlib.h>
#include "picohttpparser.h"
#include "lnm/common.h"
#include "lnm/http/consts.h"
#define LNM_HTTP_MAX_REQ_HEADERS 32
@ -31,6 +33,7 @@ typedef struct lnm_http_req {
struct phr_header arr[LNM_HTTP_MAX_REQ_HEADERS];
size_t len;
} headers;
uint64_t content_length;
} lnm_http_req;
typedef enum lnm_http_parse_err {
@ -57,4 +60,26 @@ 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);
/**
* Retrieve a specific header from the request.
*
* @param out where to write pointer to header value
* @param out_len where to store length of out value
* @param req request to look for header in
* @param type type of header to look for
*/
lnm_err lnm_http_req_header_get(const char **out, size_t *out_len,
lnm_http_req *req, lnm_http_header type);
/**
* Retrieve a specific header from the request by specifying its name.
*
* @param out where to write pointer to header value
* @param out_len where to store length of out value
* @param req request to look for header in
* @param name name of the header; matches case-insensitive
*/
lnm_err lnm_http_req_header_get_s(const char **out, size_t *out_len,
lnm_http_req *req, const char *name);
#endif