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 * Library of Mathematics, developed at the Computer Science
7 * Department, University of Bologna, Italy.
9 * HELM is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * HELM is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with HELM; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24 * For details, see the HELM World-Wide-Web page,
25 * http://helm.cs.unibo.it/
28 open Http_getter_types;;
34 let version = Http_getter_const.version
37 | Environment (* read setting from environment variables *)
38 | Conffile (* read setting from configuration file *)
39 | Both (* read setting from both; environment override config file *)
41 let conf_file_tree = ref None
43 let (conf_file, conf_dir) =
46 Pcre.replace ~pat:"/$" (Sys.getenv "HELM_CONFIGURATION_DIR")
48 (conf_dir ^ "/" ^ Http_getter_const.conffile, conf_dir)
49 with Not_found -> failwith "HELM_CONFIGURATION_DIR undefined"
51 let safe_getenv ?(from = Both) var =
52 (let rec read_from_file () =
53 (match !conf_file_tree with
57 (parse_wfcontent_entity
58 default_config (from_file conf_file) default_spec);
62 Some (find_element (String.lowercase var) t)#data
63 with Not_found -> None))
65 let read_from_env () = try Some (Sys.getenv var) with Not_found -> None in
66 let return_value name = function
68 | None -> failwith ("Setting " ^ name ^ " is not defined")
71 | Environment -> return_value var (read_from_env ())
72 | Conffile -> return_value var (read_from_file ())
74 (match read_from_env () with
75 | None -> return_value var (read_from_file ())
76 | v -> return_value var v)))
78 let servers_file = safe_getenv "HTTP_GETTER_SERVERS_FILE"
80 (* TODO BUG HERE: is commented lines are included in the servers file the
81 server index (used for example by the remove_server method) gets out of sync!
83 let parse_servers () =
84 List.rev (Http_getter_misc.fold_file
86 if Http_getter_misc.is_blank_line line then servers else line::servers)
90 let servers = ref (parse_servers ())
91 let reload_servers () = servers := parse_servers ()
93 let cic_dbm = safe_getenv "HTTP_GETTER_CIC_DBM"
94 let nuprl_dbm = safe_getenv "HTTP_GETTER_NUPRL_DBM"
95 let rdf_dbm = safe_getenv "HTTP_GETTER_RDF_DBM"
96 let xsl_dbm = safe_getenv "HTTP_GETTER_XSL_DBM"
97 let xml_index = safe_getenv "HTTP_GETTER_XML_INDEXNAME"
98 let rdf_index = safe_getenv "HTTP_GETTER_RDF_INDEXNAME"
99 let xsl_index = safe_getenv "HTTP_GETTER_XSL_INDEXNAME"
100 let cic_dir = safe_getenv "HTTP_GETTER_CIC_DIR"
101 let nuprl_dir = safe_getenv "HTTP_GETTER_NUPRL_DIR"
102 let rdf_dir = safe_getenv "HTTP_GETTER_RDF_DIR"
103 let dtd_dir = safe_getenv "HTTP_GETTER_DTD_DIR"
106 let port = safe_getenv "HTTP_GETTER_PORT" in
109 with Failure "int_of_string" ->
110 failwith ("Invalid port value: " ^ port)
112 let buf = Buffer.create 20 in
113 Shell.call ~stdout:(Shell.to_buffer buf) [Shell.cmd "hostname" ["-f"]];
114 Pcre.replace ~pat:"\n+$" (Buffer.contents buf)
117 "http://%s%s" (* without trailing '/' *)
119 (if port = 80 then "" else (sprintf ":%d" port))
120 let dtd_base_url = safe_getenv "HTTP_GETTER_DTD_BASE_URL"
123 match String.lowercase (safe_getenv "HTTP_GETTER_CACHE_MODE") with
124 | "normal" -> Enc_normal
125 | "gz" -> Enc_gzipped
126 | mode -> failwith ("Invalid cache mode: " ^ mode)
131 let env_to_string () =
133 "HTTP Getter %s (the OCaml one!)
157 version cic_dbm nuprl_dbm rdf_dbm xsl_dbm xml_index rdf_index xsl_index
158 cic_dir nuprl_dir rdf_dir dtd_dir servers_file host port my_own_url
160 (match cache_mode with Enc_normal -> "Normal" | Enc_gzipped -> "GZipped")
162 (String.concat "\n\t" (* servers list prepended with server number *)
164 (let idx = ref ~-1 in
165 fun server -> incr idx; sprintf "%3d: %s" !idx server)
168 let add_server ?position url =
170 | Some p -> Http_getter_misc.add_line ~fname:servers_file ~position:p url
171 | None -> Http_getter_misc.add_line ~fname:servers_file url);
174 let remove_server position =
175 Http_getter_misc.remove_line ~fname:servers_file position;