]> matita.cs.unibo.it Git - helm.git/blob - helm/http_getter/main.ml
added helm:exception handling of Http_getter_types.Key_not_found
[helm.git] / helm / http_getter / main.ml
1 (*
2  * Copyright (C) 2003-2004:
3  *    Stefano Zacchiroli <zack@cs.unibo.it>
4  *    for the HELM Team http://helm.cs.unibo.it/
5  *
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.
9  *
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.
14  *
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.
19  *
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,
23  *  MA  02111-1307, USA.
24  *
25  *  For details, see the HELM World-Wide-Web page,
26  *  http://helm.cs.unibo.it/
27  *)
28
29 open Printf
30
31 open Http_getter_common
32 open Http_getter_const
33 open Http_getter_misc
34 open Http_getter_types
35
36   (* constants *)
37
38 let configuration_file = BuildTimeOpts.conffile
39
40 let common_headers = [
41   "Cache-Control", "no-cache";
42   "Pragma", "no-cache";
43   "Expires", "0"
44 ]
45
46   (* HTTP queries argument parsing *)
47
48   (* parse encoding ("format" parameter), default is `Normal *)
49 let parse_enc (req: Http_types.request) =
50   try
51     (match req#param "format" with
52     | "normal" -> `Normal
53     | "gz" -> `Gzipped
54     | s -> raise (Bad_request ("Invalid format: " ^ s)))
55   with Http_types.Param_not_found _ -> `Normal
56
57   (* parse "patch_dtd" parameter, default is true *)
58 let parse_patch (req: Http_types.request) =
59   try
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
65
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" -> `Text
70   | s when String.lowercase s = "xml" -> `Xml
71   | s -> raise (Bad_request ("Invalid /" ^ meth ^ " format: " ^ s))
72
73   (* parse "position" argument, default is 0 *)
74 let parse_position (req: Http_types.request) =
75   try
76     let res = int_of_string (req#param "position") in
77     if res < 0 then
78       raise (Failure "int_of_string");
79     res
80   with
81   | Http_types.Param_not_found _ -> 0
82   | Failure "int_of_string" ->
83     raise (Bad_request
84       (sprintf "position must be a non negative integer (%s given)"
85         (req#param "position")))
86
87 let parse_rdf_class (req: Http_types.request) =
88   match req#param "class" with
89   | "forward" -> `Forward
90   | "backward" -> `Backward
91   | c -> raise (Bad_request ("Invalid RDF class: " ^ c))
92
93 let html_tag ?exn () =
94   let xml_decl = "<?xml version=\"1.0\"?>\n" in
95   match exn with
96   | Some exn ->
97       sprintf
98         "%s<html xmlns=\"%s\"\nxmlns:helm=\"%s\"\nhelm:exception=\"%s\">\n"
99         xml_decl xhtml_ns helm_ns exn
100   | None ->
101       sprintf "%s<html xmlns=\"%s\"\nxmlns:helm=\"%s\">\n"
102         xml_decl xhtml_ns helm_ns
103
104 let mk_return_fun pp_fun contype msg outchan =
105   Http_daemon.respond
106     ~body:(pp_fun msg) ~headers:["Content-Type", contype] outchan
107 let pp_msg s = sprintf "%s<body>%s</body></html>" (html_tag ()) s
108 let null_pp s = s
109 let return_html_error exn =
110   let pp_error s =
111     sprintf "%s\n<body>Http Getter error: <span style=\"color:red\">%s</span></body></html>"
112       (html_tag ~exn ()) s
113   in
114   mk_return_fun pp_error "text/xml"
115 let return_html_internal_error exn =
116   let pp_internal_error s =
117     sprintf "%s\n<body>Http Getter Internal error: <span style=\"color:red\">%s</span></body></html>"
118       (html_tag ~exn ()) s
119   in
120   mk_return_fun pp_internal_error "text/xml"
121 let return_html_msg = mk_return_fun pp_msg "text/xml"
122 let return_html_raw = mk_return_fun null_pp "text/xml"
123 let return_xml_raw = mk_return_fun null_pp "text/xml"
124 let return_400 exn body = return_html_error exn body
125
126 let return_all_foo_uris doctype uris outchan =
127   Http_daemon.send_basic_headers ~code:(`Code 200) outchan;
128   Http_daemon.send_header "Content-Type" "text/xml" outchan;
129   Http_daemon.send_headers common_headers outchan;
130   Http_daemon.send_CRLF outchan;
131   output_string
132     outchan
133     (sprintf
134 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
135 <!DOCTYPE %s SYSTEM \"%s/getdtd?uri=%s.dtd\">
136
137 <%s>
138 "
139       doctype
140       (Lazy.force Http_getter_env.my_own_url)
141       doctype
142       doctype);
143   List.iter
144     (fun uri -> output_string outchan (sprintf "\t<uri value=\"%s\" />\n" uri))
145     uris;
146   output_string outchan (sprintf "</%s>\n" doctype)
147
148 let return_all_xml_uris fmt outchan =
149  let uris = Http_getter.getalluris () in
150   match fmt with
151    | `Text ->
152       let buf = Buffer.create 10240 in
153        List.iter (bprintf buf "%s\n") uris ;
154        let body = Buffer.contents buf in
155        Http_daemon.respond
156          ~headers:(("Content-Type", "text/plain") :: common_headers)
157          ~body outchan
158    | `Xml ->
159       return_all_foo_uris "alluris" uris outchan
160   
161 let return_all_rdf_uris classs outchan =
162   return_all_foo_uris "allrdfuris" (Http_getter.getallrdfuris classs) outchan
163
164 let return_ls regexp fmt outchan =
165   let ls_items = Http_getter.ls regexp in
166   let buf = Buffer.create 10240 in
167   (match fmt with
168   | `Text ->
169       List.iter
170         (function
171           | Ls_section dir -> bprintf buf "dir, %s\n" dir
172           | Ls_object obj ->
173               bprintf buf "object, %s, <%s,%s,%s,%s>\n"
174               obj.uri (if obj.ann then "YES" else "NO")
175               (string_of_ls_flag obj.types)
176               (string_of_ls_flag obj.body)
177               (string_of_ls_flag obj.proof_tree))
178         ls_items
179   | `Xml ->
180       Buffer.add_string buf "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
181       bprintf buf "<!DOCTYPE ls SYSTEM \"%s/getdtd?uri=ls.dtd\">\n"
182         (Lazy.force Http_getter_env.my_own_url);
183       Buffer.add_string buf "<ls>\n";
184       List.iter
185         (function
186           | Ls_section dir -> bprintf buf "<section>%s</section>\n" dir
187           | Ls_object obj ->
188               bprintf buf
189 "<object name=\"%s\">
190 \t<ann value=\"%s\" />
191 \t<types value=\"%s\" />
192 \t<body value=\"%s\" />
193 \t<proof_tree value=\"%s\" />
194 </object>
195 "
196               obj.uri (if obj.ann then "YES" else "NO")
197               (string_of_ls_flag obj.types)
198               (string_of_ls_flag obj.body)
199               (string_of_ls_flag obj.proof_tree))
200         ls_items;
201       Buffer.add_string buf "</ls>\n");
202   let body = Buffer.contents buf in
203   Http_daemon.respond
204     ~headers:(("Content-Type", "text/plain") :: common_headers)
205     ~body outchan
206
207 let return_help outchan = return_html_raw (Http_getter.help ()) outchan
208
209 let return_resolve uri outchan =
210   try
211     return_xml_raw
212       (sprintf "<url value=\"%s\" />\n" (Http_getter.resolve uri))
213       outchan
214   with
215   | Unresolvable_URI _ -> return_xml_raw "<unresolvable />\n" outchan
216   | Key_not_found _ -> return_xml_raw "<not_found />\n" outchan
217
218 let return_list_servers outchan =
219   return_html_raw
220     (sprintf "%s<body><table>\n%s\n</table></body></html>"
221       (html_tag ())
222       (String.concat "\n"
223         (List.map
224           (fun (pos, server) ->
225             sprintf "<tr><td>%d</td><td>%s</td></tr>" pos server)
226           (Http_getter.list_servers ()))))
227     outchan
228
229 let log_failure msg = Http_getter_logger.log ("Request not fulfilled: " ^ msg)
230
231   (** given an action (i.e. a function which expects a logger and do something
232    * using it as a logger), perform it sending its output incrementally to the
233    * given output channel. Response is sent embedded in an HTML document.
234    * Channel is closed afterwards. *)
235 let send_log_to ?prepend action outchan =
236   Http_daemon.send_basic_headers ~code:(`Code 200) outchan;
237   Http_daemon.send_header "Content-Type" "text/xml" outchan;
238   Http_daemon.send_CRLF outchan;
239   output_string outchan (sprintf "%s<body>\n" (html_tag ()));
240   flush outchan;
241   (match prepend with
242   | None -> ()
243   | Some text -> output_string outchan text; flush outchan);
244   let logger tag =
245     output_string outchan (HelmLogger.html_of_html_tag tag);
246     flush outchan
247   in
248   action logger;
249   output_string outchan "\n</body></html>";
250   close_out outchan
251
252   (* thread action *)
253
254 let callback (req: Http_types.request) outchan =
255   try
256     Http_getter_logger.log ("Connection from " ^ req#clientAddr);
257     Http_getter_logger.log ("Received request: " ^ req#path);
258     (match req#path with
259     | "/help" -> return_help outchan
260     | "/getxml" ->
261         let uri = req#param "uri" in
262         Http_getter_cache.respond_xml ~url:(Http_getter.resolve uri) ~uri
263           ~enc:(parse_enc req) ~patch:(parse_patch req) outchan
264     | "/getxslt" ->
265         Http_getter_cache.respond_xsl
266           ~url:(Http_getter.resolve (req#param "uri"))
267           ~patch:(parse_patch req) outchan
268     | "/getdtd" ->
269         Http_getter_cache.respond_dtd ~patch:(parse_patch req)
270           ~url:(sprintf "%s/%s"
271             (Helm_registry.get "getter.dtd_dir") (req#param "uri"))
272           outchan
273     | "/resolve" -> return_resolve (req#param "uri") outchan
274     | "/register" ->
275         Http_getter.register ~uri:(req#param "uri") ~url:(req#param "url");
276         return_html_msg "Register done" outchan
277     | "/unregister" ->
278         Http_getter.unregister (req#param "uri");
279         return_html_msg "Unregister done" outchan
280     | "/clean_cache" ->
281         Http_getter.clean_cache ();
282         return_html_msg "Done." outchan
283     | "/update" ->
284         Http_getter_env.reload (); (* reload servers list from servers file *)
285         send_log_to (fun logger -> Http_getter.update ~logger ()) outchan
286     | "/list_servers" -> return_list_servers outchan
287     | "/add_server" ->
288         let name = req#param "url" in
289         let position = parse_position req in
290         let prepend =
291           sprintf "Added server %s in position %d)<br />\n" name position
292         in
293         send_log_to ~prepend
294           (fun logger -> Http_getter.add_server ~logger ~position name) outchan
295     | "/remove_server" ->
296         let position = parse_position req in
297         if not (Http_getter.has_server position) then
298           raise (Bad_request (sprintf "no server with position %d" position))
299         else
300           let prepend =
301             sprintf "Removed server at position %d<br />\n" position
302           in
303           send_log_to ~prepend
304             (fun logger -> Http_getter.remove_server ~logger position) outchan
305     | "/getalluris" ->
306        return_all_xml_uris (parse_output_format "getalluris" req) outchan
307     | "/getallrdfuris" -> return_all_rdf_uris (parse_rdf_class req) outchan
308     | "/ls" ->
309         return_ls (req#param "baseuri") (parse_output_format "ls" req) outchan
310     | "/getempty" ->
311         Http_daemon.respond ~body:Http_getter_const.empty_xml outchan
312     | invalid_request ->
313         Http_daemon.respond_error ~code:(`Status (`Client_error `Bad_request))
314           outchan);
315     Http_getter_logger.log "Done!\n"
316   with
317   | Http_types.Param_not_found attr_name ->
318       let msg = sprintf "Parameter '%s' is missing" attr_name in
319       log_failure msg;
320       return_400 "Bad_request" msg outchan
321   | Bad_request msg ->
322       log_failure msg;
323       return_html_error "Bad_request" msg outchan
324   | Internal_error msg ->
325       log_failure msg;
326       return_html_internal_error "Internal_error" msg outchan
327   | Shell.Subprocess_error l ->
328       let msgs =
329         List.map
330           (fun (cmd, code) ->
331             sprintf "Command '%s' returned %s" cmd (string_of_proc_status code))
332           l
333       in
334       log_failure (String.concat ", " msgs);
335       return_html_internal_error "Subprocess_error"
336         (String.concat "<br />\n" msgs) outchan
337   | exc ->
338       let msg = "Uncaught exception: " ^ (Printexc.to_string exc) in
339       (match exc with
340       | Http_getter_types.Key_not_found uri ->
341           return_html_error "Key_not_found" msg outchan
342       | _ ->
343           log_failure msg;
344           return_html_error "Uncaught_exception" msg outchan)
345
346     (* Main *)
347
348 let main () =
349   Helm_registry.load_from configuration_file;
350   Http_getter_logger.set_log_level
351     (Helm_registry.get_opt_default Helm_registry.get_int 1 "getter.log_level");
352   Http_getter_logger.set_log_file
353     (Helm_registry.get_opt Helm_registry.get_string "getter.log_file");
354   Http_getter_env.reload ();
355   print_string (Http_getter_env.env_to_string ());
356   flush stdout;
357   let batch_update =
358     try Sys.argv.(1) = "-update" with Invalid_argument _ -> false
359   in
360   if batch_update then  (* batch mode: performs update and exit *)
361     Http_getter.update ~logger:Http_getter.stdout_logger ()
362   else begin            (* daemon mode: start http daemon *)
363     at_exit Http_getter.close_maps;
364     Sys.catch_break true;
365     try
366       Http_daemon.start' ~mode:`Thread
367         ~timeout:(Some 600) ~port:(Helm_registry.get_int "getter.port")
368         callback
369     with Sys.Break -> ()  (* 'close_maps' already registered with 'at_exit' *)
370   end
371
372 let _ = main ()
373