lander/lnm/include/lnm/http/req.h

112 lines
2.7 KiB
C

#ifndef LNM_HTTP_REQ
#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
#define LNM_HTTP_MAX_REGEX_GROUPS 4
typedef struct lnm_http_req_header {
struct {
size_t o;
size_t len;
} name;
struct {
size_t o;
size_t len;
} value;
} lnm_http_req_header;
/**
* Represents the parsed HTTP request
*/
typedef struct lnm_http_req {
struct {
char *s;
size_t len;
bool owned;
} buf;
int minor_version;
lnm_http_method method;
struct {
size_t o;
size_t len;
regmatch_t groups[LNM_HTTP_MAX_REGEX_GROUPS];
} path;
struct {
size_t o;
size_t len;
} query;
struct {
lnm_http_req_header arr[LNM_HTTP_MAX_REQ_HEADERS];
size_t len;
} headers;
struct {
uint64_t expected_len;
uint64_t len;
char *buf;
bool owned;
} body;
} 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);
/**
* Reset the given request object, free'ing all its relevant parts and allowing
* it to be reused as a new object.
*
* @param req object to reset
*/
void lnm_http_req_reset(lnm_http_req *req);
/**
* Retrieve a specific header from the request.
*
* Pointers retrieved from this function should never be used between step
* functions; simply request the header again if you need to.
*
* @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.
*
* Pointers retrieved from this function should never be used between step
* functions; simply request the header again if you need to.
*
* @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