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
37 let configuration_file = BuildTimeOpts.conffile
39 let common_headers = [
40 "Cache-Control", "no-cache";
45 (* HTTP queries argument parsing *)
47 (* parse encoding ("format" parameter), default is `Normal *)
48 let parse_enc (req: Http_types.request) =
50 (match req#param "format" with
53 | s -> raise (Bad_request ("Invalid format: " ^ s)))
54 with Http_types.Param_not_found _ -> `Normal
56 (* parse "patch_dtd" parameter, default is true *)
57 let parse_patch (req: Http_types.request) =
59 (match req#param "patch_dtd" with
60 | s when String.lowercase s = "yes" -> true
61 | s when String.lowercase s = "no" -> false
62 | s -> raise (Bad_request ("Invalid patch_dtd value: " ^ s)))
63 with Http_types.Param_not_found _ -> true
65 (* parse output format ("format" parameter), no default value *)
66 let parse_output_format meth (req: Http_types.request) =
67 match req#param "format" with
68 | s when String.lowercase s = "txt" -> `Text
69 | s when String.lowercase s = "xml" -> `Xml
70 | s -> raise (Bad_request ("Invalid /" ^ meth ^ " format: " ^ s))
72 (* parse "position" argument, default is 0 *)
73 let parse_position (req: Http_types.request) =
75 let res = int_of_string (req#param "position") in
77 raise (Failure "int_of_string");
80 | Http_types.Param_not_found _ -> 0
81 | Failure "int_of_string" ->
83 (sprintf "position must be a non negative integer (%s given)"
84 (req#param "position")))
86 let parse_rdf_class (req: Http_types.request) =
87 match req#param "class" with
88 | "forward" -> `Forward
89 | "backward" -> `Backward
90 | c -> raise (Bad_request ("Invalid RDF class: " ^ c))
92 let mk_return_fun pp_fun contype msg outchan =
94 ~body:(pp_fun msg) ~headers:["Content-Type", contype] outchan
96 sprintf "<html><body>Http Getter error: <span style=\"color:red\">%s</span></body></html>" s
97 let pp_internal_error s =
98 sprintf "<html><body>Http Getter Internal error: <span style=\"color:red\">%s</span></body></html>" s
99 let pp_msg s = sprintf "<html><body>%s</body></html>" s
101 let return_html_error = mk_return_fun pp_error "text/html"
102 let return_html_internal_error = mk_return_fun pp_internal_error "text/html"
103 let return_html_msg = mk_return_fun pp_msg "text/html"
104 let return_html_raw = mk_return_fun null_pp "text/html"
105 let return_xml_raw = mk_return_fun null_pp "text/xml"
106 let return_400 body outchan =
107 Http_daemon.respond_error ~code:(`Code 400) ~body outchan
109 let return_all_foo_uris doctype uris outchan =
110 Http_daemon.send_basic_headers ~code:(`Code 200) outchan;
111 Http_daemon.send_header "Content-Type" "text/xml" outchan;
112 Http_daemon.send_headers common_headers outchan;
113 Http_daemon.send_CRLF outchan;
117 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
118 <!DOCTYPE %s SYSTEM \"%s/getdtd?uri=%s.dtd\">
123 (Lazy.force Http_getter_env.my_own_url)
127 (fun uri -> output_string outchan (sprintf "\t<uri value=\"%s\" />\n" uri))
129 output_string outchan (sprintf "</%s>\n" doctype)
131 let return_all_xml_uris fmt outchan =
132 let uris = Http_getter.getalluris () in
135 let buf = Buffer.create 10240 in
136 List.iter (bprintf buf "%s\n") uris ;
137 let body = Buffer.contents buf in
139 ~headers:(("Content-Type", "text/plain") :: common_headers)
142 return_all_foo_uris "alluris" uris outchan
144 let return_all_rdf_uris classs outchan =
145 return_all_foo_uris "allrdfuris" (Http_getter.getallrdfuris classs) outchan
147 let return_ls regexp fmt outchan =
148 let ls_items = Http_getter.ls regexp in
149 let buf = Buffer.create 10240 in
154 | Ls_section dir -> bprintf buf "dir, %s\n" dir
156 bprintf buf "object, %s, <%s,%s,%s,%s>\n"
157 obj.uri (if obj.ann then "YES" else "NO")
158 (string_of_ls_flag obj.types)
159 (string_of_ls_flag obj.body)
160 (string_of_ls_flag obj.proof_tree))
163 Buffer.add_string buf "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
164 bprintf buf "<!DOCTYPE ls SYSTEM \"%s/getdtd?uri=ls.dtd\">\n"
165 (Lazy.force Http_getter_env.my_own_url);
166 Buffer.add_string buf "<ls>\n";
169 | Ls_section dir -> bprintf buf "<section>%s</section>\n" dir
172 "<object name=\"%s\">
173 \t<ann value=\"%s\" />
174 \t<types value=\"%s\" />
175 \t<body value=\"%s\" />
176 \t<proof_tree value=\"%s\" />
179 obj.uri (if obj.ann then "YES" else "NO")
180 (string_of_ls_flag obj.types)
181 (string_of_ls_flag obj.body)
182 (string_of_ls_flag obj.proof_tree))
184 Buffer.add_string buf "</ls>\n");
185 let body = Buffer.contents buf in
187 ~headers:(("Content-Type", "text/plain") :: common_headers)
190 let return_help outchan = return_html_raw (Http_getter.help ()) outchan
192 let return_resolve uri outchan =
195 (sprintf "<url value=\"%s\" />\n" (Http_getter.resolve uri))
197 with Unresolvable_URI uri ->
198 return_xml_raw "<unresolved />\n" outchan
200 let return_list_servers outchan =
202 (sprintf "<html><body><table>\n%s\n</table></body></html>"
205 (fun (pos, server) ->
206 sprintf "<tr><td>%d</td><td>%s</td></tr>" pos server)
207 (Http_getter.list_servers ()))))
210 let log_failure msg = Http_getter_logger.log ("Request not fulfilled: " ^ msg)
212 (** given an action (i.e. a function which expects a logger and do something
213 * using it as a logger), perform it sending its output incrementally to the
214 * given output channel. Response is sent embedded in an HTML document.
215 * Channel is closed afterwards. *)
216 let send_log_to ?prepend action outchan =
217 Http_daemon.send_basic_headers ~code:(`Code 200) outchan;
218 Http_daemon.send_header "Content-Type" "text/html" outchan;
219 Http_daemon.send_CRLF outchan;
220 output_string outchan "<html><body>\n"; flush outchan;
223 | Some text -> output_string outchan text; flush outchan);
225 output_string outchan (HelmLogger.html_of_html_tag tag);
229 output_string outchan "\n</body></html>";
234 let callback (req: Http_types.request) outchan =
236 Http_getter_logger.log ("Connection from " ^ req#clientAddr);
237 Http_getter_logger.log ("Received request: " ^ req#path);
239 | "/help" -> return_help outchan
241 let uri = req#param "uri" in
242 Http_getter_cache.respond_xml ~url:(Http_getter.resolve uri) ~uri
243 ~enc:(parse_enc req) ~patch:(parse_patch req) outchan
245 Http_getter_cache.respond_xsl
246 ~url:(Http_getter.resolve (req#param "uri"))
247 ~patch:(parse_patch req) outchan
249 Http_getter_cache.respond_dtd ~patch:(parse_patch req)
250 ~url:(sprintf "%s/%s"
251 (Helm_registry.get "getter.dtd_dir") (req#param "uri"))
253 | "/resolve" -> return_resolve (req#param "uri") outchan
255 Http_getter.register ~uri:(req#param "uri") ~url:(req#param "url");
256 return_html_msg "Register done" outchan
258 Http_getter.clean_cache ();
259 return_html_msg "Done." outchan
261 Http_getter_env.reload (); (* reload servers list from servers file *)
262 send_log_to (fun logger -> Http_getter.update ~logger ()) outchan
263 | "/list_servers" -> return_list_servers outchan
265 let name = req#param "url" in
266 let position = parse_position req in
268 sprintf "Added server %s in position %d)<br />\n" name position
271 (fun logger -> Http_getter.add_server ~logger ~position name) outchan
272 | "/remove_server" ->
273 let position = parse_position req in
274 if not (Http_getter.has_server position) then
275 raise (Bad_request (sprintf "no server with position %d" position))
278 sprintf "Removed server at position %d<br />\n" position
281 (fun logger -> Http_getter.remove_server ~logger position) outchan
283 return_all_xml_uris (parse_output_format "getalluris" req) outchan
284 | "/getallrdfuris" -> return_all_rdf_uris (parse_rdf_class req) outchan
286 return_ls (req#param "baseuri") (parse_output_format "ls" req) outchan
288 Http_daemon.respond ~body:Http_getter_const.empty_xml outchan
290 Http_daemon.respond_error ~code:(`Status (`Client_error `Bad_request))
292 Http_getter_logger.log "Done!\n"
294 | Http_types.Param_not_found attr_name ->
295 let msg = sprintf "Parameter '%s' is missing" attr_name in
297 return_400 msg outchan
300 return_html_error msg outchan
301 | Internal_error msg ->
303 return_html_internal_error msg outchan
304 | Shell.Subprocess_error l ->
308 sprintf "Command '%s' returned %s" cmd (string_of_proc_status code))
311 log_failure (String.concat ", " msgs);
312 return_html_internal_error (String.concat "<br />\n" msgs) outchan
314 let msg = "Uncaught exception: " ^ (Printexc.to_string exc) in
316 return_html_error msg outchan
321 Helm_registry.load_from configuration_file;
322 Http_getter_logger.set_log_level
323 (Helm_registry.get_opt_default Helm_registry.get_int 1 "getter.log_level");
324 Http_getter_logger.set_log_file
325 (Helm_registry.get_opt Helm_registry.get_string "getter.log_file");
326 Http_getter_env.reload ();
327 print_string (Http_getter_env.env_to_string ());
330 try Sys.argv.(1) = "-update" with Invalid_argument _ -> false
332 if batch_update then (* batch mode: performs update and exit *)
333 Http_getter.update ~logger:Http_getter.stdout_logger ()
334 else begin (* daemon mode: start http daemon *)
335 at_exit Http_getter.close_maps;
336 Sys.catch_break true;
338 Http_daemon.start' ~mode:`Thread
339 ~timeout:(Some 600) ~port:(Helm_registry.get_int "getter.port")
341 with Sys.Break -> () (* 'close_maps' already registered with 'at_exit' *)