Csilk 0.2.1
A lightweight, high-performance C HTTP web framework
Loading...
Searching...
No Matches
server_internal.h File Reference

Internal layout of csilk_server_s and csilk_client_s. More...

#include <openssl/ssl.h>
#include <stdatomic.h>
#include <uv.h>
#include <llhttp.h>
#include <nghttp2/nghttp2.h>
#include "csilk/csilk.h"
#include "context_internal.h"
Include dependency graph for server_internal.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  csilk_hook_node_t
 Hook handler node in a linked list. More...
 
struct  csilk_server_t
 Main Server structure — represents the core HTTP server instance. More...
 
struct  csilk_client_t
 Client connection structure — represents a single TCP connection. More...
 

Macros

#define CSILK_DEFAULT_IDLE_TIMEOUT   5000
 Default idle timeout in milliseconds.
 
#define CSILK_DEFAULT_MAX_BODY_SIZE   (1024UL * 1024UL)
 Default maximum request body size in bytes.
 
#define CSILK_DEFAULT_MAX_HEADER_SIZE   (64UL * 1024UL)
 Default maximum request header size in bytes.
 
#define CSILK_DEFAULT_LISTEN_BACKLOG   128
 Default TCP listen backlog.
 
#define CSILK_DEFAULT_ARENA_SIZE   4096
 Default request arena chunk size.
 

Enumerations

enum  csilk_protocol_t { CSILK_PROTO_UNKNOWN , CSILK_PROTO_HTTP1 , CSILK_PROTO_HTTP2 }
 Protocol type for a client connection. More...
 

Functions

void csilk_client_write (csilk_client_t *client, const uint8_t *data, size_t length)
 Write data to the client's TCP socket, handling TLS encryption if necessary.
 

Detailed Description

Internal layout of csilk_server_s and csilk_client_s.

This header defines the memory layout of the server and client structures. It is included by src/core/server.c and other internal framework code that needs access to the server's internal state (e.g., MQ, loop, hooks).


Data Structure Documentation

◆ csilk_hook_node_t

struct csilk_hook_node_t

Hook handler node in a linked list.

Data Fields
void * handler
struct csilk_hook_node_s * next

◆ csilk_server_s

struct csilk_server_s

Main Server structure — represents the core HTTP server instance.

Main Server structure.

Manages the libuv event loop, HTTP listener, configuration, global middleware chain, hook registrations, and client connection pooling. Thread-safe for multi-threaded operation via atomic counters and mutexes.

Collaboration diagram for csilk_server_t:
Data Fields
csilk_client_t * active_clients

Head of active connections list.

atomic_int active_connections

Current connection count (atomic).

uv_async_t async_handle

Async handle for cross-thread wakeup.

csilk_cipher_driver_t * cipher_driver

Cipher algorithm driver.

csilk_client_t * client_pool[32]

Connection object free list.

int client_pool_count

Number of free clients in pool.

uv_mutex_t clients_mutex

Mutex for active clients list.

csilk_server_config_t config

Server configuration.

csilk_crypto_driver_t * crypto_driver

Crypto algorithm driver.

csilk_hook_node_t * hooks[CSILK_HOOK_COUNT]

Registered hooks.

uv_loop_t * loop

libuv event loop.

int max_connections

Max concurrent connections (0=unlimited).

int middleware_count

Number of global middlewares.

csilk_handler_t middlewares[32]

Global middlewares.

csilk_mq_t * mq

Message Queue instance.

csilk_handler_t not_found_handler

Custom 404 handler (NULL = default).

uv_mutex_t pool_mutex

Mutex for connection pool access.

csilk_router_t * router

Associated router instance.

uv_tcp_t server_handle

TCP server handle.

llhttp_settings_t settings

HTTP parser callback settings.

uv_signal_t sig_handle

SIGINT signal handler.

char * spa_doc_root

SPA fallback doc root (NULL = disabled).

SSL_CTX * ssl_ctx

OpenSSL context.

csilk_storage_driver_t * storage_driver

Context storage driver.

