feat: start of http part of server

This commit is contained in:
Jef Roosens 2023-05-25 12:01:20 +02:00
parent 6d45ec59dc
commit c58e53eb2a
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
7 changed files with 91 additions and 29 deletions

View file

@ -74,34 +74,31 @@ bool event_loop_handle_request(event_loop_conn *conn) {
&minor_version, headers, &num_headers, 0);
if (res > 0) {
/* conn->close_after_write = true; */
// TODO allow HTTP pipelining
/* conn->close_after_write = true; */
for (int i = 0; i < num_headers; i++) {
/* printf("%.*s: ", headers[i].name_len, headers[i].name); */
/* printf("%.*s\n", headers[i].value_len, headers[i].value); */
if (strncmp("Connection", headers[i].name, headers[i].name_len) == 0) {
if (strncmp("close", headers[i].value, headers[i].value_len) == 0) {
conn->close_after_write = true;
break;
}
}
}
/* for (size_t i = 0; i < num_headers; i++) { */
/* /1* printf("%.*s: ", headers[i].name_len, headers[i].name); *1/ */
/* /1* printf("%.*s\n", headers[i].value_len, headers[i].value); *1/ */
/* if (strncmp("Connection", headers[i].name, headers[i].name_len) == 0) { */
/* if (strncmp("close", headers[i].value, headers[i].value_len) == 0) { */
/* conn->close_after_write = true; */
/* break; */
/* } */
/* } */
/* } */
memcpy(conn->wbuf, http_200_ok, sizeof(http_200_ok) - 1);
/* memcpy(conn->wbuf, http_500, http_500_len); */
// Move the new request up to the front of the read buffer
memmove(conn->rbuf, &conn->rbuf[res], conn->rbuf_size - res);
conn->rbuf_size -= res;
/* // Move the new request up to the front of the read buffer */
/* memmove(conn->rbuf, &conn->rbuf[res], conn->rbuf_size - res); */
/* conn->rbuf_size -= res; */
conn->state = event_loop_conn_state_res;
conn->wbuf_size = sizeof(http_200_ok) - 1;
conn->wbuf_sent = 0;
/* conn->state = event_loop_conn_state_res; */
/* conn->wbuf_size = http_500_len; */
/* conn->wbuf_sent = 0; */
event_loop_conn_io_res(conn);
/* /1* event_loop_conn_io_res(conn); *1/ */
// End the connection if the http request is invalid, or when the response
// is too large to fit into a single read buffer
/* // End the connection if the http request is invalid, or when the response */
/* // is too large to fit into a single read buffer */
} else if (res == -1 ||
(res == -2 && conn->rbuf_size == EVENT_LOOP_BUFFER_SIZE)) {
conn->state = event_loop_conn_state_end;

View file

@ -1,11 +0,0 @@
#include <stdlib.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_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

View file

@ -53,9 +53,4 @@ int event_loop_accept(event_loop *loop, int fd);
void event_loop_conn_io(event_loop_conn *conn);
extern const char http_404[];
extern const size_t http_404_len;
extern const char http_500[];
extern const size_t http_500_len;
#endif