]> matita.cs.unibo.it Git - helm.git/blob - helm/searchEngine/searchEngine.ml
- the mathql interpreter is not helm-dependent any more
[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 exception NotAnInductiveDefinition
159
160 let port =
161   try
162     int_of_string (Sys.getenv port_env_var)
163   with
164   | Not_found -> default_port
165   | Failure "int_of_string" ->
166       prerr_endline "Warning: invalid port, reverting to default";
167       default_port
168 ;;
169
170 let pp_error = sprintf "<html><body><h1>Error: %s</h1></body></html>";;
171
172 let bad_request body outchan =
173   Http_daemon.respond_error ~status:(`Client_error `Bad_request) ~body outchan
174 ;;
175
176 let contype = "Content-Type", "text/html";;
177
178 (* SEARCH ENGINE functions *)
179
180 let get_constraints term =
181  function
182     | "/locateInductivePrinciple" ->
183       let uri = 
184        match term with
185           Cic.MutInd (uri,t,_) -> MQueryUtil.string_of_uriref (uri,[t])
186         | _ -> raise NotAnInductiveDefinition
187       in
188       let constr_obj =
189        [(`InHypothesis, uri); (`MainHypothesis (Some 0), uri)]
190       in
191       let constr_rel = [`MainConclusion None] in
192       let constr_sort = [(`MainHypothesis (Some 1), T.Prop)] in
193        U.universe_for_search_pattern,
194         (constr_obj, constr_rel, constr_sort), (None,None,None)
195     | "/searchPattern" ->
196      let constr_obj, constr_rel, constr_sort =
197        CGSearchPattern.get_constraints term in
198       U.universe_for_search_pattern,
199        (constr_obj, constr_rel, constr_sort),
200        (Some constr_obj, Some constr_rel, Some constr_sort)
201     | "/matchConclusion" ->
202      let list_of_must, only = CGMatchConclusion.get_constraints [] [] term in
203 (* FG: there is no way to choose the block number ***************************)
204      let block = pred (List.length list_of_must) in 
205       U.universe_for_match_conclusion, 
206       (List.nth list_of_must block, [], []), (Some only, None, None)
207     | _ -> assert false
208 ;;
209
210 (*
211   format:
212     <must_obj> ':' <must_rel> ':' <must_sort> ':' <only_obj> ':' <only_rel> ':' <only_sort>
213
214     <must_*> ::= ('0'|'1') ('_'|<int>) (',' ('0'|'1') ('_'|<int>))*
215     <only> ::= '0'|'1'
216 *)
217 let add_user_constraints ~constraints
218  ((obj, rel, sort), (only_obj, only_rel, only_sort))
219 =
220   let parse_must s =
221     let l = Pcre.split ~pat:"," s in
222     (try
223       List.map
224         (fun s ->
225           let subs = Pcre.extract ~pat:"^(.)(\\d+|_)$" s in
226           (bool_of_string' subs.(1), int_of_string' subs.(2)))
227         l
228      with
229       Not_found -> failwith ("Can't parse constraint string: " ^ constraints)
230     )
231   in
232     (* to be used on "obj" *)
233   let add_user_must33 user_must must =
234     List.map2
235      (fun (b, i) (p, u) ->
236        if b then Some (U.set_full_position p i, u) else None)
237      user_must must
238   in
239     (* to be used on "rel" *)
240   let add_user_must22 user_must must =
241     List.map2
242      (fun (b, i) p -> if b then Some (U.set_main_position p i) else None)
243      user_must must
244   in
245     (* to be used on "sort" *)
246   let add_user_must32 user_must must =
247     List.map2
248      (fun (b, i) (p, s)-> if b then Some (U.set_main_position p i, s) else None)
249      user_must must
250   in
251   match Pcre.split ~pat:":" constraints with
252   | [user_obj;user_rel;user_sort;user_only_obj;user_only_rel;user_only_sort] ->
253       let
254        (user_obj,user_rel,user_sort,user_only_obj,user_only_rel,user_only_sort)
255       =
256         (parse_must user_obj,
257         parse_must user_rel,
258         parse_must user_sort,
259         bool_of_string' user_only_obj,
260         bool_of_string' user_only_rel,
261         bool_of_string' user_only_sort)
262       in
263       let only' =
264        (if user_only_obj  then only_obj else None),
265        (if user_only_rel  then only_rel else None),
266        (if user_only_sort then only_sort else None)
267       in
268       let must' =
269        let rec filter_some =
270         function
271            [] -> []
272          | None::tl -> filter_some tl
273          | (Some x)::tl -> x::(filter_some tl) 
274        in
275         filter_some (add_user_must33 user_obj obj),
276         filter_some (add_user_must22 user_rel rel),
277         filter_some (add_user_must32 user_sort sort)
278       in
279       (must', only')
280   | _ -> failwith ("Can't parse constraint string: " ^ constraints)
281 in
282
283 (* HTTP DAEMON CALLBACK *)
284
285 let callback (req: Http_types.request) outchan =
286   try
287     debug_print (sprintf "Received request: %s" req#path);
288     (match req#path with
289     | "/execute" ->
290         let mqi_handle = C.init mqi_flags debug_print in 
291         let query_string = req#param "query" in
292         let lexbuf = Lexing.from_string query_string in
293         let query = MQueryUtil.query_of_text lexbuf in
294         let result = MQueryInterpreter.execute mqi_handle query in
295         let result_string = pp_result result in
296               C.close mqi_handle;
297         Http_daemon.respond ~body:result_string ~headers:[contype] outchan
298     | "/locate" ->
299         let mqi_handle = C.init mqi_flags debug_print in
300         let id = req#param "id" in
301         let query = G.locate id in
302         let result = MQueryInterpreter.execute mqi_handle query in
303               C.close mqi_handle;
304         Http_daemon.respond ~headers:[contype] ~body:(pp_result result) outchan
305     | "/unreferred" ->
306         let mqi_handle = C.init mqi_flags debug_print in
307         let target = req#param "target" in
308         let source = req#param "source" in
309         let query = G.unreferred target source in
310         let result = MQueryInterpreter.execute mqi_handle query in
311               C.close mqi_handle;
312         Http_daemon.respond ~headers:[contype] ~body:(pp_result result) outchan
313     | "/getpage" ->
314         (* TODO implement "is_permitted" *)
315         (let is_permitted _ = true in
316         let remove_fragment uri = Pcre.replace ~pat:"#.*" uri in
317         let page = remove_fragment (req#param "url") in
318         let preprocess =
319           (try
320             bool_of_string (req#param "preprocess")
321           with Invalid_argument _ | Http_types.Param_not_found _ -> false)
322         in
323         (match page with
324         | page when is_permitted page ->
325             (let fname = sprintf "%s/%s" pages_dir (remove_fragment page) in
326             Http_daemon.send_basic_headers ~code:200 outchan;
327             Http_daemon.send_header "Content-Type" "text/html" outchan;
328             Http_daemon.send_CRLF outchan;
329             if preprocess then begin
330               iter_file
331                 (fun line ->
332                   output_string outchan
333                     ((apply_substs
334                        (List.map
335                          (function (key,value) ->
336                            let key' =
337                             (Pcre.extract ~pat:"param\\.(.*)" key).(1)
338                            in
339                             Pcre.regexp ("@" ^ key' ^ "@"), value
340                          )
341                          (List.filter
342                            (fun (key,_) as p-> Pcre.pmatch ~pat:"^param\\." key)
343                            req#params)
344                        )
345                        line) ^
346                     "\n"))
347                 fname
348             end else
349               Http_daemon.send_file ~src:(FileSrc fname) outchan)
350         | page -> Http_daemon.respond_forbidden ~url:page outchan))
351     | "/ask_uwobo" ->
352       let url = req#param "url" in
353       let server_and_port =
354         (Pcre.extract ~rex:server_and_port_url_RE url).(1)
355       in
356       if List.mem server_and_port valid_servers then
357         Http_daemon.respond
358           ~headers:["Content-Type", "text/html"]
359           ~body:(Http_client.Convenience.http_get url)
360           outchan
361       else
362         Http_daemon.respond
363           ~body:(pp_error ("Untrusted UWOBO server: " ^ server_and_port))
364           outchan
365     | "/searchPattern"
366     | "/matchConclusion"
367     | "/locateInductivePrinciple" ->
368         let mqi_handle = C.init mqi_flags debug_print in
369         let term_string = req#param "term" in
370         let lexbuf = Lexing.from_string term_string in
371         let (context, metasenv) = ([], []) in
372         let (dom, mk_metasenv_and_expr) =
373           CicTextualParserContext.main
374             ~context ~metasenv CicTextualLexer.token lexbuf
375         in
376         let id_to_uris_raw = req#param "aliases" in
377         let tokens = Pcre.split ~pat:"\\s" id_to_uris_raw in
378         let rec parse_tokens keys lookup = function (* TODO spostarla fuori *)
379           | [] -> keys, lookup
380           | "alias" :: key :: value :: rest ->
381               let key' = CicTextualParser0.Id key in
382                parse_tokens
383                  (key'::keys)
384                  (fun id ->
385                    if id = key' then
386                      Some
387                       (CicTextualParser0.Uri (MQueryMisc.cic_textual_parser_uri_of_string value))
388                    else lookup id)
389                  rest
390           | _ -> failwith "Can't parse aliases"
391         in
392         let parse_choices choices_raw =
393           let choices = Pcre.split ~pat:";" choices_raw in
394           List.fold_left
395             (fun f x ->
396               match Pcre.split ~pat:"\\s" x with
397               | ""::id::tail
398               | id::tail when id<>"" ->
399                   (fun id' ->
400 prerr_endline ("#### " ^ id ^ " :=");
401 List.iter (fun u -> prerr_endline ("<" ^ Netencoding.Url.decode u ^ ">")) tail;
402                     if id = id' then
403                       Some (List.map (fun u -> Netencoding.Url.decode u) tail)
404                     else
405                       f id')
406               | _ -> failwith "Can't parse choices")
407             (fun _ -> None)
408             choices
409         in
410         let (id_to_uris : Disambiguate.domain_and_interpretation) =
411          parse_tokens [] (fun _ -> None) tokens in
412         let id_to_choices =
413           try
414             let choices_raw = req#param "choices" in
415             parse_choices choices_raw
416           with Http_types.Param_not_found _ -> (fun _ -> None)
417         in
418         let module Chat: Disambiguate.Callbacks =
419           struct
420
421             let get_metasenv () =
422              !CicTextualParser0.metasenv
423
424             let set_metasenv metasenv =
425               CicTextualParser0.metasenv := metasenv
426
427             let output_html = prerr_endline
428
429             let interactive_user_uri_choice
430               ~selection_mode ?ok
431               ?enable_button_for_non_vars ~(title: string) ~(msg: string)
432               ~(id: string) (choices: string list)
433               =
434                 (match id_to_choices id with
435                 | Some choices -> choices
436                 | None ->
437                   let msg = Pcre.replace ~pat:"\'" ~templ:"\\\'" msg in
438                   (match selection_mode with
439                   | `SINGLE -> assert false
440                   | `EXTENDED ->
441                       Http_daemon.send_basic_headers ~code:200 outchan ;
442                       Http_daemon.send_CRLF outchan ;
443                       iter_file
444                         (fun line ->
445                           let formatted_choices =
446                             String.concat ","
447                               (List.map (fun uri -> sprintf "\'%s\'" uri) choices)
448                           in
449                           let processed_line =
450                             apply_substs
451                               [title_tag_RE, title;
452                                choices_tag_RE, formatted_choices;
453                                msg_tag_RE, msg;
454                                id_to_uris_RE, id_to_uris_raw;
455                                id_RE, id]
456                               line
457                           in
458                           output_string outchan (processed_line ^ "\n"))
459                         interactive_user_uri_choice_TPL;
460                       raise Chat_unfinished))
461
462             let interactive_interpretation_choice interpretations =
463               let html_interpretations_labels =
464                 String.concat ", "
465                   (List.map
466                     (fun l ->
467                       "\'" ^
468                       (String.concat "<br />"
469                         (List.map
470                           (fun (id, value) ->
471                             (sprintf "alias %s %s" id value))
472                           l)) ^
473                       "\'")
474                   interpretations)
475               in
476               let html_interpretations =
477                 String.concat ", "
478                   (List.map
479                     (fun l ->
480                       "\'" ^
481                       (String.concat " "
482                         (List.map
483                           (fun (id, value) ->
484                             (sprintf "alias %s %s"
485                               id
486                               (MQueryMisc.wrong_xpointer_format_from_wrong_xpointer_format'
487                                 value)))
488                           l)) ^
489                       "\'")
490                     interpretations)
491               in
492               Http_daemon.send_basic_headers ~code:200 outchan ;
493               Http_daemon.send_CRLF outchan ;
494               iter_file
495                 (fun line ->
496                   let processed_line =
497                     apply_substs
498                       [interpretations_RE, html_interpretations;
499                        interpretations_labels_RE, html_interpretations_labels]
500                       line
501                   in
502                   output_string outchan (processed_line ^ "\n"))
503                 interactive_interpretation_choice_TPL;
504               raise Chat_unfinished
505
506             let input_or_locate_uri ~title =
507               UriManager.uri_of_string "cic:/Coq/Init/DataTypes/nat_ind.con"
508
509           end
510         in
511         let module Disambiguate' = Disambiguate.Make (Chat) in
512         let (id_to_uris', metasenv', term') =
513           Disambiguate'.disambiguate_input mqi_handle
514             context metasenv dom mk_metasenv_and_expr id_to_uris
515         in
516         (match metasenv' with
517         | [] ->
518             let universe,
519                 ((must_obj, must_rel, must_sort) as must'),
520                 ((only_obj, only_rel, only_sort) as only) =
521               get_constraints term' req#path
522             in
523             let must'', only' =
524               (try
525                 add_user_constraints
526                   ~constraints:(req#param "constraints")
527                   (must', only)
528               with Http_types.Param_not_found _ ->
529                 let variables =
530                  "var aliases = '" ^ id_to_uris_raw ^ "';\n" ^
531                  "var constr_obj_len = " ^
532                   string_of_int (List.length must_obj) ^ ";\n" ^
533                  "var constr_rel_len = " ^
534                   string_of_int (List.length must_rel) ^ ";\n" ^
535                  "var constr_sort_len = " ^
536                   string_of_int (List.length must_sort) ^ ";\n" in
537                 let form =
538                   (if must_obj = [] then "" else
539                     "<h4>Obj constraints</h4>" ^
540                     "<table>" ^
541                     (String.concat "\n" (List.map html_of_r_obj must_obj)) ^
542                     "</table>" ^
543                     (* The following three lines to make Javascript create *)
544                     (* the constr_obj[] and obj_depth[] arrays even if we  *)
545                     (* have only one real entry.                           *)
546                     "<input type=\"hidden\" name=\"constr_obj\" />" ^
547                     "<input type=\"hidden\" name=\"obj_depth\" />") ^
548                   (if must_rel = [] then "" else
549                    "<h4>Rel constraints</h4>" ^
550                    "<table>" ^
551                    (String.concat "\n" (List.map html_of_r_rel must_rel)) ^
552                    "</table>" ^
553                     (* The following two lines to make Javascript create *)
554                     (* the constr_rel[] and rel_depth[] arrays even if   *)
555                     (* we have only one real entry.                      *)
556                     "<input type=\"hidden\" name=\"constr_rel\" />" ^
557                     "<input type=\"hidden\" name=\"rel_depth\" />") ^
558                   (if must_sort = [] then "" else
559                     "<h4>Sort constraints</h4>" ^
560                     "<table>" ^
561                     (String.concat "\n" (List.map html_of_r_sort must_sort)) ^
562                     "</table>" ^
563                     (* The following two lines to make Javascript create *)
564                     (* the constr_sort[] and sort_depth[] arrays even if *)
565                     (* we have only one real entry.                      *)
566                     "<input type=\"hidden\" name=\"constr_sort\" />" ^
567                     "<input type=\"hidden\" name=\"sort_depth\" />") ^
568                     "<h4>Only constraints</h4>" ^
569                     "Enforce Only constraints for objects: " ^
570                       "<input type='checkbox' name='only_obj'" ^
571                       (if only_obj = None then "" else " checked='yes'") ^ " /><br />" ^
572                     "Enforce Rel constraints for objects: " ^
573                       "<input type='checkbox' name='only_rel'" ^
574                       (if only_rel = None then "" else " checked='yes'") ^ " /><br />" ^
575                     "Enforce Sort constraints for objects: " ^
576                       "<input type='checkbox' name='only_sort'" ^
577                       (if only_sort = None then "" else " checked='yes'") ^ " /><br />"
578                 in
579                 Http_daemon.send_basic_headers ~code:200 outchan ;
580                 Http_daemon.send_CRLF outchan ;
581                 iter_file
582                   (fun line ->
583                     let processed_line =
584                       apply_substs
585                        [form_RE, form ;
586                         variables_initialization_RE, variables] line
587                     in
588                     output_string outchan (processed_line ^ "\n"))
589                   constraints_choice_TPL;
590                   raise Chat_unfinished)
591             in
592             let query =
593              G.query_of_constraints (Some universe) must'' only'
594             in
595                   let results = MQueryInterpreter.execute mqi_handle query in 
596              Http_daemon.send_basic_headers ~code:200 outchan ;
597              Http_daemon.send_CRLF outchan ;
598              iter_file
599                (fun line ->
600                  let new_aliases =
601                    match id_to_uris' with
602                    | (domain, f) ->
603                        String.concat ", "
604                          (List.map
605                            (fun name ->
606                              sprintf "\'alias %s cic:%s\'"
607                                (match name with
608                                    CicTextualParser0.Id name -> name
609                                  | _ -> assert false (*CSC: completare *))
610                                (match f name with
611                                | None -> assert false
612                                | Some (CicTextualParser0.Uri t) ->
613                                    MQueryMisc.string_of_cic_textual_parser_uri
614                                      t
615                                | _ -> assert false (*CSC: completare *)))
616                            domain)
617                  in
618                  let processed_line =
619                    apply_substs
620                      [results_RE, theory_of_result results ;
621                       new_aliases_RE, new_aliases]
622                      line
623                  in
624                  output_string outchan (processed_line ^ "\n"))
625                final_results_TPL
626         | _ -> (* unable to instantiate some implicit variable *)
627             Http_daemon.respond
628               ~headers:[contype]
629               ~body:"some implicit variables are still unistantiated :-("
630               outchan);
631             C.close mqi_handle
632     | invalid_request ->
633         Http_daemon.respond_error ~status:(`Client_error `Bad_request) outchan);
634     debug_print (sprintf "%s done!" req#path)
635   with
636   | Chat_unfinished -> prerr_endline "Chat unfinished, Try again!"
637   | Http_types.Param_not_found attr_name ->
638       bad_request (sprintf "Parameter '%s' is missing" attr_name) outchan
639   | exc ->
640       Http_daemon.respond
641         ~body:(pp_error ("Uncaught exception: " ^ (Printexc.to_string exc)))
642         outchan
643 in
644 printf "%s started and listening on port %d\n" daemon_name port;
645 printf "Current directory is %s\n" (Sys.getcwd ());
646 printf "HTML directory is %s\n" pages_dir;
647 flush stdout;
648 Unix.putenv "http_proxy" "";
649 Http_daemon.start' ~port callback;
650 printf "%s is terminating, bye!\n" daemon_name
651