feat: inching our way towards http routing
This commit is contained in:
parent
5dc772e507
commit
6d4b94c55e
8 changed files with 126 additions and 135 deletions
|
|
@ -10,7 +10,7 @@
|
|||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "event_loop_internal.h"
|
||||
#include "event_loop.h"
|
||||
#include "log.h"
|
||||
#include "picohttpparser.h"
|
||||
|
||||
|
|
|
|||
|
|
@ -11,9 +11,9 @@
|
|||
#include <unistd.h>
|
||||
|
||||
#include "picohttpparser.h"
|
||||
#include "http.h"
|
||||
|
||||
#include "event_loop_internal.h"
|
||||
#include "event_loop.h"
|
||||
#include "http.h"
|
||||
|
||||
void event_loop_conn_io_res(event_loop_conn *conn) {
|
||||
while (1) {
|
||||
|
|
@ -59,58 +59,34 @@ bool event_loop_handle_request(event_loop_conn *conn) {
|
|||
return false;
|
||||
}
|
||||
|
||||
/* if (conn->process_func != NULL) { */
|
||||
/* conn->process_func(conn); */
|
||||
/* } */
|
||||
http_parse_error res =
|
||||
http_parse_request(&conn->req, (const char *)&conn->rbuf[conn->rbuf_read],
|
||||
conn->rbuf_size - conn->rbuf_read);
|
||||
|
||||
const char *method, *path;
|
||||
struct phr_header headers[16];
|
||||
size_t method_len, path_len, num_headers;
|
||||
int minor_version;
|
||||
|
||||
num_headers = sizeof(headers) / sizeof(headers[0]);
|
||||
|
||||
int res = phr_parse_request((const char *)&conn->rbuf[conn->rbuf_read], conn->rbuf_size - conn->rbuf_read,
|
||||
&method, &method_len, &path, &path_len,
|
||||
&minor_version, headers, &num_headers, 0);
|
||||
|
||||
if (res > 0) {
|
||||
/* 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; */
|
||||
/* } */
|
||||
/* } */
|
||||
/* } */
|
||||
|
||||
// Advance the read buffer
|
||||
conn->rbuf_read += res;
|
||||
if (res == http_parse_error_ok) {
|
||||
conn->rbuf_read += conn->req.len;
|
||||
|
||||
memcpy(conn->wbuf, http_404, http_404_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; */
|
||||
|
||||
conn->state = event_loop_conn_state_res;
|
||||
conn->wbuf_size = http_404_len;
|
||||
conn->wbuf_sent = 0;
|
||||
|
||||
event_loop_conn_io_res(conn);
|
||||
/* event_loop_conn_io_res(conn); */
|
||||
|
||||
/* // 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)) {
|
||||
}
|
||||
// Both in the case of an invalid HTTP request or one that's larger than the
|
||||
// read buffer, we cannot determine when the next, possibly valid, HTTP
|
||||
// request begins in the data stream. Therefore, we close the connection,
|
||||
// even if additional pipelined requests are incoming.
|
||||
else if (res == http_parse_error_invalid ||
|
||||
(res == http_parse_error_incomplete &&
|
||||
conn->rbuf_size == EVENT_LOOP_BUFFER_SIZE)) {
|
||||
conn->state = event_loop_conn_state_end;
|
||||
}
|
||||
|
||||
// TODO in highly concurrent situations, it might actually be better to always
|
||||
// return false here, as this allows cycling better through all connections
|
||||
return conn->state == event_loop_conn_state_req;
|
||||
}
|
||||
|
||||
|
|
@ -123,7 +99,8 @@ bool event_loop_handle_request(event_loop_conn *conn) {
|
|||
void event_loop_conn_io_req(event_loop_conn *conn) {
|
||||
do {
|
||||
// Move remaining data to start of buffer
|
||||
memmove(conn->rbuf, &conn->rbuf[conn->rbuf_read], conn->rbuf_size - conn->rbuf_read);
|
||||
memmove(conn->rbuf, &conn->rbuf[conn->rbuf_read],
|
||||
conn->rbuf_size - conn->rbuf_read);
|
||||
conn->rbuf_size -= conn->rbuf_read;
|
||||
conn->rbuf_read = 0;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,57 +0,0 @@
|
|||
#ifndef LANDER_EVENT_LOOP_INTERNAL
|
||||
#define LANDER_EVENT_LOOP_INTERNAL
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "event_loop.h"
|
||||
|
||||
typedef struct event_loop_conn {
|
||||
int fd;
|
||||
event_loop_conn_state state;
|
||||
// buffer for reading
|
||||
size_t rbuf_size;
|
||||
size_t rbuf_read;
|
||||
uint8_t rbuf[EVENT_LOOP_BUFFER_SIZE];
|
||||
// buffer for writing
|
||||
size_t wbuf_size;
|
||||
size_t wbuf_sent;
|
||||
uint8_t wbuf[EVENT_LOOP_BUFFER_SIZE];
|
||||
|
||||
// If true, the server will close the connection after the final write buffer
|
||||
// has been written
|
||||
bool close_after_write;
|
||||
/* void (*process_func)(struct event_loop_conn *); */
|
||||
} event_loop_conn;
|
||||
|
||||
/*
|
||||
* Initialize a new event_loop_conn struct
|
||||
*/
|
||||
event_loop_conn *event_loop_conn_init();
|
||||
|
||||
typedef struct event_loop {
|
||||
event_loop_conn **connections;
|
||||
size_t connection_count;
|
||||
} event_loop;
|
||||
|
||||
/*
|
||||
* Initialize a new event_loop struct
|
||||
*/
|
||||
event_loop *event_loop_init();
|
||||
|
||||
/*
|
||||
* Place a new connection into the event loop's internal array.
|
||||
*
|
||||
* Returns -1 if the internal realloc failed
|
||||
*/
|
||||
int event_loop_put(event_loop *loop, event_loop_conn *conn);
|
||||
|
||||
/**
|
||||
* Accept a new connection for the given file descriptor.
|
||||
*/
|
||||
int event_loop_accept(event_loop *loop, int fd);
|
||||
|
||||
void event_loop_conn_io(event_loop_conn *conn);
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue