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_types
33 let version = Http_getter_const.version
35 let servers_file = lazy (
36 Helm_registry.get_opt Helm_registry.string "getter.servers_file")
37 let cic_dbm = lazy (Helm_registry.get "getter.maps_dir" ^ "/cic_db")
38 let cic_dbm_real = lazy (Helm_registry.get "getter.maps_dir" ^ "/cic_db.pag")
39 let nuprl_dbm = lazy (Helm_registry.get "getter.maps_dir" ^ "/nuprl_db")
40 let rdf_dbm = lazy (Helm_registry.get "getter.maps_dir" ^ "/rdf_db")
41 let xsl_dbm = lazy (Helm_registry.get "getter.maps_dir" ^ "/xsl_db")
42 let dump_file = lazy (Helm_registry.get "getter.maps_dir" ^
44 let prefetch = lazy (Helm_registry.get_bool "getter.prefetch")
45 let xml_index = lazy (
46 Helm_registry.get_opt_default Helm_registry.string ~default:"index.txt"
47 "getter.xml_indexname")
48 let rdf_index = lazy (
49 Helm_registry.get_opt_default Helm_registry.string ~default:"rdf_index.txt"
50 "getter.rdf_indexname")
51 let xsl_index = lazy (
52 Helm_registry.get_opt_default Helm_registry.string ~default:"xslt_index.txt"
53 "getter.xsl_indexname")
54 let cic_dir = lazy (Helm_registry.get "getter.cache_dir" ^ "/cic")
55 let nuprl_dir = lazy (Helm_registry.get "getter.cache_dir" ^ "/nuprl")
56 let rdf_dir = lazy (Helm_registry.get "getter.cache_dir" ^ "/rdf")
57 let dtd_dir = lazy (Helm_registry.get "getter.dtd_dir")
58 let dtd_base_urls = lazy (
59 let rex = Pcre.regexp "/*$" in
62 Helm_registry.get_list Helm_registry.string "getter.dtd_base_urls"
64 | [] -> ["http://helm.cs.unibo.it/dtd"; "http://mowgli.cs.unibo.it/dtd"]
67 List.map (Pcre.replace ~rex) raw_urls)
69 Helm_registry.get_opt_default Helm_registry.int ~default:58081 "getter.port")
71 let _servers = ref None
76 | None -> failwith "Getter not yet initialized: servers not available"
77 | Some servers -> servers)
81 match Lazy.force servers_file with
83 List.map (fun s -> incr pos; (!pos, s))
84 (Helm_registry.get_list Helm_registry.string "getter.servers")
85 | Some servers_file ->
86 List.rev (Http_getter_misc.fold_file
88 if Http_getter_misc.is_blank_line line then
91 (incr pos; (!pos, line) :: servers))
95 let reload_servers () = _servers := Some (load_servers ())
98 match Lazy.force servers_file with
100 | Some servers_file ->
101 let oc = open_out servers_file in
102 List.iter (fun (_,server) -> output_string oc (server ^ "\n"))
108 (let buf = Buffer.create 20 in
109 Shell.call ~stdout:(Shell.to_buffer buf) [Shell.cmd "hostname" ["-f"]];
110 Pcre.replace ~pat:"\n+$" (Buffer.contents buf))
114 (let (host, port) = (Lazy.force host, Lazy.force port) in
115 sprintf "http://%s%s" (* without trailing '/' *)
116 host (if port = 80 then "" else (sprintf ":%d" port)))
121 Helm_registry.get_opt_default Helm_registry.string ~default:"gz"
124 match String.lowercase mode_string with
125 | "normal" -> `Normal
127 | mode -> failwith ("Invalid cache mode: " ^ mode))
129 let reload () = reload_servers ()
131 let env_to_string () =
133 "HTTP Getter %s (the OCaml one!)
159 version (Lazy.force cic_dbm) (Lazy.force nuprl_dbm) (Lazy.force rdf_dbm)
160 (Lazy.force xsl_dbm) (Lazy.force xml_index)
161 (Lazy.force rdf_index) (Lazy.force xsl_index) (Lazy.force cic_dir)
162 (Lazy.force nuprl_dir) (Lazy.force rdf_dir)
163 (Lazy.force dtd_dir) (Lazy.force dump_file) (Lazy.force prefetch)
164 (match Lazy.force servers_file with
165 | None -> "no servers file"
166 | Some servers_file -> servers_file)
168 (Lazy.force port) (Lazy.force my_own_url)
169 (String.concat " " (Lazy.force dtd_base_urls))
170 (match Lazy.force cache_mode with
171 | `Normal -> "Normal"
172 | `Gzipped -> "GZipped")
173 (String.concat "\n\t" (* (position * server) list *)
174 (List.map (fun (pos, server) -> sprintf "%3d: %s" pos server)
176 (match Http_getter_logger.get_log_file () with None -> "None" | Some f -> f)
177 (Http_getter_logger.get_log_level ())
179 let add_server ?position url =
181 let servers = servers () in
183 | None -> servers @ [-1, url];
184 | Some p when p > 0 ->
185 let rec add_after pos = function
187 | hd :: tl when p = 1 -> hd :: (-1, url) :: tl
188 | hd :: tl (* when p > 1 *) -> hd :: (add_after (pos - 1) tl)
191 | Some 0 -> (-1, url)::servers
192 | Some _ -> assert false
194 _servers := Some new_servers;
198 let remove_server position =
199 _servers := Some (List.remove_assoc position (servers ()));