Network
Three namespaces cover the network. Http sends requests and serves them, Net opens TCP listeners and connections, and Url takes URLs and query strings apart and puts them back together. They meet at two points: Http.serve answers requests on a Net.Listener, and a handler reads its raw query with Url.parseQuery. Net is TCP only, with no TLS, UDP, or Unix sockets. The client speaks http and https, and Http.serve speaks plain HTTP.
func search(req: Http.Request): Http.Reply {
let query = Url.parseQuery(req.query)
if query is Err { return Http.Reply{ status: 400, headers: Map{}, body: query.error.message } }
let q = query.value["q"]?.first() ?? ""
Http.Reply{ status: 200, headers: Map{}, body: "${req.path}: ${q}" }
}
let listener = try Net.listen("127.0.0.1:0")
go { _ = Http.serve(listener, search) }
let url = try Url.parse("http://${listener.address()}/find")
url.query = Url.stringifyQuery(Map{ "q": ["ant & bee"] })
let res = try Http.get(url.stringify())
if res.status != 200 { panic("status ${res.status}") }
println(res.text())/find: ant & beeA TCP connection is a File.Handle, so the File functions read and write it. A line protocol is a loop over readLine:
let listener = try Net.listen("127.0.0.1:0")
defer { listener.close() }
go {
let conn = Result.unwrap(listener.accept())
defer { conn.close() }
let line = Result.unwrap(conn.readLine())
Result.unwrap(conn.writeLine("hello, ${line ?? "nobody"}"))
}
let conn = try Net.dial(listener.address())
defer { conn.close() }
try conn.writeLine("Ann")
println(try conn.readLine())hello, AnnEvery call that connects, listens, accepts, sends, or receives returns a Result: a failure to connect, a timeout, or a done context is Err. An HTTP response of any status is Ok, so check status before you read the body. The client follows no redirect: a 3xx comes back as it is, with its Location header. Header names arrive in canonical form, so read res.headers["Content-Type"], whatever case the server sent.
Functions by job
Sending HTTP requests
- Http.get: a GET request, and the whole response
- Http.post: a POST request with a body
- Http.request: a request with any method, body, and headers
- Http.Response: what the client gets back: status, headers, and the body as
bytes
Serving HTTP
- Http.serve: answer every request on a listener with a handler, each on a goroutine of its own
- Http.Request: what a handler gets: method, path, query, headers, body, and the client's address
- Http.Reply: what a handler returns: status, headers, and a body of text or
Bytes
Reading a body
- Http.text: the body of a response or a request, decoded as text
Listening for connections
- Net.listen: a TCP listener on
host:port, any free port with:0 - Net.address: the address a listener listens on
- Net.accept: the next connection that arrives on a listener
- Net.close: stop a listener, so pending and later accepts are
Err - Net.Listener: a listening TCP socket
Connecting
Talking over a connection
- File.readLine: the next line, or
nullat the end - File.readAll: everything until the other end closes
- File.writeLine: a line of text and a line break
- File.writeText: text as it is
- File.pipe: copy one handle into another until its end
- File.close: close the connection
Whole URLs
- Url.parse: a reference divided into its parts,
Errwhen it is malformed - Url.stringify: the reference a set of parts makes
- Url.Parts: the record of parts, from
schemetofragment
Query strings
- Url.parseQuery:
k=v&k=vas a map from each key to its values - Url.stringifyQuery: a map of keys and values written as a query
- Url.escape: one query key or value with
%XXescapes and a space as+ - Url.unescape: the reverse,
Erron a bad escape
Bounding and stopping
timeout:on Http.request, Http.get, and Http.post: a bound on one request, from connecting to the end of the body- Context: a deadline or a cancel shared by several calls
- Os.interrupt: a context done on Ctrl-C, to stop a server that runs on the main path
See also
Http · Net · Url · Files and paths · Concurrency · Data formats
