// //------------------ DOK FILE -------------------------------------------------- +Header core.net.http.cli =pkg core.net.http.cli =title core.net.http.cli =short http requests =desc This library allows to make http requests. This can be done with the simple synchronous function [[httpGet]], or with step-by-step functions [[httpCreate]], ..., [[httpSend]]. The source file of [[httpGet]] is a good illustration: fun httpGet (url)= await (lambda (join)= httpSend (httpCreate (nil, url, HTTP_1_0), lambda (code, data)= joinSend (join, data)) );; // // //------------------ PROTO ----------------------------------------------------- httpGet +Proto httpGet =type fun Str -> Str =mode function =pkg core.net.http.cli =impl mcy =link =desc This function performs a simple synchronous http request. It returns nil on a network error. However a html error such 404 (file not found) often returns a page and therefore a body content. > httpGet ("https://minimacy.net/robots.txt") >-> Str: "User-agent: *\\n" > httpGet ("https://minimacy.net/thisFileDoesNotExist") >-> Str: "\\n\\n404 Not Found\\n\\n

Not Found

\\n

The requested URL was not found on this server.

\\n\\n" > httpGet ("https://thisSiteDoesNotExist") >-> Str: nil For more complex interaction, see [[httpCreate]] and [[httpSend]]. // +arg url =type Str =desc A complete url // +arg result =type Str =desc The body of the reply, or nil when an error occured // // //------------------ PROTO ----------------------------------------------------- httpSend +Proto httpSend =type fun Http (fun Int Str -> a1) -> Http =mode function =pkg core.net.http.cli =impl mcy =link =desc This function sends the request on the net. When the request is complete, it calls the lambda function fDone with 2 parameters: - code: the code extracted from the first line of the reply. - content: the reply's body // +arg h =type Http =desc A http client issued by [[httpCreate]] // +arg fDone =type fun Int Str -> a1 =desc A lambda function with 2 parameters: code, content // +arg result =type Http =desc The same http client // // //------------------ PROTO ----------------------------------------------------- httpSetMultipart +Proto httpSetMultipart =type fun Http list HttpArgs -> Str =mode function =pkg core.net.http.cli =impl mcy =link =desc This function encodes a list of contents into a multipart post data, and sets the Content-Type of the http client. // +arg h =type Http =desc A http client // +arg l =type list HttpArgs =desc A list of contents // +arg result =type Str =desc The resulting post data // // //------------------ PROTO ----------------------------------------------------- fileArg +Proto fileArg =type fun Str Str Str -> HttpArgs =mode constructor =pkg core.net.http.cli =impl mcy =link =desc This is the constructor for a file multipart content, with name and file name and file content. > echo httpSetMultipart (nil, (fileArg "foo" "foo.txt" load ("foo.txt"))::nil) >------d0996b09496c3fe42b159add97df305d0e >Content-Disposition: form-data; name="foo"; filename="foo.txt" >Content-Type: text/plain > >[foo.txt content...] >------d0996b09496c3fe42b159add97df305d0e // // //------------------ PROTO ----------------------------------------------------- stringArg +Proto stringArg =type fun Str Str -> HttpArgs =mode constructor =pkg core.net.http.cli =impl mcy =link =desc This is the constructor for a string multipart content, with name and content. > echo httpSetMultipart (nil, (stringArg "foo" "mycontent")::nil) >------ac950a36bff56eddbd1b239229d0c870ae >Content-Disposition: form-data; name="foo" > >mycontent >------ac950a36bff56eddbd1b239229d0c870ae // // //------------------ PROTO ----------------------------------------------------- HttpArgs +Proto HttpArgs =type HttpArgs =mode sum =pkg core.net.http.cli =impl mcy =link =desc A sum type to define contents in a multipart post data request. // // //------------------ PROTO ----------------------------------------------------- httpSetClientCertificate +Proto httpSetClientCertificate =type fun Http Str Str -> Str =mode function =pkg core.net.http.cli =impl mcy =link =desc This function allows to use a client certificate to authenticate the request. It must have been signed by the server certificate which is provided in [[httpSrvStart]] under the parameter authForClientCert. // +arg h =type Http =desc A http client // +arg clientCertificate =type Str =desc A client certificate (PEM content) // +arg clientKey =type Str =desc A client key (PEM content) // +arg result =type Str =desc The same client key // // //------------------ PROTO ----------------------------------------------------- httpSetCipherSuites +Proto httpSetCipherSuites =type fun Http list Int -> list Int =mode function =pkg core.net.http.cli =impl mcy =link =desc This function defines the cipher suite to use in the request. // +arg h =type Http =desc A http client // +arg cipherSuites =type list Int =desc A list of cipherSuites // +arg result =type list Int =desc The same list // // //------------------ PROTO ----------------------------------------------------- httpSetPostData +Proto httpSetPostData =type fun Http Str -> Str =mode function =pkg core.net.http.cli =impl mcy =link =desc This function defines post data in an http client. // +arg h =type Http =desc A http client // +arg data =type Str =desc Any string // +arg result =type Str =desc The same string // // //------------------ PROTO ----------------------------------------------------- httpCreate +Proto httpCreate =type fun Str Str Str -> Http =mode function =pkg core.net.http.cli =impl mcy =link =desc This function prepares an http request, but does not perform it. It will be done with [[httpSend]], but before that the request may be refined with headers, post data, ... // +arg verb =type Str =desc A request verb (POST, PUT). nil means GET. // +arg url =type Str =desc An url // +arg version =type Str =desc A version [[HTTP_1_0]] or [[HTTP_1_1]] // +arg result =type Http =desc An Http request // // //------------------ PROTO ----------------------------------------------------- httpReuse +Proto httpReuse =type fun Http Str Str Str -> Http =mode function =pkg core.net.http.cli =impl mcy =link =desc This function is similar to [[httpCreate]] except that it doesn't create a new http request, it reuse an existing one. Note that this can be done only for [[HTTP_1_1]] requests. It speeds up the connection especially for https requests. There is no need to test if the connection is still on, as the [[httpSend]] function will check for it and start a new connection when needed. It is possible to reuse a http request only when the previous request is done. // +arg h =type Http =desc A http client first issued by [[httpCreate]] // +arg verb =type Str =desc A request verb (POST, PUT). nil means GET. // +arg url =type Str =desc An url // +arg version =type Str =desc A version [[HTTP_1_0]] or [[HTTP_1_1]] // +arg result =type Http =desc An Http request // // //------------------ PROTO ----------------------------------------------------- httpFormUrlEncodedFromList +Proto httpFormUrlEncodedFromList =type fun list [Str Str] -> Str =mode function =pkg core.net.http.cli =impl mcy =link =desc This function encodes a list of values into a uri. > httpFormUrlEncodedFromList (["foo", "F O O"]::["bar", "BAR"]::nil) >-> Str: "foo=F+O+O&bar=BAR" // +arg lArgs =type list [Str Str] =desc A list of tuples [key value] // +arg result =type Str =desc A string // // //------------------ PROTO ----------------------------------------------------- httpGetRemote +Proto httpGetRemote =type fun Http -> [Str Int] =mode function =pkg core.net.http.cli =impl mcy =link =desc This function returns the remote ip address and port. // +arg h =type Http =desc A http client // +arg result =type [Str Int] =desc A tuple [ip_address port] // // //------------------ PROTO ----------------------------------------------------- httpSetHeader +Proto httpSetHeader =type fun Http Str Str -> Http =mode function =pkg core.net.http.cli =impl mcy =link =desc This function defines a header to send with the client http request. When value is nil, the header is removed. // +arg h =type Http =desc A http client // +arg key =type Str =desc A header name // +arg val =type Str =desc A header value // +arg result =type Http =desc The same http client // // //------------------ PROTO ----------------------------------------------------- httpGet12 +Proto httpGet12 =type fun Str -> Str =mode Function =pkg core.net.http.cli =impl mcy =link =desc // +arg url =type Str =desc // +arg result =type Str =desc // // //------------------ PROTO ----------------------------------------------------- httpSetUrlEncoded +Proto httpSetUrlEncoded =type fun Http list [a1 Str] -> Str =mode Function =pkg core.net.http.cli =impl mcy =link =desc // +arg h =type Http =desc // +arg l =type list [a1 Str] =desc // +arg result =type Str =desc // // //------------------ PROTO ----------------------------------------------------- httpForceTls12 +Proto httpForceTls12 =type fun Http -> Http =mode Function =pkg core.net.http.cli =impl mcy =link =desc // +arg h =type Http =desc // +arg result =type Http =desc // // //------------------ PROTO ----------------------------------------------------- httpSetTimeout +Proto httpSetTimeout =type fun Http Int -> Int =mode Function =pkg core.net.http.cli =impl mcy =link =desc // +arg h =type Http =desc // +arg timeout =type Int =desc // +arg result =type Int =desc // // //------------------ PROTO ----------------------------------------------------- httpGetHeader +Proto httpGetHeader =type fun Http Str -> Str =mode Function =pkg core.net.http.cli =impl mcy =link =desc // +arg h =type Http =desc // +arg headerName =type Str =desc // +arg result =type Str =desc // // //------------------ PROTO ----------------------------------------------------- httpGetHeaders +Proto httpGetHeaders =type fun Http -> (hashmap Str -> Str) =mode Function =pkg core.net.http.cli =impl native =link =desc // +arg A =type Http =desc // +arg result =type hashmap Str -> Str =desc // // //------------------ PROTO ----------------------------------------------------- httpClose +Proto httpClose =type fun Http -> a1 =mode function =pkg core.net.http.cli =impl mcy =link =desc // +arg A =type Http =desc // +arg result =type a1 =desc // // //------------------ PROTO ----------------------------------------------------- httpGetCertificateChain +Proto httpGetCertificateChain =type fun Http -> list X509 =mode function =pkg core.net.http.cli =impl mcy =link =desc // +arg A =type Http =desc // +arg result =type list X509 =desc