2 * Copyright (C) 2003-2004:
3 * Stefano Zacchiroli <zack@cs.unibo.it>
4 * for the HELM Team http://helm.cs.unibo.it/
6 * This file is part of HELM, an Hypertextual, Electronic
7 * Library of Mathematics, developed at the Computer Science
8 * Department, University of Bologna, Italy.
10 * HELM is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * HELM is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with HELM; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * For details, see the HELM World-Wide-Web page,
26 * http://helm.cs.unibo.it/
31 open Http_getter_debugger
33 let trailing_dot_gz_RE = Pcre.regexp "\\.gz$" (* for g{,un}zip *)
34 let url_RE = Pcre.regexp "^([\\w.-]+)(:(\\d+))?(/.*)?$"
35 let http_scheme_RE = Pcre.regexp ~flags:[`CASELESS] "^http://"
36 let file_scheme_RE = Pcre.regexp ~flags:[`CASELESS] "^file://"
37 let dir_sep_RE = Pcre.regexp "/"
38 let heading_slash_RE = Pcre.regexp "^/"
40 let bufsiz = 16384 (* for file system I/O *)
41 let tcp_bufsiz = 4096 (* for TCP I/O *)
43 let fold_file f init fname =
44 let ic = open_in fname in
46 let line = try Some (input_line ic) with End_of_file -> None in
49 | Some line -> aux (f line acc)
51 let res = try aux init with e -> close_in ic; raise e in
55 let iter_file f = fold_file (fun line _ -> f line) ()
57 let hashtbl_sorted_fold f tbl init =
59 List.sort compare (Hashtbl.fold (fun key _ keys -> key::keys) tbl [])
61 List.fold_left (fun acc k -> f k (Hashtbl.find tbl k) acc) init sorted_keys
63 let hashtbl_sorted_iter f tbl =
65 List.sort compare (Hashtbl.fold (fun key _ keys -> key::keys) tbl [])
67 List.iter (fun k -> f k (Hashtbl.find tbl k)) sorted_keys
70 let (ic, oc) = (open_in src, open_out dst) in
71 let buf = String.create bufsiz in
74 let bytes = input ic buf 0 bufsiz in
75 if bytes = 0 then raise End_of_file else output oc buf 0 bytes
77 with End_of_file -> ());
78 close_in ic; close_out oc
83 Pcre.extract ~rex:url_RE (Pcre.replace ~rex:http_scheme_RE url)
86 (if subs.(2) = "" then 80 else int_of_string subs.(3)),
87 (if subs.(4) = "" then "/" else subs.(4)))
90 (sprintf "Can't parse url: %s (exception: %s)"
91 url (Printexc.to_string exc))
92 let init_socket addr port =
93 let inet_addr = (Unix.gethostbyname addr).Unix.h_addr_list.(0) in
94 let sockaddr = Unix.ADDR_INET (inet_addr, port) in
95 let suck = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
96 Unix.connect suck sockaddr;
97 let outchan = Unix.out_channel_of_descr suck in
98 let inchan = Unix.in_channel_of_descr suck in
100 let http_get_iter_buf ~callback url =
101 let (address, port, path) = parse_url url in
102 let buf = String.create tcp_bufsiz in
103 let (inchan, outchan) = init_socket address port in
104 output_string outchan (sprintf "GET %s\r\n" path);
108 match input inchan buf 0 tcp_bufsiz with
109 | 0 -> raise End_of_file
110 | bytes when bytes = tcp_bufsiz -> (* buffer full, no need to slice it *)
112 | bytes when bytes < tcp_bufsiz -> (* buffer not full, slice it *)
113 callback (String.sub buf 0 bytes)
114 | _ -> (* ( bytes < 0 ) || ( bytes > tcp_bufsiz ) *)
117 with End_of_file -> ());
118 close_in inchan (* close also outchan, same fd *)
120 let wget ?output url =
122 (sprintf "wgetting %s (output: %s)" url
123 (match output with None -> "default" | Some f -> f));
125 | url when Pcre.pmatch ~rex:file_scheme_RE url -> (* file:// *)
126 (let src_fname = Pcre.replace ~rex:file_scheme_RE url in
128 | Some dst_fname -> cp src_fname dst_fname
130 let dst_fname = Filename.basename src_fname in
131 if src_fname <> dst_fname then
132 cp src_fname dst_fname
133 else (* src and dst are the same: do nothing *)
135 | url when Pcre.pmatch ~rex:http_scheme_RE url -> (* http:// *)
137 open_out (match output with Some f -> f | None -> Filename.basename url)
139 http_get_iter_buf ~callback:(fun data -> output_string oc data) url;
141 | scheme -> (* unsupported scheme *)
142 failwith ("Http_getter_misc.wget: unsupported scheme: " ^ scheme)
144 let gzip ?(keep = false) ?output fname =
145 let output = match output with None -> fname ^ ".gz" | Some fname -> fname in
146 debug_print (sprintf "gzipping %s (keep: %b, output: %s)" fname keep output);
147 let (ic, oc) = (open_in fname, Gzip.open_out output) in
148 let buf = String.create bufsiz in
151 let bytes = input ic buf 0 bufsiz in
152 if bytes = 0 then raise End_of_file else Gzip.output oc buf 0 bytes
154 with End_of_file -> ());
155 close_in ic; Gzip.close_out oc;
156 if not keep then Sys.remove fname
159 let gunzip ?(keep = false) ?output fname =
160 (* assumption: given file name ends with ".gz" or output is set *)
164 if (Pcre.pmatch ~rex:trailing_dot_gz_RE fname) then
165 Pcre.replace ~rex:trailing_dot_gz_RE fname
168 "Http_getter_misc.gunzip: unable to determine output file name"
169 | Some fname -> fname
171 debug_print (sprintf "gunzipping %s (keep: %b, output: %s)"
173 let (ic, oc) = (Gzip.open_in fname, open_out output) in
174 let buf = String.create bufsiz in
177 let bytes = Gzip.input ic buf 0 bufsiz in
178 if bytes = 0 then raise End_of_file else Pervasives.output oc buf 0 bytes
180 with End_of_file -> ());
181 Gzip.close_in ic; close_out oc;
182 if not keep then Sys.remove fname
185 let tempfile () = Filename.temp_file "http_getter_" ""
187 exception Mkdir_failure of string * string;; (* dirname, failure reason *)
190 let mkdir ?(parents = false) dirname =
193 let split = Pcre.split ~rex:dir_sep_RE dirname in
194 if Pcre.pmatch ~rex:heading_slash_RE dirname then
203 sprintf "%s%s%s" pre (match pre with "/" | "" -> "" | _ -> "/") dir
206 (match (Unix.stat next_dir).Unix.st_kind with
207 | Unix.S_DIR -> () (* dir component already exists, go on! *)
208 | _ -> (* dir component already exists but isn't a dir, abort! *)
210 (Mkdir_failure (dirname,
211 sprintf "'%s' already exists but is not a dir" next_dir)))
212 with Unix.Unix_error (Unix.ENOENT, "stat", _) ->
213 (* dir component doesn't exists, create it and go on! *)
214 Unix.mkdir next_dir dir_perm);
218 if parents then mkdirhier () else Unix.mkdir dirname dir_perm
220 let string_of_proc_status = function
221 | Unix.WEXITED code -> sprintf "[Exited: %d]" code
222 | Unix.WSIGNALED sg -> sprintf "[Killed: %d]" sg
223 | Unix.WSTOPPED sg -> sprintf "[Stopped: %d]" sg
226 if Pcre.pmatch ~rex:file_scheme_RE url then begin
227 (* file:// URL. Read data from file system *)
228 let fname = Pcre.replace ~rex:file_scheme_RE url in
230 let size = (Unix.stat fname).Unix.st_size in
231 let buf = String.create size in
232 let ic = open_in fname in
233 really_input ic buf 0 size;
236 with Unix.Unix_error (Unix.ENOENT, "stat", _) -> None
237 end else (* other URL, pass it to Http_client *)
239 Some (Http_client.http_get url)
241 prerr_endline (sprintf
242 "Warning: Http_client failed on url %s with exception: %s"
243 url (Printexc.to_string e));
247 let blank_line_RE = Pcre.regexp "(^#)|(^\\s*$)" in
249 Pcre.pmatch ~rex:blank_line_RE line