]> matita.cs.unibo.it Git - helm.git/blob - helm/uwobo/src/ocaml/uwobo.ml
- written help message
[helm.git] / helm / uwobo / src / ocaml / uwobo.ml
1
2 (* Copyright (C) 2002, HELM Team.
3  * 
4  * This file is part of HELM, an Hypertextual, Electronic
5  * Library of Mathematics, developed at the Computer Science
6  * Department, University of Bologna, Italy.
7  * 
8  * HELM is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  * 
13  * HELM is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with HELM; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21  * MA  02111-1307, USA.
22  * 
23  * For details, see the HELM World-Wide-Web page,
24  * http://cs.unibo.it/helm/.
25  *)
26
27 open Printf;;
28 open Uwobo_common;;
29
30  (* debugging settings *)
31 let debug = true;;
32 let debug_level = `Debug;;
33 let debug_print s = if debug then prerr_endline s;;
34 let http_debug = false;;
35 Http_common.debug := http_debug;;
36
37   (* environment settings *)
38 let daemon_name = "UWOBO OCaml";;
39 let default_port = 8082;;
40 let port_env_var = "UWOBO_PORT";;
41 let default_media_type = "text/html";;
42 let default_encoding = "utf8";;
43 let port =
44   try
45     int_of_string (Sys.getenv port_env_var)
46   with
47   | Not_found -> default_port
48   | Failure "int_of_string" ->
49       prerr_endline "Warning: invalid port, reverting to default";
50       default_port
51 in
52
53   (* facilities *)
54 let pp_error = sprintf "<html><body><h1>Error: %s</h1></body></html>" in
55 let return_error msg outchan =
56   (* return an ok (200) http response, which display in html an error message *)
57   Http_daemon.respond ~body:(pp_error msg) outchan
58 in
59 let bad_request body outchan =  (* return a bad request http response *)
60   Http_daemon.respond_error ~status:(`Client_error `Bad_request) ~body outchan
61 in
62
63   (* values common to all threads *)
64 let syslogger = new Uwobo_logger.sysLogger ~level:debug_level () in
65 syslogger#enable;
66 let styles = new Uwobo_styles.styles in
67 let usage_string =
68   sprintf
69 "
70 <html>
71   <head>
72     <title>UWOBO's help message</title>
73   </head>
74   <body>
75     <p>
76     Usage: <kbd>http://hostname:uwoboport/</kbd><em>command</em>
77     </p>
78     <p>
79     Available commands:
80     </p>
81     <p>
82       <b><kbd>help</kbd></b><br />
83       display this help message
84     </p>
85     <p>
86       <b><kbd>add?bind=key,uri[&bind=key,stylesheet[&...]]</kbd></b><br />
87       load a new stylesheet, specified by <em>uri</em>, and bind it to key
88           <em>key</em>
89     </p>
90     <p>
91       <b><kbd>remove?[?keys=key1,key2,...]</kbd></b><br />
92       unload stylesheets specified by <em>key1, key2, ...</em> or all
93           stylesheets if no key was given
94     </p>
95     <p>
96       <b><kbd>reload?[?keys=key1,key2,...]</kbd></b><br />
97       reload stylesheets specified by <em>key1, key2, ...</em> or all
98           stylesheets if no key was given
99     </p>
100     <p>
101       <b><kbd>list</kbd></b><br />
102       return a list of loaded stylesheets
103     </p>
104     <p>
105       <b><kbd>apply?xmluri=uri&keys=key1,key2,...[&param.name=value[&param.name=value[&...]]][&param.key.name=value[&param.key.name=value[&...]]][&name[=value][&prop.name[=value][&...]]]</kbd></b><br />
106       apply a chain of stylesheets, specified by <em>key1, key2, ...</em>, to an
107       input document, specified by <em>uri</em>.<br />
108       Additional parameters can be set for each stylesheet application: global
109       parameters (i.e. parameters passed to all stylesheets) are set using
110       <em>param.name=value</em> syntax, per stylesheet parameters are set using
111       <em>param.key.name=value</em> where <em>key</em> is the key of a loaded
112       stylesheet.<br />
113       Properties of the final chain output can be set too: valueless properties
114       can be set using <em>prop.name</em> syntax, others can be set using
115       <em>prop.name=value</em> syntax.<br />
116       Current supported properties are: %s.
117     </p>
118   </body>
119 </html>
120 "
121   (String.concat ", " Uwobo_common.supported_properties)
122 in
123
124   (* thread action *)
125 let callback req outchan =
126     (* perform an 'action' that can be applied to a list of keys or, if no
127     keys was given, to all keys *)
128   let act_on_keys req styles outchan per_key_action all_keys_action logmsg =
129     let log = new Uwobo_logger.processingLogger () in
130     let keys =
131       try
132         Pcre.split ~pat:"," (req#param "keys")
133       with Http_request.Param_not_found _ -> []
134     in
135     (match keys with
136     | [] -> (* no key provided, act on all stylesheets *)
137         log#log "reloading all stylesheets ...";
138         (try all_keys_action () with e -> log#log (Printexc.to_string e))
139     | keys ->
140         List.iter
141           (fun key -> (* act on a single stylesheet *)
142             log#log (sprintf "%s stylesheet %s" logmsg key);
143             (try per_key_action key with e -> log#log (Printexc.to_string e)))
144           keys);
145     Http_daemon.respond ~body:log#asHtml outchan
146   in
147   let parse_apply_params =  (* parse parameters for '/apply' action *)
148     let is_global_param x = Pcre.pmatch ~pat:"^param(\\.[^.]+){1}$" x in
149     let is_local_param x = Pcre.pmatch ~pat:"^param(\\.[^.]+){2}$" x in
150     let is_property x = Pcre.pmatch ~pat:"^prop\\.[^.]+$" x in
151     List.fold_left
152       (fun (old_params, old_properties) (name, value) ->
153         match name with
154         | name when is_global_param name ->
155             let name = Pcre.replace ~pat:"^param\\." name in
156             ((fun x -> (old_params x) @ [name, value]), old_properties)
157         | name when is_local_param name ->
158             let pieces = Pcre.extract ~pat:"^param\\.([^.]+)\\.(.*)" name in
159             let (key, name) = (pieces.(1), pieces.(2)) in
160             ((function
161               | x when x = key -> [name, value] @ (old_params x)
162               | x -> old_params x),
163              old_properties)
164         | name when is_property name ->
165             let name = Pcre.replace ~pat:"^prop\\." name in
166             (old_params, ((name, value) :: old_properties))
167         | _ -> (old_params, old_properties))
168       ((fun _ -> []), []) (* no parameters, no properties *)
169   in
170   try
171     syslogger#log `Debug (sprintf "Received request: %s" req#path);
172     (match req#path with
173     | "/add" ->
174         (let bindings = req#paramAll "bind" in
175         if bindings = [] then
176           return_error "No [key,stylesheet] binding provided" outchan
177         else begin
178           let log = new Uwobo_logger.processingLogger () in
179           List.iter
180             (fun binding -> (* add a <key, stylesheet> binding *)
181               let pieces = Pcre.split ~pat:"," binding in
182               match pieces with
183               | [key; style] ->
184                   log#log (sprintf "adding binding <%s,%s>" key style);
185                   (try
186                     styles#add key style;
187                   with e ->
188                     log#log (Printexc.to_string e))
189               | _ -> log#log (sprintf "invalid binding %s" binding))
190             bindings;
191           Http_daemon.respond ~body:log#asHtml outchan
192         end)
193     | "/list" ->
194         (let log = new Uwobo_logger.processingLogger () in
195         log#log "Stylesheet list:";
196         List.iter (fun s -> log#log s) styles#list;
197         Http_daemon.respond ~body:log#asHtml outchan)
198     | "/remove" ->
199         act_on_keys
200           req styles outchan
201           styles#remove (fun () -> styles#removeAll) "removing"
202     | "/reload" ->
203         act_on_keys
204           req styles outchan
205           styles#reload (fun () -> styles#reloadAll) "reloading"
206     | "/apply" ->
207         if Unix.fork () = 0 then
208           (let logger = new Uwobo_logger.processingLogger () in
209           let xmluri = req#param "xmluri" in
210           let keys = Pcre.split ~pat:"," (req#param "keys") in
211           (* notation: "local" parameters are those defined on a per-stylesheet
212           pasis (i.e. param.key.param=value), "global" parameters are those
213           defined for all stylesheets (i.e. param.param=value) *)
214           let (params, props) = parse_apply_params req#params in
215           syslogger#log `Debug (sprintf "Parsing input document %s ..." xmluri);
216           let domImpl = Gdome.domImplementation () in
217           let input = domImpl#createDocumentFromURI ~uri:xmluri () in
218           syslogger#log `Debug "Applying stylesheet chain ...";
219           try
220             let (write_result, media_type, encoding) = (* out_channel -> unit *)
221               Uwobo_engine.apply
222                 ~logger:syslogger ~styles ~keys ~input ~params ~props
223             in
224             let content_type = (* value of Content-Type HTTP response header *)
225               sprintf
226                 "%s; charset=%s"
227                 (match media_type with None -> default_media_type | Some t -> t)
228                 (match encoding with None -> default_encoding | Some e -> e)
229             in
230             syslogger#log
231               `Debug
232               (sprintf
233                 "sending output to client (Content-Type: %s)...."
234                 content_type);
235             Http_daemon.send_basic_headers ~code:200 outchan;
236             Http_daemon.send_header "Content-Type" content_type outchan;
237             Http_daemon.send_CRLF outchan;
238             write_result outchan
239           with Uwobo_failure errmsg ->
240             return_error
241               (sprintf "Stylesheet chain application failed: %s" errmsg)
242               outchan)
243     | "/help" -> Http_daemon.respond ~body:usage_string outchan
244     | invalid_request ->
245         Http_daemon.respond_error ~status:(`Client_error `Bad_request) outchan)
246   with
247   | Http_request.Param_not_found attr_name ->
248       bad_request (sprintf "Parameter '%s' is missing" attr_name) outchan
249   | exc ->
250       Http_daemon.respond
251         ~body:(pp_error ("Uncaught exception: " ^ (Printexc.to_string exc)))
252         outchan
253 in
254
255   (* daemon initialization *)
256 syslogger#log
257   `Notice
258   (sprintf "%s started and listening on port %d" daemon_name port);
259 syslogger#log `Notice (sprintf "current directory is %s" (Sys.getcwd ()));
260 Http_daemon.start' ~port ~mode:`Thread callback;
261 syslogger#log `Notice (sprintf "%s is terminating, bye!" daemon_name)
262