1 (** curl lwt example *)
8 let printfn fmt
= ksprintf print_endline fmt
10 let curl_setup_simple h
=
12 set_useragent h
"Curl_lwt";
14 set_connecttimeout h
5;
16 set_followlocation h
false;
18 set_encoding h CURL_ENCODING_ANY
22 let url = get_effectiveurl h
in
23 printfn "%3d %.2f %g URL: %s (%s)%s"
27 (if get_httpcode h
/ 100 = 3 then sprintf
"%s -> %s" url (get_redirecturl h
) else url)
29 (match code
with CURLE_OK
-> "" | _
-> sprintf
" %s (%d)" (strerror code
) (errno code
))
32 let b = Buffer.create
16 in
33 Curl.set_writefunction h
(fun s
-> Buffer.add_string
b s
; String.length s
);
34 Lwt.bind
(Curl_lwt.perform h
) (fun code
-> Lwt.return
(code
, Buffer.contents
b))
37 let h = Curl.init
() in
40 begin try%lwt
(* e.g. Canceled *)
41 let%lwt
(code
,_body
) = download h in
44 (* do something with body *)
46 printfn "EXN %s URL: %s" (Printexc.to_string exn
) url;
48 end[%lwt
.finally
Curl.cleanup
h; Lwt.return
()]
57 "forge.ocamlcore.org";
61 printfn "Launch %d transfers" (List.length
urls);
62 let tasks = List.map
get urls in
63 Lwt_main.run
@@ Lwt.pick
[
64 Lwt_unix.sleep
0.75 >> Lwt.choose
tasks >> Lwt.return
(print_endline
"Cancel remaining transfers");