feat: abstract http body

This commit is contained in:
Jef Roosens 2023-05-31 11:46:34 +02:00
parent a6257a923d
commit bbfea5876e
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
9 changed files with 100 additions and 75 deletions

View file

@ -23,14 +23,7 @@ typedef struct http_request {
size_t path_len;
const char *query;
size_t query_len;
http_body_type body_type;
union {
char *buf;
FILE *file;
} body;
size_t body_len;
size_t body_received;
char *body_file_name;
http_body body;
regmatch_t regex_groups[HTTP_MAX_REGEX_GROUPS];
struct phr_header headers[HTTP_MAX_ALLOWED_HEADERS];
size_t num_headers;

View file

@ -17,15 +17,7 @@ typedef struct http_response {
const char *head;
size_t head_len;
size_t head_written;
http_body_type body_type;
union {
char *buf;
FILE *file;
} body;
size_t body_len;
size_t body_written;
// If false, the body won't be freed
bool owns_body;
http_body body;
http_response_header headers[4];
size_t header_count;
} http_response;

View file

@ -2,6 +2,8 @@
#define LANDER_HTTP_TYPES
#include <sys/types.h>
#include <stdio.h>
#include <stdbool.h>
// Array mapping the http_request_method enum to strings
extern const char *http_method_names[];
@ -129,4 +131,26 @@ typedef enum http_body_type {
http_body_file = 1
} http_body_type;
typedef struct http_body {
http_body_type type;
char *buf;
bool buf_owned;
FILE *file;
char *fname;
bool fname_owned;
// In the context of a request, expected_len is the content of the request's
// Content-Length header, and len is how many bytes have already been
// received.
// In the context of a response, expected_len is the actual length of the
// body, and len is how many have been written.
size_t expected_len;
size_t len;
} http_body;
http_body *http_body_init();
void http_body_reset(http_body *body);
void http_body_free(http_body *body);
#endif