feat(lnm): add most of the writing code
All checks were successful
ci/woodpecker/push/build Pipeline was successful

This commit is contained in:
Jef Roosens 2023-12-01 22:00:49 +01:00
parent 77b62825a6
commit e8bb089f5c
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
5 changed files with 273 additions and 11 deletions

View file

@ -17,6 +17,9 @@
} \
}
#define LNM_MIN(x, y) ((x) < (y) ? (x) : (y))
#define LNM_MAX(x, y) ((x) > (y) ? (x) : (y))
typedef enum {
lnm_err_ok = 0,
lnm_err_failed_alloc,
@ -26,4 +29,12 @@ typedef enum {
lnm_err_bad_regex
} lnm_err;
typedef struct lnm_loop lnm_http_loop;
typedef struct lnm_loop_conn lnm_http_conn;
typedef struct lnm_http_step lnm_http_step;
typedef struct lnm_http_route lnm_http_route;
#endif

View file

@ -7,14 +7,6 @@
#include "lnm/http/req.h"
#include "lnm/http/res.h"
typedef struct lnm_loop lnm_http_loop;
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 enum lnm_http_step_err {
lnm_http_step_err_done = 0,
lnm_http_step_err_io_needed,

View file

@ -2,9 +2,17 @@
#define LNM_HTTP_RES
#include <stdbool.h>
#include <stdio.h>
#include "lnm/common.h"
#include "lnm/http/consts.h"
typedef lnm_err (*data_fn)(size_t *written, char *buf, lnm_http_conn *conn,
size_t offset, size_t len);
/**
* Linked list elements used to store the response headers
*/
typedef struct lnm_http_res_header {
struct {
char *s;
@ -19,12 +27,85 @@ typedef struct lnm_http_res_header {
struct lnm_http_res_header *next;
} lnm_http_res_header;
typedef enum lnm_http_res_body_type {
lnm_http_res_body_type_file = 0,
lnm_http_res_body_type_buf,
lnm_http_res_body_type_fn,
} lnm_http_res_body_type;
typedef struct lnm_http_res {
lnm_http_status status;
struct {
lnm_http_res_header *head;
lnm_http_res_header *current;
} headers;
struct {
struct {
char *buf;
FILE *f;
data_fn fn;
} data;
size_t len;
bool owned;
lnm_http_res_body_type type;
} body;
// General-purpose; meaning depends on the current state
size_t written;
} lnm_http_res;
/**
* Add a new header of a known type to the response
*
* @param type type of header
* @param value null-terminated string containing the value of the header
* @param value_owned whether to take ownership of the value pointer; if false,
* free'ing the buffer is the caller's responsibility
*/
lnm_err lnm_http_res_add_header(lnm_http_res *res, lnm_http_header type,
char *value, bool value_owned);
/**
* Add a new header of a known type to the response with a given value length.
*
* @param type type of header
* @param value string of length `value_len` containing the value of the header
* @param value_len length of value
* @param value_owned whether to take ownership of the value pointer; if false,
* free'ing the buffer is the caller's responsibility
*/
lnm_err lnm_http_res_add_header_len(lnm_http_res *res, lnm_http_header type,
char *value, size_t value_len,
bool value_owned);
/**
* Set the request body to the given file pointer.
*
* @param res response to modify
* @param f file pointer to use as data
* @param len expected length of the file
* @param owned whether to take ownership of the file pointer
*/
void lnm_http_res_body_set_file(lnm_http_res *res, FILE *f, size_t len,
bool owned);
/**
* Set the request body to the given buffer.
*
* @param res response to modify
* @param buf buffer to use as data
* @param len length of the buffer
* @param owned whether to take ownership of the file pointer
*/
void lnm_http_res_body_set_buf(lnm_http_res *res, char *buf, size_t len,
bool owned);
/**
* Set the request body to be read from the given data function.
*
* @param res response to modify
* @param fn data reader function
* @param len expected length of the response
*/
void lnm_http_res_body_set_fn(lnm_http_res *res, data_fn fn, size_t len);
#endif