view seobeo/seobeo.h @ 22:947b81010aba

[Dowa & Seobeo] Updated so that Dowa hashmaps can use arena and not be broken. Split up web so taht it can handle different paths. Also fixes issues with hash collisions which was pain in the ass.
author June Park <parkjune1995@gmail.com>
date Tue, 07 Oct 2025 07:11:02 -0700
parents 875bb6e10db7
children 84672efec192
line wrap: on
line source

#ifndef SEOBEO_SERVER_H
#define SEOBEO_SERVER_H

/**
 * Seobeo
 * ------
 *
 * Library for starting TCP, UDP server and serving static file or path.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdbool.h>

#include "seobeo/seobeo_internal.h"

#define INITIAL_BUFFER_CAPACITY 4096

// HTTP STATUS CODE
#define HTTP_OK 200
#define HTTP_CREATED 201
#define HTTP_MOVED_PERMANENTLY 301
#define HTTP_FOUND 302
#define HTTP_BAD_REQUEST 400
#define HTTP_UNAUTHORIZED 401
#define HTTP_FORBIDDEN 403
#define HTTP_NOT_FOUND 404
#define HTTP_INTERNAL_ERROR 500

// --- 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);
// --- Generate a Client Handle from given Server Handle. ---//
extern Seobeo_PHandle Seobeo_Stream_Handle_Server_Accept(Seobeo_PHandle p_server_handle);

// --- Web --- //
/* 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);
/* 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);
/* 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