2 * Copyright (C) 2003-2004:
3 * Stefano Zacchiroli <zack@cs.unibo.it>
4 * for the HELM Team http://helm.cs.unibo.it/
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.
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.
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.
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,
25 * For details, see the HELM World-Wide-Web page,
26 * http://helm.cs.unibo.it/
31 open Http_getter_common
33 open Http_getter_debugger
34 open Http_getter_types
36 exception Not_implemented of string
37 exception UnexpectedGetterOutput
39 (* resolve_result is needed because it is not possible to raise *)
40 (* an exception in a pxp ever-processing callback. Too bad. *)
46 type logger_callback = HelmLogger.html_tag -> unit
48 let not_implemented s = raise (Not_implemented ("Http_getter." ^ s))
50 let (index_line_sep_RE, index_sep_RE, trailing_types_RE,
51 heading_cic_RE, heading_theory_RE, heading_nuprl_RE,
52 heading_rdf_cic_RE, heading_rdf_theory_RE) =
53 (Pcre.regexp "[ \t]+", Pcre.regexp "\r\n|\r|\n",
54 Pcre.regexp "\\.types$",
55 Pcre.regexp "^cic:", Pcre.regexp "^theory:", Pcre.regexp "^nuprl:",
56 Pcre.regexp "^helm:rdf.*//cic:", Pcre.regexp "^helm:rdf.*//theory:")
58 (* global maps, shared by all threads *)
61 lazy (new Http_getter_map.map (Lazy.force Http_getter_env.cic_dbm))
63 lazy (new Http_getter_map.map (Lazy.force Http_getter_env.nuprl_dbm))
65 lazy (new Http_getter_map.map (Lazy.force Http_getter_env.rdf_dbm))
67 lazy (new Http_getter_map.map (Lazy.force Http_getter_env.xsl_dbm))
69 let maps = [ cic_map; nuprl_map; rdf_map; xsl_map ]
70 let close_maps () = List.iter (fun m -> (Lazy.force m) # close) maps
71 let clear_maps () = List.iter (fun m -> (Lazy.force m) # clear) maps
72 let sync_maps () = List.iter (fun m -> (Lazy.force m) # sync) maps
74 let map_of_uri = function
75 | uri when is_cic_uri uri -> Lazy.force cic_map
76 | uri when is_nuprl_uri uri -> Lazy.force nuprl_map
77 | uri when is_rdf_uri uri -> Lazy.force rdf_map
78 | uri when is_xsl_uri uri -> Lazy.force xsl_map
79 | uri -> raise (Unresolvable_URI uri)
81 let update_from_server logger server_url = (* use global maps *)
82 debug_print ("Updating information from " ^ server_url);
83 let xml_url_of_uri = function
84 (* TODO missing sanity checks on server_url, e.g. it can contains $1 *)
85 | uri when (Pcre.pmatch ~rex:heading_cic_RE uri) ->
86 Pcre.replace ~rex:heading_cic_RE ~templ:server_url uri
87 | uri when (Pcre.pmatch ~rex:heading_theory_RE uri) ->
88 Pcre.replace ~rex:heading_theory_RE ~templ:server_url uri
89 | uri when (Pcre.pmatch ~rex:heading_nuprl_RE uri) ->
90 Pcre.replace ~rex:heading_nuprl_RE ~templ:server_url uri
91 | uri -> raise (Invalid_URI uri)
93 let rdf_url_of_uri = function (* TODO as above *)
94 | uri when (Pcre.pmatch ~rex:heading_rdf_cic_RE uri) ->
95 Pcre.replace ~rex:heading_rdf_cic_RE ~templ:server_url uri
96 | uri when (Pcre.pmatch ~rex:heading_rdf_theory_RE uri) ->
97 Pcre.replace ~rex:heading_rdf_theory_RE ~templ:server_url uri
98 | uri -> raise (Invalid_URI uri)
100 logger (`T ("Processing server: " ^ server_url));
101 let (xml_index, rdf_index, xsl_index) =
102 (* TODO keeps index in memory, is better to keep them on temp files? *)
103 (http_get (server_url ^ "/" ^ (Lazy.force Http_getter_env.xml_index)),
104 http_get (server_url ^ "/" ^ (Lazy.force Http_getter_env.rdf_index)),
105 http_get (server_url ^ "/" ^ (Lazy.force Http_getter_env.xsl_index)))
107 if (xml_index = None && rdf_index = None && xsl_index = None) then
108 debug_print (sprintf "Warning: useless server %s" server_url);
109 (match xml_index with
111 logger (`T "Updating XML db ...<br />");
114 | l when is_blank_line l -> () (* skip blank and commented lines *)
117 (match Pcre.split ~rex:index_line_sep_RE l with
119 assert (is_cic_uri uri || is_nuprl_uri uri) ;
120 (map_of_uri uri)#replace
121 uri ((xml_url_of_uri uri) ^ ".xml.gz")
123 assert (is_cic_uri uri || is_nuprl_uri uri) ;
124 (map_of_uri uri)#replace
125 uri ((xml_url_of_uri uri) ^ ".xml")
126 | _ -> logger (`T ("Ignoring invalid line: '" ^ l)))
127 with Invalid_URI uri ->
128 logger (`T ("Ignoring invalid XML URI: '" ^ l))))
129 (Pcre.split ~rex:index_sep_RE xml_index); (* xml_index lines *)
130 logger (`T "All done")
132 (match rdf_index with
134 logger (`T "Updating RDF db ...");
138 (match Pcre.split ~rex:index_line_sep_RE l with
140 (Lazy.force rdf_map) # replace uri
141 ((rdf_url_of_uri uri) ^ ".xml.gz")
143 (Lazy.force rdf_map) # replace uri
144 ((rdf_url_of_uri uri) ^ ".xml")
145 | _ -> logger (`T ("Ignoring invalid line: '" ^ l)))
146 with Invalid_URI uri ->
147 logger (`T ("Ignoring invalid RDF URI: '" ^ l)))
148 (Pcre.split ~rex:index_sep_RE rdf_index); (* rdf_index lines *)
149 logger (`T "All done")
151 (match xsl_index with
153 logger (`T "Updating XSLT db ...");
155 (fun l -> (Lazy.force xsl_map) # replace l (server_url ^ "/" ^ l))
156 (Pcre.split ~rex:index_sep_RE xsl_index);
157 logger (`T "All done")
159 debug_print "done with this server"
161 let update_from_all_servers logger () = (* use global maps *)
164 (update_from_server logger)
165 (* reverse order: 1st server is the most important one *)
166 (List.map snd (List.rev (Http_getter_env.servers ())));
169 let update_from_one_server ?(logger = fun _ -> ()) server_url =
170 update_from_server logger server_url
172 let temp_file_of_uri uri =
173 let flat_string s s' c =
174 let cs = String.copy s in
175 for i = 0 to (String.length s) - 1 do
176 if String.contains s' s.[i] then cs.[i] <- c
180 let user = try Unix.getlogin () with _ -> "" in
181 Filename.open_temp_file (user ^ flat_string uri ".-=:;!?/&" '_') ""
183 (* should we use a remote getter or not *)
186 Helm_registry.get "getter.mode" = "remote"
187 with Helm_registry.Key_not_found _ -> false
188 let getter_url () = Helm_registry.get "getter.url"
190 (* Remote interface: getter methods implemented using a remote getter *)
193 let getxslt_remote ~patch_dtd uri = not_implemented "getxslt_remote"
194 let getdtd_remote ~patch_dtd uri = not_implemented "getdtd_remote"
195 let clean_cache_remote () = not_implemented "clean_cache_remote"
196 let list_servers_remote () = not_implemented "list_servers_remote"
197 let add_server_remote ~logger ~position name = not_implemented "add_server_remote"
198 let remove_server_remote ~logger position = not_implemented "remove_server_remote"
199 let getalluris_remote () = not_implemented "getalluris_remote"
200 let getallrdfuris_remote () = not_implemented "getallrdfuris_remote"
201 let ls_remote lsuri = not_implemented "ls_remote"
204 let resolve_remote uri =
205 (* deliver resolve request to http_getter *)
206 let doc = ClientHTTP.get (sprintf "%sresolve?uri=%s" (getter_url ()) uri) in
207 let res = ref Unknown in
208 Pxp_yacc.process_entity Pxp_yacc.default_config (`Entry_content [])
209 (Pxp_yacc.create_entity_manager ~is_document:true Pxp_yacc.default_config
210 (Pxp_yacc.from_string doc))
212 | Pxp_yacc.E_start_tag ("url",["value",url],_) -> res := Resolved url
213 | Pxp_yacc.E_start_tag ("unresolved",[],_) ->
214 res := Exception (Unresolvable_URI uri)
215 | Pxp_yacc.E_start_tag _ -> res := Exception UnexpectedGetterOutput
218 | Unknown -> raise UnexpectedGetterOutput
219 | Exception e -> raise e
220 | Resolved url -> url
222 let register_remote ~uri ~url =
223 ClientHTTP.send (sprintf "%sregister?uri=%s&url=%s" (getter_url ()) uri url)
225 let register_remote ~uri ~url =
226 ClientHTTP.send (sprintf "%sregister?uri=%s&url=%s" (getter_url ()) uri url)
228 let update_remote logger () =
229 let answer = ClientHTTP.get (getter_url () ^ "update") in
232 let getxml_remote ~format ~patch_dtd uri =
233 ClientHTTP.get_and_save_to_tmp
234 (sprintf "%sgetxml?uri=%s&format=%s&patch_dtd=%s"
236 (match format with Enc_normal -> "normal" | Enc_gzipped -> "gzipped")
237 (match patch_dtd with true -> "yes" | false -> "no"))
241 let help () = Http_getter_const.usage_string (Http_getter_env.env_to_string ())
248 (map_of_uri uri)#resolve uri
249 with Http_getter_map.Key_not_found _ -> raise (Unresolvable_URI uri)
251 (* Warning: this fail if uri is already registered *)
252 let register ~uri ~url =
254 register_remote ~uri ~url
256 (map_of_uri uri)#add uri url
258 let update ?(logger = fun _ -> ()) () =
260 update_remote logger ()
262 update_from_all_servers logger ()
264 let getxml ?(format = Enc_normal) ?(patch_dtd = true) uri =
266 getxml_remote ~format ~patch_dtd uri
268 let url = resolve uri in
269 let (fname, outchan) = temp_file_of_uri uri in
270 Http_getter_cache.respond_xml ~uri ~url ~enc:format ~patch:patch_dtd
276 let getxslt ?(patch_dtd = true) uri =
278 getxslt_remote ~patch_dtd uri
280 let url = resolve uri in
281 let (fname, outchan) = temp_file_of_uri uri in
282 Http_getter_cache.respond_xsl ~url ~patch:patch_dtd outchan;
287 let getdtd ?(patch_dtd = true) uri =
289 getdtd_remote ~patch_dtd uri
291 let url = Lazy.force Http_getter_env.dtd_dir ^ "/" ^ uri in
292 let (fname, outchan) = temp_file_of_uri uri in
293 Http_getter_cache.respond_dtd ~url ~patch:patch_dtd outchan;
300 clean_cache_remote ()
302 Http_getter_cache.clean ()
304 let list_servers () =
306 list_servers_remote ()
308 Http_getter_env.servers ()
310 let add_server ?(logger = fun _ -> ()) ?(position = 0) name =
312 add_server_remote ~logger ~position name
314 if position = 0 then begin
315 Http_getter_env.add_server ~position:0 name;
316 update_from_one_server ~logger name (* quick update (new server only) *)
317 end else if position > 0 then begin
318 Http_getter_env.add_server ~position name;
320 end else (* already checked by parse_position *)
324 let has_server position = List.mem_assoc position (Http_getter_env.servers ())
326 let remove_server ?(logger = fun _ -> ()) position =
328 remove_server_remote ~logger ()
332 List.assoc position (Http_getter_env.servers ())
334 raise (Invalid_argument (sprintf "no server with position %d" position))
336 Http_getter_env.remove_server position;
340 let return_uris map filter =
342 map#iter (fun uri _ -> if filter uri then uris := uri :: !uris);
350 (Pcre.pmatch ~rex:heading_cic_RE uri) &&
351 not (Pcre.pmatch ~rex:trailing_types_RE uri)
353 return_uris (Lazy.force cic_map) filter
355 let getallrdfuris classs =
357 getallrdfuris_remote ()
360 let base = "^helm:rdf:www\\.cs\\.unibo\\.it/helm/rdf/" in
362 | `Forward -> (fun uri -> Pcre.pmatch ~pat:(base ^ "forward") uri)
363 | `Backward -> (fun uri -> Pcre.pmatch ~pat:(base ^ "backward") uri)
365 return_uris (Lazy.force rdf_map) filter
368 let (++) (oldann, oldtypes, oldbody, oldtree)
369 (newann, newtypes, newbody, newtree) =
370 ((if newann > oldann then newann else oldann),
371 (if newtypes > oldtypes then newtypes else oldtypes),
372 (if newbody > oldbody then newbody else oldbody),
373 (if newtree > oldtree then newtree else oldtree))
377 "^([^.]*\\.[^.]*)((\\.body)|(\\.proof_tree)|(\\.types))?(\\.ann)?$"
379 let (types_RE, types_ann_RE, body_RE, body_ann_RE,
380 proof_tree_RE, proof_tree_ann_RE) =
381 (Pcre.regexp "\\.types$", Pcre.regexp "\\.types\\.ann$",
382 Pcre.regexp "\\.body$", Pcre.regexp "\\.body\\.ann$",
383 Pcre.regexp "\\.proof_tree$", Pcre.regexp "\\.proof_tree\\.ann$")
385 let (slash_RE, til_slash_RE, no_slashes_RE) =
386 (Pcre.regexp "/", Pcre.regexp "^.*/", Pcre.regexp "^[^/]*$")
394 (match lsuri with Cic p -> ("cic:" ^ p) | Theory p -> ("theory:" ^ p))
396 let (dir_RE, obj_RE) =
397 (Pcre.regexp (pat ^ "/"), Pcre.regexp (pat ^ "(\\.|$)"))
399 let dirs = ref StringSet.empty in
400 let objs = Hashtbl.create 17 in
402 dirs := StringSet.add (List.hd (Pcre.split ~rex:slash_RE d)) !dirs
405 let basepart = Pcre.replace ~rex:basepart_RE ~templ:"$1" o in
406 let no_flags = false, No, No, No in
409 Hashtbl.find objs basepart
410 with Not_found -> (* no ann, no types, no body, no proof tree *)
415 | s when Pcre.pmatch ~rex:types_RE s -> (false, Yes, No, No)
416 | s when Pcre.pmatch ~rex:types_ann_RE s -> (true, Ann, No, No)
417 | s when Pcre.pmatch ~rex:body_RE s -> (false, No, Yes, No)
418 | s when Pcre.pmatch ~rex:body_ann_RE s -> (true, No, Ann, No)
419 | s when Pcre.pmatch ~rex:proof_tree_RE s -> (false, No, No, Yes)
420 | s when Pcre.pmatch ~rex:proof_tree_ann_RE s -> (true, No, No, Ann)
423 Hashtbl.replace objs basepart (oldflags ++ newflags)
425 (Lazy.force cic_map) # iter
426 (* BLEARGH Dbm module lacks support for fold-like functions *)
429 | uri when Pcre.pmatch ~rex:dir_RE uri -> (* directory hit *)
430 let localpart = Pcre.replace ~rex:dir_RE uri in
431 if Pcre.pmatch ~rex:no_slashes_RE localpart then
435 | uri when Pcre.pmatch ~rex:obj_RE uri -> (* file hit *)
436 store_obj (Pcre.replace ~rex:til_slash_RE uri)
437 | uri -> () (* miss *));
438 let ls_items = ref [] in
439 StringSet.iter (fun dir -> ls_items := Ls_section dir :: !ls_items) !dirs;
440 Http_getter_misc.hashtbl_sorted_iter
441 (fun uri (annflag, typesflag, bodyflag, treeflag) ->
444 uri = uri; ann = annflag;
445 types = typesflag; body = bodyflag; proof_tree = treeflag
451 (* Shorthands from now on *)
453 let getxml' uri = getxml (UriManager.string_of_uri uri)
454 let resolve' uri = resolve (UriManager.string_of_uri uri)
455 let register' uri url = register ~uri:(UriManager.string_of_uri uri) ~url