Http.serve
func Http.serve(l: Net.Listener, handler: func(Http.Request): Http.Reply, ctx: Context | null = null): Result[void, Error]Http.serve answers every request that arrives on l by calling handler on a goroutine of its own. It returns Ok once ctx is done and the requests in flight have finished, waiting at most five seconds for them, or serves until the run is cancelled when ctx is null; it closes l on return and is Err when l is closed under it. It speaks plain HTTP. A panic in handler sends status 500, and a reply status outside 200 to 599, or reply header names that differ only in case, panic in the handler's goroutine.
Details
Http.serveblocks untilctxis done, or with noctxuntil the run is cancelled. Run it on a goroutine to talk to the server from the same program. The program exits when the script ends, server or not.- Each request runs
handleron a goroutine of its own, so handlers run at the same time. Guard state they share with a Mutex. - A panic in
handlerends that handler's goroutine only: the server keeps serving. Http.servecloses the listener when it returns, so a listener serves once.queryis raw; parse it with Url.parseQuery.
Basic examples
Http.serve(l, handler, ctx) answers requests on a listener until ctx is done. Listen on port 0 for any free port, serve on a goroutine, and send a request with Http.get:
func hello(req: Http.Request): Http.Reply {
Http.Reply{ status: 200, headers: Map{}, body: "hello from ${req.path}" }
}
let listener = try Net.listen("127.0.0.1:0")
let stop = Context.new()
let served: Channel[Result[void, Error]] = Channel.new(1)
go { served.send(Http.serve(listener, hello, stop)) }
let reply = try Http.get("http://${Net.address(listener)}/docs")
println("${reply.status} ${reply.text()}")
stop.cancel()
println(served.receive())200 hello from /docs
Ok{ value: void }A handler can be a lambda, with the request as x:
let listener = try Net.listen("127.0.0.1:0")
let stop = Context.new()
go {
_ = Http.serve(listener, { Http.Reply{ status: 200, headers: Map{}, body: x.method } }, stop)
}
println((try Http.get("http://${Net.address(listener)}/")).text())
stop.cancel()GETA server that runs until Ctrl-C passes Os.interrupt as ctx. The handler routes on the request's path:
func handle(req: Http.Request): Http.Reply {
if req.path == "/health" {
Http.Reply{ status: 200, headers: Map{}, body: "ok" }
} else {
Http.Reply{ status: 404, headers: Map{}, body: "no ${req.path}" }
}
}
let listener = try Net.listen("127.0.0.1:48213")
println("listening on ${Net.address(listener)}")
let stop = Os.interrupt()
try Http.serve(listener, handle, stop)
println("stopped: ${stop.reason()}")listening on 127.0.0.1:48213
ok
no /missing
stopped: interruptScope
The request carries the method, the decoded path, the raw query, the headers, and the body:
func echo(req: Http.Request): Http.Reply {
let lines = [
"method: ${req.method}",
"path: ${req.path}",
"query: ${req.query}",
"tag: ${req.headers["X-Tag"]}",
"body: ${req.text()}",
]
Http.Reply{ status: 200, headers: Map{}, body: lines | String.join("\n") }
}
let listener = try Net.listen("127.0.0.1:0")
let stop = Context.new()
go { _ = Http.serve(listener, echo, stop) }
let url = "http://${Net.address(listener)}/caf%C3%A9/menu?q=a%20b&n=2"
let reply = try Http.post(url, "two teas", headers: Map{ "X-Tag": "order" })
println(reply.text())
stop.cancel()method: POST
path: /café/menu
query: q=a%20b&n=2
tag: order
body: two teasremote is the client's address, ip:port, and the headers include Host:
func whoami(req: Http.Request): Http.Reply {
let local = req.remote.startsWith("127.0.0.1:")
Http.Reply{ status: 200, headers: Map{}, body: "${local} ${req.headers["Host"]}" }
}
let listener = try Net.listen("127.0.0.1:0")
let address = Net.address(listener)
let stop = Context.new()
go { _ = Http.serve(listener, whoami, stop) }
let reply = try Http.get("http://${address}/")
println(reply.text() == "true ${address}")
stop.cancel()trueA reply body may be Bytes, sent as they are. Headers go out as given, plus Date and Content-Length:
func logo(req: Http.Request): Http.Reply {
let png = Bytes.fromArray([137, 80, 78, 71])
Http.Reply{ status: 200, headers: Map{ "Content-Type": "image/png" }, body: png }
}
let listener = try Net.listen("127.0.0.1:0")
let stop = Context.new()
go { _ = Http.serve(listener, logo, stop) }
let reply = try Http.get("http://${Net.address(listener)}/logo.png")
println(reply.headers["Content-Type"])
println(reply.bytes)
stop.cancel()image/png
Bytes(89 50 4e 47)A redirect is a reply like any other, with a 3xx status and a Location header:
func moved(req: Http.Request): Http.Reply {
Http.Reply{ status: 301, headers: Map{ "Location": "/new${req.path}" }, body: "" }
}
let listener = try Net.listen("127.0.0.1:0")
let stop = Context.new()
go { _ = Http.serve(listener, moved, stop) }
let reply = try Http.get("http://${Net.address(listener)}/page")
println("${reply.status} ${reply.headers["Location"]}")
stop.cancel()301 /new/pageHandlers run at the same time. Here a request to /wait blocks its handler until a request to /go arrives, which only works because each has a goroutine of its own:
let gate: Channel[string] = Channel.new()
func handle(req: Http.Request): Http.Reply {
if req.path == "/go" {
gate.send("released")
return Http.Reply{ status: 200, headers: Map{}, body: "sent" }
}
Http.Reply{ status: 200, headers: Map{}, body: gate.receive() ?? "closed" }
}
let listener = try Net.listen("127.0.0.1:0")
let base = "http://${Net.address(listener)}"
let stop = Context.new()
go { _ = Http.serve(listener, handle, stop) }
let waited: Channel[string] = Channel.new(1)
go { waited.send(Http.get("${base}/wait").unwrap().text()) }
println((try Http.get("${base}/go")).text())
println(waited.receive())
stop.cancel()sent
releasedNamed arguments
ctx
ctx decides when Http.serve returns. A context with a timeout serves for that long:
let listener = try Net.listen("127.0.0.1:0")
let ctx = Context.new(timeout: 100ms)
let result = Http.serve(listener, { Http.Reply{ status: 204, headers: Map{}, body: "" } }, ctx: ctx)
println(result)Ok{ value: void }A request in flight when ctx is done still gets its reply, and Http.serve waits for it before returning. A new request after that finds no server:
func slow(req: Http.Request): Http.Reply {
Time.sleep(200ms)
Http.Reply{ status: 200, headers: Map{}, body: "finished" }
}
let listener = try Net.listen("127.0.0.1:0")
let url = "http://${Net.address(listener)}/"
let stop = Context.new()
let served: Channel[Result[void, Error]] = Channel.new(1)
go { served.send(Http.serve(listener, slow, stop)) }
go {
Time.sleep(50ms)
stop.cancel()
}
let reply = try Http.get(url)
println(reply.text())
println(served.receive())
println(Http.get(url).isOk())finished
Ok{ value: void }
falseApplications
Read query parameters with Url.parseQuery:
func greet(req: Http.Request): Http.Reply {
let params = Url.parseQuery(req.query).unwrapOr(Map{})
let name = params["name"]?.get(0) ?? "stranger"
Http.Reply{ status: 200, headers: Map{}, body: "hello, ${name}" }
}
let listener = try Net.listen("127.0.0.1:0")
let base = "http://${Net.address(listener)}"
let stop = Context.new()
go { _ = Http.serve(listener, greet, stop) }
println((try Http.get("${base}/?name=Ann")).text())
println((try Http.get("${base}/")).text())
stop.cancel()hello, Ann
hello, strangerA small key-value store. Handlers share the map, so a Mutex guards it:
let store: Map[string, string] = Map{}
let lock = Mutex.new()
func handle(req: Http.Request): Http.Reply {
if req.method == "PUT" {
lock.withLock({ store[req.path] = req.text() })
return Http.Reply{ status: 204, headers: Map{}, body: "" }
}
let value = lock.withLock({ store[req.path] })
if value == null {
return Http.Reply{ status: 404, headers: Map{}, body: "not found" }
}
Http.Reply{ status: 200, headers: Map{}, body: value }
}
let listener = try Net.listen("127.0.0.1:0")
let base = "http://${Net.address(listener)}"
let stop = Context.new()
go { _ = Http.serve(listener, handle, stop) }
try Http.request("PUT", "${base}/color", "green")
let found = try Http.get("${base}/color")
let missing = try Http.get("${base}/size")
println("${found.status} ${found.text()}")
println("${missing.status} ${missing.text()}")
stop.cancel()200 green
404 not foundA JSON endpoint parses the body with Json.parse and answers with Json.stringify:
func sum(req: Http.Request): Http.Reply {
let items = Json.parse(req.text()).unwrapOr(null)
if !(items is Array) {
return Http.Reply{ status: 400, headers: Map{}, body: "want a JSON array" }
}
let total = items.map({ if x is float64 { x } else { 0.0 } }).sum() ?? 0.0
let headers = Map{ "Content-Type": "application/json" }
Http.Reply{ status: 200, headers, body: Json.stringify(Map{ "total": total }) }
}
let listener = try Net.listen("127.0.0.1:0")
let url = "http://${Net.address(listener)}/sum"
let stop = Context.new()
go { _ = Http.serve(listener, sum, stop) }
println((try Http.post(url, "[1, 2, 3.5]")).text())
let bad = try Http.post(url, "nope")
println("${bad.status} ${bad.text()}")
stop.cancel(){"total":6.5}
400 want a JSON arrayA proxy passes the upstream body on as bytes, unchanged:
let back = try Net.listen("127.0.0.1:0")
let front = try Net.listen("127.0.0.1:0")
let upstream = "http://${Net.address(back)}"
let stop = Context.new()
func origin(req: Http.Request): Http.Reply {
Http.Reply{ status: 200, headers: Map{}, body: "origin saw ${req.method} ${req.path}" }
}
func proxy(req: Http.Request): Http.Reply {
let reply = Http.request(req.method, "${upstream}${req.path}", req.bytes)
if reply is Err {
return Http.Reply{ status: 502, headers: Map{}, body: reply.error.message }
}
Http.Reply{ status: reply.value.status, headers: Map{}, body: reply.value.bytes }
}
go { _ = Http.serve(back, origin, stop) }
go { _ = Http.serve(front, proxy, stop) }
println((try Http.get("http://${Net.address(front)}/a/b")).text())
stop.cancel()origin saw GET /a/bProperties and relations
A request body arrives once, as received, in bytes, and text() decodes it. Content-Length is the reply body's length in bytes:
let listener = try Net.listen("127.0.0.1:0")
let stop = Context.new()
go {
_ = Http.serve(listener, { Http.Reply{ status: 200, headers: Map{}, body: x.text() } }, stop)
}
let reply = try Http.post("http://${Net.address(listener)}/", "naïve")
assert(reply.text() == "naïve")
assert(reply.bytes == "naïve".toBytes())
assert(reply.headers["Content-Length"] == "${reply.bytes.length()}")
stop.cancel()Http.serve closes its listener when it returns, so a listener cannot be served twice:
let listener = try Net.listen("127.0.0.1:0")
let stop = Context.new()
stop.cancel()
assert(Http.serve(listener, { Http.Reply{ status: 204, headers: Map{}, body: "" } }, stop).isOk())
println(Http.serve(listener, { Http.Reply{ status: 204, headers: Map{}, body: "" } }))Err{ error: Error{ message: "the listener is closed" } }Possible issues
A panic in a handler does not stop the server. The panic is reported on standard error, the client gets status 500 with an empty body, and the next request is served as usual:
func handle(req: Http.Request): Http.Reply {
if req.path == "/crash" { panic("handler bug") }
Http.Reply{ status: 200, headers: Map{}, body: "fine" }
}
let listener = try Net.listen("127.0.0.1:0")
let base = "http://${Net.address(listener)}"
let stop = Context.new()
go { _ = Http.serve(listener, handle, stop) }
let crashed = try Http.get("${base}/crash")
println("${crashed.status} '${crashed.text()}'")
println((try Http.get("${base}/")).text())
stop.cancel()500 ''
fineA reply status outside 200 to 599 panics in the handler's goroutine, so the client gets 500 as well. Two header names that differ only in case name one header and panic the same way:
func handle(req: Http.Request): Http.Reply {
if req.path == "/continue" {
return Http.Reply{ status: 100, headers: Map{}, body: "" }
}
Http.Reply{ status: 200, headers: Map{ "X-Id": "1", "x-id": "2" }, body: "" }
}
let listener = try Net.listen("127.0.0.1:0")
let base = "http://${Net.address(listener)}"
let stop = Context.new()
go { _ = Http.serve(listener, handle, stop) }
println((try Http.get("${base}/continue")).status)
println((try Http.get("${base}/twice")).status)
stop.cancel()500
500Closing the listener while Http.serve uses it is Err, not Ok. Cancel a context to stop a server cleanly:
let listener = try Net.listen("127.0.0.1:0")
let served: Channel[Result[void, Error]] = Channel.new(1)
go { served.send(Http.serve(listener, { Http.Reply{ status: 204, headers: Map{}, body: "" } })) }
Time.sleep(50ms)
listener.close()
println(served.receive())Err{ error: Error{ message: "the listener is closed" } }See also
Http.get, Http.post, Http.request, Http.Request, Http.Reply, Net.listen, Net.address, Os.interrupt, Context.new, Url.parseQuery
