2 (* Copyright (C) 2002, HELM Team.
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.
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.
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.
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,
23 * For details, see the HELM World-Wide-Web page,
24 * http://cs.unibo.it/helm/.
30 (* debugging settings *)
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;;
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/xml";;
42 let default_encoding = "utf8";;
45 int_of_string (Sys.getenv port_env_var)
47 | Not_found -> default_port
48 | Failure "int_of_string" ->
49 prerr_endline "Warning: invalid port, reverting to default";
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
59 let bad_request body outchan = (* return a bad request http response *)
60 Http_daemon.respond_error ~status:(`Client_error `Bad_request) ~body outchan
63 (* values common to all threads *)
64 let syslogger = new Uwobo_logger.sysLogger ~level:debug_level () in
66 let styles = new Uwobo_styles.styles in
67 let usage_string = "Help message: not yet written!!" in (* TODO *)
70 let callback req outchan =
71 (* perform an 'action' that can be applied to a list of keys or, if no
72 keys was given, to all keys *)
73 let act_on_keys req styles outchan per_key_action all_keys_action logmsg =
74 let log = new Uwobo_logger.processingLogger () in
77 Pcre.split ~pat:"," (req#param "keys")
78 with Http_request.Param_not_found _ -> []
81 | [] -> (* no key provided, act on all stylesheets *)
82 log#log "reloading all stylesheets ...";
83 (try all_keys_action () with e -> log#log (Printexc.to_string e))
86 (fun key -> (* act on a single stylesheet *)
87 log#log (sprintf "%s stylesheet %s" logmsg key);
88 (try per_key_action key with e -> log#log (Printexc.to_string e)))
90 Http_daemon.respond ~body:log#asHtml outchan
92 let parse_apply_params = (* parse parameters for '/apply' action *)
93 let is_global_param x = Pcre.pmatch ~pat:"^param(\\.[^.]+){1}$" x in
94 let is_local_param x = Pcre.pmatch ~pat:"^param(\\.[^.]+){2}$" x in
95 let is_property x = Pcre.pmatch ~pat:"^prop\\.[^.]+$" x in
97 (fun (old_params, old_properties) (name, value) ->
99 | name when is_global_param name ->
100 let name = Pcre.replace ~pat:"^param\\." name in
101 ((fun x -> (old_params x) @ [name, value]), old_properties)
102 | name when is_local_param name ->
103 let pieces = Pcre.extract ~pat:"^param\\.([^.]+)\\.(.*)" name in
104 let (key, name) = (pieces.(1), pieces.(2)) in
106 | x when x = key -> [name, value] @ (old_params x)
107 | x -> old_params x),
109 | name when is_property name ->
110 let name = Pcre.replace ~pat:"^prop\\." name in
111 (old_params, ((name, value) :: old_properties))
112 | _ -> (old_params, old_properties))
113 ((fun _ -> []), []) (* no parameters, no properties *)
116 syslogger#log `Debug (sprintf "Received request: %s" req#path);
119 (let bindings = req#paramAll "bind" in
120 if bindings = [] then
121 return_error "No [key,stylesheet] binding provided" outchan
123 let log = new Uwobo_logger.processingLogger () in
125 (fun binding -> (* add a <key, stylesheet> binding *)
126 let pieces = Pcre.split ~pat:"," binding in
129 log#log (sprintf "adding binding <%s,%s>" key style);
131 styles#add key style;
133 log#log (Printexc.to_string e))
134 | _ -> log#log (sprintf "invalid binding %s" binding))
136 Http_daemon.respond ~body:log#asHtml outchan
139 (let log = new Uwobo_logger.processingLogger () in
140 log#log "Stylesheet list:";
141 List.iter (fun s -> log#log s) styles#list;
142 Http_daemon.respond ~body:log#asHtml outchan)
146 styles#remove (fun () -> styles#removeAll) "removing"
150 styles#reload (fun () -> styles#reloadAll) "reloading"
152 (let logger = new Uwobo_logger.processingLogger () in
153 let xmluri = req#param "xmluri" in
154 let keys = Pcre.split ~pat:"," (req#param "keys") in
155 (* notation: "local" parameters are those defined on a per-stylesheet
156 pasis (i.e. param.key.param=value), "global" parameters are those
157 defined for all stylesheets (i.e. param.param=value) *)
158 let (params, props) = parse_apply_params req#params in
159 syslogger#log `Debug (sprintf "Parsing input document %s ..." xmluri);
160 let domImpl = Gdome.domImplementation () in
161 let input = domImpl#createDocumentFromURI ~uri:xmluri () in
162 syslogger#log `Debug "Applying stylesheet chain ...";
164 let (write_result, media_type, encoding) = (* out_channel -> unit *)
166 ~logger:syslogger ~styles ~keys ~input ~params ~props
168 let content_type = (* value of Content-Type HTTP response header *)
171 (match media_type with None -> default_media_type | Some t -> t)
172 (match encoding with None -> default_encoding | Some e -> e)
174 syslogger#log `Debug "sending output to client ....";
175 Http_daemon.send_basic_headers ~code:200 outchan;
176 Http_daemon.send_header "Content-Type" content_type outchan;
177 Http_daemon.send_CRLF outchan;
179 with Uwobo_failure errmsg ->
181 (sprintf "Stylesheet chain application failed: %s" errmsg)
183 | "/help" -> Http_daemon.respond ~body:usage_string outchan
185 Http_daemon.respond_error ~status:(`Client_error `Bad_request) outchan)
187 | Http_request.Param_not_found attr_name ->
188 bad_request (sprintf "Parameter '%s' is missing" attr_name) outchan
191 ~body:(pp_error ("Uncaught exception: " ^ (Printexc.to_string exc)))
195 (* daemon initialization *)
198 (sprintf "%s started and listening on port %d" daemon_name port);
199 syslogger#log `Notice (sprintf "current directory is %s" (Sys.getcwd ()));
200 Http_daemon.start' ~port ~mode:`Thread callback;
201 syslogger#log `Notice (sprintf "%s is terminating, bye!" daemon_name)