stdhttp - anssihalmeaho/funl GitHub Wiki
Services for HTTP client and server implementations.
Procedures for HTTP client to use.
Makes HTTP request from client to server.
type: procedure
Arguments:
- Method (string)
- URL (string)
- Header (map)
- Body (opaque:bytearray) (Optional)
Format:
call(stdhttp.do <method-string> <URL-string> <header-map> <body-bytearray>) -> <response-map> or <error-string>
call(stdhttp.do <method-string> <URL-string> <header-map>) -> <response-map> or <error-string>
Return value: response (map) or in case of failure error description (string)
Response map contains:
| Response key | Meaning |
|---|---|
| 'header' | response header (map) |
| 'status-text' | HTTP status text (string) |
| 'status-code' | HTTP status code (int) |
| 'body' | response body (opaque:bytearray) |
Makes HTTP request from client to server with extra information for connection. It works otherwise just like stdhttp.do but 1st argument is map containing addtional information for request.
type: procedure
Arguments:
- Method (string)
- Extra information (map)
- URL (string)
- Header (map)
- Body (opaque:bytearray) (Optional)
Extra map can have follwoing key-values:
| Key | Meaning |
|---|---|
| 'timeout' | time limit for response in nanoseconds (int) |
| 'insecure' | true/false (bool) If true TLS accepts any certificate presented by the server and any host name in that certificate |
Format:
call(stdhttp.do-with <extra-map> <method-string> <URL-string> <header-map> <body-bytearray>) -> <response-map> or <error-string>
call(stdhttp.do-with <extra-map> <method-string> <URL-string> <header-map>) -> <response-map> or <error-string>
Return value: response (map) or in case of failure error description (string)
Represents HTTP multiplexer. It routes HTTP requests to handler procedures which are registered to it.
HTTP request handler procedure uses response writer when writing response.
Procedures for HTTP server to use.
Returns new opaque:mux instance.
type: procedure
Format:
call(stdhttp.mux) -> <opaque:http-mux>
Return value: opaque:http-mux
Gracefully shuts down the server/mux without interrupting any active connections.
type: procedure
Format:
call(stdhttp.shutdown <opaque:http-mux>) -> <string>
Return value: string
Return value is:
- '' if shutdown succeeded
- in case of some failure description of error
Registers HTTP handler to mux (opaque:http-mux) (1st argument). Pattern for matching URL is given as 2nd argument (string). Procedure which is called for HTTP request is given as 3rd argument.
type: procedure
Arguments:
- mux (opaque:http-mux)
- pattern (string)
- handler (procedure)
Handler format is following:
proc(<opaque:http-response-writer> <request-map>) -> <return-value-not-used>
Request map (2nd argument for handler) contains:
| Key | Value/Meaning |
|---|---|
| 'method' | method (string), for example 'POST', 'GET' |
| 'header' | request header (map) |
| 'query' | query parameters (map) |
| 'body' | request body (opaque:bytearray) |
| 'URI' | Request-target of the Request-Line (RFC 7230, Section 3.1.1) (string) |
Format:
call(stdhttp.reg-handler <opaque:http-mux> <pattern-string> <procedure>) -> true
Example:
call(stdhttp.reg-handler mux '/todo/items' handler)
Listens TCP connection on address given as 2nd argument. Blocks until listening is stopped because of error or shutdown. Mux is given as 1st argument and it calls handler procedures for incoming requests.
type: procedure
Format:
call(stdhttp.listen-and-serve <opaque:http-mux> <address-string>) -> <string>
Return value: string
Return value is:
- '' if there are no error
- in case of some failure description of error
Example:
call(stdhttp.listen-and-serve mux ':8003')
Listens incoming TLS connections. Similar to stdhttp.listen-and-serve. Filenames containing a certificate and matching private key for the server must be provided. Address where to listen is optional (4th) argument (if not given default HTTPS port 443 is listened).
type: procedure
Arguments:
- mux (opaque:http-mux)
- filename for containing a certificate (string)
- filename for matching private key (string)
- address (string) (optional)
Format:
call(stdhttp.listen-and-serve-tls <opaque:http-mux> <cert-file-string> <key-file-string> <address-string>) -> <string>
call(stdhttp.listen-and-serve-tls <opaque:http-mux> <cert-file-string> <key-file-string>) -> <string>
Return value: string
Return value is:
- '' if there are no error
- in case of some failure description of error
Examples:
call(stdhttp.listen-and-serve-tls mux 'cert.pem' 'key.pem' ':8003')
call(stdhttp.listen-and-serve-tls mux 'cert.pem' 'key.pem')
Writes/sends HTTP response.
type: procedure
Arguments:
- response writer (opaque:http-response-writer)
- HTTP status code (int)
- response body (opaque:bytearray)
Format:
call(stdhttp.write-response <opaque:http-response-writer> <int> <opaque:bytearray>) -> <string>
Return value: string
Return value is:
- '' if there are no error
- in case of some failure description of error
Writes header to HTTP response. Header key-values are written from map given as 2nd argument. It replaces any existing values associated with key. (Note. this is difference with stdhttp.add-response-header)
type: procedure
Arguments:
- response writer (opaque:http-response-writer)
- map from which key-values are written to response header
Format:
call(stdhttp.set-response-header <opaque:http-response-writer> <map>) -> true
Return value: true (bool)
Example:
call(stdhttp.set-response-header w map('Content-Type' 'application/json'))
Writes header to HTTP response. Header key-values are written from map given as 2nd argument. It appends to any existing values associated with key. (Note. this is difference with stdhttp.set-response-header)
type: procedure
Arguments:
- response writer (opaque:http-response-writer)
- map from which key-values are written to response header
Format:
call(stdhttp.add-response-header <opaque:http-response-writer> <map>) -> true
Return value: true (bool)
Example:
call(stdhttp.add-response-header w map('Content-Type' 'application/json'))