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