--- layout: page published: true noToc: false noComments: false noDate: false title: Fun - HTTP server examples: architecture and how they work subtitle: Documentation for HTTP server examples, architecture and how they work description: Documentation for HTTP server examples, architecture and how they work permalink: /documentation/examples/net/httpserver/ lang: en tags: - architecture - example - examples - http - network - server - they - work --- This guide explains the HTTP server examples under `./examples/net/http_*` and the supporting standard library modules under `./lib/net/`. It covers what each example does, how to run it, and how the request handling/CGI pieces are implemented. ## Prerequisites - Build the project so you have a `fun` executable, or ensure `fun` is on your PATH. - Typical local builds: `./build_debug/fun` or `./build_release/fun`. - For examples that serve files/CGI scripts, content is read from `./examples/data/htdocs` by default. - Helpful env vars used by some examples: - `FUN_LIB_DIR` — path to the stdlib (`./lib` when running from the repo). - `FUN_EXEC` — override path to the `fun` interpreter used to run CGI children. - `FUN_HTDOCS` — override htdocs directory for some examples (not all). - `FUN_PORT` — override port for some examples (mainly the MT CGI one). ## Supporting stdlib modules (lib/net) - `net/http_server.fun` — Simple blocking HTTP server class serving static files, with a very small built-in “CGI for .fun files” (spawns `fun` with `QUERY_STRING`/`POST_DATA`). - `net/http_cgi_server.fun` — Blocking server with fuller CGI support using `net/cgi.fun` to translate CGI output into proper HTTP responses and richer request/header parsing. - `net/http_cgi_lib_server.fun` — Variant of the CGI server with the same core behavior, explicitly wiring the Fun interpreter path and using `CGI().cgi_to_http_response()` from `net/cgi.fun`. - `net/cgi.fun` — Helpers to work with CGI-style programs and to translate CGI output (headers + body) into full HTTP/1.1 responses. See documentation/stdlib.md → net/ for a quick index of these modules. ## Example: http_static_server.fun - File: `examples/net/http_static_server.fun` - Purpose: Minimal, hand-written HTTP server that ignores the request path and always responds with a fixed HTML page. - Key includes: `io/socket.fun` - Port/backlog: hard-coded `8088`, backlog 10. - Flow: - Creates `TcpServer(port, backlog)` → `listen()` → loop on `accept()` - `sock_recv()` reads and discards request - Constructs a literal HTTP/1.1 200 response with `Content-Length` and `Connection: close` - Sends the response with `sock_send()` and closes the client - Run: - `./examples/net/http_static_server.fun` - Open http://127.0.0.1:8080/ ## Example: http_server.fun (static + simple .fun CGI) - File: `examples/net/http_server.fun` - Uses: `#include ` (class `HTTPServer`) - Purpose: Blocking server that serves static files from an htdocs directory; if the requested path ends with `.fun`, it spawns `fun` to run the script and returns its output. - Defaults in example: - Port: `8080` - Htdocs: `./examples/data/htdocs` - Implementation highlights (see `lib/net/http_server.fun`): - Parses request line to get `method` and `path`; `/` maps to `/index.html`. - When path ends with `.fun`, builds a small environment: - `QUERY_STRING` (for `?a=b`), `POST_DATA` (raw body when `POST`). - Runs `proc_run(" fun