2 * Copyright (C) 2000, HELM Team.
4 * This file is part of HELM, an Hypertextual, Electronic
5 * Library of Mathematics, developed at the Computer Science
6 * Department, University of Bologna, Italy.
8 * HELM is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * HELM is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with HELM; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 * For details, see the HELM World-Wide-Web page,
24 * http://cs.unibo.it/helm/.
27 (* TODO optimization: precompile almost all regexp *)
29 open Http_getter_common;;
30 open Http_getter_types;;
33 (* debugging settings *)
35 let debug_print s = if debug then prerr_endline ("[HTTP-Getter] " ^ s);;
36 let http_debug = false;;
37 Http_common.debug := http_debug;;
40 (* debug_print ("Downloading URL: " ^ url); *)
42 Some (Http_client.Convenience.http_get url)
43 with Http_client.Http_error (code, _) -> None
46 (sprintf "Failed to download %s, HTTP response was %d" url code);
51 let parse_enc (req: Http_types.request) =
53 (match req#param "format" with
54 | "normal" -> Enc_normal
56 | s -> raise (Http_getter_bad_request ("Invalid format: " ^ s)))
57 with Http_types.Param_not_found _ -> Enc_normal
59 let parse_patch_dtd (req: Http_types.request) =
60 match req#param "patch_dtd" with
61 | s when String.lowercase s = "yes" -> true
62 | s when String.lowercase s = "no" -> false
63 | s -> raise (Http_getter_bad_request ("Invalid patch_dtd value: " ^ s))
65 let parse_output_format (req: Http_types.request) =
66 match req#param "format" with
67 | s when String.lowercase s = "txt" -> Fmt_text
68 | s when String.lowercase s = "xml" -> Fmt_xml
69 | s -> raise (Http_getter_bad_request ("Invalid /ls format: " ^ s))
71 let parse_ls_uri (req: Http_types.request) =
72 let baseuri = req#param "baseuri" in
74 Pcre.extract ~pat:"^(\\w+):(.*)$" (Pcre.replace ~pat:"/+$" baseuri)
76 match (subs.(1), subs.(2)) with
77 | "cic", uri -> Cic uri
78 | "theory", uri -> Theory uri
79 | _ -> raise (Http_getter_bad_request ("Invalid /ls baseuri: " ^ baseuri))
82 let xml_map = new Http_getter_map.map Http_getter_env.xml_dbm in
83 let rdf_map = new Http_getter_map.map Http_getter_env.rdf_dbm in
84 let xsl_map = new Http_getter_map.map Http_getter_env.xsl_dbm in
86 let map_of_uri = function
87 | uri when is_xml_uri uri -> xml_map
88 | uri when is_rdf_uri uri -> rdf_map
89 | uri when is_xsl_uri uri -> xsl_map
90 | uri -> raise (Http_getter_unresolvable_URI uri)
92 let resolve uri = (map_of_uri uri)#resolve uri in
93 let register uri = (map_of_uri uri )#add uri in
94 let return_all_foo_uris map doctype filter outchan =
95 Http_daemon.send_basic_headers ~code:200 outchan;
96 Http_daemon.send_header "Content-Type" "text/xml" outchan;
97 Http_daemon.send_CRLF outchan;
101 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
102 <!DOCTYPE %s SYSTEM \"%s/getdtd?uri=%s.dtd\">
107 Http_getter_env.my_own_url
113 output_string outchan (sprintf "\t<uri value=\"%s\" />\n" uri));
114 output_string outchan (sprintf "</%s>\n" doctype)
116 let return_all_xml_uris = return_all_foo_uris xml_map "alluris" in
117 let return_all_rdf_uris = return_all_foo_uris rdf_map "allrdfuris" in
119 let (++) (oldann, oldtypes, oldbody) (newann, newtypes, newbody) =
120 ((if newann > oldann then newann else oldann),
121 (if newtypes > oldtypes then newtypes else oldtypes),
122 (if newbody > oldbody then newbody else oldbody))
125 Pcre.regexp "^([^.]*\\.[^.]*)((\\.body)|(\\.types))?(\\.ann)?"
127 let (types_RE, types_ann_RE, body_RE, body_ann_RE) =
128 (Pcre.regexp "\\.types", Pcre.regexp "\\.types.ann",
129 Pcre.regexp "\\.body", Pcre.regexp "\\.body.ann")
131 let (slash_RE, til_slash_RE, no_slashes_RE) =
132 (Pcre.regexp "/", Pcre.regexp "^.*/", Pcre.regexp "^[^/]*$")
134 fun lsuri fmt outchan ->
137 (match lsuri with Cic p -> ("cic:" ^ p) | Theory p -> ("theory:" ^ p))
139 let (dir_RE, obj_RE) =
140 (Pcre.regexp (pat ^ "/"), Pcre.regexp (pat ^ "(\\.|$)"))
142 let dirs = ref StringSet.empty in
143 let objs = Hashtbl.create 17 in
145 dirs := StringSet.add (List.hd (Pcre.split ~rex:slash_RE d)) !dirs
148 let basepart = Pcre.replace ~rex:basepart_RE ~templ:"$1" o in
151 Hashtbl.find objs basepart
152 with Not_found -> (false, No, No) (* no ann, no types no body *)
156 | s when Pcre.pmatch ~rex:types_RE s -> (false, Yes, No)
157 | s when Pcre.pmatch ~rex:types_ann_RE s -> (true, Ann, No)
158 | s when Pcre.pmatch ~rex:body_RE s -> (false, No, Yes)
159 | s when Pcre.pmatch ~rex:body_ann_RE s -> (true, No, Ann)
160 | s -> (false, No, No)
162 Hashtbl.replace objs basepart (oldflags ++ newflags)
164 xml_map#iter (* BLEARGH Dbm module lacks support for fold-like functions *)
167 | uri when Pcre.pmatch ~rex:dir_RE uri -> (* directory hit *)
168 let localpart = Pcre.replace ~rex:dir_RE uri in
169 if Pcre.pmatch ~rex:no_slashes_RE localpart then
173 | uri when Pcre.pmatch ~rex:obj_RE uri -> (* file hit *)
174 store_obj (Pcre.replace ~rex:til_slash_RE uri)
175 | uri -> () (* miss *));
180 (fun s d -> sprintf "%sdir, %s\n" s d) ""
181 (StringSet.elements !dirs)) ^
182 (Http_getter_misc.hashtbl_sorted_fold
183 (fun uri (annflag, typesflag, bodyflag) cont ->
184 sprintf "%sobject, %s, <%s,%s,%s>\n"
185 cont uri (if annflag then "YES" else "NO")
186 (string_of_ls_flag typesflag) (string_of_ls_flag bodyflag))
190 ~headers:["Content-Type", "text/plain"] ~body outchan
194 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
195 <!DOCTYPE ls SYSTEM \"%s/getdtd?uri=ls.dtd\">
201 Http_getter_env.my_own_url
206 (fun d -> "<section>" ^ d ^ "</section>")
207 (StringSet.elements !dirs))) ^ "\n" ^
208 (Http_getter_misc.hashtbl_sorted_fold
209 (fun uri (annflag, typesflag, bodyflag) cont ->
211 "%s<object name=\"%s\">
212 \t<ann value=\"%s\" />
213 \t<types value=\"%s\" />
214 \t<body value=\"%s\" />
217 cont uri (if annflag then "YES" else "NO")
218 (string_of_ls_flag typesflag)
219 (string_of_ls_flag bodyflag))
223 ~headers:["Content-Type", "text/xml"] ~body outchan
225 let update_from_server logmsg server_url = (* use global maps *)
226 debug_print ("Updating information from " ^ server_url);
227 let xml_url_of_uri = function
228 (* TODO missing sanity checks on server_url, e.g. it can contains $1 *)
229 | uri when (Pcre.pmatch ~pat:"^cic:" uri) ->
230 Pcre.replace ~pat:"^cic:" ~templ:server_url uri
231 | uri when (Pcre.pmatch ~pat:"^theory:" uri) ->
232 Pcre.replace ~pat:"^theory:" ~templ:server_url uri
233 | uri -> raise (Http_getter_invalid_URI uri)
235 let rdf_url_of_uri = function (* TODO as above *)
236 | uri when (Pcre.pmatch ~pat:"^helm:rdf.*//cic:" uri) ->
237 Pcre.replace ~pat:"^helm:rdf.*//cic:" ~templ:server_url uri
238 | uri when (Pcre.pmatch ~pat:"^helm:rdf.*//theory:" uri) ->
239 Pcre.replace ~pat:"^helm:rdf.*//theory:" ~templ:server_url uri
240 | uri -> raise (Http_getter_invalid_URI uri)
242 let log = ref ("Processing server: " ^ server_url ^ "<br />\n") in
243 let (xml_index, rdf_index, xsl_index) =
244 (* TODO keeps index in memory, is better to keep them on temp files? *)
245 (http_get (server_url ^ "/" ^ Http_getter_env.xml_index),
246 http_get (server_url ^ "/" ^ Http_getter_env.rdf_index),
247 http_get (server_url ^ "/" ^ Http_getter_env.xsl_index))
249 if (xml_index = None && rdf_index = None && xsl_index = None) then
250 debug_print (sprintf "Warning: useless server %s" server_url);
251 (match xml_index with
253 (log := !log ^ "Updating XML db ...<br />\n";
257 (match Pcre.split ~pat:"[ \\t]+" l with
258 | [uri; "gz"] -> xml_map#add uri ((xml_url_of_uri uri) ^ ".xml.gz")
259 | [uri] -> xml_map#add uri ((xml_url_of_uri uri) ^ ".xml")
260 | _ -> log := !log ^ "Ignoring invalid line: " ^ l ^ "<br />\n")
261 with Http_getter_invalid_URI uri ->
262 log := !log ^ "Ignoring invalid XML URI: " ^ uri ^ "<br />\n")
263 (Pcre.split ~pat:"\n+" xml_index)) (* xml_index lines *)
265 (match rdf_index with
267 (log := !log ^ "Updating RDF db ...<br />\n";
271 (match Pcre.split ~pat:"[ \\t]+" l with
272 | [uri; "gz"] -> rdf_map#add uri ((rdf_url_of_uri uri) ^ ".xml.gz")
273 | [uri] -> rdf_map#add uri ((rdf_url_of_uri uri) ^ ".xml")
274 | _ -> log := !log ^ "Ignoring invalid line: " ^ l ^ "<br />\n")
275 with Http_getter_invalid_URI uri ->
276 log := !log ^ "Ignoring invalid RDF URI: " ^ uri ^ "<br />\n")
277 (Pcre.split ~pat:"\n+" rdf_index)) (* rdf_index lines *)
279 (match xsl_index with
281 (log := !log ^ "Updating XSLT db ...<br />\n";
283 (fun l -> xsl_map#add l (server_url ^ "/" ^ l))
284 (Pcre.split ~pat:"\n+" xsl_index);
285 log := !log ^ "All done!<br />\n")
291 let callback (req: Http_types.request) outchan =
293 debug_print ("Connection from " ^ req#clientAddr);
294 debug_print ("Received request: " ^ req#path);
296 | "/help" -> return_html_msg Http_getter_const.usage_string outchan
297 | "/getxml" | "/getxslt" | "/getdtd" | "/resolve" | "/register" ->
298 (let uri = req#param "uri" in (* common parameter *)
301 let enc = parse_enc req in
303 try parse_patch_dtd req with Http_types.Param_not_found _ -> true
305 Http_getter_cache.respond_xml
306 ~url:(resolve uri) ~uri ~enc ~patch_dtd outchan
309 try parse_patch_dtd req with Http_types.Param_not_found _ -> true
311 Http_getter_cache.respond_xsl ~url:(resolve uri) ~patch_dtd outchan
314 try parse_patch_dtd req with Http_types.Param_not_found _ -> true
316 Http_getter_cache.respond_dtd
317 ~patch_dtd ~url:(Http_getter_env.dtd_dir ^ "/" ^ uri) outchan
321 (sprintf "<url value=\"%s\" />\n" (resolve uri))
323 with Http_getter_unresolvable_URI uri ->
324 return_xml_msg "<unresolved />\n" outchan)
326 let url = req#param "url" in
328 return_html_msg "Register done" outchan
331 (xml_map#clear; rdf_map#clear; xsl_map#clear;
335 "" (* initial logmsg: empty *)
336 (* reverse order: 1st server is the most important one *)
337 (List.rev Http_getter_env.servers)
339 xml_map#sync; rdf_map#sync; xsl_map#sync;
340 return_html_msg log outchan)
344 (Pcre.pmatch ~pat:"^cic:" uri) &&
345 not (Pcre.pmatch ~pat:"\\.types$" uri))
347 | "/getallrdfuris" ->
348 (let classs = req#param "class" in
351 let base = "^helm:rdf:www\\.cs\\.unibo\\.it/helm/rdf/" in
353 | ("forward" as c) | ("backward" as c) ->
354 (fun uri -> Pcre.pmatch ~pat:(base ^ c) uri)
355 | c -> raise (Http_getter_invalid_RDF_class c)
357 return_all_rdf_uris filter outchan
358 with Http_getter_invalid_RDF_class c ->
359 raise (Http_getter_bad_request ("Invalid RDF class: " ^ c)))
360 | "/ls" -> return_ls (parse_ls_uri req) (parse_output_format req) outchan
362 Http_daemon.respond ~body:Http_getter_const.empty_xml outchan
364 Http_daemon.respond_error ~status:(`Client_error `Bad_request) outchan)
366 | Http_types.Param_not_found attr_name ->
367 return_400 (sprintf "Parameter '%s' is missing" attr_name) outchan
368 | Http_getter_bad_request msg -> return_html_error msg outchan
369 | Http_getter_internal_error msg -> return_html_internal_error msg outchan
372 ("Uncaught exception: " ^ (Printexc.to_string exc))
376 (* daemon initialization *)
377 Http_getter_env.dump_env ();
379 Unix.putenv "http_proxy" "";
381 ~timeout:None ~port:Http_getter_env.port ~mode:`Thread callback