]> matita.cs.unibo.it Git - helm.git/blob - helm/searchEngine/searchEngine.ml
- ported to new output_html Disambiguate callback
[helm.git] / helm / searchEngine / searchEngine.ml
1 (* Copyright (C) 2002, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 module T = MQGTypes
27 module U = MQGUtil
28 module G = MQueryGenerator
29 module C = MQIConn
30
31 open Http_types ;;
32
33 let debug = true;;
34 let debug_print s = if debug then prerr_endline s;;
35 Http_common.debug := true;;
36 (* Http_common.debug := true;; *)
37
38   (** accepted HTTP servers for ask_uwobo method forwarding *)
39 let valid_servers =
40  [ "mowgli.cs.unibo.it:58080" ; "mowgli.cs.unibo.it" ; "localhost:58080" ];;
41
42 let mqi_flags = [] (* default MathQL interpreter options *)
43
44 open Printf;;
45
46 let daemon_name = "Search Engine";;
47 let default_port = 58085;;
48 let port_env_var = "SEARCH_ENGINE_PORT";;
49
50 let pages_dir =
51   try
52     Sys.getenv "SEARCH_ENGINE_HTML_DIR"
53   with Not_found -> "html"  (* relative to searchEngine's document root *)
54 ;;
55 let interactive_user_uri_choice_TPL = pages_dir ^ "/templateambigpdq1.html";;
56 let interactive_interpretation_choice_TPL =
57   pages_dir ^ "/templateambigpdq2.html";;
58 let constraints_choice_TPL = pages_dir ^ "/constraints_choice_template.html";;
59 let final_results_TPL = pages_dir ^ "/templateambigpdq3.html";;
60
61 exception Chat_unfinished
62
63   (* build a bool from a 1-character-string *)
64 let bool_of_string' = function
65   | "0" -> false
66   | "1" -> true
67   | s -> failwith ("Can't parse a boolean from string: " ^ s)
68 ;;
69
70   (* build an int option from a string *)
71 let int_of_string' = function
72   | "_" -> None
73   | s ->
74       try
75         Some (int_of_string s)
76       with Failure "int_of_string" ->
77         failwith ("Can't parse an int option from string: " ^ s)
78 ;;
79
80   (* HTML pretty printers for mquery_generator types *)
81
82 let html_of_r_obj (pos, uri) =
83   sprintf
84     "<tr><td><input type='checkbox' name='constr_obj' checked='on'/></td><td>%s</td><td>%s</td><td>%s</td></tr>"
85     uri (U.text_of_position pos)
86     (if U.is_main_position pos then
87       sprintf "<input name='obj_depth' size='2' type='text' value='%s' />"
88         (U.text_of_depth pos "")
89     else
90       "<input type=\"hidden\" name=\"obj_depth\" />")
91 ;;
92
93 let html_of_r_rel pos =
94   sprintf
95     "<tr><td><input type='checkbox' name='constr_rel' checked='on'/></td><td>%s</td><td><input name='rel_depth' size='2' type='text' value='%s' /></td></tr>"
96     (U.text_of_position (pos:>T.full_position)) (U.text_of_depth (pos:>T.full_position) "")
97 ;;
98
99 let html_of_r_sort (pos, sort) =
100   sprintf
101     "<tr><td><input type='checkbox' name='constr_sort' checked='on'/></td><td>%s</td><td>%s</td><td><input name='sort_depth' size='2' type='text' value='%s'/></td></tr>"
102     (U.text_of_sort sort) (U.text_of_position (pos:>T.full_position)) (U.text_of_depth (pos:>T.full_position) "")
103 ;;
104
105   (** pretty print a MathQL query result to an HELM theory file *)
106 let theory_of_result result =
107  let results_no = List.length result in
108   if results_no > 0 then
109    let mode = if results_no > 10 then "linkonly" else "typeonly" in
110    let results =
111     let idx = ref (results_no + 1) in
112      List.fold_right
113       (fun (uri,attrs) i ->
114         decr idx ;
115         "<tr><td valign=\"top\">" ^ string_of_int !idx ^ ".</td><td><ht:OBJECT uri=\"" ^ uri ^ "\" mode=\"" ^ mode ^ "\"/></td></tr>" ^  i
116       ) result ""
117    in
118     "<h1>Query Results:</h1><table xmlns:ht=\"http://www.cs.unibo.it/helm/namespaces/helm-theory\">" ^ results ^ "</table>"
119   else
120     "<h1>Query Results:</h1><p>No results found!</p>"
121 ;;
122
123 let pp_result result =
124  "<html xmlns:ht=\"http://www.cs.unibo.it/helm/namespaces/helm-theory\">\n<head><title>Query Results</title><style> A { text-decoration: none } </style></head>\n<body>" ^ theory_of_result result ^ "</body></html>"
125 ;;
126
127   (** chain application of Pcre substitutions *)
128 let rec apply_substs substs line =
129   match substs with
130   | [] -> line
131   | (rex, templ) :: rest -> apply_substs rest (Pcre.replace ~rex ~templ line)
132   (** fold like function on files *)
133 let fold_file f init fname =
134   let inchan = open_in fname in
135   let rec fold_lines' value =
136     try 
137       let line = input_line inchan in 
138       fold_lines' (f value line)
139     with End_of_file -> value
140   in
141   let res = (try fold_lines' init with e -> (close_in inchan; raise e)) in
142   close_in inchan;
143   res
144   (** iter like function on files *)
145 let iter_file f = fold_file (fun _ line -> f line) ()
146
147 let (title_tag_RE, choices_tag_RE, msg_tag_RE, id_to_uris_RE, id_RE,
148     interpretations_RE, interpretations_labels_RE, results_RE, new_aliases_RE,
149     form_RE, variables_initialization_RE)
150   =
151   (Pcre.regexp "@TITLE@", Pcre.regexp "@CHOICES@", Pcre.regexp "@MSG@",
152   Pcre.regexp "@ID_TO_URIS@", Pcre.regexp "@ID@",
153   Pcre.regexp "@INTERPRETATIONS@", Pcre.regexp "@INTERPRETATIONS_LABELS@",
154   Pcre.regexp "@RESULTS@", Pcre.regexp "@NEW_ALIASES@", Pcre.regexp "@FORM@",
155   Pcre.regexp "@VARIABLES_INITIALIZATION@")
156 let server_and_port_url_RE = Pcre.regexp "^http://([^/]+)/.*$"
157
158 let port =
159   try
160     int_of_string (Sys.getenv port_env_var)
161   with
162   | Not_found -> default_port
163   | Failure "int_of_string" ->
164       prerr_endline "Warning: invalid port, reverting to default";
165       default_port
166 ;;
167
168 let pp_error = sprintf "<html><body><h1>Error: %s</h1></body></html>";;
169
170 let bad_request body outchan =
171   Http_daemon.respond_error ~status:(`Client_error `Bad_request) ~body outchan
172 ;;
173
174 let contype = "Content-Type", "text/html";;
175
176 (* SEARCH ENGINE functions *)
177
178 let get_constraints term =
179  function
180     | "/locateInductivePrinciple" ->
181       None,
182       (CGLocateInductive.get_constraints term),
183       (None,None,None)
184     | "/searchPattern" ->
185      let constr_obj, constr_rel, constr_sort =
186        CGSearchPattern.get_constraints term in
187      (Some CGSearchPattern.universe),
188      (constr_obj, constr_rel, constr_sort),
189      (Some constr_obj, Some constr_rel, Some constr_sort)
190     | "/matchConclusion" ->
191      let list_of_must, only = CGMatchConclusion.get_constraints [] [] term in
192 (* FG: there is no way to choose the block number ***************************)
193      let block = pred (List.length list_of_must) in 
194       (Some CGMatchConclusion.universe), 
195       (List.nth list_of_must block, [], []), (Some only, None, None)
196     | _ -> assert false
197 ;;
198
199 (*
200   format:
201     <must_obj> ':' <must_rel> ':' <must_sort> ':' <only_obj> ':' <only_rel> ':' <only_sort>
202
203     <must_*> ::= ('0'|'1') ('_'|<int>) (',' ('0'|'1') ('_'|<int>))*
204     <only> ::= '0'|'1'
205 *)
206 let add_user_constraints ~constraints
207  ((obj, rel, sort), (only_obj, only_rel, only_sort))
208 =
209   let parse_must s =
210     let l = Pcre.split ~pat:"," s in
211     (try
212       List.map
213         (fun s ->
214           let subs = Pcre.extract ~pat:"^(.)(\\d+|_)$" s in
215           (bool_of_string' subs.(1), int_of_string' subs.(2)))
216         l
217      with
218       Not_found -> failwith ("Can't parse constraint string: " ^ constraints)
219     )
220   in
221     (* to be used on "obj" *)
222   let add_user_must33 user_must must =
223     List.map2
224      (fun (b, i) (p, u) ->
225        if b then Some (U.set_full_position p i, u) else None)
226      user_must must
227   in
228     (* to be used on "rel" *)
229   let add_user_must22 user_must must =
230     List.map2
231      (fun (b, i) p -> if b then Some (U.set_main_position p i) else None)
232      user_must must
233   in
234     (* to be used on "sort" *)
235   let add_user_must32 user_must must =
236     List.map2
237      (fun (b, i) (p, s)-> if b then Some (U.set_main_position p i, s) else None)
238      user_must must
239   in
240   match Pcre.split ~pat:":" constraints with
241   | [user_obj;user_rel;user_sort;user_only_obj;user_only_rel;user_only_sort] ->
242       let
243        (user_obj,user_rel,user_sort,user_only_obj,user_only_rel,user_only_sort)
244       =
245         (parse_must user_obj,
246         parse_must user_rel,
247         parse_must user_sort,
248         bool_of_string' user_only_obj,
249         bool_of_string' user_only_rel,
250         bool_of_string' user_only_sort)
251       in
252       let only' =
253        (if user_only_obj  then only_obj else None),
254        (if user_only_rel  then only_rel else None),
255        (if user_only_sort then only_sort else None)
256       in
257       let must' =
258        let rec filter_some =
259         function
260            [] -> []
261          | None::tl -> filter_some tl
262          | (Some x)::tl -> x::(filter_some tl) 
263        in
264         filter_some (add_user_must33 user_obj obj),
265         filter_some (add_user_must22 user_rel rel),
266         filter_some (add_user_must32 user_sort sort)
267       in
268       (must', only')
269   | _ -> failwith ("Can't parse constraint string: " ^ constraints)
270 in
271
272 (* HTTP DAEMON CALLBACK *)
273
274 let callback (req: Http_types.request) outchan =
275   try
276     debug_print (sprintf "Received request: %s" req#path);
277     (match req#path with
278     | "/execute" ->
279         let mqi_handle = C.init mqi_flags debug_print in 
280         let query_string = req#param "query" in
281         let lexbuf = Lexing.from_string query_string in
282         let query = MQueryUtil.query_of_text lexbuf in
283         let result = MQueryInterpreter.execute mqi_handle query in
284         let result_string = pp_result result in
285               C.close mqi_handle;
286         Http_daemon.respond ~body:result_string ~headers:[contype] outchan
287     | "/locate" ->
288         let mqi_handle = C.init mqi_flags debug_print in
289         let id = req#param "id" in
290         let query = G.locate id in
291         let result = MQueryInterpreter.execute mqi_handle query in
292               C.close mqi_handle;
293         Http_daemon.respond ~headers:[contype] ~body:(pp_result result) outchan
294     | "/unreferred" ->
295         let mqi_handle = C.init mqi_flags debug_print in
296         let target = req#param "target" in
297         let source = req#param "source" in
298         let query = G.unreferred target source in
299         let result = MQueryInterpreter.execute mqi_handle query in
300               C.close mqi_handle;
301         Http_daemon.respond ~headers:[contype] ~body:(pp_result result) outchan
302     | "/getpage" ->
303         (* TODO implement "is_permitted" *)
304         (let is_permitted _ = true in
305         let remove_fragment uri = Pcre.replace ~pat:"#.*" uri in
306         let page = remove_fragment (req#param "url") in
307         let preprocess =
308           (try
309             bool_of_string (req#param "preprocess")
310           with Invalid_argument _ | Http_types.Param_not_found _ -> false)
311         in
312         (match page with
313         | page when is_permitted page ->
314             (let fname = sprintf "%s/%s" pages_dir (remove_fragment page) in
315             Http_daemon.send_basic_headers ~code:200 outchan;
316             Http_daemon.send_header "Content-Type" "text/html" outchan;
317             Http_daemon.send_CRLF outchan;
318             if preprocess then begin
319               iter_file
320                 (fun line ->
321                   output_string outchan
322                     ((apply_substs
323                        (List.map
324                          (function (key,value) ->
325                            let key' =
326                             (Pcre.extract ~pat:"param\\.(.*)" key).(1)
327                            in
328                             Pcre.regexp ("@" ^ key' ^ "@"), value
329                          )
330                          (List.filter
331                            (fun (key,_) as p-> Pcre.pmatch ~pat:"^param\\." key)
332                            req#params)
333                        )
334                        line) ^
335                     "\n"))
336                 fname
337             end else
338               Http_daemon.send_file ~src:(FileSrc fname) outchan)
339         | page -> Http_daemon.respond_forbidden ~url:page outchan))
340     | "/ask_uwobo" ->
341       let url = req#param "url" in
342       let server_and_port =
343         (Pcre.extract ~rex:server_and_port_url_RE url).(1)
344       in
345       if List.mem server_and_port valid_servers then
346         Http_daemon.respond
347           ~headers:["Content-Type", "text/html"]
348           ~body:(Http_client.http_get url)
349           outchan
350       else
351         Http_daemon.respond
352           ~body:(pp_error ("Untrusted UWOBO server: " ^ server_and_port))
353           outchan
354     | "/searchPattern"
355     | "/matchConclusion"
356     | "/locateInductivePrinciple" ->
357         let mqi_handle = C.init mqi_flags debug_print in
358         let term_string = req#param "term" in
359         let lexbuf = Lexing.from_string term_string in
360         let (context, metasenv) = ([], []) in
361         let (dom, mk_metasenv_and_expr) =
362           CicTextualParserContext.main
363             ~context ~metasenv CicTextualLexer.token lexbuf
364         in
365         let id_to_uris_raw = req#param "aliases" in
366         let tokens = Pcre.split ~pat:"\\s" id_to_uris_raw in
367         let rec parse_tokens keys lookup = function (* TODO spostarla fuori *)
368           | [] -> keys, lookup
369           | "alias" :: key :: value :: rest ->
370               let key' = CicTextualParser0.Id key in
371                parse_tokens
372                  (key'::keys)
373                  (fun id ->
374                    if id = key' then
375                      Some
376                       (CicTextualParser0.Uri (MQueryMisc.cic_textual_parser_uri_of_string value))
377                    else lookup id)
378                  rest
379           | _ -> failwith "Can't parse aliases"
380         in
381         let parse_choices choices_raw =
382           let choices = Pcre.split ~pat:";" choices_raw in
383           List.fold_left
384             (fun f x ->
385               match Pcre.split ~pat:"\\s" x with
386               | ""::id::tail
387               | id::tail when id<>"" ->
388                   (fun id' ->
389 prerr_endline ("#### " ^ id ^ " :=");
390 List.iter (fun u -> prerr_endline ("<" ^ Netencoding.Url.decode u ^ ">")) tail;
391                     if id = id' then
392                       Some (List.map (fun u -> Netencoding.Url.decode u) tail)
393                     else
394                       f id')
395               | _ -> failwith "Can't parse choices")
396             (fun _ -> None)
397             choices
398         in
399         let (id_to_uris : Disambiguate.domain_and_interpretation) =
400          parse_tokens [] (fun _ -> None) tokens in
401         let id_to_choices =
402           try
403             let choices_raw = req#param "choices" in
404             parse_choices choices_raw
405           with Http_types.Param_not_found _ -> (fun _ -> None)
406         in
407         let module Chat: Disambiguate.Callbacks =
408           struct
409
410             let get_metasenv () =
411              !CicTextualParser0.metasenv
412
413             let set_metasenv metasenv =
414               CicTextualParser0.metasenv := metasenv
415
416             let output_html ?(append_NL = true) html_msg =
417               let rec collect_string = function
418                 | `BR -> "\n"
419                 | `T s -> s
420                 | `L tags -> String.concat "" (List.map collect_string tags)
421               in
422               match html_msg with
423               | `Error msg | `Msg msg ->
424                   (if append_NL then prerr_endline else prerr_string)
425                     (collect_string msg ^ (if append_NL then "\n" else ""))
426
427             let interactive_user_uri_choice
428               ~selection_mode ?ok
429               ?enable_button_for_non_vars ~(title: string) ~(msg: string)
430               ~(id: string) (choices: string list)
431               =
432                 (match id_to_choices id with
433                 | Some choices -> choices
434                 | None ->
435                   let msg = Pcre.replace ~pat:"\'" ~templ:"\\\'" msg in
436                   (match selection_mode with
437                   | `SINGLE -> assert false
438                   | `MULTIPLE ->
439                       Http_daemon.send_basic_headers ~code:200 outchan ;
440                       Http_daemon.send_CRLF outchan ;
441                       iter_file
442                         (fun line ->
443                           let formatted_choices =
444                             String.concat ","
445                               (List.map (fun uri -> sprintf "\'%s\'" uri) choices)
446                           in
447                           let processed_line =
448                             apply_substs
449                               [title_tag_RE, title;
450                                choices_tag_RE, formatted_choices;
451                                msg_tag_RE, msg;
452                                id_to_uris_RE, id_to_uris_raw;
453                                id_RE, id]
454                               line
455                           in
456                           output_string outchan (processed_line ^ "\n"))
457                         interactive_user_uri_choice_TPL;
458                       raise Chat_unfinished))
459
460             let interactive_interpretation_choice interpretations =
461               let html_interpretations_labels =
462                 String.concat ", "
463                   (List.map
464                     (fun l ->
465                       "\'" ^
466                       (String.concat "<br />"
467                         (List.map
468                           (fun (id, value) ->
469                             (sprintf "alias %s %s" id value))
470                           l)) ^
471                       "\'")
472                   interpretations)
473               in
474               let html_interpretations =
475                 String.concat ", "
476                   (List.map
477                     (fun l ->
478                       "\'" ^
479                       (String.concat " "
480                         (List.map
481                           (fun (id, value) ->
482                             (sprintf "alias %s %s"
483                               id
484                               (MQueryMisc.wrong_xpointer_format_from_wrong_xpointer_format'
485                                 value)))
486                           l)) ^
487                       "\'")
488                     interpretations)
489               in
490               Http_daemon.send_basic_headers ~code:200 outchan ;
491               Http_daemon.send_CRLF outchan ;
492               iter_file
493                 (fun line ->
494                   let processed_line =
495                     apply_substs
496                       [interpretations_RE, html_interpretations;
497                        interpretations_labels_RE, html_interpretations_labels]
498                       line
499                   in
500                   output_string outchan (processed_line ^ "\n"))
501                 interactive_interpretation_choice_TPL;
502               raise Chat_unfinished
503
504             let input_or_locate_uri ~title =
505               UriManager.uri_of_string "cic:/Coq/Init/DataTypes/nat_ind.con"
506
507           end
508         in
509         let module Disambiguate' = Disambiguate.Make (Chat) in
510         let (id_to_uris', metasenv', term') =
511           Disambiguate'.disambiguate_input mqi_handle
512             context metasenv dom mk_metasenv_and_expr id_to_uris
513         in
514         (match metasenv' with
515         | [] ->
516             let universe,
517                 ((must_obj, must_rel, must_sort) as must'),
518                 ((only_obj, only_rel, only_sort) as only) =
519               get_constraints term' req#path
520             in
521             let must'', only' =
522               (try
523                 add_user_constraints
524                   ~constraints:(req#param "constraints")
525                   (must', only)
526               with Http_types.Param_not_found _ ->
527                 let variables =
528                  "var aliases = '" ^ id_to_uris_raw ^ "';\n" ^
529                  "var constr_obj_len = " ^
530                   string_of_int (List.length must_obj) ^ ";\n" ^
531                  "var constr_rel_len = " ^
532                   string_of_int (List.length must_rel) ^ ";\n" ^
533                  "var constr_sort_len = " ^
534                   string_of_int (List.length must_sort) ^ ";\n" in
535                 let form =
536                   (if must_obj = [] then "" else
537                     "<h4>Obj constraints</h4>" ^
538                     "<table>" ^
539                     (String.concat "\n" (List.map html_of_r_obj must_obj)) ^
540                     "</table>" ^
541                     (* The following three lines to make Javascript create *)
542                     (* the constr_obj[] and obj_depth[] arrays even if we  *)
543                     (* have only one real entry.                           *)
544                     "<input type=\"hidden\" name=\"constr_obj\" />" ^
545                     "<input type=\"hidden\" name=\"obj_depth\" />") ^
546                   (if must_rel = [] then "" else
547                    "<h4>Rel constraints</h4>" ^
548                    "<table>" ^
549                    (String.concat "\n" (List.map html_of_r_rel must_rel)) ^
550                    "</table>" ^
551                     (* The following two lines to make Javascript create *)
552                     (* the constr_rel[] and rel_depth[] arrays even if   *)
553                     (* we have only one real entry.                      *)
554                     "<input type=\"hidden\" name=\"constr_rel\" />" ^
555                     "<input type=\"hidden\" name=\"rel_depth\" />") ^
556                   (if must_sort = [] then "" else
557                     "<h4>Sort constraints</h4>" ^
558                     "<table>" ^
559                     (String.concat "\n" (List.map html_of_r_sort must_sort)) ^
560                     "</table>" ^
561                     (* The following two lines to make Javascript create *)
562                     (* the constr_sort[] and sort_depth[] arrays even if *)
563                     (* we have only one real entry.                      *)
564                     "<input type=\"hidden\" name=\"constr_sort\" />" ^
565                     "<input type=\"hidden\" name=\"sort_depth\" />") ^
566                     "<h4>Only constraints</h4>" ^
567                     "Enforce Only constraints for objects: " ^
568                       "<input type='checkbox' name='only_obj'" ^
569                       (if only_obj = None then "" else " checked='yes'") ^ " /><br />" ^
570                     "Enforce Rel constraints for objects: " ^
571                       "<input type='checkbox' name='only_rel'" ^
572                       (if only_rel = None then "" else " checked='yes'") ^ " /><br />" ^
573                     "Enforce Sort constraints for objects: " ^
574                       "<input type='checkbox' name='only_sort'" ^
575                       (if only_sort = None then "" else " checked='yes'") ^ " /><br />"
576                 in
577                 Http_daemon.send_basic_headers ~code:200 outchan ;
578                 Http_daemon.send_CRLF outchan ;
579                 iter_file
580                   (fun line ->
581                     let processed_line =
582                       apply_substs
583                        [form_RE, form ;
584                         variables_initialization_RE, variables] line
585                     in
586                     output_string outchan (processed_line ^ "\n"))
587                   constraints_choice_TPL;
588                   raise Chat_unfinished)
589             in
590             let query =
591              G.query_of_constraints universe must'' only'
592             in
593                   let results = MQueryInterpreter.execute mqi_handle query in 
594              Http_daemon.send_basic_headers ~code:200 outchan ;
595              Http_daemon.send_CRLF outchan ;
596              iter_file
597                (fun line ->
598                  let new_aliases =
599                    match id_to_uris' with
600                    | (domain, f) ->
601                        String.concat ", "
602                          (List.map
603                            (fun name ->
604                              sprintf "\'alias %s cic:%s\'"
605                                (match name with
606                                    CicTextualParser0.Id name -> name
607                                  | _ -> assert false (*CSC: completare *))
608                                (match f name with
609                                | None -> assert false
610                                | Some (CicTextualParser0.Uri t) ->
611                                    MQueryMisc.string_of_cic_textual_parser_uri
612                                      t
613                                | _ -> assert false (*CSC: completare *)))
614                            domain)
615                  in
616                  let processed_line =
617                    apply_substs
618                      [results_RE, theory_of_result results ;
619                       new_aliases_RE, new_aliases]
620                      line
621                  in
622                  output_string outchan (processed_line ^ "\n"))
623                final_results_TPL
624         | _ -> (* unable to instantiate some implicit variable *)
625             Http_daemon.respond
626               ~headers:[contype]
627               ~body:"some implicit variables are still unistantiated :-("
628               outchan);
629             C.close mqi_handle
630     | invalid_request ->
631         Http_daemon.respond_error ~status:(`Client_error `Bad_request) outchan);
632     debug_print (sprintf "%s done!" req#path)
633   with
634   | Chat_unfinished -> prerr_endline "Chat unfinished, Try again!"
635   | Http_types.Param_not_found attr_name ->
636       bad_request (sprintf "Parameter '%s' is missing" attr_name) outchan
637   | exc ->
638       Http_daemon.respond
639         ~body:(pp_error ("Uncaught exception: " ^ (Printexc.to_string exc)))
640         outchan
641 in
642 printf "%s started and listening on port %d\n" daemon_name port;
643 printf "Current directory is %s\n" (Sys.getcwd ());
644 printf "HTML directory is %s\n" pages_dir;
645 flush stdout;
646 Unix.putenv "http_proxy" "";
647 Http_daemon.start' ~port callback;
648 printf "%s is terminating, bye!\n" daemon_name
649