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 try_lwt
(* e.g. Canceled *)
41 lwt
(code
,_body
) = download h in
44 (* do something with body *)
46 printfn "EXN %s URL: %s" (Printexc.to_string exn
) url;
59 "forge.ocamlcore.org";
63 printfn "Launch %d transfers" (List.length
urls);
64 let tasks = List.map
get urls in
65 Lwt_main.run
@@ Lwt.pick
[
66 Lwt_unix.sleep
0.75 >> Lwt.choose
tasks >> Lwt.return
(print_endline
"Cancel remaining transfers");