stdhttp - anssihalmeaho/funl GitHub Wiki

stdhttp

Services for HTTP client and server implementations.

Client procedures

Procedures for HTTP client to use.

do

Makes HTTP request from client to server.

type: procedure

Arguments:

  1. Method (string)
  2. URL (string)
  3. Header (map)
  4. 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)

do-with

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:

  1. Method (string)
  2. Extra information (map)
  3. URL (string)
  4. Header (map)
  5. 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)

Server Opaque Types

Opaque Type: http-mux

Represents HTTP multiplexer. It routes HTTP requests to handler procedures which are registered to it.

Opaque Type: http-response-writer

HTTP request handler procedure uses response writer when writing response.

Server procedures

Procedures for HTTP server to use.

mux

Returns new opaque:mux instance.

type: procedure

Format:

call(stdhttp.mux) -> <opaque:http-mux>

Return value: opaque:http-mux

shutdown

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

reg-handler

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:

  1. mux (opaque:http-mux)
  2. pattern (string)
  3. 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)

listen-and-serve

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')

listen-and-serve-tls

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:

  1. mux (opaque:http-mux)
  2. filename for containing a certificate (string)
  3. filename for matching private key (string)
  4. 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')

write-response

Writes/sends HTTP response.

type: procedure

Arguments:

  1. response writer (opaque:http-response-writer)
  2. HTTP status code (int)
  3. 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

set-response-header

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:

  1. response writer (opaque:http-response-writer)
  2. 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'))

add-response-header

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:

  1. response writer (opaque:http-response-writer)
  2. 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'))
⚠️ **GitHub.com Fallback** ⚠️