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