1 (* Copyright (C) 2002, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
29 let debug_print s = if debug then prerr_endline s;;
30 Http_common.debug := true;;
31 (* Http_common.debug := true;; *)
33 (** accepted HTTP servers for ask_uwobo method forwarding *)
34 let valid_servers = [ "mowgli.cs.unibo.it:58080" ; "mowgli.cs.unibo.it" ; "localhost:58080" ] ;;
38 let postgresConnectionString =
40 Sys.getenv "POSTGRESQL_CONNECTION_STRING"
42 Not_found -> "host=mowgli.cs.unibo.it dbname=helm_mowgli_new_schema user=helm"
45 let daemon_name = "Search Engine";;
46 let default_port = 58085;;
47 let port_env_var = "SEARCH_ENGINE_PORT";;
51 Sys.getenv "SEARCH_ENGINE_HTML_DIR"
52 with Not_found -> "html" (* relative to searchEngine's document root *)
54 let interactive_user_uri_choice_TPL = pages_dir ^ "/templateambigpdq1.html";;
55 let interactive_interpretation_choice_TPL = pages_dir ^ "/templateambigpdq2.html";;
56 let final_results_TPL = pages_dir ^ "/templateambigpdq3.html";;
58 exception Chat_unfinished
60 (** pretty print a MathQL query result to an HELM theory file *)
61 let theory_of_result result =
62 let results_no = List.length result in
63 if results_no > 0 then
64 let mode = if results_no > 10 then "linkonly" else "typeonly" in
66 let idx = ref (results_no + 1) in
70 "<tr><td valign=\"top\">" ^ string_of_int !idx ^ ".</td><td><ht:OBJECT uri=\"" ^ uri ^ "\" mode=\"" ^ mode ^ "\"/></td></tr>" ^ i
73 "<h1>Query Results:</h1><table xmlns:ht=\"http://www.cs.unibo.it/helm/namespaces/helm-theory\">" ^ results ^ "</table>"
75 "<h1>Query Results:</h1><p>No results found!</p>"
78 let pp_result result =
79 "<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>"
82 (** chain application of Pcre substitutions *)
83 let rec apply_substs substs line =
86 | (rex, templ) :: rest -> apply_substs rest (Pcre.replace ~rex ~templ line)
87 (** fold like function on files *)
88 let fold_file f init fname =
89 let inchan = open_in fname in
90 let rec fold_lines' value =
92 let line = input_line inchan in
93 fold_lines' (f value line)
94 with End_of_file -> value
96 let res = (try fold_lines' init with e -> (close_in inchan; raise e)) in
99 (** iter like function on files *)
100 let iter_file f = fold_file (fun _ line -> f line) ()
102 let (title_tag_RE, choices_tag_RE, msg_tag_RE, id_to_uris_RE, id_RE,
103 interpretations_RE, interpretations_labels_RE, results_RE, new_aliases_RE)
105 (Pcre.regexp "@TITLE@", Pcre.regexp "@CHOICES@", Pcre.regexp "@MSG@",
106 Pcre.regexp "@ID_TO_URIS@", Pcre.regexp "@ID@",
107 Pcre.regexp "@INTERPRETATIONS@", Pcre.regexp "@INTERPRETATIONS_LABELS@",
108 Pcre.regexp "@RESULTS@", Pcre.regexp "@NEW_ALIASES@")
109 let server_and_port_url_RE = Pcre.regexp "^http://([^/]+)/.*$"
113 int_of_string (Sys.getenv port_env_var)
115 | Not_found -> default_port
116 | Failure "int_of_string" ->
117 prerr_endline "Warning: invalid port, reverting to default";
120 let pp_error = sprintf "<html><body><h1>Error: %s</h1></body></html>" in
121 let bad_request body outchan =
122 Http_daemon.respond_error ~status:(`Client_error `Bad_request) ~body outchan
124 let contype = "Content-Type", "text/html" in
126 (* SEARCH ENGINE functions *)
128 let refine_constraints (x, y, z) = (x, y, z), (Some x, Some y, Some z) in
130 (* HTTP DAEMON CALLBACK *)
132 let callback (req: Http_types.request) outchan =
134 debug_print (sprintf "Received request: %s" req#path);
135 if req#path <> "/getpage" then
136 Mqint.init postgresConnectionString;
139 let query_string = req#param "query" in
140 let lexbuf = Lexing.from_string query_string in
141 let query = MQueryUtil.query_of_text lexbuf in
142 let result = MQueryGenerator.execute_query query in
143 let result_string = pp_result result in
144 Http_daemon.respond ~body:result_string ~headers:[contype] outchan
146 let id = req#param "id" in
147 let result = MQueryGenerator.locate id in
148 Http_daemon.respond ~headers:[contype] ~body:(pp_result result) outchan
150 (* TODO implement "is_permitted" *)
151 (let is_permitted _ = true in
152 let remove_fragment uri = Pcre.replace ~pat:"#.*" uri in
153 let page = remove_fragment (req#param "url") in
156 bool_of_string (req#param "preprocess")
157 with Invalid_argument _ | Http_types.Param_not_found _ -> false)
160 | page when is_permitted page ->
161 (let fname = sprintf "%s/%s" pages_dir (remove_fragment page) in
162 Http_daemon.send_basic_headers ~code:200 outchan;
163 Http_daemon.send_header "Content-Type" "text/html" outchan;
164 Http_daemon.send_CRLF outchan;
165 if preprocess then begin
168 output_string outchan
171 (function (key,value) ->
173 (Pcre.extract ~pat:"param\\.(.*)" key).(1)
175 Pcre.regexp ("@" ^ key' ^ "@"), value
178 (fun (key,_) as p-> Pcre.pmatch ~pat:"^param\\." key)
185 Http_daemon.send_file ~src:(FileSrc fname) outchan)
186 | page -> Http_daemon.respond_forbidden ~url:page outchan))
188 let url = req#param "url" in
189 let server_and_port =
190 (Pcre.extract ~rex:server_and_port_url_RE url).(1)
192 if List.mem server_and_port valid_servers then
194 ~headers:["Content-Type", "text/html"]
195 ~body:(Http_client.Convenience.http_get url)
199 ~body:(pp_error ("Untrusted UWOBO server: " ^ server_and_port))
201 | "/searchPattern" ->
202 let term_string = req#param "term" in
203 let lexbuf = Lexing.from_string term_string in
204 let (context, metasenv) = ([], []) in
205 let (dom, mk_metasenv_and_expr) =
206 CicTextualParserContext.main
207 ~context ~metasenv CicTextualLexer.token lexbuf
209 let id_to_uris_raw = req#param "aliases" in
210 let tokens = Pcre.split ~pat:"\\s" id_to_uris_raw in
211 let rec parse_tokens keys lookup = function (* TODO spostarla fuori *)
213 | "alias" :: key :: value :: rest ->
214 let key' = CicTextualParser0.Id key in
220 (CicTextualParser0.Uri (MQueryMisc.cic_textual_parser_uri_of_string value))
223 | _ -> failwith "Can't parse aliases"
225 let parse_choices choices_raw =
226 let choices = Pcre.split ~pat:";" choices_raw in
229 match Pcre.split ~pat:"\\s" x with
231 | id::tail when id<>"" ->
233 prerr_endline ("#### " ^ id ^ " :=");
234 List.iter (fun u -> prerr_endline ("<" ^ Netencoding.Url.decode u ^ ">")) tail;
236 Some (List.map (fun u -> Netencoding.Url.decode u) tail)
239 | _ -> failwith "Can't parse choices")
243 let (id_to_uris : Disambiguate.domain_and_interpretation) =
244 parse_tokens [] (fun _ -> None) tokens in
247 let choices_raw = req#param "choices" in
248 parse_choices choices_raw
249 with Http_types.Param_not_found _ -> (fun _ -> None)
251 let module Chat: Disambiguate.Callbacks =
254 let get_metasenv () =
255 !CicTextualParser0.metasenv
257 let set_metasenv metasenv =
258 CicTextualParser0.metasenv := metasenv
260 let output_html = prerr_endline
262 let interactive_user_uri_choice
264 ?enable_button_for_non_vars ~(title: string) ~(msg: string)
265 ~(id: string) (choices: string list)
267 (match id_to_choices id with
268 | Some choices -> choices
270 let msg = Pcre.replace ~pat:"\"" ~templ:"\\\"" msg in
271 (match selection_mode with
272 | `SINGLE -> assert false
274 Http_daemon.send_basic_headers ~code:200 outchan ;
275 Http_daemon.send_CRLF outchan ;
278 let formatted_choices =
280 (List.map (fun uri -> sprintf "\"%s\"" uri) choices)
284 [title_tag_RE, title;
285 choices_tag_RE, formatted_choices;
287 id_to_uris_RE, id_to_uris_raw;
291 output_string outchan (processed_line ^ "\n"))
292 interactive_user_uri_choice_TPL;
293 raise Chat_unfinished))
295 let interactive_interpretation_choice interpretations =
296 let html_interpretations_labels =
301 (String.concat "<br />"
304 (sprintf "alias %s %s" id value))
309 let html_interpretations =
317 (sprintf "alias %s %s"
319 (MQueryMisc.wrong_xpointer_format_from_wrong_xpointer_format'
325 Http_daemon.send_basic_headers ~code:200 outchan ;
326 Http_daemon.send_CRLF outchan ;
331 [interpretations_RE, html_interpretations;
332 interpretations_labels_RE, html_interpretations_labels]
335 output_string outchan (processed_line ^ "\n"))
336 interactive_interpretation_choice_TPL;
337 raise Chat_unfinished
339 let input_or_locate_uri ~title =
340 UriManager.uri_of_string "cic:/Coq/Init/DataTypes/nat_ind.con"
344 let module Disambiguate' = Disambiguate.Make (Chat) in
345 let (id_to_uris', metasenv', term') =
346 Disambiguate'.disambiguate_input
347 context metasenv dom mk_metasenv_and_expr id_to_uris
349 (match metasenv' with
351 let must = MQueryLevels2.get_constraints term' in
352 let must',only = refine_constraints must in
353 let results = MQueryGenerator.searchPattern must' only in
354 Http_daemon.send_basic_headers ~code:200 outchan ;
355 Http_daemon.send_CRLF outchan ;
359 match id_to_uris' with
364 sprintf "\"alias %s cic:%s\""
366 CicTextualParser0.Id name -> name
367 | _ -> assert false (*CSC: completare *))
369 | None -> assert false
370 | Some (CicTextualParser0.Uri t) ->
371 MQueryMisc.string_of_cic_textual_parser_uri
373 | _ -> assert false (*CSC: completare *)))
378 [results_RE, theory_of_result results ;
379 new_aliases_RE, new_aliases]
382 output_string outchan (processed_line ^ "\n"))
384 | _ -> (* unable to instantiate some implicit variable *)
387 ~body:"some implicit variables are still unistantiated :-("
391 Http_daemon.respond_error ~status:(`Client_error `Bad_request) outchan);
392 if req#path <> "/getpage" then
394 debug_print (sprintf "%s done!" req#path)
396 | Chat_unfinished -> prerr_endline "Chat unfinished, Try again!"
397 | Http_types.Param_not_found attr_name ->
398 bad_request (sprintf "Parameter '%s' is missing" attr_name) outchan
401 ~body:(pp_error ("Uncaught exception: " ^ (Printexc.to_string exc)))
404 printf "%s started and listening on port %d\n" daemon_name port;
405 printf "Current directory is %s\n" (Sys.getcwd ());
406 printf "HTML directory is %s\n" pages_dir;
408 Unix.putenv "http_proxy" "";
409 Mqint.set_database Mqint.postgres_db;
410 Http_daemon.start' ~port callback;
411 printf "%s is terminating, bye!\n" daemon_name