replace SSLVERIFYHOST_EXISTENCE with SSLVERIFYHOST_HOSTNAME for new libcurl
[ocurl.git] / examples / omulti.ml
blob61a3e164229af97e28ba3956195ebc1684932277
1 (*
2 * omulti.ml
4 * Copyright (c) 2009, ygrek, <ygrek@autistici.org>
5 *)
7 module M = Curl.Multi
9 let loop mt =
10 while M.perform mt > 0 do
11 ignore (M.wait mt)
12 done;
13 let rec rm n =
14 match M.remove_finished mt with
15 | None -> n
16 | Some _ -> rm (n+1)
18 let n = rm 0 in
19 Printf.printf "Removed %u handles\n" n
21 let input_lines file =
22 let ch = open_in file in
23 let lines = ref [] in
24 try while true do lines := input_line ch :: !lines done; []
25 with End_of_file -> close_in_noerr ch; List.rev !lines
27 let () =
28 let module A = Array in
29 let urls =
30 let urls = ref [] in
31 let n = ref 10 in
32 let args = Arg.align
33 ["-n",Arg.Set_int n,"<N> ";
34 "-i",Arg.String (fun s -> urls := input_lines s @ !urls),"<file> read urls from file";
35 "-l",Arg.String (fun s -> urls := s :: !urls),"<url> fetch url";
38 Arg.parse args failwith "Options :";
39 match !urls with
40 | [url] -> A.make !n url
41 | l -> A.of_list l
43 (* if A.length urls = 0 then failwith "Specify urls to download"; *)
44 let init url =
45 let h = Curl.init () in
46 Curl.set_url h url;
47 Curl.set_writefunction h String.length;
50 let cleanup h =
51 Printf.printf "Time: %f Size: %Lu Speed: %.2f KB/s URL: %s\n"
52 (Curl.get_totaltime h)
53 (Int64.of_float (Curl.get_sizedownload h))
54 (Curl.get_speeddownload h /. 1024.)
55 (Curl.get_effectiveurl h);
56 Curl.cleanup h
58 let hs = A.map init urls in
59 let mt = M.create () in
60 A.iter (M.add mt) hs;
61 loop mt;
62 A.iter cleanup hs;
63 M.cleanup mt;
64 print_endline "Finished";