feat: decouple event loop from http

This commit is contained in:
Jef Roosens 2023-05-27 11:47:39 +02:00
parent f3fced908f
commit 68f9227436
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
12 changed files with 136 additions and 112 deletions

View file

@ -1,16 +0,0 @@
#include "http.h"
const char http_404[] = "HTTP/1.1 404 Not Found\n"
"Connection: close\n"
"Content-Length: 0\n\n";
const size_t http_404_len = sizeof(http_404) - 1;
const char http_405[] = "HTTP/1.1 405 Method Not Allowed\n"
"Connection: close\n"
"Content-Length: 0\n\n";
const size_t http_405_len = sizeof(http_405) - 1;
const char http_500[] = "HTTP/1.1 500 Internal Server Error\n"
"Connection: close\n"
"Content-Length: 0\n\n";
const size_t http_500_len = sizeof(http_500) - 1;

View file

@ -1,25 +0,0 @@
#include "http.h"
#include "picohttpparser.h"
/*
* given the HTTP path, parse the request
*/
http_parse_error http_parse_request(http_request *req, const char *buf,
size_t buf_size) {
// First we try to parse the incoming HTTP request
size_t num_headers = HTTP_MAX_ALLOWED_HEADERS;
int res = phr_parse_request(buf, buf_size, &req->method, &req->method_len,
&req->path, &req->path_len, &req->minor_version,
req->headers, &num_headers, 0);
if (res == -1) {
return http_parse_error_invalid;
} else if (res == -2) {
return http_parse_error_incomplete;
}
req->len = res;
return http_parse_error_ok;
}

View file

@ -1,13 +0,0 @@
#include <string.h>
#include "event_loop.h"
#include "http.h"
typedef void (*routing_func)(event_loop_conn *);
void http_route(event_loop_conn *conn) {
// TODO routing
// Fallthrough is to return a 404
http_write_standard_response(conn, http_not_found);
}

View file

@ -1,23 +0,0 @@
#include "http.h"
void http_write_standard_response(event_loop_conn *conn,
http_response_type type) {
const char *s;
size_t len;
switch (type) {
case 404:
s = http_404;
len = http_404_len;
break;
case 405:
s = http_405;
len = http_405_len;
}
memcpy(conn->wbuf, s, len);
conn->state = event_loop_conn_state_res;
conn->wbuf_size = len;
conn->wbuf_sent = 0;
}