require CURLOPT_PRIVATE and CURLINFO_PRIVATE
[ocurl.git] / examples / test_lwt.ml
blobacfb563c50fba9ad4f8e2731867acf92dfd1b17a
1 (** curl lwt example *)
3 open Printf
5 let (@@) f x = f x
6 let (|>) x f = f x
8 let printfn fmt = ksprintf print_endline fmt
10 let curl_setup_simple h =
11 let open Curl in
12 set_useragent h "Curl_lwt";
13 set_nosignal h true;
14 set_connecttimeout h 5;
15 set_timeout h 10;
16 set_followlocation h false;
17 set_maxredirs h 1;
18 set_encoding h CURL_ENCODING_ANY
20 let log_curl h code =
21 let open Curl in
22 let url = get_effectiveurl h in
23 printfn "%3d %.2f %g URL: %s (%s)%s"
24 (get_httpcode h)
25 (get_totaltime h)
26 (get_sizedownload h)
27 (if get_httpcode h / 100 = 3 then sprintf "%s -> %s" url (get_redirecturl h) else url)
28 (get_contenttype h)
29 (match code with CURLE_OK -> "" | _ -> sprintf " %s (%d)" (strerror code) (errno code))
31 let download h =
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))
36 let get url =
37 let h = Curl.init () in
38 Curl.set_url h url;
39 curl_setup_simple h;
40 try_lwt (* e.g. Canceled *)
41 lwt (code,_body) = download h in
42 log_curl h code;
43 Lwt.return ()
44 (* do something with body *)
45 with exn ->
46 printfn "EXN %s URL: %s" (Printexc.to_string exn) url;
47 Lwt.fail exn
48 finally
49 Curl.cleanup h;
50 Lwt.return ()
52 let urls =
54 "www.google.com";
55 "ya.ru";
56 "www.forth.org.ru";
57 "caml.inria.fr";
58 "www.mozart-oz.org";
59 "forge.ocamlcore.org";
62 let () =
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");
67 Lwt.join tasks