main.ch

Declarations

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