-- Showcase the calog http client end to end with NO external dependency: a -- spawned "lua" child task acts as a tiny one-shot HTTP/1.1 server -- it -- tcpListens on a fixed high port, accepts one connection, reads the request, -- and sends back a valid 200 response with a Content-Length body. The main -- context sleeps briefly so the child can bind, then httpGets that URL and -- prints the returned status and body before exiting. -- Run: bin/calog examples/scripts/libraries/http.lua local PORT = 48080 -- The child runs as its own context. Its tcpAccept/tcpRecv block that child's -- thread, leaving the main context free to make the request. Inside this long -- string the \r\n sequences are literal text that the child parses as escapes. local serverCode = [[ local PORT = 48080 local listener = tcpListen(PORT) calogPrint("server: listening on port", PORT) local client = tcpAccept(listener) local request = tcpRecv(client, 4096) local requestLine = request and string.match(request, "^[^\r\n]+") or "?" calogPrint("server: request line:", requestLine) local body = "Hello from the calog TCP server!\n" local response = "HTTP/1.1 200 OK\r\n" .. "Content-Type: text/plain\r\n" .. "Content-Length: " .. #body .. "\r\n" .. "Connection: close\r\n" .. "\r\n" .. body tcpSend(client, response) tcpClose(client) tcpClose(listener) calogPrint("server: response sent") ]] taskSpawn("lua", serverCode) -- Give the child a moment to reach its tcpListen before we connect. timeSleep(400) local url = "http://127.0.0.1:" .. PORT .. "/" calogPrint("client: requesting", url) local response = httpGet(url) calogPrint("client: status", response.status) calogPrint("client: body", response.body) calogExit(0)