Something went wrong. Try again.
Eio flows with progress bars
Something went wrong. Try again.
1.6 kB · 54 lines
OCaml
at main
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455open Eio
let get ~sw client uri = let resp, body = Cohttp_eio.Client.get ~sw client uri in match (Http.Response.status resp, Http.Response.content_length resp) with | `OK, Some size -> Eio_progress.of_source ~message:(Uri.to_string uri) ~size:(Int64.of_int size) body | `OK, None -> Eio_progress.of_source ~message:(Uri.to_string uri) ~size:(Int64.of_int 1) body | status, _ -> Fmt.epr "Err status: %a\n%!" Http.Status.pp status; Cohttp_eio.Body.of_string ""
let sites = [ "https://example.com"; "https://anil.recoil.org"; "https://ryan.freumh.org"; "https://patrick.sirref.org"; ]
let authenticator = match Ca_certs.authenticator () with | Ok x -> x | Error (`Msg m) -> Fmt.failwith "Failed to create system store X509 authenticator: %s" m
let https ~authenticator = let tls_config = match Tls.Config.client ~authenticator () with | Error (`Msg msg) -> failwith ("tls configuration problem: " ^ msg) | Ok tls_config -> tls_config in fun uri raw -> let host = Uri.host uri |> Option.map (fun x -> Domain_name.(host_exn (of_string_exn x))) in Tls_eio.client_of_flow ?host tls_config raw
let () = Mirage_crypto_rng_unix.use_default (); Eio_main.run @@ fun env -> let https = https ~authenticator in let client = Cohttp_eio.Client.make ~https:(Some https) env#net in Eio_progress.with_display @@ fun _ -> Eio.Switch.run @@ fun sw -> Fiber.List.iter ~max_fibers:1 (fun uri -> let s = Eio.Flow.read_all (get ~sw client (Uri.of_string uri)) in ignore s) sites