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 let file_scheme_prefix = "file://"
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_scheme_prefix)
37 let dir_sep_RE = Pcre.regexp "/"
38 let heading_slash_RE = Pcre.regexp "^/"
41 let rex = Pcre.regexp ("^(" ^ file_scheme_prefix ^ ")(.*)(.gz)$") in
44 Some ((Pcre.extract ~rex s).(2))
45 with Not_found -> None
47 let bufsiz = 16384 (* for file system I/O *)
48 let tcp_bufsiz = 4096 (* for TCP I/O *)
50 let fold_file f init fname =
51 let ic = open_in fname in
53 let line = try Some (input_line ic) with End_of_file -> None in
56 | Some line -> aux (f line acc)
58 let res = try aux init with e -> close_in ic; raise e in
62 let iter_file f = fold_file (fun line _ -> f line) ()
64 let iter_buf_size = 10240
66 let iter_file_data f fname =
67 let ic = open_in fname in
68 let buf = String.create iter_buf_size in
71 let bytes = input ic buf 0 iter_buf_size in
72 if bytes = 0 then raise End_of_file;
73 f (String.sub buf 0 bytes)
75 with End_of_file -> close_in ic
77 let hashtbl_sorted_fold f tbl init =
79 List.sort compare (Hashtbl.fold (fun key _ keys -> key::keys) tbl [])
81 List.fold_left (fun acc k -> f k (Hashtbl.find tbl k) acc) init sorted_keys
83 let hashtbl_sorted_iter f tbl =
85 List.sort compare (Hashtbl.fold (fun key _ keys -> key::keys) tbl [])
87 List.iter (fun k -> f k (Hashtbl.find tbl k)) sorted_keys
91 let ic = open_in src in
93 let oc = open_out dst in
94 let buf = String.create bufsiz in
97 let bytes = input ic buf 0 bufsiz in
98 if bytes = 0 then raise End_of_file else output oc buf 0 bytes
103 close_in ic; close_out oc
106 Http_getter_logger.log s;
109 Http_getter_logger.log (Printexc.to_string e);
114 Http_getter_logger.log s
116 Http_getter_logger.log (Printexc.to_string e);
119 let wget ?output url =
120 Http_getter_logger.log
121 (sprintf "wgetting %s (output: %s)" url
122 (match output with None -> "default" | Some f -> f));
124 | url when Pcre.pmatch ~rex:file_scheme_RE url -> (* file:// *)
125 (let src_fname = Pcre.replace ~rex:file_scheme_RE url in
127 | Some dst_fname -> cp src_fname dst_fname
129 let dst_fname = Filename.basename src_fname in
130 if src_fname <> dst_fname then
131 cp src_fname dst_fname
132 else (* src and dst are the same: do nothing *)
134 | url when Pcre.pmatch ~rex:http_scheme_RE url -> (* http:// *)
136 open_out (match output with Some f -> f | None -> Filename.basename url)
138 Http_user_agent.get_iter (fun data -> output_string oc data) url;
140 | scheme -> (* unsupported scheme *)
141 failwith ("Http_getter_misc.wget: unsupported scheme: " ^ scheme)
143 let gzip ?(keep = false) ?output fname =
144 let output = match output with None -> fname ^ ".gz" | Some fname -> fname in
145 Http_getter_logger.log ~level:3
146 (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 Http_getter_logger.log ~level:3
172 (sprintf "gunzipping %s (keep: %b, output: %s)" fname keep output);
173 (* Open the zipped file manually since Gzip.open_in may
174 * leak the descriptor if it raises an exception *)
175 let zic = open_in fname in
178 let ic = Gzip.open_in_chan zic in
179 let oc = open_out output in
180 let buf = String.create bufsiz in
183 let bytes = Gzip.input ic buf 0 bufsiz in
184 if bytes = 0 then raise End_of_file else Pervasives.output oc buf 0 bytes
186 with End_of_file -> ());
190 e -> close_in zic ; raise e
192 if not keep then Sys.remove fname
195 let tempfile () = Filename.temp_file "http_getter_" ""
197 exception Mkdir_failure of string * string;; (* dirname, failure reason *)
200 let mkdir ?(parents = false) dirname =
203 let split = Pcre.split ~rex:dir_sep_RE dirname in
204 if Pcre.pmatch ~rex:heading_slash_RE dirname then
213 sprintf "%s%s%s" pre (match pre with "/" | "" -> "" | _ -> "/") dir
216 (match (Unix.stat next_dir).Unix.st_kind with
217 | Unix.S_DIR -> () (* dir component already exists, go on! *)
218 | _ -> (* dir component already exists but isn't a dir, abort! *)
220 (Mkdir_failure (dirname,
221 sprintf "'%s' already exists but is not a dir" next_dir)))
222 with Unix.Unix_error (Unix.ENOENT, "stat", _) ->
223 (* dir component doesn't exists, create it and go on! *)
224 Unix.mkdir next_dir dir_perm);
228 if parents then mkdirhier () else Unix.mkdir dirname dir_perm
230 let string_of_proc_status = function
231 | Unix.WEXITED code -> sprintf "[Exited: %d]" code
232 | Unix.WSIGNALED sg -> sprintf "[Killed: %d]" sg
233 | Unix.WSTOPPED sg -> sprintf "[Stopped: %d]" sg
236 if Pcre.pmatch ~rex:file_scheme_RE url then begin
237 (* file:// URL. Read data from file system *)
238 let fname = Pcre.replace ~rex:file_scheme_RE url in
240 let size = (Unix.stat fname).Unix.st_size in
241 let buf = String.create size in
242 let ic = open_in fname in
243 really_input ic buf 0 size ;
246 with Unix.Unix_error (Unix.ENOENT, "stat", _) -> None
247 end else (* other URL, pass it to Http_user_agent *)
249 Some (Http_user_agent.get url)
251 Http_getter_logger.log (sprintf
252 "Warning: Http_user_agent failed on url %s with exception: %s"
253 url (Printexc.to_string e));
257 let blank_line_RE = Pcre.regexp "(^#)|(^\\s*$)" in
259 Pcre.pmatch ~rex:blank_line_RE line