]> matita.cs.unibo.it Git - helm.git/blob - helm/http_getter/http_getter_env.ml
Initial revision
[helm.git] / helm / http_getter / http_getter_env.ml
1 (*
2  * Copyright (C) 2003:
3  *    Stefano Zacchiroli <zack@cs.unibo.it>
4  *    for the HELM Team http://helm.cs.unibo.it/
5  *
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.
9  *
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.
14  *
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.
19  *
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,
23  *  MA  02111-1307, USA.
24  *
25  *  For details, see the HELM World-Wide-Web page,
26  *  http://helm.cs.unibo.it/
27  *)
28
29 open Http_getter_types;;
30 open Printf;;
31 open Pxp_document;;
32 open Pxp_types;;
33 open Pxp_yacc;;
34
35 let version = Http_getter_const.version
36
37 type setting_src =
38   | Environment (* read setting from environment variables *)
39   | Conffile    (* read setting from configuration file *)
40   | Both        (* read setting from both; environment override config file *)
41
42 let conf_file_tree = ref None
43
44 let (conf_file, conf_dir) =
45   try
46     let conf_dir =
47       Pcre.replace ~pat:"/$" (Sys.getenv "HELM_CONFIGURATION_DIR")
48     in
49     (conf_dir ^ "/" ^ Http_getter_const.conffile, conf_dir)
50   with Not_found -> failwith "HELM_CONFIGURATION_DIR undefined"
51
52 let safe_getenv ?(from = Both) var =
53   (let rec read_from_file () =
54     (match !conf_file_tree with
55     | None ->
56         conf_file_tree :=
57           Some
58             (parse_wfcontent_entity
59               default_config (from_file conf_file) default_spec);
60         read_from_file ()
61     | Some t ->
62         (try
63           Some (find_element (String.lowercase var) t)#data
64         with Not_found -> None))
65   in
66   let read_from_env () = try Some (Sys.getenv var) with Not_found -> None in
67   let return_value name = function
68     | Some v -> v
69     | None -> failwith ("Setting " ^ name ^ " is not defined")
70   in
71   (match from with
72   | Environment -> return_value var (read_from_env ())
73   | Conffile -> return_value var (read_from_file ())
74   | Both ->
75       (match read_from_env () with
76       | None -> return_value var (read_from_file ())
77       | v -> return_value var v)))
78
79 let servers_file = safe_getenv "HTTP_GETTER_SERVERS_FILE"
80 let parse_servers () =
81   (let cons hd tl = hd @ [ tl ] in
82   Http_getter_misc.fold_file cons [] servers_file)
83 let servers = ref (parse_servers ())
84 let reload_servers () = servers := parse_servers ()
85
86 let xml_dbm = safe_getenv "HTTP_GETTER_XML_DBM"
87 let rdf_dbm = safe_getenv "HTTP_GETTER_RDF_DBM"
88 let xsl_dbm = safe_getenv "HTTP_GETTER_XSL_DBM"
89 let xml_index = safe_getenv "HTTP_GETTER_XML_INDEXNAME"
90 let rdf_index = safe_getenv "HTTP_GETTER_RDF_INDEXNAME"
91 let xsl_index = safe_getenv "HTTP_GETTER_XSL_INDEXNAME"
92 let xml_dir = safe_getenv "HTTP_GETTER_XML_DIR"
93 let rdf_dir = safe_getenv "HTTP_GETTER_RDF_DIR"
94 let dtd_dir = safe_getenv "HTTP_GETTER_DTD_DIR"
95
96 let port =
97   let port = safe_getenv "HTTP_GETTER_PORT" in
98   try
99     int_of_string port
100   with Failure "int_of_string" ->
101     failwith ("Invalid port value: " ^ port)
102 let host =
103   let buf = Buffer.create 20 in
104   Shell.call ~stdout:(Shell.to_buffer buf) [Shell.cmd "hostname" ["-f"]];
105   Pcre.replace ~pat:"\n+$" (Buffer.contents buf)
106 let my_own_url =
107   sprintf
108     "http://%s%s" (* without trailing '/' *)
109     host
110     (if port = 80 then "" else (sprintf ":%d" port))
111 let dtd_base_url = safe_getenv "HTTP_GETTER_DTD_BASE_URL"
112
113 let cache_mode =
114   match String.lowercase (safe_getenv "HTTP_GETTER_CACHE_MODE") with
115   | "normal" -> Enc_normal
116   | "gz" -> Enc_gzipped
117   | mode -> failwith ("Invalid cache mode: " ^ mode)
118
119 let reload () =
120   reload_servers ()
121
122 let dump_env () =
123   printf
124 "HTTP Getter %s (the OCaml one!)
125
126 xml_dbm:\t%s
127 rdf_dbm:\t%s
128 xsl_dbm:\t%s
129 xml_index:\t%s
130 rdf_index:\t%s
131 xsl_index:\t%s
132 xml_dir:\t%s
133 rdf_dir:\t%s
134 dtd_dir:\t%s
135 servers_file:\t%s
136 host:\t\t%s
137 port:\t\t%d
138 my_own_url:\t%s
139 dtd_base_url:\t%s
140 cache_mode:\t%s
141 conf_file:\t%s
142 conf_dir:\t%s
143 servers:
144 \t%s
145 "
146     version xml_dbm rdf_dbm xsl_dbm xml_index rdf_index xsl_index
147     xml_dir rdf_dir dtd_dir servers_file host port my_own_url dtd_base_url
148     (match cache_mode with Enc_normal -> "Normal" | Enc_gzipped -> "GZipped")
149     conf_file conf_dir (String.concat "\n\t" !servers);
150   flush stdout
151