Update stuff

This commit is contained in:
2026-05-25 23:13:27 -07:00
parent 5d631b594f
commit 5dfbdc43c0
7 changed files with 133 additions and 168 deletions
+33 -50
View File
@@ -5,12 +5,10 @@
#include <string.h>
#include <unistd.h>
static const char* EMPTY_STRING = "";
static const char *EMPTY_STRING = "";
HTTPHeaderList* http_header_list_push(HTTPHeaderList* list, const char* key,
const char* value)
{
HTTPHeaderList* new = malloc(sizeof(HTTPHeaderList));
HTTPHeaderList *http_header_list_push(HTTPHeaderList *list, const char *key, const char *value) {
HTTPHeaderList *new = malloc(sizeof(HTTPHeaderList));
if (!new) {
return NULL;
}
@@ -33,10 +31,9 @@ HTTPHeaderList* http_header_list_push(HTTPHeaderList* list, const char* key,
return new;
}
void free_http_header_list(HTTPHeaderList* list)
{
void free_http_header_list(HTTPHeaderList *list) {
while (list) {
HTTPHeaderList* next = list->next;
HTTPHeaderList *next = list->next;
free(list->key);
free(list->value);
free(list);
@@ -44,9 +41,7 @@ void free_http_header_list(HTTPHeaderList* list)
}
}
const char* http_header_list_search(HTTPHeaderList* list, const char* key,
const char* def)
{
const char *http_header_list_search(HTTPHeaderList *list, const char *key, const char *def) {
while (list) {
if (strcmp(list->key, key) == 0) {
return list->value;
@@ -56,23 +51,21 @@ const char* http_header_list_search(HTTPHeaderList* list, const char* key,
return def;
}
#define MAX_REQUEST_LENGTH 16384
#define MAX_METHOD_LENGTH 16
#define MAX_URI_LENGTH 256
#define MAX_VERSION_LENGTH 3
#define MAX_HEADER_KEY_LENGTH 2048
#define MAX_REQUEST_LENGTH 16384
#define MAX_METHOD_LENGTH 16
#define MAX_URI_LENGTH 256
#define MAX_VERSION_LENGTH 3
#define MAX_HEADER_KEY_LENGTH 2048
#define MAX_HEADER_VALUE_LENGTH 2048
#define RETURN_IF_READ_ERROR(s) \
if (ferror((s))) { \
return HRPR_READ_FAILED; \
#define RETURN_IF_READ_ERROR(s) \
if (ferror((s))) { \
return HRPR_READ_FAILED; \
}
// if an error occured, req->uri is not allocated
static HTTPRequestParseResult parse_method_uri_line(FILE* stream,
size_t* restrict bytes_read,
HTTPRequest* restrict req)
{
static HTTPRequestParseResult parse_method_uri_line(
FILE *stream, size_t *restrict bytes_read, HTTPRequest *restrict req) {
// allow for some leeway in passing incorrect methods
char method[MAX_METHOD_LENGTH + 1];
char uri[MAX_URI_LENGTH + 1];
@@ -80,9 +73,8 @@ static HTTPRequestParseResult parse_method_uri_line(FILE* stream,
char whitespace[4];
ssize_t signed_bytes_read;
#define S1(s) #s
#define S(s) S1(s)
int nconv = fscanf(
stream,
#define S(s) S1(s)
int nconv = fscanf(stream,
// clang-format off
"%" S(MAX_METHOD_LENGTH) "[A-Z]"
"%c"
@@ -92,8 +84,8 @@ static HTTPRequestParseResult parse_method_uri_line(FILE* stream,
"%c%c"
"%zn",
// clang-format on
method, &whitespace[0], uri, &whitespace[1], version_str,
&whitespace[2], &whitespace[3], &signed_bytes_read);
method, &whitespace[0], uri, &whitespace[1], version_str, &whitespace[2], &whitespace[3],
&signed_bytes_read);
#undef S
*bytes_read = signed_bytes_read;
RETURN_IF_READ_ERROR(stream);
@@ -117,10 +109,8 @@ static HTTPRequestParseResult parse_method_uri_line(FILE* stream,
// return true if there are more headers and no error occurred, false otherwise
// this will *not* free LIST if an error occurs
static bool next_header(FILE* stream, size_t* restrict bytes_read,
HTTPRequestParseResult* restrict res,
HTTPHeaderList* restrict* restrict list)
{
static bool next_header(FILE *stream, size_t *restrict bytes_read,
HTTPRequestParseResult *restrict res, HTTPHeaderList *restrict *restrict list) {
char c = fgetc(stream);
RETURN_IF_READ_ERROR(stream);
if (c == '\r') {
@@ -140,7 +130,7 @@ static bool next_header(FILE* stream, size_t* restrict bytes_read,
char whitespace[3];
ssize_t signed_bytes_read;
#define S1(s) #s
#define S(s) S1(s)
#define S(s) S1(s)
int nconv = fscanf(stream,
// clang-format off
"%" S(MAX_HEADER_KEY_LENGTH) "[a-zA-Z0-9.-]"
@@ -149,8 +139,7 @@ static bool next_header(FILE* stream, size_t* restrict bytes_read,
"%c%c"
"%zn",
// clang-format on
key, &whitespace[0], value, &whitespace[1], &whitespace[2],
&signed_bytes_read);
key, &whitespace[0], value, &whitespace[1], &whitespace[2], &signed_bytes_read);
#undef S
*bytes_read = signed_bytes_read;
RETURN_IF_READ_ERROR(stream);
@@ -165,9 +154,7 @@ static bool next_header(FILE* stream, size_t* restrict bytes_read,
return true;
}
HTTPRequestParseResult parse_http_request(FILE* stream,
HTTPRequest* restrict out)
{
HTTPRequestParseResult parse_http_request(FILE *stream, HTTPRequest *restrict out) {
out->uri = EMPTY_STRING;
out->path = EMPTY_STRING;
out->method = EMPTY_STRING;
@@ -187,22 +174,20 @@ HTTPRequestParseResult parse_http_request(FILE* stream,
return res;
}
void free_http_request(HTTPRequest* restrict req)
{
void free_http_request(HTTPRequest *restrict req) {
if (req->method != EMPTY_STRING) {
free((char*)req->method);
free((char *) req->method);
}
if (req->uri != EMPTY_STRING) {
free((char*)req->uri);
free((char *) req->uri);
}
free_http_header_list(req->headers);
}
const char* status_code_to_message(int status, size_t* restrict length)
{
const char *status_code_to_message(int status, size_t *restrict length) {
static const struct {
int code;
const char* msg;
const char *msg;
size_t size;
} CODES[] = {
#define P(s, m) { s, m, sizeof(m) - 1 }
@@ -228,13 +213,11 @@ const char* status_code_to_message(int status, size_t* restrict length)
return NULL;
}
void format_http_response(FILE* stream, HTTPResponse* restrict resp)
{
void format_http_response(FILE *stream, HTTPResponse *restrict resp) {
assert(status_code_to_message(resp->status, NULL));
fprintf(stream, "HTTP/1.1 %d %s\r\n", resp->status,
status_code_to_message(resp->status, NULL));
fprintf(stream, "HTTP/1.1 %d %s\r\n", resp->status, status_code_to_message(resp->status, NULL));
fprintf(stream, "Content-Length: %zu\r\n", resp->body_length);
for (HTTPHeaderList* h = resp->headers; h; h = h->next) {
for (HTTPHeaderList *h = resp->headers; h; h = h->next) {
fwrite(h->key, 1, h->key_length, stream);
fwrite(": ", 1, 2, stream);
fwrite(h->value, 1, h->value_length, stream);