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 save_maps () = xml_map#close; rdf_map#close; xsl_map#close in
87 let map_of_uri = function
88 | uri when is_xml_uri uri -> xml_map
89 | uri when is_rdf_uri uri -> rdf_map
90 | uri when is_xsl_uri uri -> xsl_map
91 | uri -> raise (Http_getter_unresolvable_URI uri)
93 let resolve uri = (map_of_uri uri)#resolve uri in
94 let register uri = (map_of_uri uri )#add uri in
95 let return_all_foo_uris map doctype filter outchan =
96 Http_daemon.send_basic_headers ~code:200 outchan;
97 Http_daemon.send_header "Content-Type" "text/xml" outchan;
98 Http_daemon.send_CRLF outchan;
102 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
103 <!DOCTYPE %s SYSTEM \"%s/getdtd?uri=%s.dtd\">
108 Http_getter_env.my_own_url
114 output_string outchan (sprintf "\t<uri value=\"%s\" />\n" uri));
115 output_string outchan (sprintf "</%s>\n" doctype)
117 let return_all_xml_uris = return_all_foo_uris xml_map "alluris" in
118 let return_all_rdf_uris = return_all_foo_uris rdf_map "allrdfuris" in
120 let (++) (oldann, oldtypes, oldbody) (newann, newtypes, newbody) =
121 ((if newann > oldann then newann else oldann),
122 (if newtypes > oldtypes then newtypes else oldtypes),
123 (if newbody > oldbody then newbody else oldbody))
126 Pcre.regexp "^([^.]*\\.[^.]*)((\\.body)|(\\.types))?(\\.ann)?"
128 let (types_RE, types_ann_RE, body_RE, body_ann_RE) =
129 (Pcre.regexp "\\.types", Pcre.regexp "\\.types.ann",
130 Pcre.regexp "\\.body", Pcre.regexp "\\.body.ann")
132 let (slash_RE, til_slash_RE, no_slashes_RE) =
133 (Pcre.regexp "/", Pcre.regexp "^.*/", Pcre.regexp "^[^/]*$")
135 fun lsuri fmt outchan ->
138 (match lsuri with Cic p -> ("cic:" ^ p) | Theory p -> ("theory:" ^ p))
140 let (dir_RE, obj_RE) =
141 (Pcre.regexp (pat ^ "/"), Pcre.regexp (pat ^ "(\\.|$)"))
143 let dirs = ref StringSet.empty in
144 let objs = Hashtbl.create 17 in
146 dirs := StringSet.add (List.hd (Pcre.split ~rex:slash_RE d)) !dirs
149 let basepart = Pcre.replace ~rex:basepart_RE ~templ:"$1" o in
152 Hashtbl.find objs basepart
153 with Not_found -> (false, No, No) (* no ann, no types no body *)
157 | s when Pcre.pmatch ~rex:types_RE s -> (false, Yes, No)
158 | s when Pcre.pmatch ~rex:types_ann_RE s -> (true, Ann, No)
159 | s when Pcre.pmatch ~rex:body_RE s -> (false, No, Yes)
160 | s when Pcre.pmatch ~rex:body_ann_RE s -> (true, No, Ann)
161 | s -> (false, No, No)
163 Hashtbl.replace objs basepart (oldflags ++ newflags)
165 xml_map#iter (* BLEARGH Dbm module lacks support for fold-like functions *)
168 | uri when Pcre.pmatch ~rex:dir_RE uri -> (* directory hit *)
169 let localpart = Pcre.replace ~rex:dir_RE uri in
170 if Pcre.pmatch ~rex:no_slashes_RE localpart then
174 | uri when Pcre.pmatch ~rex:obj_RE uri -> (* file hit *)
175 store_obj (Pcre.replace ~rex:til_slash_RE uri)
176 | uri -> () (* miss *));
181 (fun s d -> sprintf "%sdir, %s\n" s d) ""
182 (StringSet.elements !dirs)) ^
183 (Http_getter_misc.hashtbl_sorted_fold
184 (fun uri (annflag, typesflag, bodyflag) cont ->
185 sprintf "%sobject, %s, <%s,%s,%s>\n"
186 cont uri (if annflag then "YES" else "NO")
187 (string_of_ls_flag typesflag) (string_of_ls_flag bodyflag))
191 ~headers:["Content-Type", "text/plain"] ~body outchan
195 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
196 <!DOCTYPE ls SYSTEM \"%s/getdtd?uri=ls.dtd\">
202 Http_getter_env.my_own_url
207 (fun d -> "<section>" ^ d ^ "</section>")
208 (StringSet.elements !dirs))) ^ "\n" ^
209 (Http_getter_misc.hashtbl_sorted_fold
210 (fun uri (annflag, typesflag, bodyflag) cont ->
212 "%s<object name=\"%s\">
213 \t<ann value=\"%s\" />
214 \t<types value=\"%s\" />
215 \t<body value=\"%s\" />
218 cont uri (if annflag then "YES" else "NO")
219 (string_of_ls_flag typesflag)
220 (string_of_ls_flag bodyflag))
224 ~headers:["Content-Type", "text/xml"] ~body outchan
226 let update_from_server logmsg server_url = (* use global maps *)
227 debug_print ("Updating information from " ^ server_url);
228 let xml_url_of_uri = function
229 (* TODO missing sanity checks on server_url, e.g. it can contains $1 *)
230 | uri when (Pcre.pmatch ~pat:"^cic:" uri) ->
231 Pcre.replace ~pat:"^cic:" ~templ:server_url uri
232 | uri when (Pcre.pmatch ~pat:"^theory:" uri) ->
233 Pcre.replace ~pat:"^theory:" ~templ:server_url uri
234 | uri -> raise (Http_getter_invalid_URI uri)
236 let rdf_url_of_uri = function (* TODO as above *)
237 | uri when (Pcre.pmatch ~pat:"^helm:rdf.*//cic:" uri) ->
238 Pcre.replace ~pat:"^helm:rdf.*//cic:" ~templ:server_url uri
239 | uri when (Pcre.pmatch ~pat:"^helm:rdf.*//theory:" uri) ->
240 Pcre.replace ~pat:"^helm:rdf.*//theory:" ~templ:server_url uri
241 | uri -> raise (Http_getter_invalid_URI uri)
243 let log = ref (logmsg ^ "Processing server: " ^ server_url ^ "<br />\n") in
244 let (xml_index, rdf_index, xsl_index) =
245 (* TODO keeps index in memory, is better to keep them on temp files? *)
246 (http_get (server_url ^ "/" ^ Http_getter_env.xml_index),
247 http_get (server_url ^ "/" ^ Http_getter_env.rdf_index),
248 http_get (server_url ^ "/" ^ Http_getter_env.xsl_index))
250 if (xml_index = None && rdf_index = None && xsl_index = None) then
251 debug_print (sprintf "Warning: useless server %s" server_url);
252 (match xml_index with
254 (log := !log ^ "Updating XML db ...<br />\n";
258 (match Pcre.split ~pat:"[ \\t]+" l with
259 | [uri; "gz"] -> xml_map#add uri ((xml_url_of_uri uri) ^ ".xml.gz")
260 | [uri] -> xml_map#add uri ((xml_url_of_uri uri) ^ ".xml")
261 | _ -> log := !log ^ "Ignoring invalid line: " ^ l ^ "<br />\n")
262 with Http_getter_invalid_URI uri ->
263 log := !log ^ "Ignoring invalid XML URI: " ^ uri ^ "<br />\n")
264 (Pcre.split ~pat:"\n+" xml_index)) (* xml_index lines *)
266 (match rdf_index with
268 (log := !log ^ "Updating RDF db ...<br />\n";
272 (match Pcre.split ~pat:"[ \\t]+" l with
273 | [uri; "gz"] -> rdf_map#add uri ((rdf_url_of_uri uri) ^ ".xml.gz")
274 | [uri] -> rdf_map#add uri ((rdf_url_of_uri uri) ^ ".xml")
275 | _ -> log := !log ^ "Ignoring invalid line: " ^ l ^ "<br />\n")
276 with Http_getter_invalid_URI uri ->
277 log := !log ^ "Ignoring invalid RDF URI: " ^ uri ^ "<br />\n")
278 (Pcre.split ~pat:"\n+" rdf_index)) (* rdf_index lines *)
280 (match xsl_index with
282 (log := !log ^ "Updating XSLT db ...<br />\n";
284 (fun l -> xsl_map#add l (server_url ^ "/" ^ l))
285 (Pcre.split ~pat:"\n+" xsl_index);
286 log := !log ^ "All done!<br />\n")
292 let callback (req: Http_types.request) outchan =
294 debug_print ("Connection from " ^ req#clientAddr);
295 debug_print ("Received request: " ^ req#path);
297 | "/help" -> return_html_msg Http_getter_const.usage_string outchan
298 | "/getxml" | "/getxslt" | "/getdtd" | "/resolve" | "/register" ->
299 (let uri = req#param "uri" in (* common parameter *)
302 let enc = parse_enc req in
304 try parse_patch_dtd req with Http_types.Param_not_found _ -> true
306 Http_getter_cache.respond_xml
307 ~url:(resolve uri) ~uri ~enc ~patch_dtd outchan
310 try parse_patch_dtd req with Http_types.Param_not_found _ -> true
312 Http_getter_cache.respond_xsl ~url:(resolve uri) ~patch_dtd outchan
315 try parse_patch_dtd req with Http_types.Param_not_found _ -> true
317 Http_getter_cache.respond_dtd
318 ~patch_dtd ~url:(Http_getter_env.dtd_dir ^ "/" ^ uri) outchan
322 (sprintf "<url value=\"%s\" />\n" (resolve uri))
324 with Http_getter_unresolvable_URI uri ->
325 return_xml_msg "<unresolved />\n" outchan)
327 let url = req#param "url" in
329 return_html_msg "Register done" outchan
332 (xml_map#clear; rdf_map#clear; xsl_map#clear;
336 "" (* initial logmsg: empty *)
337 (* reverse order: 1st server is the most important one *)
338 (List.rev Http_getter_env.servers)
340 xml_map#sync; rdf_map#sync; xsl_map#sync;
341 return_html_msg log outchan)
345 (Pcre.pmatch ~pat:"^cic:" uri) &&
346 not (Pcre.pmatch ~pat:"\\.types$" uri))
348 | "/getallrdfuris" ->
349 (let classs = req#param "class" in
352 let base = "^helm:rdf:www\\.cs\\.unibo\\.it/helm/rdf/" in
354 | ("forward" as c) | ("backward" as c) ->
355 (fun uri -> Pcre.pmatch ~pat:(base ^ c) uri)
356 | c -> raise (Http_getter_invalid_RDF_class c)
358 return_all_rdf_uris filter outchan
359 with Http_getter_invalid_RDF_class c ->
360 raise (Http_getter_bad_request ("Invalid RDF class: " ^ c)))
361 | "/ls" -> return_ls (parse_ls_uri req) (parse_output_format req) outchan
363 Http_daemon.respond ~body:Http_getter_const.empty_xml outchan
365 Http_daemon.respond_error ~status:(`Client_error `Bad_request) outchan);
368 | Http_types.Param_not_found attr_name ->
369 return_400 (sprintf "Parameter '%s' is missing" attr_name) outchan
370 | Http_getter_bad_request msg -> return_html_error msg outchan
371 | Http_getter_internal_error msg -> return_html_internal_error msg outchan
374 ("Uncaught exception: " ^ (Printexc.to_string exc))
378 (* daemon initialization *)
379 Http_getter_env.dump_env ();
381 Unix.putenv "http_proxy" "";
382 Sys.catch_break true;
385 ~timeout:None ~port:Http_getter_env.port ~mode:`Thread callback