int worker_count

Number of worker threads created.

uv_async_t * worker_stop_async

Per-worker async handles for graceful stop.

int worker_stop_count

Number of worker_stop_async entries.

uv_thread_t * worker_tids

Worker thread IDs (NULL if single-thread).

◆ csilk_client_s

struct csilk_client_s

Client connection structure — represents a single TCP connection.

Forward declaration for client connection structure.

Holds the libuv stream handle, HTTP parser state, timers for keep-alive and timeouts, TLS context (if HTTPS), and the request/response context. Clients are pooled and reused for performance.

Collaboration diagram for csilk_client_t:
Data Fields
int close_pending

Pending close refs before freeing client.

csilk_ctx_t ctx

Request context for this connection (HTTP/1.1 only for now).

char * current_header_field

Temporary header field name.

char * current_header_value

Temporary header field value.

char * current_url

Current URL being parsed.

size_t current_url_capacity

Allocated size of current_url.

nghttp2_session * h2_session

HTTP/2 session state (if HTTP/2).

csilk_ctx_t * h2_streams

Linked list of active HTTP/2 stream contexts.

uv_tcp_t handle

libuv TCP stream handle.

size_t header_count

Number of headers parsed so far.

size_t header_field_capacity

Allocated size of current_header_field.

size_t header_value_capacity

Allocated size of current_header_value.

struct csilk_client_s * next

Next client in active list.

llhttp_t parser

HTTP request parser (if HTTP/1.1).

struct csilk_client_s * prev

Previous client in active list.

csilk_protocol_t protocol

Protocol negotiated for this connection.

BIO * read_bio

BIO for reading encrypted data.

uv_timer_t read_timer

Read timeout timer.

uv_timer_t request_timer

Request timeout timer.

csilk_server_t * server

Owning server instance.

SSL * ssl

OpenSSL session object.

uv_timer_t timer

Connection idle (keep-alive) timer.

size_t total_header_size

Total size of headers parsed so far.

BIO * write_bio

BIO for writing encrypted data.

uv_timer_t write_timer

Write timeout timer.

Macro Definition Documentation

◆ CSILK_DEFAULT_ARENA_SIZE

#define CSILK_DEFAULT_ARENA_SIZE   4096

Default request arena chunk size.

◆ CSILK_DEFAULT_IDLE_TIMEOUT

#define CSILK_DEFAULT_IDLE_TIMEOUT   5000

Default idle timeout in milliseconds.

◆ CSILK_DEFAULT_LISTEN_BACKLOG

#define CSILK_DEFAULT_LISTEN_BACKLOG   128

Default TCP listen backlog.

◆ CSILK_DEFAULT_MAX_BODY_SIZE

#define CSILK_DEFAULT_MAX_BODY_SIZE   (1024UL * 1024UL)

Default maximum request body size in bytes.

◆ CSILK_DEFAULT_MAX_HEADER_SIZE

#define CSILK_DEFAULT_MAX_HEADER_SIZE   (64UL * 1024UL)

Default maximum request header size in bytes.

Enumeration Type Documentation

◆ csilk_protocol_t

Protocol type for a client connection.

Enumerator
CSILK_PROTO_UNKNOWN 
CSILK_PROTO_HTTP1 
CSILK_PROTO_HTTP2 

Function Documentation

◆ csilk_client_write()

void csilk_client_write ( csilk_client_t *  client,
const uint8_t *  data,
size_t  len 
)

Write data to the client's TCP socket, handling TLS encryption if necessary.

Parameters
clientThe client connection.
dataThe data to write.
lengthThe length of the data.

Write data to the client's TCP socket, handling TLS encryption if necessary.

If TLS is active, writes through the SSL session and flushes the write BIO. Otherwise, allocates a write request, copies the data, and queues the write via libuv. The data buffer is freed by the write completion callback.

Parameters
cThe request context.
dataData buffer to send.
lenLength of data in bytes.
Note
This is an internal function used by the framework to send HTTP responses, chunked frames, and WebSocket frames.