feat: inching our way towards http routing

This commit is contained in:
Jef Roosens 2023-05-25 22:58:00 +02:00 committed by Chewing_Bever
parent 5dc772e507
commit 6d4b94c55e
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
8 changed files with 126 additions and 135 deletions

View file

@ -1,6 +1,12 @@
#ifndef LANDER_EVENT_LOOP
#define LANDER_EVENT_LOOP
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include "http_req.h"
// Size of the read and write buffers for each connection, in bytes
#define EVENT_LOOP_BUFFER_SIZE 1024
@ -30,4 +36,52 @@ event_loop *event_loop_init();
*/
void event_loop_run(event_loop *el, int port);
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 *); */
http_request req;
} 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

View file

@ -3,31 +3,16 @@
#include <stdlib.h>
#include "picohttpparser.h"
#include "event_loop.h"
#include "http_req.h"
extern const char http_404[];
extern const size_t http_404_len;
extern const char http_500[];
extern const size_t http_500_len;
typedef enum http_request_entity {
event_loop_request_type_unknown = 0
} http_request_entity;
typedef enum http_request_method {
http_request_method_get = 0,
http_request_method_post = 1,
http_request_method_put = 2,
http_request_method_patch = 3,
http_request_method_delete = 4
} http_request_method;
/*
* Struct representing the specific type of request
*/
typedef struct http_request {
http_request_method type;
http_request_entity entity;
} http_request;
typedef enum http_parse_error {
http_parse_error_ok = 0,
http_parse_error_incomplete = 1,
@ -37,4 +22,6 @@ typedef enum http_parse_error {
http_parse_error http_parse_request(http_request *req, const char *path,
size_t path_len);
void http_route(event_loop_conn *conn);
#endif

31
include/http_req.h Normal file
View file

@ -0,0 +1,31 @@
#ifndef HTTP_REQ
#define HTTP_REQ
#include <stdlib.h>
#include "picohttpparser.h"
#define HTTP_MAX_ALLOWED_HEADERS 16
typedef enum http_request_method {
http_request_method_get = 0,
http_request_method_post = 1,
http_request_method_put = 2,
http_request_method_patch = 3,
http_request_method_delete = 4
} http_request_method;
/*
* Struct representing the specific type of request
*/
typedef struct http_request {
size_t len;
int minor_version;
const char *method;
size_t method_len;
const char *path;
size_t path_len;
struct phr_header headers[HTTP_MAX_ALLOWED_HEADERS];
} http_request;
#endif