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_types
34 open Http_getter_debugger
38 let configuration_file = "/projects/helm/etc/http_getter.conf.xml"
40 let common_headers = [
41 "Cache-Control", "no-cache";
46 (* HTTP queries argument parsing *)
48 (* parse encoding ("format" parameter), default is Enc_normal *)
49 let parse_enc (req: Http_types.request) =
51 (match req#param "format" with
52 | "normal" -> Enc_normal
54 | s -> raise (Bad_request ("Invalid format: " ^ s)))
55 with Http_types.Param_not_found _ -> Enc_normal
57 (* parse "patch_dtd" parameter, default is true *)
58 let parse_patch (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 (Bad_request ("Invalid patch_dtd value: " ^ s)))
64 with Http_types.Param_not_found _ -> true
66 (* parse output format ("format" parameter), no default value *)
67 let parse_output_format meth (req: Http_types.request) =
68 match req#param "format" with
69 | s when String.lowercase s = "txt" -> Fmt_text
70 | s when String.lowercase s = "xml" -> Fmt_xml
71 | s -> raise (Bad_request ("Invalid /" ^ meth ^ " format: " ^ s))
73 (* parse "baseuri" format for /ls method, no default value *)
75 let parse_ls_RE = Pcre.regexp "^(\\w+):(.*)$" in
76 let trailing_slash_RE = Pcre.regexp "/+$" in
78 raise (Bad_request ("Invalid /ls baseuri: " ^ uri))
80 fun (req: Http_types.request) ->
81 let baseuri = req#param "baseuri" in
84 Pcre.extract ~rex:parse_ls_RE
85 (Pcre.replace ~rex:trailing_slash_RE baseuri)
87 (match (subs.(1), subs.(2)) with
88 | "cic", uri -> Cic uri
89 | "theory", uri -> Theory uri
90 | _ -> wrong_uri baseuri)
91 with Not_found -> wrong_uri baseuri
93 (* parse "position" argument, default is 0 *)
94 let parse_position (req: Http_types.request) =
96 let res = int_of_string (req#param "position") in
98 raise (Failure "int_of_string");
101 | Http_types.Param_not_found _ -> 0
102 | Failure "int_of_string" ->
104 (sprintf "position must be a non negative integer (%s given)"
105 (req#param "position")))
107 let parse_rdf_class (req: Http_types.request) =
108 match req#param "class" with
109 | "forward" -> `Forward
110 | "backward" -> `Backward
111 | c -> raise (Bad_request ("Invalid RDF class: " ^ c))
113 let return_all_foo_uris doctype uris outchan =
114 Http_daemon.send_basic_headers ~code:200 outchan;
115 Http_daemon.send_header "Content-Type" "text/xml" outchan;
116 Http_daemon.send_headers common_headers outchan;
117 Http_daemon.send_CRLF outchan;
121 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
122 <!DOCTYPE %s SYSTEM \"%s/getdtd?uri=%s.dtd\">
127 (Lazy.force Http_getter_env.my_own_url)
131 (fun uri -> output_string outchan (sprintf "\t<uri value=\"%s\" />\n" uri))
133 output_string outchan (sprintf "</%s>\n" doctype)
135 let return_all_xml_uris fmt outchan =
136 let uris = Http_getter.getalluris () in
139 let buf = Buffer.create 10240 in
140 List.iter (bprintf buf "%s\n") uris ;
141 let body = Buffer.contents buf in
143 ~headers:(("Content-Type", "text/plain") :: common_headers)
146 return_all_foo_uris "alluris" uris outchan
148 let return_all_rdf_uris classs outchan =
149 return_all_foo_uris "allrdfuris" (Http_getter.getallrdfuris classs) outchan
151 let return_ls xmluri fmt outchan =
152 let ls_items = Http_getter.ls xmluri in
153 let buf = Buffer.create 10240 in
158 | Ls_section dir -> bprintf buf "dir, %s\n" dir
160 bprintf buf "object, %s, <%s,%s,%s,%s>\n"
161 obj.uri (if obj.ann then "YES" else "NO")
162 (string_of_ls_flag obj.types)
163 (string_of_ls_flag obj.body)
164 (string_of_ls_flag obj.proof_tree))
167 Buffer.add_string buf "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
168 bprintf buf "<!DOCTYPE ls SYSTEM \"%s/getdtd?uri=ls.dtd\">\n"
169 (Lazy.force Http_getter_env.my_own_url);
170 Buffer.add_string buf "<ls>\n";
173 | Ls_section dir -> bprintf buf "<section>%s</section>\n" dir
176 "<object name=\"%s\">
177 \t<ann value=\"%s\" />
178 \t<types value=\"%s\" />
179 \t<body value=\"%s\" />
180 \t<proof_tree value=\"%s\" />
183 obj.uri (if obj.ann then "YES" else "NO")
184 (string_of_ls_flag obj.types)
185 (string_of_ls_flag obj.body)
186 (string_of_ls_flag obj.proof_tree))
188 Buffer.add_string buf "</ls>\n");
189 let body = Buffer.contents buf in
191 ~headers:(("Content-Type", "text/plain") :: common_headers)
194 let return_help outchan = return_html_raw (Http_getter.help ()) outchan
196 let return_resolve uri outchan =
199 (sprintf "<url value=\"%s\" />\n" (Http_getter.resolve uri))
201 with Unresolvable_URI uri ->
202 return_xml_raw "<unresolved />\n" outchan
204 let return_list_servers outchan =
206 (sprintf "<html><body><table>\n%s\n</table></body></html>"
209 (fun (pos, server) ->
210 sprintf "<tr><td>%d</td><td>%s</td></tr>" pos server)
211 (Http_getter.list_servers ()))))
216 let callback (req: Http_types.request) outchan =
218 debug_print ("Connection from " ^ req#clientAddr);
219 debug_print ("Received request: " ^ req#path);
221 | "/help" -> return_help outchan
223 let uri = req#param "uri" in
224 Http_getter_cache.respond_xml ~url:(Http_getter.resolve uri) ~uri
225 ~enc:(parse_enc req) ~patch:(parse_patch req) outchan
227 Http_getter_cache.respond_xsl
228 ~url:(Http_getter.resolve (req#param "uri"))
229 ~patch:(parse_patch req) outchan
231 Http_getter_cache.respond_dtd ~patch:(parse_patch req)
232 ~url:(sprintf "%s/%s"
233 (Helm_registry.get "getter.dtd_dir") (req#param "uri"))
235 | "/resolve" -> return_resolve (req#param "uri") outchan
237 Http_getter.register ~uri:(req#param "uri") ~url:(req#param "url");
238 return_html_msg "Register done" outchan
240 Http_getter.clean_cache ();
241 return_html_msg "Done." outchan
243 Http_getter_env.reload (); (* reload servers list from servers file *)
244 let log = Http_getter.update () in
245 return_html_msg (HelmLogger.html_of_html_msg log) outchan
246 | "/list_servers" -> return_list_servers outchan
248 let name = req#param "url" in
249 let position = parse_position req in
250 let log = Http_getter.add_server ~position name in
252 (sprintf "Added server %s in position %d)<br />\n%s"
253 name position (HelmLogger.html_of_html_msg log))
255 | "/remove_server" ->
256 let position = parse_position req in
259 Http_getter.remove_server position
260 with Invalid_argument _ ->
261 raise (Bad_request (sprintf "no server with position %d" position))
264 (sprintf "Removed server at position %d<br />\n%s"
265 position (HelmLogger.html_of_html_msg log))
268 return_all_xml_uris (parse_output_format "getalluris" req) outchan
269 | "/getallrdfuris" -> return_all_rdf_uris (parse_rdf_class req) outchan
271 return_ls (parse_ls_uri req) (parse_output_format "ls" req) outchan
273 Http_daemon.respond ~body:Http_getter_const.empty_xml outchan
275 Http_daemon.respond_error ~status:(`Client_error `Bad_request) outchan);
276 debug_print "Done!\n"
278 | Http_types.Param_not_found attr_name ->
279 return_400 (sprintf "Parameter '%s' is missing" attr_name) outchan
280 | Bad_request msg -> return_html_error msg outchan
281 | Internal_error msg -> return_html_internal_error msg outchan
282 | Shell.Subprocess_error l ->
283 return_html_internal_error
284 (String.concat "<br />\n"
287 sprintf "Command '%s' returned %s"
288 cmd (string_of_proc_status code))
293 ("Uncaught exception: " ^ (Printexc.to_string exc))
299 Helm_registry.load_from configuration_file;
300 Http_getter_env.reload ();
301 print_string (Http_getter_env.env_to_string ());
303 at_exit Http_getter.close_maps;
304 Sys.catch_break true;
306 Http_daemon.start' ~mode:`Thread
307 ~timeout:(Some 600) ~port:(Helm_registry.get_int "getter.port")
309 with Sys.Break -> () (* 'close_maps' already registered with 'at_exit' *)