Format stuff
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
;;; Directory Local Variables -*- no-byte-compile: t -*-
|
||||
;;; For more information see (info "(emacs) Directory Variables")
|
||||
|
||||
((nil . ((eval . (add-to-list (make-local-variable 'exec-path) "/usr/lib/llvm18/bin/"))))
|
||||
(c-ts-base-mode . ((apheleia-formatter . clang-format))))
|
||||
@@ -7,7 +7,8 @@
|
||||
|
||||
static const char* EMPTY_STRING = "";
|
||||
|
||||
HTTPHeaderList *http_header_list_push(HTTPHeaderList *list, const char *key, const char *value) {
|
||||
HTTPHeaderList* http_header_list_push(HTTPHeaderList* list, const char* key, const char* value)
|
||||
{
|
||||
HTTPHeaderList* new = malloc(sizeof(HTTPHeaderList));
|
||||
if (!new) {
|
||||
return NULL;
|
||||
@@ -31,7 +32,8 @@ HTTPHeaderList *http_header_list_push(HTTPHeaderList *list, const char *key, con
|
||||
return new;
|
||||
}
|
||||
|
||||
void free_http_header_list(HTTPHeaderList *list) {
|
||||
void free_http_header_list(HTTPHeaderList* list)
|
||||
{
|
||||
while (list) {
|
||||
HTTPHeaderList* next = list->next;
|
||||
free(list->key);
|
||||
@@ -41,7 +43,8 @@ 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;
|
||||
@@ -65,7 +68,8 @@ const char *http_header_list_search(HTTPHeaderList *list, const char *key, const
|
||||
|
||||
// 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) {
|
||||
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];
|
||||
@@ -110,7 +114,8 @@ static HTTPRequestParseResult parse_method_uri_line(
|
||||
// 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) {
|
||||
HTTPRequestParseResult* restrict res, HTTPHeaderList* restrict* restrict list)
|
||||
{
|
||||
char c = fgetc(stream);
|
||||
RETURN_IF_READ_ERROR(stream);
|
||||
if (c == '\r') {
|
||||
@@ -154,7 +159,8 @@ 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;
|
||||
@@ -174,7 +180,8 @@ HTTPRequestParseResult parse_http_request(FILE *stream, HTTPRequest *restrict ou
|
||||
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);
|
||||
}
|
||||
@@ -184,13 +191,15 @@ void free_http_request(HTTPRequest *restrict req) {
|
||||
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;
|
||||
size_t size;
|
||||
} CODES[] = {
|
||||
#define P(s, m) { s, m, sizeof(m) - 1 }
|
||||
#define P(s, m) \
|
||||
{ s, m, sizeof(m) - 1 }
|
||||
P(200, "OK"),
|
||||
P(201, "Created"),
|
||||
P(400, "Bad Request"),
|
||||
@@ -213,7 +222,8 @@ 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));
|
||||
dprintf(fileno(stream), "HTTP/1.1 %d %s\r\n", resp->status,
|
||||
status_code_to_message(resp->status, NULL));
|
||||
|
||||
Reference in New Issue
Block a user