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/
34 open Http_getter_types
36 let version = Http_getter_const.version
38 let servers_file = lazy (Helm_registry.get "getter.servers_file")
39 let cic_dbm = lazy (Helm_registry.get "getter.cic_dbm")
40 let nuprl_dbm = lazy (Helm_registry.get "getter.nuprl_dbm")
41 let rdf_dbm = lazy (Helm_registry.get "getter.rdf_dbm")
42 let xsl_dbm = lazy (Helm_registry.get "getter.xsl_dbm")
43 let xml_index = lazy (Helm_registry.get "getter.xml_indexname")
44 let rdf_index = lazy (Helm_registry.get "getter.rdf_indexname")
45 let xsl_index = lazy (Helm_registry.get "getter.xsl_indexname")
46 let cic_dir = lazy (Helm_registry.get "getter.cic_dir")
47 let nuprl_dir = lazy (Helm_registry.get "getter.nuprl_dir")
48 let rdf_dir = lazy (Helm_registry.get "getter.rdf_dir")
49 let dtd_dir = lazy (Helm_registry.get "getter.dtd_dir")
50 let dtd_base_urls = lazy (
51 let rex = Pcre.regexp "/*$" in
52 let raw_urls = Helm_registry.get_string_list "getter.dtd_base_urls" in
53 List.map (Pcre.replace ~rex) raw_urls)
54 let port = lazy (Helm_registry.get_int "getter.port")
56 let _servers = ref None
61 | None -> failwith "Getter not yet initialized: servers not available"
62 | Some servers -> servers)
66 List.rev (Http_getter_misc.fold_file
68 if Http_getter_misc.is_blank_line line then
71 (incr pos; (!pos, line) :: servers))
73 (Lazy.force servers_file))
75 let reload_servers () = _servers := Some (load_servers ())
78 let oc = open_out (Lazy.force servers_file) in
79 List.iter (fun (_,server) -> output_string oc (server ^ "\n"))
85 (let buf = Buffer.create 20 in
86 Shell.call ~stdout:(Shell.to_buffer buf) [Shell.cmd "hostname" ["-f"]];
87 Pcre.replace ~pat:"\n+$" (Buffer.contents buf))
91 (let (host, port) = (Lazy.force host, Lazy.force port) in
92 sprintf "http://%s%s" (* without trailing '/' *)
93 host (if port = 80 then "" else (sprintf ":%d" port)))
97 (match String.lowercase (Helm_registry.get "getter.cache_mode") with
100 | mode -> failwith ("Invalid cache mode: " ^ mode))
102 let reload () = reload_servers ()
104 let env_to_string () =
106 "HTTP Getter %s (the OCaml one!)
130 version (Lazy.force cic_dbm) (Lazy.force nuprl_dbm) (Lazy.force rdf_dbm)
131 (Lazy.force xsl_dbm) (Lazy.force xml_index)
132 (Lazy.force rdf_index) (Lazy.force xsl_index) (Lazy.force cic_dir)
133 (Lazy.force nuprl_dir) (Lazy.force rdf_dir)
134 (Lazy.force dtd_dir) (Lazy.force servers_file) (Lazy.force host)
135 (Lazy.force port) (Lazy.force my_own_url)
136 (String.concat " " (Lazy.force dtd_base_urls))
137 (match Lazy.force cache_mode with
138 | `Normal -> "Normal"
139 | `Gzipped -> "GZipped")
140 (String.concat "\n\t" (* (position * server) list *)
141 (List.map (fun (pos, server) -> sprintf "%3d: %s" pos server)
143 (match Http_getter_logger.get_log_file () with None -> "None" | Some f -> f)
144 (Http_getter_logger.get_log_level ())
146 let add_server ?position url =
148 let servers = servers () in
150 | None -> servers @ [-1, url];
151 | Some p when p > 0 ->
152 let rec add_after pos = function
154 | hd :: tl when p = 1 -> hd :: (-1, url) :: tl
155 | hd :: tl (* when p > 1 *) -> hd :: (add_after (pos - 1) tl)
158 | Some 0 -> (-1, url)::servers
159 | Some _ -> assert false
161 _servers := Some new_servers;
165 let remove_server position =
166 _servers := Some (List.remove_assoc position (servers ()));