]> matita.cs.unibo.it Git - helm.git/blob - helm/http_getter/main.ml
- Configuration file moved to /projects/helm/etc.
[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_misc
33 open Http_getter_types
34 open Http_getter_debugger
35
36   (* constants *)
37
38 let configuration_file = "/projects/helm/etc/http_getter.conf.xml"
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 Enc_normal *)
49 let parse_enc (req: Http_types.request) =
50   try
51     (match req#param "format" with
52     | "normal" -> Enc_normal
53     | "gz" -> Enc_gzipped
54     | s -> raise (Bad_request ("Invalid format: " ^ s)))
55   with Http_types.Param_not_found _ -> Enc_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 (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))
72
73   (* parse "baseuri" format for /ls method, no default value *)
74 let parse_ls_uri =
75   let parse_ls_RE = Pcre.regexp "^(\\w+):(.*)$" in
76   let trailing_slash_RE = Pcre.regexp "/+$" in
77   let wrong_uri uri =
78     raise (Bad_request ("Invalid /ls baseuri: " ^ uri))
79   in
80   fun (req: Http_types.request) ->
81     let baseuri = req#param "baseuri" in
82     try
83       let subs =
84         Pcre.extract ~rex:parse_ls_RE
85           (Pcre.replace ~rex:trailing_slash_RE  baseuri)
86       in
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
92
93   (* parse "position" argument, default is 0 *)
94 let parse_position (req: Http_types.request) =
95   try
96     let res = int_of_string (req#param "position") in
97     if res < 0 then
98       raise (Failure "int_of_string");
99     res
100   with
101   | Http_types.Param_not_found _ -> 0
102   | Failure "int_of_string" ->
103     raise (Bad_request
104       (sprintf "position must be a non negative integer (%s given)"
105         (req#param "position")))
106
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))
112
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;
118   output_string
119     outchan
120     (sprintf
121 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
122 <!DOCTYPE %s SYSTEM \"%s/getdtd?uri=%s.dtd\">
123
124 <%s>
125 "
126       doctype
127       (Lazy.force Http_getter_env.my_own_url)
128       doctype
129       doctype);
130   List.iter
131     (fun uri -> output_string outchan (sprintf "\t<uri value=\"%s\" />\n" uri))
132     uris;
133   output_string outchan (sprintf "</%s>\n" doctype)
134
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
139
140 let return_ls xmluri fmt outchan =
141   let ls_items = Http_getter.ls xmluri in
142   let buf = Buffer.create 10240 in
143   (match fmt with
144   | Fmt_text ->
145       List.iter
146         (function
147           | Ls_section dir -> bprintf buf "dir, %s\n" dir
148           | Ls_object obj ->
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))
154         ls_items
155   | Fmt_xml ->
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";
160       List.iter
161         (function
162           | Ls_section dir -> bprintf buf "<section>%s</section>\n" dir
163           | Ls_object obj ->
164               bprintf buf
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\" />
170 </object>
171 "
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))
176         ls_items;
177       Buffer.add_string buf "</ls>\n");
178   let body = Buffer.contents buf in
179   Http_daemon.respond
180     ~headers:(("Content-Type", "text/plain") :: common_headers)
181     ~body outchan
182
183 let return_help outchan = return_html_raw (Http_getter.help ()) outchan
184
185 let return_resolve uri outchan =
186   try
187     return_xml_raw
188       (sprintf "<url value=\"%s\" />\n" (Http_getter.resolve uri))
189       outchan
190   with Unresolvable_URI uri ->
191     return_xml_raw "<unresolved />\n" outchan
192
193 let return_list_servers outchan =
194   return_html_raw
195     (sprintf "<html><body><table>\n%s\n</table></body></html>"
196       (String.concat "\n"
197         (List.map
198           (fun (pos, server) ->
199             sprintf "<tr><td>%d</td><td>%s</td></tr>" pos server)
200           (Http_getter.list_servers ()))))
201     outchan
202
203   (* thread action *)
204
205 let callback (req: Http_types.request) outchan =
206   try
207     debug_print ("Connection from " ^ req#clientAddr);
208     debug_print ("Received request: " ^ req#path);
209     (match req#path with
210     | "/help" -> return_help outchan
211     | "/getxml" ->
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
215     | "/getxslt" ->
216         Http_getter_cache.respond_xsl
217           ~url:(Http_getter.resolve (req#param "uri"))
218           ~patch:(parse_patch req) outchan
219     | "/getdtd" ->
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"))
223           outchan
224     | "/resolve" -> return_resolve (req#param "uri") outchan
225     | "/register" ->
226         Http_getter.register ~uri:(req#param "uri") ~url:(req#param "url");
227         return_html_msg "Register done" outchan
228     | "/clean_cache" ->
229         Http_getter.clean_cache ();
230         return_html_msg "Done." outchan
231     | "/update" ->
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
236     | "/add_server" ->
237         let name = req#param "url" in
238         let position = parse_position req in
239         let log = Http_getter.add_server ~position name in
240         return_html_msg
241           (sprintf "Added server %s in position %d)<br />\n%s"
242             name position (HelmLogger.html_of_html_msg log))
243           outchan
244     | "/remove_server" ->
245         let position = parse_position req in
246         let log =
247           try
248             Http_getter.remove_server position
249           with Invalid_argument _ ->
250             raise (Bad_request (sprintf "no server with position %d" position))
251         in
252         return_html_msg
253           (sprintf "Removed server at position %d<br />\n%s"
254             position (HelmLogger.html_of_html_msg log))
255           outchan
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
259     | "/getempty" ->
260         Http_daemon.respond ~body:Http_getter_const.empty_xml outchan
261     | invalid_request ->
262         Http_daemon.respond_error ~status:(`Client_error `Bad_request) outchan);
263     debug_print "Done!\n"
264   with
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"
272           (List.map
273             (fun (cmd, code) ->
274               sprintf "Command '%s' returned %s"
275                 cmd (string_of_proc_status code))
276             l))
277         outchan
278   | exc ->
279       return_html_error
280         ("Uncaught exception: " ^ (Printexc.to_string exc))
281         outchan
282
283     (* Main *)
284
285 let main () =
286   Helm_registry.load_from configuration_file;
287   Http_getter_env.reload ();
288   print_string (Http_getter_env.env_to_string ());
289   flush stdout;
290   at_exit Http_getter.close_maps;
291   Sys.catch_break true;
292   try
293     Http_daemon.start' ~mode:`Thread
294       ~timeout:(Some 600) ~port:(Helm_registry.get_int "getter.port")
295       callback
296   with Sys.Break -> ()  (* 'close_maps' already registered with 'at_exit' *)
297
298 let _ = main ()
299