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 (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 /ls 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 outchan =
136 return_all_foo_uris "alluris" (Http_getter.getalluris ()) outchan
137 let return_all_rdf_uris classs outchan =
138 return_all_foo_uris "allrdfuris" (Http_getter.getallrdfuris classs) outchan
140 let return_ls xmluri fmt outchan =
141 let ls_items = Http_getter.ls xmluri in
142 let buf = Buffer.create 10240 in
147 | Ls_section dir -> bprintf buf "dir, %s\n" dir
149 bprintf buf "object, %s, <%s,%s,%s,%s>\n"
150 obj.uri (if obj.ann then "YES" else "NO")
151 (string_of_ls_flag obj.types)
152 (string_of_ls_flag obj.body)
153 (string_of_ls_flag obj.proof_tree))
156 Buffer.add_string buf "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
157 bprintf buf "<!DOCTYPE ls SYSTEM \"%s/getdtd?uri=ls.dtd\">\n"
158 (Lazy.force Http_getter_env.my_own_url);
159 Buffer.add_string buf "<ls>\n";
162 | Ls_section dir -> bprintf buf "<section>%s</section>\n" dir
165 "<object name=\"%s\">
166 \t<ann value=\"%s\" />
167 \t<types value=\"%s\" />
168 \t<body value=\"%s\" />
169 \t<proof_tree value=\"%s\" />
172 obj.uri (if obj.ann then "YES" else "NO")
173 (string_of_ls_flag obj.types)
174 (string_of_ls_flag obj.body)
175 (string_of_ls_flag obj.proof_tree))
177 Buffer.add_string buf "</ls>\n");
178 let body = Buffer.contents buf in
180 ~headers:(("Content-Type", "text/plain") :: common_headers)
183 let return_help outchan = return_html_raw (Http_getter.help ()) outchan
185 let return_resolve uri outchan =
188 (sprintf "<url value=\"%s\" />\n" (Http_getter.resolve uri))
190 with Unresolvable_URI uri ->
191 return_xml_raw "<unresolved />\n" outchan
193 let return_list_servers outchan =
195 (sprintf "<html><body><table>\n%s\n</table></body></html>"
198 (fun (pos, server) ->
199 sprintf "<tr><td>%d</td><td>%s</td></tr>" pos server)
200 (Http_getter.list_servers ()))))
205 let callback (req: Http_types.request) outchan =
207 debug_print ("Connection from " ^ req#clientAddr);
208 debug_print ("Received request: " ^ req#path);
210 | "/help" -> return_help outchan
212 let uri = req#param "uri" in
213 Http_getter_cache.respond_xml ~url:(Http_getter.resolve uri) ~uri
214 ~enc:(parse_enc req) ~patch:(parse_patch req) outchan
216 Http_getter_cache.respond_xsl
217 ~url:(Http_getter.resolve (req#param "uri"))
218 ~patch:(parse_patch req) outchan
220 Http_getter_cache.respond_dtd ~patch:(parse_patch req)
221 ~url:(sprintf "%s/%s"
222 (Helm_registry.get "getter.dtd_dir") (req#param "uri"))
224 | "/resolve" -> return_resolve (req#param "uri") outchan
226 Http_getter.register ~uri:(req#param "uri") ~url:(req#param "url");
227 return_html_msg "Register done" outchan
229 Http_getter.clean_cache ();
230 return_html_msg "Done." outchan
232 Http_getter_env.reload (); (* reload servers list from servers file *)
233 let log = Http_getter.update () in
234 return_html_msg (HelmLogger.html_of_html_msg log) outchan
235 | "/list_servers" -> return_list_servers outchan
237 let name = req#param "url" in
238 let position = parse_position req in
239 let log = Http_getter.add_server ~position name in
241 (sprintf "Added server %s in position %d)<br />\n%s"
242 name position (HelmLogger.html_of_html_msg log))
244 | "/remove_server" ->
245 let position = parse_position req in
248 Http_getter.remove_server position
249 with Invalid_argument _ ->
250 raise (Bad_request (sprintf "no server with position %d" position))
253 (sprintf "Removed server at position %d<br />\n%s"
254 position (HelmLogger.html_of_html_msg log))
256 | "/getalluris" -> return_all_xml_uris outchan
257 | "/getallrdfuris" -> return_all_rdf_uris (parse_rdf_class req) outchan
258 | "/ls" -> return_ls (parse_ls_uri req) (parse_output_format req) outchan
260 Http_daemon.respond ~body:Http_getter_const.empty_xml outchan
262 Http_daemon.respond_error ~status:(`Client_error `Bad_request) outchan);
263 debug_print "Done!\n"
265 | Http_types.Param_not_found attr_name ->
266 return_400 (sprintf "Parameter '%s' is missing" attr_name) outchan
267 | Bad_request msg -> return_html_error msg outchan
268 | Internal_error msg -> return_html_internal_error msg outchan
269 | Shell.Subprocess_error l ->
270 return_html_internal_error
271 (String.concat "<br />\n"
274 sprintf "Command '%s' returned %s"
275 cmd (string_of_proc_status code))
280 ("Uncaught exception: " ^ (Printexc.to_string exc))
286 Helm_registry.load_from configuration_file;
287 Http_getter_env.reload ();
288 print_string (Http_getter_env.env_to_string ());
290 at_exit Http_getter.close_maps;
291 Sys.catch_break true;
293 Http_daemon.start' ~mode:`Thread
294 ~timeout:(Some 600) ~port:(Helm_registry.get_int "getter.port")
296 with Sys.Break -> () (* 'close_maps' already registered with 'at_exit' *)