Mercurial
diff seobeo/seobeo.h @ 19:875bb6e10db7
[Seobeo] Chaning Function naming to be easily readable.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Mon, 06 Oct 2025 09:55:34 -0700 |
| parents | fa2b8af609d9 |
| children | 84672efec192 |
line wrap: on
line diff
--- a/seobeo/seobeo.h Mon Oct 06 08:21:34 2025 -0700 +++ b/seobeo/seobeo.h Mon Oct 06 09:55:34 2025 -0700 @@ -1,9 +1,12 @@ #ifndef SEOBEO_SERVER_H #define SEOBEO_SERVER_H -// --- Seobeo --- // -// Library for creating sockets, binding sockets, and listening to a sockets. -// Sending and unpacking bytes of data. +/** + * Seobeo + * ------ + * + * Library for starting TCP, UDP server and serving static file or path. + */ #include <stdio.h> #include <stdlib.h> @@ -22,11 +25,7 @@ #include <stdatomic.h> #include <stdbool.h> -// SSL -#include <openssl/ssl.h> -#include <openssl/err.h> - -#include "dowa/dowa.h" +#include "seobeo/seobeo_internal.h" #define INITIAL_BUFFER_CAPACITY 4096 @@ -41,90 +40,33 @@ #define HTTP_NOT_FOUND 404 #define HTTP_INTERNAL_ERROR 500 -typedef enum { - SEOBEO_STREAM_TYPE_SERVER, - SEOBEO_STREAM_TYPE_CLIENT, -} Seobeo_SocketType; - -typedef struct { - uint32 ip; - uint16 port; -} Seobeo_Address, Seobeo_PAddress; - -typedef struct { - int32 socket; - Seobeo_SocketType type; - boolean connected; - - char *host; - char *port; - - SSL_CTX *ssl_ctx; - SSL *ssl; - - uint8 *read_buffer; - uint32 read_buffer_capacity; - uint32 read_buffer_len; // current size - uint32 read_buffer_used; // TODO: Implement this for client - - uint8 *write_buffer; - uint32 write_buffer_capacity; - uint32 write_buffer_len; // current size - - void *file; - void *text_copy; - char *file_name; - - atomic_bool destroyed; -} Sebeo_Handle, *Seobeo_PHandle; - -typedef struct { - Seobeo_PHandle srv; - Dowa_PHashMap cache; - int evfd; // epoll‐fd or kqueue‐fd -} WorkerArgs; - -typedef enum { - SEOBEO_MODE_FORK, - SEOBEO_MODE_EDGE, -} Seobeo_ServerMode; - -// --- Socket, IP related --- // -extern boolean Seobeo_DNS_LookUp(Seobeo_Address *address, char *dns_name); -extern void *Seobeo_GetIP4OrIP6(struct sockaddr *sa); - // --- TCP --- // +// --- Generate a Server Handle. ---// extern Seobeo_PHandle Seobeo_Stream_Handle_Server_Create(const char *host, const char* port); +// --- Generate a Client Handle. ---// extern Seobeo_PHandle Seobeo_Stream_Handle_Client_Create(const char *host, const char* port, boolean use_tls); -extern Seobeo_PHandle Seobeo_Stream_Handle_Accept(Seobeo_PHandle p_server_handle); -extern int Seobeo_Stream_Handle_Read(Seobeo_PHandle p_handle); +// --- Generate a Client Handle from given Server Handle. ---// +extern Seobeo_PHandle Seobeo_Stream_Handle_Server_Accept(Seobeo_PHandle p_server_handle); // --- Web --- // -extern void Seobeo_Web_GenerateResponseHeader(void *buffer, int status, const char *content_type, const int content_length); -extern void Seobeo_Web_HandleClientRequest(Seobeo_PHandle cli, Dowa_PHashMap p_html_cache); -extern int Seobeo_Web_ParseClientHeader(Seobeo_PHandle p_handle, Dowa_PHashMap map); -extern int Seobeo_Web_StartBasicHTTPServer(const char *folder_path, const char *port, Seobeo_ServerMode mode, int thread_count); -extern void *Seobeo_Web_Edge_Worker(void *vargs); // Maybe not web only... -extern void Seobeo_Web_Edge(Seobeo_PHandle p_server_handle, int thread_count, Dowa_PHashMap p_html_cache); -extern int Seobeo_Web_ClientGet(const char *host, const char *port, const char *path); - +/* Generate HTTP 1.1 Header value with given content_types and length. */ +extern void Seobeo_Web_Header_Generate(void *buffer, int status, const char *content_type, const int content_length); +/* Start a Generic HTTP static file server with given folder. It will store folder into memory. */ +extern int Seobeo_Web_Server_Start(const char *folder_path, const char *port, Seobeo_ServerMode mode, int thread_count); +/* Generic HTTP GET Rquest to given host and port with path. It will mimic chrome. */ +extern int Seobeo_Web_Client_Get(const char *host, const char *port, const char *path); // --- Helper functions --- // +/* Destroy handle. It will handle all NULL poointers. */ extern void Seobeo_Handle_Destroy(Seobeo_PHandle p_handle); -extern int Seobeo_Handle_Flush(Seobeo_PHandle p_handle); -extern int Seobeo_Handle_Queue(Seobeo_PHandle p_handle, const uint8_t *data, uint32_t data_size); -extern int Seobeo_Handle_Read(Seobeo_PHandle p_handle); +/* Write to socket from write_buffer in the handle. */ +extern int Seobeo_Handle_Flush(Seobeo_PHandle p_handle); +/* Write to socket with given data source, if the data source is bigger than the write buffer for handle then we just directly write from the data source. */ +extern int Seobeo_Handle_Queue(Seobeo_PHandle p_handle, const uint8_t *data, uint32_t data_size); +/* Read to socket from read_buffer in the handle. */ +extern int Seobeo_Handle_Read(Seobeo_PHandle p_handle); +/* Move to read_buffer to front and we already consumed the given amount in the handle. */ extern void Seobeo_Handle_Consume(Seobeo_PHandle p_handle, uint32 consumed); - -static void init_openssl(void) -{ - SSL_load_error_strings(); - OpenSSL_add_ssl_algorithms(); -} - -static void cleanup_openssl(void) -{ - EVP_cleanup(); -} - +/* Assign IP4 or IP6 to sockaddr. TODO: Maybe create my own struct for this? */ +extern void *Seobeo_Get_IP4_Or_IP6(struct sockaddr *sa); #endif