comparison 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
comparison
equal deleted inserted replaced
18:fa2b8af609d9 19:875bb6e10db7
1 #ifndef SEOBEO_SERVER_H 1 #ifndef SEOBEO_SERVER_H
2 #define SEOBEO_SERVER_H 2 #define SEOBEO_SERVER_H
3 3
4 // --- Seobeo --- // 4 /**
5 // Library for creating sockets, binding sockets, and listening to a sockets. 5 * Seobeo
6 // Sending and unpacking bytes of data. 6 * ------
7 *
8 * Library for starting TCP, UDP server and serving static file or path.
9 */
7 10
8 #include <stdio.h> 11 #include <stdio.h>
9 #include <stdlib.h> 12 #include <stdlib.h>
10 #include <unistd.h> 13 #include <unistd.h>
11 #include <errno.h> 14 #include <errno.h>
20 #include <fcntl.h> 23 #include <fcntl.h>
21 #include <pthread.h> 24 #include <pthread.h>
22 #include <stdatomic.h> 25 #include <stdatomic.h>
23 #include <stdbool.h> 26 #include <stdbool.h>
24 27
25 // SSL 28 #include "seobeo/seobeo_internal.h"
26 #include <openssl/ssl.h>
27 #include <openssl/err.h>
28
29 #include "dowa/dowa.h"
30 29
31 #define INITIAL_BUFFER_CAPACITY 4096 30 #define INITIAL_BUFFER_CAPACITY 4096
32 31
33 // HTTP STATUS CODE 32 // HTTP STATUS CODE
34 #define HTTP_OK 200 33 #define HTTP_OK 200
39 #define HTTP_UNAUTHORIZED 401 38 #define HTTP_UNAUTHORIZED 401
40 #define HTTP_FORBIDDEN 403 39 #define HTTP_FORBIDDEN 403
41 #define HTTP_NOT_FOUND 404 40 #define HTTP_NOT_FOUND 404
42 #define HTTP_INTERNAL_ERROR 500 41 #define HTTP_INTERNAL_ERROR 500
43 42
44 typedef enum {
45 SEOBEO_STREAM_TYPE_SERVER,
46 SEOBEO_STREAM_TYPE_CLIENT,
47 } Seobeo_SocketType;
48
49 typedef struct {
50 uint32 ip;
51 uint16 port;
52 } Seobeo_Address, Seobeo_PAddress;
53
54 typedef struct {
55 int32 socket;
56 Seobeo_SocketType type;
57 boolean connected;
58
59 char *host;
60 char *port;
61
62 SSL_CTX *ssl_ctx;
63 SSL *ssl;
64
65 uint8 *read_buffer;
66 uint32 read_buffer_capacity;
67 uint32 read_buffer_len; // current size
68 uint32 read_buffer_used; // TODO: Implement this for client
69
70 uint8 *write_buffer;
71 uint32 write_buffer_capacity;
72 uint32 write_buffer_len; // current size
73
74 void *file;
75 void *text_copy;
76 char *file_name;
77
78 atomic_bool destroyed;
79 } Sebeo_Handle, *Seobeo_PHandle;
80
81 typedef struct {
82 Seobeo_PHandle srv;
83 Dowa_PHashMap cache;
84 int evfd; // epoll‐fd or kqueue‐fd
85 } WorkerArgs;
86
87 typedef enum {
88 SEOBEO_MODE_FORK,
89 SEOBEO_MODE_EDGE,
90 } Seobeo_ServerMode;
91
92 // --- Socket, IP related --- //
93 extern boolean Seobeo_DNS_LookUp(Seobeo_Address *address, char *dns_name);
94 extern void *Seobeo_GetIP4OrIP6(struct sockaddr *sa);
95
96 // --- TCP --- // 43 // --- TCP --- //
44 // --- Generate a Server Handle. ---//
97 extern Seobeo_PHandle Seobeo_Stream_Handle_Server_Create(const char *host, const char* port); 45 extern Seobeo_PHandle Seobeo_Stream_Handle_Server_Create(const char *host, const char* port);
46 // --- Generate a Client Handle. ---//
98 extern Seobeo_PHandle Seobeo_Stream_Handle_Client_Create(const char *host, const char* port, boolean use_tls); 47 extern Seobeo_PHandle Seobeo_Stream_Handle_Client_Create(const char *host, const char* port, boolean use_tls);
99 extern Seobeo_PHandle Seobeo_Stream_Handle_Accept(Seobeo_PHandle p_server_handle); 48 // --- Generate a Client Handle from given Server Handle. ---//
100 extern int Seobeo_Stream_Handle_Read(Seobeo_PHandle p_handle); 49 extern Seobeo_PHandle Seobeo_Stream_Handle_Server_Accept(Seobeo_PHandle p_server_handle);
101 50
102 // --- Web --- // 51 // --- Web --- //
103 extern void Seobeo_Web_GenerateResponseHeader(void *buffer, int status, const char *content_type, const int content_length); 52 /* Generate HTTP 1.1 Header value with given content_types and length. */
104 extern void Seobeo_Web_HandleClientRequest(Seobeo_PHandle cli, Dowa_PHashMap p_html_cache); 53 extern void Seobeo_Web_Header_Generate(void *buffer, int status, const char *content_type, const int content_length);
105 extern int Seobeo_Web_ParseClientHeader(Seobeo_PHandle p_handle, Dowa_PHashMap map); 54 /* Start a Generic HTTP static file server with given folder. It will store folder into memory. */
106 extern int Seobeo_Web_StartBasicHTTPServer(const char *folder_path, const char *port, Seobeo_ServerMode mode, int thread_count); 55 extern int Seobeo_Web_Server_Start(const char *folder_path, const char *port, Seobeo_ServerMode mode, int thread_count);
107 extern void *Seobeo_Web_Edge_Worker(void *vargs); // Maybe not web only... 56 /* Generic HTTP GET Rquest to given host and port with path. It will mimic chrome. */
108 extern void Seobeo_Web_Edge(Seobeo_PHandle p_server_handle, int thread_count, Dowa_PHashMap p_html_cache); 57 extern int Seobeo_Web_Client_Get(const char *host, const char *port, const char *path);
109 extern int Seobeo_Web_ClientGet(const char *host, const char *port, const char *path);
110
111 58
112 // --- Helper functions --- // 59 // --- Helper functions --- //
60 /* Destroy handle. It will handle all NULL poointers. */
113 extern void Seobeo_Handle_Destroy(Seobeo_PHandle p_handle); 61 extern void Seobeo_Handle_Destroy(Seobeo_PHandle p_handle);
114 extern int Seobeo_Handle_Flush(Seobeo_PHandle p_handle); 62 /* Write to socket from write_buffer in the handle. */
115 extern int Seobeo_Handle_Queue(Seobeo_PHandle p_handle, const uint8_t *data, uint32_t data_size); 63 extern int Seobeo_Handle_Flush(Seobeo_PHandle p_handle);
116 extern int Seobeo_Handle_Read(Seobeo_PHandle p_handle); 64 /* 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. */
65 extern int Seobeo_Handle_Queue(Seobeo_PHandle p_handle, const uint8_t *data, uint32_t data_size);
66 /* Read to socket from read_buffer in the handle. */
67 extern int Seobeo_Handle_Read(Seobeo_PHandle p_handle);
68 /* Move to read_buffer to front and we already consumed the given amount in the handle. */
117 extern void Seobeo_Handle_Consume(Seobeo_PHandle p_handle, uint32 consumed); 69 extern void Seobeo_Handle_Consume(Seobeo_PHandle p_handle, uint32 consumed);
118 70 /* Assign IP4 or IP6 to sockaddr. TODO: Maybe create my own struct for this? */
119 static void init_openssl(void) 71 extern void *Seobeo_Get_IP4_Or_IP6(struct sockaddr *sa);
120 {
121 SSL_load_error_strings();
122 OpenSSL_add_ssl_algorithms();
123 }
124
125 static void cleanup_openssl(void)
126 {
127 EVP_cleanup();
128 }
129
130 #endif 72 #endif