| Not_found -> default_port
| Failure "int_of_string" ->
prerr_endline "Warning: invalid port, reverting to default";
- default_port;;
+ default_port
+;;
+
+let respond_html body outchan =
+ Http_daemon.respond ~body ~headers:["Content-Type", "text/html"] outchan
+;;
(** perform an 'action' that can be applied to a list of keys or, if no keys
was given, to all keys *)
-let act_on_keys keys_param styles outchan per_key_action all_keys_action logmsg
+let act_on_keys keys_param styles logger per_key_action all_keys_action logmsg
=
- let log = new Uwobo_logger.processingLogger () in
let keys =
try
Pcre.split ~pat:"," keys_param
with Http_types.Param_not_found _ -> []
in
- (match keys with
+ match keys with
| [] -> (* no key provided, act on all stylesheets *)
- log#log "reloading all stylesheets ...";
- (try all_keys_action () with e -> log#log (Printexc.to_string e))
+ logger#log (sprintf "%s all stylesheets ..." logmsg);
+ (try all_keys_action () with e -> logger#log (Printexc.to_string e))
| keys ->
List.iter
(fun key -> (* act on a single stylesheet *)
- log#log (sprintf "%s stylesheet %s" logmsg key);
- (try per_key_action key with e -> log#log (Printexc.to_string e)))
- keys);
- output_string outchan log#asHtml;
- flush outchan
+ logger#log (sprintf "%s stylesheet %s" logmsg key);
+ (try per_key_action key with e -> logger#log (Printexc.to_string e)))
+ keys
;;
(** parse parameters for '/apply' action *)
on ~res_pipe (with a timeout of 60 seconds) and send over outchan data
received from ~res_pipe *)
let short_circuit_grandfather_and_client ~cmd ~cmd_pipe ~res_pipe outchan =
- debug_print (sprintf "Sending command '%s' to grandparent ..." cmd);
+(* debug_print (sprintf "Sending command '%s' to grandparent ..." cmd); *)
output_string cmd_pipe (cmd ^ "\n"); (* send command to grandfather *)
flush cmd_pipe;
let res_pipe_fd = Unix.descr_of_in_channel res_pipe in
(match read_fds with
| [fd] when fd = res_pipe_fd -> (* send answer to http client *)
Http_daemon.send_basic_headers ~code:200 outchan;
+ Http_daemon.send_header "Content-Type" "text/html" outchan;
Http_daemon.send_CRLF outchan;
(try
while true do
exception Restart_HTTP_daemon;;
- (* reuquest handler action
+ (** log a list of libxslt's messages using a processing logger *)
+let log_libxslt_msgs logger =
+ List.iter
+ (function
+ | Uwobo_styles.LibXsltErrorMsg msg ->
+ logger#logBold ("LibXSLT ERROR: " ^ msg)
+ | Uwobo_styles.LibXsltDebugMsg msg ->
+ logger#logEmph ("LibXSLT DEBUG " ^ msg))
+;;
+
+ (* request handler action
@param syslogger Uwobo_logger.sysLogger instance used for logginf
@param styles Uwobo_styles.styles instance which keeps the stylesheets list
@param cmd_pipe output _channel_ used to _write_ update messages
short_circuit_grandfather_and_client ~cmd ~cmd_pipe ~res_pipe outchan
| "/list" ->
(let log = new Uwobo_logger.processingLogger () in
- log#log "Stylesheet list:";
- List.iter (fun s -> log#log s) styles#list;
- Http_daemon.respond ~body:log#asHtml outchan)
+ (match styles#list with
+ | [] -> log#log "No stylesheets loaded (yet)!"
+ | l ->
+ log#log "Stylesheets list:";
+ List.iter (fun s -> log#log s) l);
+ respond_html log#asHtml outchan)
| "/apply" ->
let logger = new Uwobo_logger.processingLogger () in
let xmluri = req#param "xmluri" in
return_error
(sprintf "Stylesheet chain application failed: %s" errmsg)
outchan)
- | "/help" -> Http_daemon.respond ~body:usage_string outchan
+ | "/help" -> respond_html usage_string outchan
| invalid_request ->
Http_daemon.respond_error ~status:(`Client_error `Bad_request) outchan);
syslogger#log `Debug (sprintf "%s done!" req#path);
bad_request (sprintf "Parameter '%s' is missing" attr_name) outchan
| exc ->
return_error ("Uncaught exception: " ^ (Printexc.to_string exc)) outchan
-in
+;;
(* UWOBO's startup *)
let main () =
| child when child > 0 -> (* (4) parent: listen on cmd pipe for updates *)
http_child := Some child;
let stop_http_daemon () = (* kill child *)
- debug_print (sprintf "Grandparent: killing pid %d" child);
+ debug_print (sprintf "UWOBOmaster: killing pid %d" child);
Unix.kill child Sys.sigterm; (* kill child ... *)
- debug_print "Grandparent: waiting for its zombie ...";
ignore (Unix.waitpid [] child); (* ... and its zombie *)
- debug_print "Grandparent: murder completed!!!"
in
Unix.close cmd_pipe_entrance;
Unix.close res_pipe_exit;
while true do
(* INVARIANT: 'Restart_HTTP_daemon' exception is raised only after
child process has been killed *)
- debug_print "Grandparent: waiting for commands ...";
+ debug_print "UWOBOmaster: waiting for commands ...";
let cmd = input_line cmd_pipe in
- debug_print (sprintf "Grandparent: received %s command" cmd);
+ debug_print (sprintf "UWOBOmaster: received %s command" cmd);
(match cmd with (* command from grandchild *)
| "test" ->
- debug_print "Grandparent: Hello, world!";
stop_http_daemon ();
- output_string res_pipe "Grandparent: Hello, world!\n";
+ output_string res_pipe "UWOBOmaster: Hello, world!\n";
flush res_pipe;
raise Restart_HTTP_daemon
| line when Pcre.pmatch ~rex:add_cmd_RE line -> (* /add *)
| [key; style] ->
log#log (sprintf "adding binding <%s,%s>" key style);
(try
- styles#add key style;
+ log_libxslt_msgs log (styles#add key style)
with e ->
log#log (Printexc.to_string e))
| _ -> log#log (sprintf "invalid binding %s" binding))
| line when Pcre.pmatch ~rex:remove_cmd_RE line -> (* /remove *)
stop_http_daemon ();
let arg = Pcre.replace ~rex:remove_cmd_RE line in
+ let logger = new Uwobo_logger.processingLogger () in
act_on_keys
- arg styles res_pipe
- styles#remove (fun () -> styles#removeAll)
+ arg styles logger
+ (fun key -> log_libxslt_msgs logger (styles#remove key))
+ (fun () -> log_libxslt_msgs logger styles#removeAll)
"removing";
+ output_string res_pipe (logger#asHtml);
raise Restart_HTTP_daemon
| line when Pcre.pmatch ~rex:reload_cmd_RE line -> (* /reload *)
stop_http_daemon ();
let arg = Pcre.replace ~rex:reload_cmd_RE line in
+ let logger = new Uwobo_logger.processingLogger () in
act_on_keys
- arg styles res_pipe
- styles#reload (fun () -> styles#reloadAll)
+ arg styles logger
+ (fun key -> log_libxslt_msgs logger (styles#reload key))
+ (fun () -> log_libxslt_msgs logger styles#reloadAll)
"reloading";
+ output_string res_pipe (logger#asHtml);
raise Restart_HTTP_daemon
| cmd -> (* invalid interprocess command received *)
syslogger#log `Warning
last_process := false;
let cmd_pipe = Unix.out_channel_of_descr cmd_pipe_entrance in
let res_pipe = Unix.in_channel_of_descr res_pipe_exit in
- debug_print "Starting HTTP daemon ...";
+ debug_print (sprintf "Starting HTTP daemon on port %d ..." port);
(* next invocation doesn't return, process will keep on serving HTTP
requests until it will get killed by father *)
Http_daemon.start'~port ~mode:`Fork
| _ (* < 0 *) -> (* fork failed :-((( *)
failwith "Can't fork :-("
done
-in
+;;
(* daemon initialization *)
try
Sys.catch_break true;
main ()
with Sys.Break -> () (* 'die_nice' registered with at_exit *)
+;;