main.ch

Declarations

Public Only
Publicnamespace net
namespace net
Publictypealias Socket
type Socket = usize
Publicstruct timeval
struct timeval
timeval struct for setsockopt SO_RCVTIMEO
Publicfunction inet_pton
func inet_pton(Family : int, pszAddrString : *char, pAddrBuf : *void) : int
Publicstruct in_addr
struct in_addr
IPv4 sockaddr structs (C layout)
Publicstruct sockaddr_in
struct sockaddr_in
Publicfunction set_recv_timeout
func set_recv_timeout(s : Socket, secs : long, usecs : long) : void
helper: set socket recv timeout (seconds + microseconds)
Publicfunction set_keep_alive
func set_keep_alive(s : Socket, enable : bool) : void
helper: set keep alive
Publicfunction htons_port
func htons_port(p : u16) : u16
Publicfunction listen_addr
func listen_addr(addr_str : *char, port : uint) : Socket
listen by address string and port using getaddrinfo -> bind -> listen loop
Publicfunction accept_socket
func accept_socket(listen_sock : Socket) : Socket
Publicfunction recv_all
func recv_all(s : Socket, buf : *u8, cap : usize) : int
Publicfunction send_all
func send_all(s : Socket, p : *char, len : int) : void
Publicfunction close_socket
func close_socket(s : Socket) : void
Publicnamespace io
namespace io
===== Buffer implementation =====
Publicstruct Buffer
struct Buffer
Publicfunction constructor
func constructor() : Buffer
how many bytes consumed from the front
Publicfunction len
func len(self : &Buffer) : usize
Publicfunction ensure_capacity
func ensure_capacity(self : &Buffer, extra : usize) : void
Publicfunction append_bytes
func append_bytes(self : &Buffer, src : *u8, n : usize) : void
Publicfunction consume
func consume(self : &Buffer, n : usize) : void
Publicfunction as_ptr
func as_ptr(self : &Buffer) : *u8
Publicfunction get_byte
func get_byte(self : &Buffer, idx : usize) : u8
function delete
func delete(self : *Buffer) : void
Publicnamespace http
namespace http
===== HTTP utilities and parser =====
Publicstruct HeaderMap
struct HeaderMap
Publicfunction make
func make() : HeaderMap
Publicfunction empty
func empty(self : &HeaderMap) : bool
Publicfunction print
func print(self : &HeaderMap) : void
Publicfunction insert
func insert(self : &HeaderMap, k : string, v : string) : void
Publicfunction insert_view
func insert_view(self : &HeaderMap, k : &string_view, v : &string_view) : void
Publicfunction get
func get(self : &HeaderMap, name : *char) : Option<string>
function delete
func delete(self : *HeaderMap) : void
Publicfunction strcasecmp
func strcasecmp(a : *char, b : *char) : int
helper: simple ASCII case-insensitive strcmp
Publicstruct Body
struct Body
Body: lazy reader for request body. Does NOT close the socket by itself.
Publicfunction empty_make
func empty_make() : Body
how many body bytes have been delivered to user (enforce max_body)
Publicfunction make_body
func make_body(sock : Socket, buf_ptr : *Buffer, content_len : isize, chunked : bool, timeout_secs : long, max_body : usize) : Body
construct body from request metadata (call from handle_conn after parsing headers)
function body_recv
func body_recv(b : *Body, dst : *u8, cap : usize) : int
low-level recv helper that honors timeout and returns <=0 on error/timeout
function copy_from_buffer
func copy_from_buffer(b : *Body, dst : *u8, want : usize) : usize
Helper: copy up to `want` bytes from the already-read buffer into dst, returning copied count.
function read
func read(b : &Body, dst : *u8, cap : usize) : int extension
PUBLIC: read up to `cap` bytes into dst; returns number of bytes read or <=0 on error.
Publicfunction read_to_string
func read_to_string(b : &Body) : Option<string> extension
read all remaining body into a string (useful for small bodies)
Publicfunction read_exact
func read_exact(b : &Body, n : usize) : Option<string> extension
read exactly `n` bytes (or return None on error/EOF)
Publicfunction drain
func drain(b : &Body) : bool extension
drain remaining body to /dev/null (useful when you want to discard the body)
Publicfunction close
func close(b : &Body) : void extension
close returns resources (no-op here except mark closed)
Publicstruct Request
struct Request
Publicfunction constructor
func constructor() : Request
function delete
func delete(self : *Request) : void
Publicstruct ResponseWriter
struct ResponseWriter
Publicfunction constructor
func constructor(s : Socket) : ResponseWriter
Publicfunction set_header
func set_header(self : &ResponseWriter, k : string, v : string) : void
Publicfunction set_header_view
func set_header_view(self : &ResponseWriter, k : &string_view, v : &string_view) : void
Publicfunction send_headers
func send_headers(self : &ResponseWriter, content_len : usize) : void
Publicfunction write_string
func write_string(self : &ResponseWriter, s : string) : void
Publicfunction write_view
func write_view(self : &ResponseWriter, v : &string_view) : void
Publicfunction send_file
func send_file(self : &ResponseWriter, path_view : &string_view, content_type : &string_view) : bool
Adds a zero-copy send_file method to ResponseWriter.
Publicfunction finish
func finish(self : &ResponseWriter) : void
function delete
func delete(self : *ResponseWriter) : void
Publicfunction url_decode
func url_decode(in_s : &string) : string
URL decode (percent-decoding)
Publicfunction hex_value
func hex_value(c : char) : int
Publicfunction parse_query
func parse_query(qs : &string) : vector<pair<string, string>>
Parse query string into simple vector of pairs
Publicfunction read_request_incremental
func read_request_incremental(s : Socket, buf : &Buffer, header_timeout_secs : long, max_header_bytes : usize, max_headers : uint) : Option<Request>
Incremental request reader that reads headers with timeout and enforces limits
Publicfunction parse_request_from_bytes
func parse_request_from_bytes(buf : *u8, n : usize, s : Socket) : Option<Request>
parse_request_from_bytes: same as earlier parse_request_from_buf but uses raw bytes and returns Request
Publicnamespace web
namespace web
===== Router + middleware =====
Publictypealias Handler
type Handler = function<func(Request, ResponseWriter) : void>
Publictypealias Middleware
type Middleware = function<func(Handler) : Handler>
Publicstruct Route
struct Route
function delete
func delete(self : *Route) : void
Publicstruct Router
struct Router
Publicfunction constructor
func constructor() : Router
Publicfunction add
func add(self : &Router, method : *char, pattern : *char, h : Handler) : void
Publicfunction use_middleware
func use_middleware(self : &Router, m : Middleware) : void
Publicfunction match_route
func match_route(self : &Router, method : &string, path : &string, params_out : *vector<pair<string, string>>) : Option<Handler>
Publicfunction apply_middlewares
func apply_middlewares(self : &Router, base : Handler) : Handler
apply middlewares to a handler and return final handler
function delete
func delete(self : *Router) : void
Publicfunction match_pattern
func match_pattern(pattern : &string, path : &string, params_out : *vector<pair<string, string>>) : bool
match pattern like /users/:id against path and fill params
Publicfunction split_segments
func split_segments(s : &string) : vector<string>
Publicnamespace server
namespace server
===== Server wiring all pieces together =====
Publicstruct ServerConfig
struct ServerConfig
Publicfunction constructor
func constructor() : ServerConfig
function delete
func delete(self : *ServerConfig) : void
Publicstruct Server
struct Server
Publicfunction constructor
func constructor(cfg_ : ServerConfig) : Server
Publicfunction handle_conn
func handle_conn(self : &Server, s : Socket) : void
production handler: parse request, route, and respond
Publicfunction accept_main
func accept_main(arg : *void) : *void
accept loop — submit work to threadpool
Publicfunction start
func start(self : &Server, port : uint) : void
Publicfunction serve_non_iocp
func serve_non_iocp(self : &Server, port : uint) : void
Publicfunction serve_async
func serve_async(self : &Server, port : uint) : Thread
Publicfunction serve
func serve(self : &Server, port : uint) : void
Publicfunction shutdown
func shutdown(self : &Server) : void
function delete
func delete(self : *Server) : void