* http://cs.unibo.it/helm/.
*)
+(* TODO braindead situation: /add of a stylesheet which uri is an uwobo
+invocation *)
+
open Printf;;
open Uwobo_common;;
let daemon_name = "UWOBO OCaml";;
let default_port = 8082;;
let port_env_var = "UWOBO_PORT";;
+let default_media_type = "text/html";;
+let default_encoding = "utf8";;
let port =
try
int_of_string (Sys.getenv port_env_var)
(* facilities *)
let pp_error = sprintf "<html><body><h1>Error: %s</h1></body></html>" in
-let invocation_error msg outchan =
- (* return an ok (200) http response, which display in html an invocation error
- message *)
+let return_error msg outchan =
+ (* return an ok (200) http response, which display in html an error message *)
Http_daemon.respond ~body:(pp_error msg) outchan
in
let bad_request body outchan = (* return a bad request http response *)
let syslogger = new Uwobo_logger.sysLogger ~level:debug_level () in
syslogger#enable;
let styles = new Uwobo_styles.styles in
-let usage_string = "Help message: not yet written!!" in (* TODO *)
+let usage_string =
+ sprintf
+"
+<html>
+ <head>
+ <title>UWOBO's help message</title>
+ </head>
+ <body>
+ <p>
+ Usage: <kbd>http://hostname:uwoboport/</kbd><em>command</em>
+ </p>
+ <p>
+ Available commands:
+ </p>
+ <p>
+ <b><kbd>help</kbd></b><br />
+ display this help message
+ </p>
+ <p>
+ <b><kbd>add?bind=key,uri[&bind=key,stylesheet[&...]]</kbd></b><br />
+ load a new stylesheet, specified by <em>uri</em>, and bind it to key
+ <em>key</em>
+ </p>
+ <p>
+ <b><kbd>remove?[?keys=key1,key2,...]</kbd></b><br />
+ unload stylesheets specified by <em>key1, key2, ...</em> or all
+ stylesheets if no key was given
+ </p>
+ <p>
+ <b><kbd>reload?[?keys=key1,key2,...]</kbd></b><br />
+ reload stylesheets specified by <em>key1, key2, ...</em> or all
+ stylesheets if no key was given
+ </p>
+ <p>
+ <b><kbd>list</kbd></b><br />
+ return a list of loaded stylesheets
+ </p>
+ <p>
+ <b><kbd>apply?xmluri=uri&keys=key1,key2,...[¶m.name=value[¶m.name=value[&...]]][¶m.key.name=value[¶m.key.name=value[&...]]][&name[=value][&prop.name[=value][&...]]]</kbd></b><br />
+ apply a chain of stylesheets, specified by <em>key1, key2, ...</em>, to an
+ input document, specified by <em>uri</em>.<br />
+ Additional parameters can be set for each stylesheet application: global
+ parameters (i.e. parameters passed to all stylesheets) are set using
+ <em>param.name=value</em> syntax, per stylesheet parameters are set using
+ <em>param.key.name=value</em> where <em>key</em> is the key of a loaded
+ stylesheet.<br />
+ Properties of the final chain output can be set too: valueless properties
+ can be set using <em>prop.name</em> syntax, others can be set using
+ <em>prop.name=value</em> syntax.<br />
+ Current supported properties are: %s.
+ </p>
+ </body>
+</html>
+"
+ (String.concat ", " Uwobo_common.supported_properties)
+in
(* thread action *)
let callback req outchan =
keys);
Http_daemon.respond ~body:log#asHtml outchan
in
+ let parse_apply_params = (* parse parameters for '/apply' action *)
+ let is_global_param x = Pcre.pmatch ~pat:"^param(\\.[^.]+){1}$" x in
+ let is_local_param x = Pcre.pmatch ~pat:"^param(\\.[^.]+){2}$" x in
+ let is_property x = Pcre.pmatch ~pat:"^prop\\.[^.]+$" x in
+ List.fold_left
+ (fun (old_params, old_properties) (name, value) ->
+ match name with
+ | name when is_global_param name ->
+ let name = Pcre.replace ~pat:"^param\\." name in
+ ((fun x -> (old_params x) @ [name, value]), old_properties)
+ | name when is_local_param name ->
+ let pieces = Pcre.extract ~pat:"^param\\.([^.]+)\\.(.*)" name in
+ let (key, name) = (pieces.(1), pieces.(2)) in
+ ((function
+ | x when x = key -> [name, value] @ (old_params x)
+ | x -> old_params x),
+ old_properties)
+ | name when is_property name ->
+ let name = Pcre.replace ~pat:"^prop\\." name in
+ (old_params, ((name, value) :: old_properties))
+ | _ -> (old_params, old_properties))
+ ((fun _ -> []), []) (* no parameters, no properties *)
+ in
try
syslogger#log `Debug (sprintf "Received request: %s" req#path);
(match req#path with
| "/add" ->
(let bindings = req#paramAll "bind" in
if bindings = [] then
- invocation_error "No [key,stylesheet] binding provided" outchan
+ return_error "No [key,stylesheet] binding provided" outchan
else begin
let log = new Uwobo_logger.processingLogger () in
List.iter
req styles outchan
styles#reload (fun () -> styles#reloadAll) "reloading"
| "/apply" ->
- (let logger = new Uwobo_logger.processingLogger () in
- let xmluri = req#param "xmluri" in
- let keys = Pcre.split ~pat:"," (req#param "keys") in
- (* notation: "local" parameters are those defined on a per-stylesheet
- pasis (i.e. param.key.param=value), "global" parameters are those
- defined for all stylesheets (i.e. param.param=value) *)
- let is_global_param x = Pcre.pmatch ~pat:"^param(\\.[^.]+){1}$" x in
- let is_local_param x = Pcre.pmatch ~pat:"^param(\\.[^.]+){2}$" x in
- let is_property x = Pcre.pmatch ~pat:"^prop\\.[^.]+$" x in
- let (params, props) =
- List.fold_left
- (fun (old_params, old_properties) (name, value) ->
- match name with
- | name when is_global_param name ->
- let name = Pcre.replace ~pat:"^param\\." name in
- ((fun x -> (old_params x) @ [name, value]),
- old_properties)
- | name when is_local_param name ->
- let pieces = Pcre.extract ~pat:"^param\\.([^.]+)\\.(.*)" name in
- let (key, name) = (pieces.(1), pieces.(2)) in
- ((function
- | x when x = key -> [name, value] @ (old_params x)
- | x -> old_params x),
- old_properties)
- | name when is_property name ->
- let name = Pcre.replace ~pat:"^prop\\." name in
- (old_params, ((name, value) :: old_properties))
- | _ -> (old_params, old_properties))
- ((fun _ -> []), []) (* no parameters, no properties *)
- req#params
- in
- syslogger#log `Debug (sprintf "Parsing input document %s ..." xmluri);
- let input = styles#domImpl#createDocumentFromURI ~uri:xmluri () in
- let output =
- Uwobo_engine.apply ~logger ~styles ~keys ~input ~params ~props
- (* TODO uhm ... what to do if Uwobo_failure is raised? *)
- in
- syslogger#log `Debug logger#asText;
- let tempfile = (* temporary file on which save XML output *)
- (* TODO I don't need a tempfile, but gdome seems not to permit to
- return the string representation of a Gdome.document *)
- let inchan = Unix.open_process_in "tempfile --prefix=uwobo" in
- let name = input_line inchan in
- close_in inchan;
- name
- in
- syslogger#log
- `Debug
- (sprintf "saving output document to %s ..." tempfile);
- let res =
- styles#domImpl#saveDocumentToFile ~doc:output ~name:tempfile ()
- in
- if not res then
- raise (Uwobo_failure ("unable to save output to file " ^ tempfile));
- syslogger#log `Debug "sending output to client ....";
- Http_daemon.send_basic_headers ~code:200 outchan;
- (* TODO set Content-Type *)
- Http_daemon.send_CRLF outchan;
- Http_daemon.send_file ~name:tempfile outchan;
- Unix.unlink tempfile)
+ if Unix.fork () = 0 then
+ (let logger = new Uwobo_logger.processingLogger () in
+ let xmluri = req#param "xmluri" in
+ let keys = Pcre.split ~pat:"," (req#param "keys") in
+ (* notation: "local" parameters are those defined on a per-stylesheet
+ pasis (i.e. param.key.param=value), "global" parameters are those
+ defined for all stylesheets (i.e. param.param=value) *)
+ let (params, props) = parse_apply_params req#params in
+ syslogger#log `Debug (sprintf "Parsing input document %s ..." xmluri);
+ let domImpl = Gdome.domImplementation () in
+ let input = domImpl#createDocumentFromURI ~uri:xmluri () in
+ syslogger#log `Debug "Applying stylesheet chain ...";
+ try
+ let (write_result, media_type, encoding) = (* out_channel -> unit *)
+ Uwobo_engine.apply
+ ~logger:syslogger ~styles ~keys ~input ~params ~props
+ in
+ let content_type = (* value of Content-Type HTTP response header *)
+ sprintf
+ "%s; charset=%s"
+ (match media_type with None -> default_media_type | Some t -> t)
+ (match encoding with None -> default_encoding | Some e -> e)
+ in
+ syslogger#log
+ `Debug
+ (sprintf
+ "sending output to client (Content-Type: %s)...."
+ content_type);
+ Http_daemon.send_basic_headers ~code:200 outchan;
+ Http_daemon.send_header "Content-Type" content_type outchan;
+ Http_daemon.send_CRLF outchan;
+ write_result outchan
+ with Uwobo_failure errmsg ->
+ return_error
+ (sprintf "Stylesheet chain application failed: %s" errmsg)
+ outchan)
| "/help" -> Http_daemon.respond ~body:usage_string outchan
| invalid_request ->
Http_daemon.respond_error ~status:(`Client_error `Bad_request) outchan)
`Notice
(sprintf "%s started and listening on port %d" daemon_name port);
syslogger#log `Notice (sprintf "current directory is %s" (Sys.getcwd ()));
+Unix.putenv "http_proxy" ""; (* reset http_proxy to avoid libxslt problems *)
Http_daemon.start' ~port ~mode:`Thread callback;
syslogger#log `Notice (sprintf "%s is terminating, bye!" daemon_name)