- (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
- 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 = 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)