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