]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/getter/http_getter.ml
- changes defaults of getxml (format gzipped, don't patch dtd)
[helm.git] / helm / ocaml / getter / http_getter.ml
1 (*
2  * Copyright (C) 2003-2004:
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 Printf
30
31 open Http_getter_common
32 open Http_getter_misc
33 open Http_getter_types
34
35 exception Not_implemented of string
36 exception UnexpectedGetterOutput
37
38 (* resolve_result is needed because it is not possible to raise *)
39 (* an exception in a pxp ever-processing callback. Too bad.     *)
40 type resolve_result =
41   | Unknown
42   | Exception of exn
43   | Resolved of string
44
45 type logger_callback = HelmLogger.html_tag -> unit
46
47 let stdout_logger tag =  print_string (HelmLogger.string_of_html_tag tag)
48
49 let not_implemented s = raise (Not_implemented ("Http_getter." ^ s))
50
51 let (index_line_sep_RE, index_sep_RE, trailing_types_RE,
52     heading_cic_RE, heading_theory_RE, heading_nuprl_RE,
53     heading_rdf_cic_RE, heading_rdf_theory_RE) =
54   (Pcre.regexp "[ \t]+", Pcre.regexp "\r\n|\r|\n",
55   Pcre.regexp "\\.types$",
56   Pcre.regexp "^cic:", Pcre.regexp "^theory:", Pcre.regexp "^nuprl:",
57   Pcre.regexp "^helm:rdf.*//cic:", Pcre.regexp "^helm:rdf.*//theory:")
58
59   (* global maps, shared by all threads *)
60
61 let cic_map =
62   lazy (new Http_getter_map.map (Lazy.force Http_getter_env.cic_dbm))
63 let nuprl_map =
64   lazy (new Http_getter_map.map (Lazy.force Http_getter_env.nuprl_dbm))
65 let rdf_map =
66   lazy (new Http_getter_map.map (Lazy.force Http_getter_env.rdf_dbm))
67 let xsl_map =
68   lazy (new Http_getter_map.map (Lazy.force Http_getter_env.xsl_dbm))
69
70 let uri_tree = ref None
71 let deref_if_some r = 
72   match !r with
73   | None -> assert false 
74   | Some x -> x
75 let is_prefetch_on () =
76   match !uri_tree with None -> false | Some _ -> true
77   
78 let dump_tree () =
79   let path = Lazy.force Http_getter_env.dump_file in
80   Tree.save_to_disk path (deref_if_some uri_tree);
81   Http_getter_md5.create_hash [
82       (Lazy.force Http_getter_env.cic_dbm_real);
83       path ] 
84   
85 let load_tree () = 
86   if not (Http_getter_md5.check_hash ()) then
87     assert false
88   else
89     uri_tree := Some (Tree.load_from_disk 
90       (Lazy.force Http_getter_env.dump_file))
91
92 let sync_with_map () =    
93   if not (Http_getter_md5.check_hash ()) then begin
94     let tree = ref (Some Tree.empty_tree) in
95     Http_getter_logger.log "Updating cic map dump...";
96     let t = Unix.time () in
97     (Lazy.force cic_map)#iter
98       (fun k _ ->
99         tree := Some (Tree.add_uri k (deref_if_some tree)));
100     uri_tree := !tree;
101     Http_getter_logger.log
102       (sprintf "done in %.0f sec" (Unix.time () -. t));
103       dump_tree ()
104   end else begin
105     Http_getter_logger.log "Cic map dump is up to date!";
106     load_tree ()  (* XXX TASSI: race condition here *)
107   end
108   
109 let maps = [ cic_map; nuprl_map; rdf_map; xsl_map ]
110 let close_maps () = List.iter (fun m -> (Lazy.force m) # close) maps
111 let clear_maps () = List.iter (fun m -> (Lazy.force m) # clear) maps
112 let sync_maps  () = 
113   List.iter (fun m -> (Lazy.force m) # sync) maps;
114   sync_with_map ()
115
116 let map_of_uri = function
117   | uri when is_cic_uri uri -> Lazy.force cic_map
118   | uri when is_nuprl_uri uri -> Lazy.force nuprl_map
119   | uri when is_rdf_uri uri -> Lazy.force rdf_map
120   | uri when is_xsl_uri uri -> Lazy.force xsl_map
121   | uri -> raise (Unresolvable_URI uri)
122
123 let update_from_server logger server_url = (* use global maps *)
124   Http_getter_logger.log ("Updating information from " ^ server_url);
125   let xml_url_of_uri = function
126       (* TODO missing sanity checks on server_url, e.g. it can contains $1 *)
127     | uri when (Pcre.pmatch ~rex:heading_cic_RE uri) ->
128         Pcre.replace ~rex:heading_cic_RE ~templ:server_url uri
129     | uri when (Pcre.pmatch ~rex:heading_theory_RE uri) ->
130         Pcre.replace ~rex:heading_theory_RE ~templ:server_url uri
131     | uri when (Pcre.pmatch ~rex:heading_nuprl_RE uri) ->
132         Pcre.replace ~rex:heading_nuprl_RE ~templ:server_url uri
133     | uri -> raise (Invalid_URI uri)
134   in
135   let rdf_url_of_uri = function (* TODO as above *)
136     | uri when (Pcre.pmatch ~rex:heading_rdf_cic_RE uri) ->
137         Pcre.replace ~rex:heading_rdf_cic_RE ~templ:server_url uri
138     | uri when (Pcre.pmatch ~rex:heading_rdf_theory_RE uri) ->
139         Pcre.replace ~rex:heading_rdf_theory_RE ~templ:server_url uri
140     | uri -> raise (Invalid_URI uri)
141   in
142   logger (`T ("Processing server: " ^ server_url));
143   logger `BR;
144   let (xml_index, rdf_index, xsl_index) =
145     (* TODO keeps index in memory, is better to keep them on temp files? *)
146     (http_get (server_url ^ "/" ^ (Lazy.force Http_getter_env.xml_index)),
147      http_get (server_url ^ "/" ^ (Lazy.force Http_getter_env.rdf_index)),
148      http_get (server_url ^ "/" ^ (Lazy.force Http_getter_env.xsl_index)))
149   in
150   if (xml_index = None && rdf_index = None && xsl_index = None) then
151     Http_getter_logger.log (sprintf "Warning: useless server %s" server_url);
152   (match xml_index with
153   | Some xml_index ->
154       logger (`T "- Updating XML db ...");
155 (*       logger `BR; *)
156       List.iter
157         (function
158           | l when is_blank_line l -> ()  (* skip blank and commented lines *)
159           | l ->
160               (try
161                 (match Pcre.split ~rex:index_line_sep_RE l with
162                 | [uri; "gz"] ->
163                    assert (is_cic_uri uri || is_nuprl_uri uri) ;
164                    (map_of_uri uri)#replace
165                     uri ((xml_url_of_uri uri) ^ ".xml.gz")
166                 | [uri] ->
167                    assert (is_cic_uri uri || is_nuprl_uri uri) ;
168                    (map_of_uri uri)#replace
169                     uri ((xml_url_of_uri uri) ^ ".xml")
170                 | _ ->
171                     logger (`T ("Ignoring invalid line: '" ^ l));
172                     logger `BR)
173               with Invalid_URI uri ->
174                 logger (`T ("Ignoring invalid XML URI: '" ^ l));
175                 logger `BR))
176         (Pcre.split ~rex:index_sep_RE xml_index); (* xml_index lines *)
177       logger (`T "All done");
178       logger `BR
179   | None -> ());
180   (match rdf_index with
181   | Some rdf_index ->
182       logger (`T "- Updating RDF db ...");
183 (*       logger `BR; *)
184       List.iter
185         (fun l ->
186           try
187             (match Pcre.split ~rex:index_line_sep_RE l with
188             | [uri; "gz"] ->
189                 (Lazy.force rdf_map) # replace uri
190                   ((rdf_url_of_uri uri) ^ ".xml.gz")
191             | [uri] ->
192                 (Lazy.force rdf_map) # replace uri
193                   ((rdf_url_of_uri uri) ^ ".xml")
194             | _ ->
195                 logger (`T ("Ignoring invalid line: '" ^ l));
196                 logger `BR)
197           with Invalid_URI uri ->
198             logger (`T ("Ignoring invalid RDF URI: '" ^ l));
199             logger `BR)
200         (Pcre.split ~rex:index_sep_RE rdf_index); (* rdf_index lines *)
201       logger (`T "All done");
202       logger `BR
203   | None -> ());
204   (match xsl_index with
205   | Some xsl_index ->
206       logger (`T "- Updating XSLT db ...");
207 (*       logger `BR; *)
208       List.iter
209         (fun l -> (Lazy.force xsl_map) # replace l (server_url ^ "/" ^ l))
210         (Pcre.split ~rex:index_sep_RE xsl_index);
211       logger (`T "All done");
212       logger `BR
213   | None -> ());
214   Http_getter_logger.log "done with this server"
215
216 let update_from_all_servers logger () =  (* use global maps *)
217   clear_maps ();
218   List.iter
219     (update_from_server logger)
220       (* reverse order: 1st server is the most important one *)
221     (List.map snd (List.rev (Http_getter_env.servers ())));
222   sync_maps ()
223
224 let update_from_one_server ?(logger = fun _ -> ()) server_url =
225   update_from_server logger server_url
226
227 let temp_file_of_uri uri =
228   let flat_string s s' c =
229     let cs = String.copy s in
230     for i = 0 to (String.length s) - 1 do
231       if String.contains s' s.[i] then cs.[i] <- c
232     done;
233     cs
234   in
235   let user = try Unix.getlogin () with _ -> "" in
236   Filename.open_temp_file (user ^ flat_string uri ".-=:;!?/&" '_') ""
237
238   (* should we use a remote getter or not *)
239 let remote () =
240   try
241     Helm_registry.get "getter.mode" = "remote"
242   with Helm_registry.Key_not_found _ -> false
243 let getter_url () = Helm_registry.get "getter.url"
244
245 (* Remote interface: getter methods implemented using a remote getter *)
246
247   (* <TODO> *)
248 let getxslt_remote ~patch_dtd uri = not_implemented "getxslt_remote"
249 let getdtd_remote ~patch_dtd uri = not_implemented "getdtd_remote"
250 let clean_cache_remote () = not_implemented "clean_cache_remote"
251 let list_servers_remote () = not_implemented "list_servers_remote"
252 let add_server_remote ~logger ~position name =
253   not_implemented "add_server_remote"
254 let remove_server_remote ~logger position =
255   not_implemented "remove_server_remote"
256 let getalluris_remote () = not_implemented "getalluris_remote"
257 let getallrdfuris_remote () = not_implemented "getallrdfuris_remote"
258 let ls_remote lsuri = not_implemented "ls_remote"
259   (* </TODO> *)
260
261 let resolve_remote uri =
262   (* deliver resolve request to http_getter *)
263   let doc = ClientHTTP.get (sprintf "%sresolve?uri=%s" (getter_url ()) uri) in
264   let res = ref Unknown in
265    Pxp_ev_parser.process_entity PxpHelmConf.pxp_config (`Entry_content [])
266     (Pxp_ev_parser.create_entity_manager ~is_document:true
267       PxpHelmConf.pxp_config (Pxp_yacc.from_string doc))
268     (function
269       | Pxp_types.E_start_tag ("url",["value",url],_,_) -> res := Resolved url
270       | Pxp_types.E_start_tag ("unresolvable",[],_,_) ->
271           res := Exception (Unresolvable_URI uri)
272       | Pxp_types.E_start_tag ("not_found",[],_,_) ->
273           res := Exception (Key_not_found uri)
274       | Pxp_types.E_start_tag (x,_,_,_) -> 
275           res := Exception UnexpectedGetterOutput
276       | _ -> ());
277    match !res with
278    | Unknown -> raise UnexpectedGetterOutput
279    | Exception e -> raise e
280    | Resolved url -> url
281
282 let register_remote ~uri ~url =
283   ClientHTTP.send (sprintf "%sregister?uri=%s&url=%s" (getter_url ()) uri url)
284
285 let unregister_remote uri =
286   ClientHTTP.send (sprintf "%sunregister?uri=%s" (getter_url ()) uri)
287
288 let update_remote logger  () =
289   let answer = ClientHTTP.get (getter_url () ^ "update") in
290   logger (`T answer);
291   logger `BR
292
293 let getxml_remote ~format ~patch_dtd uri =
294   let uri =
295     sprintf "%sgetxml?uri=%s&format=%s&patch_dtd=%s"
296       (getter_url ()) uri
297       (match format with `Normal -> "normal" | `Gzipped -> "gz")
298       (match patch_dtd with true -> "yes" | false -> "no")
299   in
300   ClientHTTP.get_and_save_to_tmp uri
301
302 (* API *)
303
304 let help () = Http_getter_const.usage_string (Http_getter_env.env_to_string ())
305
306 let resolve uri =
307   if remote () then
308     resolve_remote uri
309   else
310     (map_of_uri uri)#resolve uri
311         
312 let register ~uri ~url =
313   if remote () then
314     register_remote ~uri ~url
315   else
316     begin
317     (map_of_uri uri)#add uri url;
318     if is_prefetch_on () then
319       uri_tree := Some (Tree.add_uri uri (deref_if_some uri_tree))
320     end
321
322 let unregister uri =
323   if remote () then
324     unregister_remote uri
325   else
326     try
327       begin
328       (map_of_uri uri)#remove uri;
329       if is_prefetch_on () then 
330         uri_tree := Some (Tree.remove_uri uri (deref_if_some uri_tree))
331       end
332     with Key_not_found _ -> ()
333
334 let update ?(logger = fun _ -> ()) () =
335   if remote () then
336     update_remote logger ()
337   else
338     update_from_all_servers logger ()
339
340 let getxml ?(format = `Gzipped) ?(patch_dtd = false) uri =
341   if remote () then
342     getxml_remote ~format ~patch_dtd uri
343   else begin
344     Http_getter_logger.log ~level:2 ("getxml: " ^ uri);
345     let url = resolve uri in
346     Http_getter_logger.log ~level:2 ("resolved_uri: " ^ url) ;
347     let (fname, outchan) = temp_file_of_uri uri in
348     Http_getter_cache.respond_xml ~via_http:false ~enc:format ~patch:patch_dtd
349       ~uri ~url outchan;
350     close_out outchan;
351     fname
352   end
353
354 let getxslt ?(patch_dtd = true) uri =
355   if remote () then
356     getxslt_remote ~patch_dtd uri
357   else begin
358     
359     let url = resolve uri in
360     let (fname, outchan) = temp_file_of_uri uri in
361     Http_getter_cache.respond_xsl ~via_http:false ~url ~patch:patch_dtd outchan;
362     close_out outchan;
363     fname
364   end
365
366 let getdtd ?(patch_dtd = true) uri =
367   if remote () then
368     getdtd_remote ~patch_dtd uri
369   else begin
370     let url = Lazy.force Http_getter_env.dtd_dir ^ "/" ^ uri in
371     let (fname, outchan) = temp_file_of_uri uri in
372     Http_getter_cache.respond_dtd ~via_http:false ~url ~patch:patch_dtd outchan;
373     close_out outchan;
374     fname
375   end
376
377 let clean_cache () =
378   if remote () then
379     clean_cache_remote ()
380   else
381     Http_getter_cache.clean ()
382
383 let list_servers () =
384   if remote () then
385     list_servers_remote ()
386   else
387     Http_getter_env.servers ()
388
389 let add_server ?(logger = fun _ -> ()) ?(position = 0) name =
390   if remote () then
391     add_server_remote ~logger ~position name
392   else begin
393     if position = 0 then begin
394       Http_getter_env.add_server ~position:0 name;
395       update_from_one_server ~logger name (* quick update (new server only) *)
396     end else if position > 0 then begin
397       Http_getter_env.add_server ~position name;
398       update ~logger ()
399     end else  (* already checked by parse_position *)
400       assert false
401   end
402
403 let has_server position = List.mem_assoc position (Http_getter_env.servers ())
404
405 let remove_server ?(logger = fun _ -> ()) position =
406   if remote () then
407     remove_server_remote ~logger ()
408   else begin
409     let server_name =
410       try
411         List.assoc position (Http_getter_env.servers ())
412       with Not_found ->
413         raise (Invalid_argument (sprintf "no server with position %d" position))
414     in
415     Http_getter_env.remove_server position;
416     update ~logger ()
417   end
418
419 let return_uris map filter =
420   let uris = ref [] in
421   map#iter (fun uri _ -> if filter uri then uris := uri :: !uris);
422   List.rev !uris
423
424 let getalluris () =
425   if remote () then
426     getalluris_remote ()
427   else
428     let filter uri =
429       (Pcre.pmatch ~rex:heading_cic_RE uri)
430 (*       && not (Pcre.pmatch ~rex:trailing_types_RE uri) *)
431     in
432     return_uris (Lazy.force cic_map) filter
433
434 let getallrdfuris classs =
435   if remote () then
436     getallrdfuris_remote ()
437   else
438     let filter =
439       let base = "^helm:rdf:www\\.cs\\.unibo\\.it/helm/rdf/" in
440       match classs with
441       | `Forward -> (fun uri -> Pcre.pmatch ~pat:(base ^ "forward") uri)
442       | `Backward -> (fun uri -> Pcre.pmatch ~pat:(base ^ "backward") uri)
443     in
444     return_uris (Lazy.force rdf_map) filter
445
446
447 let (++) (oldann, oldtypes, oldbody, oldtree)
448          (newann, newtypes, newbody, newtree) =
449   ((if newann   > oldann    then newann   else oldann),
450    (if newtypes > oldtypes  then newtypes else oldtypes),
451    (if newbody  > oldbody   then newbody  else oldbody),
452    (if newtree  > oldtree   then newtree  else oldtree))
453     
454 let (types_RE, types_ann_RE, body_RE, body_ann_RE,
455      proof_tree_RE, proof_tree_ann_RE, trailing_slash_RE, theory_RE) =
456   (Pcre.regexp "\\.types$", Pcre.regexp "\\.types\\.ann$",
457    Pcre.regexp "\\.body$", Pcre.regexp "\\.body\\.ann$",
458    Pcre.regexp "\\.proof_tree$", Pcre.regexp "\\.proof_tree\\.ann$",
459    Pcre.regexp "/$", Pcre.regexp "\\.theory$")
460   
461 let basepart_RE =
462   Pcre.regexp
463     "^([^.]*\\.[^.]*)((\\.body)|(\\.proof_tree)|(\\.types))?(\\.ann)?$"
464     
465 let (slash_RE, til_slash_RE, no_slashes_RE) =
466   (Pcre.regexp "/", Pcre.regexp "^.*/", Pcre.regexp "^[^/]*$")
467   
468 let ls regexp =
469   if remote () then
470     ls_remote regexp
471   else begin
472     let looking_for_dir = Pcre.pmatch ~rex:trailing_slash_RE regexp in
473     let pat = Pcre.replace ~rex:trailing_slash_RE ("^" ^ regexp) in
474     let (dir_RE, dir_local_RE, obj_RE, first_comp_RE) =
475       Pcre.regexp (pat ^ "/"), Pcre.regexp "[^/]+/[^/]*",
476       Pcre.regexp (pat ^ "(\\.|$)"), Pcre.regexp "/.*"
477     in
478     let toplevel_theory =
479       match List.rev (Pcre.split ~rex:slash_RE pat) with
480       | dir :: _ -> Some (dir ^ ".theory")
481       | _ -> None
482     in
483     let dirs = ref StringSet.empty in
484     let objs = Hashtbl.create 17 in
485     let store_dir d =
486       dirs := StringSet.add (List.hd (Pcre.split ~rex:slash_RE d)) !dirs
487     in
488     let store_obj o =
489       let basepart = Pcre.replace ~rex:basepart_RE ~templ:"$1" o in
490       let no_flags = false, No, No, No in
491       let oldflags =
492         try
493           Hashtbl.find objs basepart
494         with Not_found -> (* no ann, no types, no body, no proof tree *)
495           no_flags
496       in
497       let newflags =
498         match o with
499         | s when Pcre.pmatch ~rex:types_RE s          -> (false, Yes, No, No)
500         | s when Pcre.pmatch ~rex:types_ann_RE s      -> (true,  Ann, No, No)
501         | s when Pcre.pmatch ~rex:body_RE s           -> (false, No, Yes, No)
502         | s when Pcre.pmatch ~rex:body_ann_RE s       -> (true,  No, Ann, No)
503         | s when Pcre.pmatch ~rex:proof_tree_RE s     -> (false, No, No, Yes)
504         | s when Pcre.pmatch ~rex:proof_tree_ann_RE s -> (true,  No, No, Ann)
505         | s -> no_flags
506       in
507       Hashtbl.replace objs basepart (oldflags ++ newflags)
508     in
509     (* Variables used in backward compatibility code to map
510        theory:/path/t.theory into theory:/path/t/index.theory
511        when cic:/path/t/ exists *)
512     let the_candidate_for_remapping =
513      (* CSC: Here I am making a strong assumption: the pattern
514         can be only of the form  [^:]*:/path where path is
515         NOT a regular expression *)
516      "theory:" ^ Pcre.replace ~rex:(Pcre.regexp "[^:]*:") pat
517     in
518     let index_not_generated_yet = ref true in
519     let valid_candidates = ref [] in
520     let candidates_found = ref [] in
521     
522     (*(Lazy.force cic_map) # iter*)
523
524     (* depending on prefetch *)
525     let if_prefetch if_is if_not = 
526       if is_prefetch_on() then if_is else if_not 
527     in
528     
529     let iter_on_right_thing = if_prefetch
530       (fun f -> List.iter (fun k -> f k "") 
531       (Tree.ls_path regexp (deref_if_some uri_tree)))
532       (fun f -> (Lazy.force cic_map) # iter f)
533     in
534     let calculate_localpart = if_prefetch 
535         (fun uri -> uri)
536         (fun uri -> Pcre.replace ~rex:dir_RE uri)
537     in
538     let check_if_x_RE = if_prefetch
539         (fun x_RE uri -> true)
540         (fun x_RE uri -> Pcre.pmatch ~rex:x_RE uri)
541     in
542     let check_if_dir_RE = check_if_x_RE dir_RE in
543     let check_if_obj_RE = check_if_x_RE obj_RE in
544
545     iter_on_right_thing
546       (fun key _ ->
547         (* we work in two ways:
548          * 1 iteration on the whole map
549          * 2 tree visit
550          *
551          * Since in the first case 'key' is a complete uri, while
552          * in the second case it is only the subtree rooted in the
553          * query regex, we must relay only on the localpath.
554          *
555          * example:
556          *   query::= cic:/aaa/bbb/
557          *
558          *   answer1 ::= the whole map
559          *
560          *   aswer2  ::= [ "ccc/"; "c1.something"] where 
561          *     cic:/aaa/bbb/ccc/ and cic:/aaa/bbb/c1.something
562          *     are the (partials) uri that matched query
563          *     
564          * after the localpath extracion we have more uris in the first case,
565          * but at least the are all rooted in the same node.
566          *
567          * the Tree.get_frontier may be changed to return the same stuff as 
568          * the map iteration+localpath extraction does, but I hope it is not
569          * necessary
570          *)
571         match key with
572         | uri when looking_for_dir && check_if_dir_RE uri ->
573             (* directory hit *)
574             let localpart = calculate_localpart uri in
575             if Pcre.pmatch ~rex:no_slashes_RE localpart then
576              begin
577               (* Backward compatibility code to map
578                  theory:/path/t.theory into theory:/path/t/index.theory
579                  when cic:/path/t/ exists *)
580               if Pcre.pmatch ~rex:theory_RE localpart then
581                candidates_found := localpart :: !candidates_found
582               else
583                store_obj localpart
584              end
585             else
586              begin
587              store_dir localpart ;
588              if Pcre.pmatch localpart ~rex:dir_local_RE then 
589                begin
590                let valid = 
591                  Pcre.replace ~rex:first_comp_RE localpart ^ ".theory"
592                in
593                if not (List.mem valid !valid_candidates) then
594                  valid_candidates := valid::!valid_candidates
595                end
596              end
597         | uri when (not looking_for_dir) && check_if_obj_RE uri ->
598             (* file hit *)
599             store_obj (Pcre.replace ~rex:til_slash_RE uri)
600         | uri -> ());
601 (*
602                    (* miss *)
603            if !index_not_generated_yet &&
604               Pcre.pmatch ~rex:orig_theory_RE uri
605            then
606             (index_not_generated_yet := false ;
607              store_obj "index.theory"));
608  *)
609     store_obj "index.theory";
610     List.iter
611      (fun localpart ->
612        if not (List.mem localpart !valid_candidates) then
613         store_obj localpart
614      ) !candidates_found ;
615     let ls_items = ref [] in
616     StringSet.iter (fun dir -> ls_items := Ls_section dir :: !ls_items) !dirs;
617     Http_getter_misc.hashtbl_sorted_iter
618       (fun uri (annflag, typesflag, bodyflag, treeflag) ->
619         ls_items :=
620           Ls_object {
621             uri = uri; ann = annflag;
622             types = typesflag; body = bodyflag; proof_tree = treeflag
623           } :: !ls_items)
624       objs;
625     List.rev !ls_items
626   end
627
628 (* Shorthands from now on *)
629
630 let getxml' uri = getxml (UriManager.string_of_uri uri)
631 let resolve' uri = resolve (UriManager.string_of_uri uri)
632 let register' uri url = register ~uri:(UriManager.string_of_uri uri) ~url
633 let unregister' uri = unregister (UriManager.string_of_uri uri)
634
635 let sync_dump_file () =
636   if is_prefetch_on () then
637     dump_tree ()
638   
639 let init () =
640   Http_getter_logger.set_log_level
641     (Helm_registry.get_opt_default Helm_registry.get_int 1 "getter.log_level");
642   Http_getter_logger.set_log_file
643     (Helm_registry.get_opt Helm_registry.get_string "getter.log_file");
644   Http_getter_env.reload ();
645   let is_prefetch_set = 
646     Helm_registry.get_opt_default Helm_registry.get_bool false "getter.prefetch"
647   in
648   if is_prefetch_set then
649     ignore (Thread.create sync_with_map ())
650