A HTTP client for MirageOS
Find a file
Reynir Björnsson c6cd38db9c Require mirage-entropy>=0.5.0 with test
mirage-random-stdlib requires mirage-entropy, and the opam solver may
choose mirage-entropy.0.4.1 which requires we provide `Mirage_os`
somehow.
2023-02-02 09:09:32 +01:00
src Put a comment about the given buffer & the ownership story 2023-02-01 12:53:45 +01:00
test Apply ocamlformat on our tests 2022-11-01 14:41:10 +01:00
.gitignore Add a .gitignore 2022-10-19 11:49:01 +02:00
.ocamlformat Add an .ocamlformat - from decompress distribution 2022-10-19 11:49:17 +02:00
CHANGES.md Prepare for release 0.0.2 2023-02-01 12:53:16 +01:00
dune-project initial commit (copy from opam-mirror) 2022-10-05 13:58:07 +02:00
http-mirage-client.opam Require mirage-entropy>=0.5.0 with test 2023-02-02 09:09:32 +01:00
LICENSE.md Add a LICENSE.md (MIT) 2022-10-17 14:36:24 +02:00
README.md adjust documentation 2022-10-27 20:59:49 +02:00

An HTTP (http/1.1 or h2) client for MirageOS

This little library provides an HTTP client which can be usable inside a unikernel/MirageOS. It follows the same API as http-lwt-client which is pretty simple and uses:

This library wants to be easy to use and it is associated to a MirageOS device in order to facilite functoria to compose everything (mainly the TCP/IP stack) according to the user's target and give a witness so as to be able to allocate a new connection to a peer and process the HTTP flow.

How to use it?

First, you need to describe a new http_client device:

open Mirage

type http_client = HTTP_client
let http_client = typ HTTP_client

let http_client =
  let connect _ modname = function
    | [ _pclock; _tcpv4v6; ctx ] ->
      Fmt.str {ocaml|%s.connect %s|ocaml} modname ctx
    | _ -> assert false in
  impl ~connect "Http_mirage_client.Make"
    (pclock @-> tcpv4v6 @-> git_client @-> http_client)

Then, you can decide how to construct such device:

let stack = generic_stackv4v6 default_network
let dns   = generic_dns_client stack
let tcp   = tcpv4v6_of_stackv4v6 stack

let http_client =
  (* XXX(dinosaure): it seems unconventional to use [git_happy_eyeballs] here
     when we want to do HTTP requests only. The name was not so good and we
     will fix that into the next release of the mirage tool. But structurally,
     you don't bring anything related to Git. It's just a bad choice of name. *)
  let happy_eyeballs = git_happy_eyeballs stack dns
    (generic_happy_eyeballs stack dns) in
  http_client $ default_posix_clock $ tcp $ happy_eyeballs

Finally, you can use the witness into your unikernel.ml:

open Lwt.Infix

module Make (HTTP_client : Http_mirage_client.S) = struct
  let start http_client =
    let body_f _response acc data = Lwt.return (acc ^ data) in
    Http_mirage_client.one_request http_client "https://mirage.io/" body_f ""
    >>= function
    | Ok (resp, body) -> ...
    | Error _ -> ...
end