Web Server¶
A tiny synchronous HTTP/1.1 server built on raw sockets, plus a server_handle dispatcher that works without any network at all — so routes are trivially testable.
The server object (ABS_SERVER) holds a listening socket and an ABS_DICT mapping request paths to function handlers (ABS_FUNC).
API¶
| Function | Description |
|---|---|
var Server(int port) |
Create an ABS_SERVER. Server(0) binds an ephemeral port. |
void route(var app, const char *path, AbsFunc handler) |
Register a handler for a path (e.g. "/", "/about"). |
var server_handle(var app, const char *request_line) |
Dispatch one request line like "GET /about HTTP/1.1"; returns the handler's body, or NULL for a 404. |
void server_run(var app) |
Serve HTTP until killed (blocking loop). |
Handlers use the standard function signature var (*)(var); the request is passed as an argument (usually ignored).
static var api_home(var req) {
(void)req;
return v("<h1>Abscom Framework</h1>");
}
var app = Server(0);
route(app, "/", api_home);
/* Dispatch without a socket: handy for tests. */
var body = server_handle(app, "GET / HTTP/1.1"); /* <h1>Abscom Framework</h1> */
if (server_handle(app, "GET /missing HTTP/1.1") == NULL) {
print(v("404"));
}
/* Blocking HTTP server (Ctrl+C to stop). */
server_run(app);
Notes¶
- If the socket cannot be created or bound,
Server()still returns a validABS_SERVERwithsocket_fd == -1, soserver_handle()continues to work. server_runresponds200 OKwhen a route matches and404 Not Foundotherwise, withContent-Type: text/html; charset=utf-8andConnection: close.- Windows uses Winsock (linked automatically as
ws2_32); POSIX uses the standard socket interface. No extra flags are needed on POSIX for the server itself. - See
examples/framework_demo.candtests/test_server.cfor more.
Back to README.