// //------------------ DOK FILE -------------------------------------------------- +Header core.net.http.srv =pkg core.net.http.srv =title core.net.http.srv =short a http server including websocket capability =desc This library makes it easy to run a http server: - http and https are supported, even with multi-site - it handles regular http request and also websocket connection use core.net.http.srv;; fun httpServer()= httpSrvStart (nil, nil, 1234, lambda (h, verb, uri)= "Hello world!" );; // // //------------------ PROTO ----------------------------------------------------- COOKIE_SAMESITE_NONE +Proto COOKIE_SAMESITE_NONE =type Int =mode constant =pkg core.net.http.srv =impl mcy =link =desc Http cookie flag // // //------------------ PROTO ----------------------------------------------------- COOKIE_SAMESITE_STRICT +Proto COOKIE_SAMESITE_STRICT =type Int =mode constant =pkg core.net.http.srv =impl mcy =link =desc Http cookie flag // // //------------------ PROTO ----------------------------------------------------- COOKIE_SAMESITE_LAX +Proto COOKIE_SAMESITE_LAX =type Int =mode constant =pkg core.net.http.srv =impl mcy =link =desc Http cookie flag // // //------------------ PROTO ----------------------------------------------------- COOKIE_HTTP_ONLY +Proto COOKIE_HTTP_ONLY =type Int =mode constant =pkg core.net.http.srv =impl mcy =link =desc Http cookie flag // // //------------------ PROTO ----------------------------------------------------- COOKIE_SECURE +Proto COOKIE_SECURE =type Int =mode constant =pkg core.net.http.srv =impl mcy =link =desc Http cookie flag // // //------------------ PROTO ----------------------------------------------------- HttpConnection +Proto HttpConnection =type HttpConnection =mode structure =pkg core.net.http.srv =impl mcy =link =desc The http server type, or more precisely the server side of an http connection. For each incoming request a [[HttpConnection]] structure is created and processed in a dedicated thread. It always starts as http and may become a websocket. In [[HTTP_1_1]] mode, it can process several requests on the same [[HttpConnection]] structure. // // //------------------ PROTO ----------------------------------------------------- httpWsClose +Proto httpWsClose =type fun HttpConnection -> Int =mode function =pkg core.net.http.srv =impl mcy =link =desc This function sends a CLOSE message on a websocket and closes the connection. // +arg h =type HttpConnection =desc A http server connection // +arg result =type Int =desc Always 0 // // //------------------ PROTO ----------------------------------------------------- httpWsSend +Proto httpWsSend =type fun HttpConnection Int Str -> Int =mode function =pkg core.net.http.srv =impl mcy =link =desc This function sends a message on a websocket. // +arg h =type HttpConnection =desc A http server connection // +arg opcode =type Int =desc A Websocket Opcode such as WS_OPCODE_TEXT // +arg data =type Str =desc Any string // +arg result =type Int =desc Always 0 // // //------------------ PROTO ----------------------------------------------------- httpSrvParseArgs +Proto httpSrvParseArgs =type fun HttpConnection -> [(hashmap Str -> Str) (hashmap Str -> [Str Str])] =mode function =pkg core.net.http.srv =impl mcy =link =desc This function retrieve the request arguments, whatever the encoding was. - the first hashmap is for simple content: key -> value - the second hashmap is for files: key -> [file_name file_content] // +arg h =type HttpConnection =desc A http server connection // +arg result =type [(hashmap Str -> Str) (hashmap Str -> [Str Str])] =desc A tuple with two hashmaps // // //------------------ PROTO ----------------------------------------------------- httpSrvGetClientCertificateLogin +Proto httpSrvGetClientCertificateLogin =type fun HttpConnection -> Str =mode function =pkg core.net.http.srv =impl mcy =link =desc This function returns the authenticated login of the request, when the request is done with a client certificate. It returns nil when no certificate. // +arg h =type HttpConnection =desc A http server connection // +arg result =type Str =desc A login or nil // // //------------------ PROTO ----------------------------------------------------- httpSrvGetClientCertificate +Proto httpSrvGetClientCertificate =type fun HttpConnection -> X509 =mode function =pkg core.net.http.srv =impl mcy =link =desc This function returns the client certificate, when the request is done with a client certificate. Then the result is the certificate. See [core.crypto.cer](page://ref.core.crypto.cer). It returns nil when no certificate. // +arg h =type HttpConnection =desc A http server connection // +arg result =type X509 =desc A hashmap // // //------------------ PROTO ----------------------------------------------------- httpSrvSetStatus +Proto httpSrvSetStatus =type fun HttpConnection Str Int Str -> Int =mode function =pkg core.net.http.srv =impl mcy =link =desc This function sets the reply status. // +arg h =type HttpConnection =desc A http server connection // +arg version =type Str =desc The version, usually "HTTP/1.1" // +arg code =type Int =desc A code which should be 200 when everything is fine // +arg text =type Str =desc A string which should be "OK" when everything is fine // +arg result =type Int =desc Always 0 // // //------------------ PROTO ----------------------------------------------------- httpSrvGetPostData +Proto httpSrvGetPostData =type fun HttpConnection -> Str =mode function =pkg core.net.http.srv =impl mcy =link =desc This function returns the post data of the request. // +arg h =type HttpConnection =desc A http server connection // +arg result =type Str =desc A string // // //------------------ PROTO ----------------------------------------------------- httpSrvGetRequestHeaders +Proto httpSrvGetRequestHeaders =type fun HttpConnection -> (hashmap Str -> Str) =mode function =pkg core.net.http.srv =impl mcy =link =desc This function returns the hashmap of the request headers key->value. Keys are always lowercase. // +arg h =type HttpConnection =desc A http server connection // +arg result =type hashmap Str -> Str =desc A hashmap // // //------------------ PROTO ----------------------------------------------------- httpSrvGetRequest +Proto httpSrvGetRequest =type fun HttpConnection -> [Str Str Str] =mode function =pkg core.net.http.srv =impl mcy =link =desc This function returns the 3-tuple of the request. // +arg h =type HttpConnection =desc A http server connection // +arg result =type [Str Str Str] =desc A 3-tuple [verb uri version] // // //------------------ PROTO ----------------------------------------------------- httpSrvGetCookie +Proto httpSrvGetCookie =type fun HttpConnection Str -> Str =mode function =pkg core.net.http.srv =impl mcy =link =desc This function returns the value of a specific cookie in the request. // +arg h =type HttpConnection =desc A http server connection // +arg cookie =type Str =desc A cookie name // +arg result =type Str =desc A string or nil // // //------------------ PROTO ----------------------------------------------------- httpSrvGetCookies +Proto httpSrvGetCookies =type fun HttpConnection -> (hashmap Str -> Str) =mode function =pkg core.net.http.srv =impl mcy =link =desc This function returns all the request headers. // +arg h =type HttpConnection =desc A http server connection // +arg result =type hashmap Str -> Str =desc A hashmap cookie_name->cookie_value // // //------------------ PROTO ----------------------------------------------------- httpSrvSetCookie +Proto httpSrvSetCookie =type fun HttpConnection a1 a2 Int Str Str Int -> HttpConnection =mode function =pkg core.net.http.srv =impl mcy =link =desc This function adds a cookie to the reply. // +arg h =type HttpConnection =desc A http server connection // +arg name =type a1 =desc The cookie name // +arg value =type a2 =desc The cookie value // +arg expires =type Int =desc A timestamp in seconds // +arg path =type Str =desc A path // +arg domain =type Str =desc A domain // +arg attr =type Int =desc A composition of flags such as [[COOKIE_SAMESITE_NONE]] // +arg result =type HttpConnection =desc The same http server connection // // //------------------ PROTO ----------------------------------------------------- httpSrvRedirect +Proto httpSrvRedirect =type fun HttpConnection Str -> Str =mode function =pkg core.net.http.srv =impl mcy =link =desc This function replies to the http request with a redirection to url. // +arg h =type HttpConnection =desc A http server connection // +arg url =type Str =desc A url // +arg result =type Str =desc Always the empty string "" // // //------------------ PROTO ----------------------------------------------------- httpSrvSetHeader +Proto httpSrvSetHeader =type fun HttpConnection Str Str -> HttpConnection =mode function =pkg core.net.http.srv =impl mcy =link =desc This function defines the value of a specific header. It removes the previous value if any. // +arg h =type HttpConnection =desc A http server connection // +arg key =type Str =desc An header key // +arg val =type Str =desc A value // +arg result =type HttpConnection =desc The same http server connection // // //------------------ PROTO ----------------------------------------------------- httpSrvAddHeader +Proto httpSrvAddHeader =type fun HttpConnection Str Str -> HttpConnection =mode function =pkg core.net.http.srv =impl mcy =link =desc This function adds a value for a specific header. This can lead to multiple lines for the same header key, as for Set-Cookie. // +arg h =type HttpConnection =desc A http server connection // +arg key =type Str =desc An header key // +arg val =type Str =desc A value // +arg result =type HttpConnection =desc The same http server connection // // //------------------ PROTO ----------------------------------------------------- httpSrvGetRemote +Proto httpSrvGetRemote =type fun HttpConnection -> [Str Int] =mode function =pkg core.net.http.srv =impl mcy =link =desc This function returns network information on the client of the request. // +arg h =type HttpConnection =desc A http server connection // +arg result =type [Str Int] =desc A tuple [ip_address port] // // //------------------ PROTO ----------------------------------------------------- httpSrvStart +Proto httpSrvStart =type fun list [Str Str list Str Str] Str Int (fun HttpConnection Str Str -> Str) -> HttpSrv =mode Function =pkg core.net.http.srv =impl mcy =link =desc This function creates a simple http/https server. When hostList is not nil it is the list of cryptographic content for each host [privateKey pwd certList authForClientCert]: - privateKey: the content of a pem file - pwd: the password for this privateKey (nil if no password) - certList: the list of certificates to send to the clients (each certificate is the content of an unencrypted pem file) - authForClientCert: if not nil, this is the content of the unencrypted pem certificate to use to authenticate clients. The lambda function onRequest is called once a request is received. This lambda function is expected to return the resulting page content as a string. // +arg hostList =type list [Str Str list Str Str] =desc A list of tuples [privateKey pwd certList authForClientCert] // +arg ip =type Str =desc The local ip to listen to (nil for all) // +arg port =type Int =desc The local port to listen to // +arg onRequest =type fun HttpConnection Str Str -> Str =desc A lambda function // +arg result =type HttpSrv =desc A Http server structure // // //------------------ PROTO ----------------------------------------------------- httpSrvSetIdleTimeout +Proto httpSrvSetIdleTimeout =type fun HttpConnection Int -> HttpConnection =mode function =pkg core.net.http.srv =impl mcy =link =desc // +arg h =type HttpConnection =desc // +arg timeout =type Int =desc // +arg result =type HttpConnection =desc // // //------------------ PROTO ----------------------------------------------------- httpSrvGetServerName +Proto httpSrvGetServerName =type fun HttpConnection -> Str =mode Function =pkg core.net.http.srv =impl mcy =link =desc // +arg h =type HttpConnection =desc // +arg result =type Str =desc // // //------------------ PROTO ----------------------------------------------------- httpSrvGetRequestHeader +Proto httpSrvGetRequestHeader =type fun HttpConnection Str -> Str =mode Function =pkg core.net.http.srv =impl mcy =link =desc // +arg h =type HttpConnection =desc // +arg headerName =type Str =desc // +arg result =type Str =desc // // //------------------ PROTO ----------------------------------------------------- httpSrvOnMessage +Proto httpSrvOnMessage =type fun HttpConnection (fun Int Str -> a1) -> (fun Int Str -> Bool) =mode Function =pkg core.net.http.srv =impl mcy =link =desc // +arg h =type HttpConnection =desc // +arg cb =type fun Int Str -> a1 =desc // +arg result =type fun Int Str -> Bool =desc // // //------------------ PROTO ----------------------------------------------------- httpSrvOnWebSocket +Proto httpSrvOnWebSocket =type fun HttpConnection (fun Str Str -> Bool) -> (fun Str Str -> Bool) =mode Function =pkg core.net.http.srv =impl mcy =link =desc // +arg h =type HttpConnection =desc // +arg cb =type fun Str Str -> Bool =desc // +arg result =type fun Str Str -> Bool =desc // // //------------------ PROTO ----------------------------------------------------- httpSrvOnRequest +Proto httpSrvOnRequest =type fun HttpConnection (fun Str Str -> Str) -> (fun Str Str -> Str) =mode Function =pkg core.net.http.srv =impl mcy =link =desc // +arg h =type HttpConnection =desc // +arg cb =type fun Str Str -> Str =desc // +arg result =type fun Str Str -> Str =desc // // //------------------ PROTO ----------------------------------------------------- httpSrvStop +Proto httpSrvStop =type fun HttpSrv -> Bool =mode Function =pkg core.net.http.srv =impl mcy =link =desc // +arg srv =type HttpSrv =desc // +arg result =type Bool =desc // // //------------------ PROTO ----------------------------------------------------- httpSrvSetDefaultIdleTimeout +Proto httpSrvSetDefaultIdleTimeout =type fun HttpSrv Int -> HttpSrv =mode Function =pkg core.net.http.srv =impl mcy =link =desc // +arg srv =type HttpSrv =desc // +arg timeout =type Int =desc // +arg result =type HttpSrv =desc // // //------------------ PROTO ----------------------------------------------------- httpSrvStartExt +Proto httpSrvStartExt =type fun list [Str Str list Str Str] Str Int (fun HttpConnection -> Bool) -> HttpSrv =mode Function =pkg core.net.http.srv =impl mcy =link =desc // +arg hostList =type list [Str Str list Str Str] =desc // +arg ip =type Str =desc // +arg port =type Int =desc // +arg onConnect =type fun HttpConnection -> Bool =desc // +arg result =type HttpSrv =desc // // //------------------ PROTO ----------------------------------------------------- httpSrvUpdateHostList +Proto httpSrvUpdateHostList =type fun HttpSrv list [Str Str list Str Str] -> Bool =mode Function =pkg core.net.http.srv =impl mcy =link =desc // +arg srv =type HttpSrv =desc // +arg hostList =type list [Str Str list Str Str] =desc // +arg result =type Bool =desc // // //------------------ PROTO ----------------------------------------------------- HttpSrv +Proto HttpSrv =type HttpSrv =mode structure =pkg core.net.http.srv =impl mcy =link =desc // // //------------------ PROTO ----------------------------------------------------- httpSrvSetMaxConnections +Proto httpSrvSetMaxConnections =type fun HttpSrv Int Int -> HttpSrv =mode function =pkg core.net.http.srv =impl mcy =link =desc // +arg srv =type HttpSrv =desc // +arg maxConnections =type Int =desc // +arg evictionTimeout =type Int =desc // +arg result =type HttpSrv =desc // // //------------------ PROTO ----------------------------------------------------- httpSrvSetMaxRequestSize +Proto httpSrvSetMaxRequestSize =type fun HttpConnection Int -> HttpConnection =mode function =pkg core.net.http.srv =impl mcy =link =desc // +arg h =type HttpConnection =desc // +arg maxRequestSize =type Int =desc // +arg result =type HttpConnection =desc