120 lines
3.2 KiB
C
120 lines
3.2 KiB
C
#ifndef LNM_HTTP_RES
|
|
#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;
|
|
size_t len;
|
|
bool owned;
|
|
} name;
|
|
struct {
|
|
char *s;
|
|
size_t len;
|
|
bool owned;
|
|
} value;
|
|
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;
|
|
uint64_t len;
|
|
bool owned;
|
|
lnm_http_res_body_type type;
|
|
} body;
|
|
// General-purpose; meaning depends on the current state
|
|
uint64_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);
|
|
|
|
/**
|
|
* Reset the given response object, properly free'ing any allocated buffers,
|
|
* allowing it to be reused for later connections.
|
|
*
|
|
* @param res res to reset
|
|
*/
|
|
void lnm_http_res_reset(lnm_http_res *res);
|
|
|
|
#endif
|