]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/getter/http_getter.ml
test branch
[helm.git] / helm / ocaml / getter / http_getter.ml
1 (* Copyright (C) 2005, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 (* $Id$ *)
27
28 open Printf
29
30 open Http_getter_common
31 open Http_getter_misc
32 open Http_getter_types
33
34 exception Not_implemented of string
35 exception UnexpectedGetterOutput
36
37 type resolve_result =
38   | Unknown
39   | Exception of exn
40   | Resolved of string
41
42 type logger_callback = HelmLogger.html_tag -> unit
43
44 let stdout_logger tag = print_string (HelmLogger.string_of_html_tag tag)
45
46 let not_implemented s = raise (Not_implemented ("Http_getter." ^ s))
47
48 let index_line_sep_RE     = Pcre.regexp "[ \t]+"
49 let index_sep_RE          = Pcre.regexp "\r\n|\r|\n"
50 let trailing_types_RE     = Pcre.regexp "\\.types$"
51 let heading_cic_RE        = Pcre.regexp "^cic:"
52 let heading_theory_RE     = Pcre.regexp "^theory:"
53 let heading_nuprl_RE      = Pcre.regexp "^nuprl:"
54 let types_RE              = Pcre.regexp "\\.types$"
55 let types_ann_RE          = Pcre.regexp "\\.types\\.ann$"
56 let body_RE               = Pcre.regexp "\\.body$"
57 let body_ann_RE           = Pcre.regexp "\\.body\\.ann$"
58 let proof_tree_RE         = Pcre.regexp "\\.proof_tree$"
59 let proof_tree_ann_RE     = Pcre.regexp "\\.proof_tree\\.ann$"
60 let theory_RE             = Pcre.regexp "\\.theory$"
61 let basepart_RE           = Pcre.regexp
62   "^([^.]*\\.[^.]*)((\\.body)|(\\.proof_tree)|(\\.types))?(\\.ann)?$"
63 let slash_RE              = Pcre.regexp "/"
64 let pipe_RE               = Pcre.regexp "\\|"
65 let til_slash_RE          = Pcre.regexp "^.*/"
66 let no_slashes_RE         = Pcre.regexp "^[^/]*$"
67 let fix_regexp_RE         = Pcre.regexp ("^" ^ (Pcre.quote "(cic|theory)"))
68 let showable_file_RE      =
69   Pcre.regexp "(\\.con|\\.ind|\\.var|\\.body|\\.types|\\.proof_tree)$"
70
71 let xml_suffix = ".xml"
72 let theory_suffix = ".theory"
73
74   (* global maps, shared by all threads *)
75
76 let ends_with_slash s =
77   try
78     s.[String.length s - 1] = '/'
79   with Invalid_argument _ -> false
80
81   (* should we use a remote getter or not *)
82 let remote () =
83   try
84     Helm_registry.get "getter.mode" = "remote"
85   with Helm_registry.Key_not_found _ -> false
86
87 let getter_url () = Helm_registry.get "getter.url"
88
89 (* Remote interface: getter methods implemented using a remote getter *)
90
91   (* <TODO> *)
92 let getxml_remote uri = not_implemented "getxml_remote"
93 let getxslt_remote uri = not_implemented "getxslt_remote"
94 let getdtd_remote uri = not_implemented "getdtd_remote"
95 let clean_cache_remote () = not_implemented "clean_cache_remote"
96 let list_servers_remote () = not_implemented "list_servers_remote"
97 let add_server_remote ~logger ~position name =
98   not_implemented "add_server_remote"
99 let remove_server_remote ~logger position =
100   not_implemented "remove_server_remote"
101 let getalluris_remote () = not_implemented "getalluris_remote"
102 let ls_remote lsuri = not_implemented "ls_remote"
103 let exists_remote uri = not_implemented "exists_remote"
104   (* </TODO> *)
105
106 let resolve_remote uri =
107   (* deliver resolve request to http_getter *)
108   let doc =
109     Http_getter_wget.get (sprintf "%sresolve?uri=%s" (getter_url ()) uri)
110   in
111   let res = ref Unknown in
112   let start_element tag attrs =
113     match tag with
114     | "url" ->
115         (try
116           res := Resolved (List.assoc "value" attrs)
117         with Not_found -> ())
118     | "unresolvable" -> res := Exception (Unresolvable_URI uri)
119     | "not_found" -> res := Exception (Key_not_found uri)
120     | _ -> ()
121   in
122   let callbacks = {
123     XmlPushParser.default_callbacks with
124       XmlPushParser.start_element = Some start_element
125   } in
126   let xml_parser = XmlPushParser.create_parser callbacks in
127   XmlPushParser.parse xml_parser (`String doc);
128   XmlPushParser.final xml_parser;
129   match !res with
130   | Unknown -> raise UnexpectedGetterOutput
131   | Exception e -> raise e
132   | Resolved url -> url
133
134 let deref_index_theory uri =
135  if Http_getter_storage.exists (uri ^ xml_suffix) then uri
136  else if is_theory_uri uri && Filename.basename uri = "index.theory" then
137     strip_trailing_slash (Filename.dirname uri) ^ theory_suffix
138  else
139     uri
140
141 (* API *)
142
143 let help () = Http_getter_const.usage_string (Http_getter_env.env_to_string ())
144
145 let exists uri =
146 (*   prerr_endline ("Http_getter.exists " ^ uri); *)
147   if remote () then
148     exists_remote uri
149   else
150    let uri = deref_index_theory uri in
151     Http_getter_storage.exists (uri ^ xml_suffix)
152         
153 let resolve uri =
154   if remote () then
155     resolve_remote uri
156   else
157    let uri = deref_index_theory uri in
158     try
159       Http_getter_storage.resolve (uri ^ xml_suffix)
160     with Http_getter_storage.Resource_not_found _ -> raise (Key_not_found uri)
161
162 let getxml uri =
163   if remote () then getxml_remote uri
164   else begin
165     let uri' = deref_index_theory uri in
166     (try
167       Http_getter_storage.filename (uri' ^ xml_suffix)
168     with Http_getter_storage.Resource_not_found _ -> raise (Key_not_found uri))
169   end
170
171 let getxslt uri =
172   if remote () then getxslt_remote uri
173   else Http_getter_storage.filename ~find:true ("xslt:/" ^ uri)
174
175 let getdtd uri =
176   if remote () then
177     getdtd_remote uri
178   else begin
179     let fname = Lazy.force Http_getter_env.dtd_dir ^ "/" ^ uri in
180     if not (Sys.file_exists fname) then raise (Dtd_not_found uri);
181     fname
182   end
183
184 let clean_cache () =
185   if remote () then
186     clean_cache_remote ()
187   else
188     Http_getter_storage.clean_cache ()
189
190 let (++) (oldann, oldtypes, oldbody, oldtree)
191          (newann, newtypes, newbody, newtree) =
192   ((if newann   > oldann    then newann   else oldann),
193    (if newtypes > oldtypes  then newtypes else oldtypes),
194    (if newbody  > oldbody   then newbody  else oldbody),
195    (if newtree  > oldtree   then newtree  else oldtree))
196     
197 let store_obj tbl o =
198 (*   prerr_endline ("Http_getter.store_obj " ^ o); *)
199   if Pcre.pmatch ~rex:showable_file_RE o then begin
200     let basepart = Pcre.replace ~rex:basepart_RE ~templ:"$1" o in
201     let no_flags = false, No, No, No in
202     let oldflags =
203       try
204         Hashtbl.find tbl basepart
205       with Not_found -> (* no ann, no types, no body, no proof tree *)
206         no_flags
207     in
208     let newflags =
209       match o with
210       | s when Pcre.pmatch ~rex:types_RE s          -> (false, Yes, No, No)
211       | s when Pcre.pmatch ~rex:types_ann_RE s      -> (true,  Ann, No, No)
212       | s when Pcre.pmatch ~rex:body_RE s           -> (false, No, Yes, No)
213       | s when Pcre.pmatch ~rex:body_ann_RE s       -> (true,  No, Ann, No)
214       | s when Pcre.pmatch ~rex:proof_tree_RE s     -> (false, No, No, Yes)
215       | s when Pcre.pmatch ~rex:proof_tree_ann_RE s -> (true,  No, No, Ann)
216       | s -> no_flags
217     in
218     Hashtbl.replace tbl basepart (oldflags ++ newflags)
219   end
220   
221 let store_dir set_ref d =
222   set_ref := StringSet.add (List.hd (Pcre.split ~rex:slash_RE d)) !set_ref
223
224 let collect_ls_items dirs_set objs_tbl =
225   let items = ref [] in
226   StringSet.iter (fun dir -> items := Ls_section dir :: !items) dirs_set;
227   Http_getter_misc.hashtbl_sorted_iter
228     (fun uri (annflag, typesflag, bodyflag, treeflag) ->
229       items :=
230         Ls_object {
231           uri = uri; ann = annflag;
232           types = typesflag; body = bodyflag; proof_tree = treeflag
233         } :: !items)
234     objs_tbl;
235   List.rev !items
236
237 let contains_object = (<>) []
238
239   (** non regexp-aware version of ls *)
240 let rec dumb_ls uri_prefix =
241 (*   prerr_endline ("Http_getter.dumb_ls " ^ uri_prefix); *)
242   if is_cic_obj_uri uri_prefix then begin
243     let dirs = ref StringSet.empty in
244     let objs = Hashtbl.create 17 in
245     List.iter
246       (fun fname ->
247         if ends_with_slash fname then
248           store_dir dirs fname
249         else
250           try
251             store_obj objs (strip_suffix ~suffix:xml_suffix fname)
252           with Invalid_argument _ -> ())
253       (Http_getter_storage.ls uri_prefix);
254     collect_ls_items !dirs objs
255   end else if is_theory_uri uri_prefix then begin
256     let items = ref [] in
257     let add_theory fname =
258       items :=
259         Ls_object {
260           uri = fname; ann = false; types = No; body = No; proof_tree = No }
261         :: !items
262     in
263     let cic_uri_prefix =
264       Pcre.replace_first ~rex:heading_theory_RE ~templ:"cic:" uri_prefix
265     in
266     List.iter
267       (fun fname ->
268         if ends_with_slash fname then
269           items := Ls_section (strip_trailing_slash fname) :: !items
270         else
271           try
272             let fname = strip_suffix ~suffix:xml_suffix fname in
273             let theory_name = strip_suffix ~suffix:theory_suffix fname in
274             let sub_theory = normalize_dir cic_uri_prefix ^ theory_name ^ "/" in
275             if is_empty_theory sub_theory then add_theory fname
276           with Invalid_argument _ -> ())
277       (Http_getter_storage.ls uri_prefix);
278     (try
279       if contains_object (dumb_ls cic_uri_prefix)
280         && exists (strip_trailing_slash uri_prefix ^ theory_suffix)
281       then
282         add_theory "index.theory";
283     with Unresolvable_URI _ -> ());
284     !items
285   end else
286     raise (Invalid_URI uri_prefix)
287
288 and is_empty_theory uri_prefix =
289 (*   prerr_endline ("is_empty_theory " ^ uri_prefix); *)
290   not (contains_object (dumb_ls uri_prefix))
291
292   (* handle simple regular expressions of the form "...(..|..|..)..." on cic
293    * uris, not meant to be a real implementation of regexp. The only we use is
294    * "(cic|theory):/..." *)
295 let explode_ls_regexp regexp =
296   try
297     let len = String.length regexp in
298     let lparen_idx = String.index regexp '(' in
299     let rparen_idx = String.index_from regexp lparen_idx ')' in
300     let choices_str = (* substring between parens, parens excluded *)
301       String.sub regexp (lparen_idx + 1) (rparen_idx - lparen_idx - 1)
302     in
303     let choices = Pcre.split ~rex:pipe_RE choices_str in
304     let prefix = String.sub regexp 0 lparen_idx in
305     let suffix = String.sub regexp (rparen_idx + 1) (len - (rparen_idx + 1)) in
306     List.map (fun choice -> prefix ^ choice ^ suffix) choices
307   with Not_found -> [regexp]
308
309 let merge_results results =
310   let rec aux objects_acc dirs_acc = function
311     | [] -> dirs_acc @ objects_acc
312     | Ls_object _ as obj :: tl -> aux (obj :: objects_acc) dirs_acc tl
313     | Ls_section _ as dir :: tl ->
314         if List.mem dir dirs_acc then (* filters out dir duplicates *)
315           aux objects_acc dirs_acc tl
316         else
317           aux objects_acc (dir :: dirs_acc) tl
318   in
319   aux [] [] (List.concat results)
320
321 let ls regexp =
322   if remote () then
323     ls_remote regexp
324   else
325     let prefixes = explode_ls_regexp regexp in
326     merge_results (List.map dumb_ls prefixes)
327
328 let getalluris () =
329   let rec aux acc = function
330     | [] -> acc
331     | dir :: todo ->
332         let acc', todo' =
333           List.fold_left
334             (fun (acc, subdirs) result ->
335               match result with
336               | Ls_object obj -> (dir ^ obj.uri) :: acc, subdirs
337               | Ls_section sect -> acc, (dir ^ sect ^ "/") :: subdirs)
338             (acc, todo)
339             (dumb_ls dir)
340         in
341         aux acc' todo'
342   in
343   aux [] ["cic:/"] (* trailing slash required *)
344
345 (* Shorthands from now on *)
346
347 let getxml' uri = getxml (UriManager.string_of_uri uri)
348 let resolve' uri = resolve (UriManager.string_of_uri uri)
349 let exists' uri = exists (UriManager.string_of_uri uri)
350
351 let tilde_expand_key k =
352   try
353     Helm_registry.set k (HExtlib.tilde_expand (Helm_registry.get k))
354   with Helm_registry.Key_not_found _ -> ()
355
356 let init () =
357   List.iter tilde_expand_key ["getter.cache_dir"; "getter.dtd_dir"];
358   Http_getter_logger.set_log_level
359     (Helm_registry.get_opt_default Helm_registry.int ~default:1
360       "getter.log_level");
361   Http_getter_logger.set_log_file
362     (Helm_registry.get_opt Helm_registry.string "getter.log_file")
363