Slide 29 of 37

Networking: HTTP

proc handle(request: hive.net.HttpRequest): hive.net.HttpResponse {
	headers := [["Content-Type", "text/plain"]]
	return hive.net.HttpResponse(200, headers, "hello, {request.url}!\n")
}

proc main(): void {
	hive.net.httpServe(8080, handle)
}
hive.net's HTTP corner needs no external dependency in either direction. httpServe(port, handler) starts a server that blocks forever, calling a proc(HttpRequest): HttpResponse once per request on a thread of its own — exactly the shape of the handler serving this very page. httpRequest performs a client request and answers with a Result, since getting no response at all — a bad URL, a refused connection, a timeout — is a Result.Error rather than an empty HttpResponse. resolve and localAddress round out the module: the first looks up every address behind a name, the second is the address another machine would reach this one on.