-(* Copyright (C) 2002, HELM Team.
+(* Copyright (C) 2002-2004, HELM Team.
*
* This file is part of HELM, an Hypertextual, Electronic
* Library of Mathematics, developed at the Computer Science
module G = MQueryGenerator
module C = MQIConn
-open Http_types ;;
+open Http_types
-let debug = true;;
-let debug_print s = if debug then prerr_endline s;;
-Http_common.debug := true;;
-(* Http_common.debug := true;; *)
+let debug = true
+let debug_print s = if debug then prerr_endline s
+let _ = Http_common.debug := true
+(* let _ = Http_common.debug := false *)
-open Printf;;
+open Printf
-let daemon_name = "Search Engine";;
+let daemon_name = "Search Engine"
let string_tail s =
let len = String.length s in
let _ =
let configuration_file = "/projects/helm/etc/searchEngine.conf.xml" in
Helm_registry.load_from configuration_file
-;;
-let port = Helm_registry.get_int "search_engine.port";;
+let port = Helm_registry.get_int "search_engine.port"
-let pages_dir = Helm_registry.get "search_engine.html_dir";;
+let pages_dir = Helm_registry.get "search_engine.html_dir"
(** accepted HTTP servers for ask_uwobo method forwarding *)
-let valid_servers= Helm_registry.get_string_list "search_engine.valid_servers";;
+let valid_servers= Helm_registry.get_string_list "search_engine.valid_servers"
-let interactive_user_uri_choice_TPL = pages_dir ^ "/moogle_chat1.html";;
-let interactive_interpretation_choice_TPL = pages_dir ^ "/moogle_chat2.html";;
-let constraints_choice_TPL = pages_dir ^ "/moogle_constraints_choice.html";;
-(* let final_results_TPL = pages_dir ^ "/templateambigpdq3.html";; *)
-let start_TPL = pages_dir ^ "/moogle.html";;
-let final_results_TPL = pages_dir ^ "/moogle.html";;
+let interactive_user_uri_choice_TPL = pages_dir ^ "/moogle_chat1.html"
+let interactive_interpretation_choice_TPL = pages_dir ^ "/moogle_chat2.html"
+let constraints_choice_TPL = pages_dir ^ "/moogle_constraints_choice.html"
+let moogle_TPL = pages_dir ^ "/moogle.html"
let my_own_url =
let ic = Unix.open_process_in "hostname -f" in
let hostname = input_line ic in
ignore (Unix.close_process_in ic);
sprintf "http://%s:%d" hostname port
-;;
exception Chat_unfinished
exception Invalid_action of string (* invalid action for "/search" method *)
+exception Unbound_identifier of string
let javascript_quote s =
let rex = Pcre.regexp "'" in
let rex' = Pcre.regexp "\"" in
Pcre.replace ~rex ~templ:"\\'"
(Pcre.replace ~rex:rex' ~templ:"\\\"" s)
-;;
(* build a bool from a 1-character-string *)
let bool_of_string' = function
| "0" -> false
| "1" -> true
| s -> failwith ("Can't parse a boolean from string: " ^ s)
-;;
(* build an int option from a string *)
let int_of_string' = function
Some (int_of_string s)
with Failure "int_of_string" ->
failwith ("Can't parse an int option from string: " ^ s)
-;;
(* HTML pretty printers for mquery_generator types *)
(U.text_of_depth pos "")
else
"<input type=\"hidden\" name=\"obj_depth\" />")
-;;
let html_of_r_rel pos =
sprintf
"<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>"
(U.text_of_position (pos:>T.full_position)) (U.text_of_depth (pos:>T.full_position) "")
-;;
let html_of_r_sort (pos, sort) =
sprintf
"<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>"
(U.text_of_sort sort) (U.text_of_position (pos:>T.full_position)) (U.text_of_depth (pos:>T.full_position) "")
-;;
+
+let query_of_req (req: Http_types.request) =
+ match req#path with
+ | "/elim" -> "Elim"
+ | "/match" -> "Match"
+ | "/hint" -> "Hint"
+ | "/locate" -> "Locate"
+ | _ -> assert false
(** pretty print a MathQL query result to an HELM theory file *)
-let theory_of_result result =
+let theory_of_result req result =
let results_no = List.length result in
if results_no > 0 then
let mode = if results_no > 10 then "linkonly" else "typeonly" in
List.fold_right
(fun uri i ->
decr idx ;
- "<tr><td valign=\"top\">" ^ string_of_int !idx ^ ".</td><td><ht:OBJECT uri=\"" ^ uri ^ "\" mode=\"" ^ mode ^ "\"/></td></tr>" ^ i
- ) result ""
+ sprintf
+ "<tr>
+ <td valign=\"top\">%d.</td>
+ <td><ht:OBJECT uri=\"%s\" mode=\"%s\"/></td>
+ </tr>%s"
+ !idx uri mode i)
+ result ""
in
- "<b><font size=\"+1\">Query Results:</font></b><table xmlns:ht=\"http://www.cs.unibo.it/helm/namespaces/helm-theory\">" ^ results ^ "</table>"
+ sprintf
+ "<div class='resultsbar'>
+ <table width='100%%'>
+ <tr>
+ <td class='left'><b class='query_kind'>%s</b></td>
+ <td class='right'><b>%d</b> result%s found</td>
+ </tr>
+ </table>
+ </div>
+ <br />
+ <div>
+ <table xmlns:ht=\"http://www.cs.unibo.it/helm/namespaces/helm-theory\">%s</table>
+ </div>"
+ (query_of_req req)
+ results_no (if results_no > 1 then "s" else "") results
else
- "<b><font size=\"+1\">Query Results:</font></b><p>No results found!</p>"
-;;
-
-let pp_result result =
- "<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>"
-;;
+ "<div class='resultsbar'>no results found</div>"
+
+let pp_result req result =
+ sprintf
+ "<html xmlns:ht=\"http://www.cs.unibo.it/helm/namespaces/helm-theory\">
+ <head>
+ <title>Query results</title>
+ <style> A { text-decoration: none } </style>
+ </head>
+ <body>%s</body>
+ </html>"
+ (theory_of_result req result)
(** chain application of Pcre substitutions *)
let rec apply_substs substs line =
(** iter like function on files *)
let iter_file f = fold_file (fun _ line -> f line) ()
-let (expression_tag_RE,
- action_tag_RE,
- advanced_tag_RE,
+let (expression_tag_RE, action_tag_RE, advanced_tag_RE,
+ advanced_checked_RE, simple_checked_RE,
title_tag_RE, no_choices_tag_RE, current_choices_tag_RE,
choices_tag_RE, msg_tag_RE, id_to_uris_RE, id_RE, iden_tag_RE,
interpretations_RE, interpretations_labels_RE, results_RE, new_aliases_RE,
form_RE, variables_initialization_RE, search_engine_url_RE)
=
- (Pcre.regexp "@EXPRESSION@",
- Pcre.regexp "@ACTION@",
- Pcre.regexp "@ADVANCED@",
+ (Pcre.regexp "@EXPRESSION@", Pcre.regexp "@ACTION@", Pcre.regexp "@ADVANCED@",
+ Pcre.regexp "@ADVANCED_CHECKED@", Pcre.regexp "@SIMPLE_CHECKED@",
Pcre.regexp "@TITLE@", Pcre.regexp "@NO_CHOICES@",
Pcre.regexp "@CURRENT_CHOICES@",
Pcre.regexp "@CHOICES@", Pcre.regexp "@MSG@",
Pcre.regexp "@VARIABLES_INITIALIZATION@", Pcre.regexp "@SEARCH_ENGINE_URL@")
let server_and_port_url_RE = Pcre.regexp "^http://([^/]+)/.*$"
-let pp_error = sprintf "<html><body><h1>Error: %s</h1></body></html>";;
+(* let pp_error = sprintf "<html><body><h1>Error: %s</h1></body></html>" *)
+let pp_error title msg =
+ sprintf "<hr size='1' /><div><b class='error'>%s:</b> %s</div>" title msg
let bad_request body outchan =
Http_daemon.respond_error ~code:(`Status (`Client_error `Bad_request)) ~body
outchan
-;;
-let contype = "Content-Type", "text/html";;
+let contype = "Content-Type", "text/html"
(* SEARCH ENGINE functions *)
(Some CGMatchConclusion.universe),
(List.nth list_of_must block, [], []), (Some only, None, None)
| _ -> assert false
-;;
(*
format:
in
(must', only')
| _ -> failwith ("Can't parse constraint string: " ^ constraints)
-in
let send_results results
?(id_to_uris = DisambiguatingParser.EnvironmentP3.of_string "")
Http_daemon.send_basic_headers ~code:(`Code 200) outchan ;
Http_daemon.send_header "Content-Type" "text/xml" outchan;
Http_daemon.send_CRLF outchan ;
+ let results_string =
+ match results with
+ | `Results r -> theory_of_result req r
+ | `Error msg -> msg
+ in
let subst =
(search_engine_url_RE, my_own_url) ::
- (results_RE, theory_of_result results)::
+ (results_RE, results_string)::
(advanced_tag_RE, req#param "advanced")::
(expression_tag_RE, req#param "expression")::
(List.map
let key' = (Pcre.extract ~pat:"param\\.(.*)" key).(1) in
Pcre.regexp ("@" ^ key' ^ "@"), value)
(List.filter
- (fun (key,_) as p-> Pcre.pmatch ~pat:"^param\\." key)
- req#params))
- in
- iter_file
- (fun line ->
- let new_aliases =
- DisambiguatingParser.EnvironmentP3.to_string id_to_uris
- in
- let processed_line =
- apply_substs
- (* CSC: Bug here: this is a string, not an array! *)
- ((new_aliases_RE, "'" ^ javascript_quote new_aliases ^ "'")::subst)
- line
- in
- output_string outchan (processed_line ^ "\n"))
- final_results_TPL
-in
+ (fun (key,_) as p-> Pcre.pmatch ~pat:"^param\\." key)
+ req#params)) @
+ (if req#param "advanced" = "no" then
+ [ simple_checked_RE, "checked='true'";
+ advanced_checked_RE, "" ]
+ else
+ [ simple_checked_RE, "";
+ advanced_checked_RE, "checked='true'" ])
+ in
+ iter_file
+ (fun line ->
+ let new_aliases =
+ DisambiguatingParser.EnvironmentP3.to_string id_to_uris
+ in
+ let processed_line =
+ apply_substs
+ (* CSC: Bug here: this is a string, not an array! *)
+ ((new_aliases_RE, "'" ^ javascript_quote new_aliases ^ "'")::subst)
+ line
+ in
+ output_string outchan (processed_line ^ "\n"))
+ moogle_TPL
let exec_action mqi_handle (req: Http_types.request) outchan =
let term_string = req#param "expression" in
raise Chat_unfinished
let input_or_locate_uri ~title ?id () =
- assert false
+ match id with
+ | Some id -> raise (Unbound_identifier id)
+ | None -> assert false
end
in
| _ -> assert false
in
let results = List.map snd (Match_concl.cmatch dbd term') in
- send_results results ~id_to_uris:id_to_uris' req outchan
+ send_results (`Results results) ~id_to_uris:id_to_uris' req outchan
else
let must'', only' =
(try
G.query_of_constraints universe must'' only'
in
let results = MQueryInterpreter.execute mqi_handle query in
- send_results (List.map fst results) ~id_to_uris:id_to_uris' req outchan
-in
+ send_results (`Results (List.map fst results))
+ ~id_to_uris:id_to_uris' req outchan
(* HTTP DAEMON CALLBACK *)
let p =
String.concat "&" (List.map (fun (key,value) -> (key ^ "=" ^ (Netencoding.Url.encode value))) params) in
url ^ "?" ^ p
-in
let build_uwobo_request (req: Http_types.request) outchan =
prerr_endline ("ECCOLO: " ^ req#param "param.SEARCH_ENGINE_URL");
outchan
else
Http_daemon.respond
- ~body:(pp_error ("Untrusted UWOBO server: " ^ server_and_port ^
- (String.concat "\n" valid_servers)))
+ ~body:(pp_error "Untrusted UWOBO server" server_and_port)
outchan
-in
let proxy url outchan =
let server_and_port =
outchan
else
Http_daemon.respond
- ~body:(pp_error ("Untrusted UWOBO server: " ^ server_and_port))
+ ~body:(pp_error "Untrusted UWOBO server" server_and_port)
outchan
-in
let callback mqi_handle (req: Http_types.request) outchan =
try
(Pcre.replace ~pat:"^\\s*" initial_expression)
in
if expression = "" then
- send_results [] req outchan
+ send_results (`Results []) req outchan
else
let results =
let query = G.locate expression in
MQueryInterpreter.execute mqi_handle query
in
- send_results (List.map fst results) req outchan
+ send_results (`Results (List.map fst results)) req outchan
| "/execute" ->
let query_string = req#param "query" in
let lexbuf = Lexing.from_string query_string in
let query = MQueryUtil.query_of_text lexbuf in
let result = MQueryInterpreter.execute mqi_handle query in
- let result_string = pp_result (List.map fst result) in
+ let result_string = pp_result req (List.map fst result) in
Http_daemon.respond ~body:result_string ~headers:[contype] outchan
-(* Http_daemon.respond ~headers:[contype] ~body:(pp_result result) outchan *)
+(* Http_daemon.respond ~headers:[contype] ~body:(pp_result req result) outchan *)
| "/unreferred" ->
let target = req#param "target" in
let source = req#param "source" in
let query = G.unreferred target source in
let result = MQueryInterpreter.execute mqi_handle query in
Http_daemon.respond ~headers:[contype]
- ~body:(pp_result (List.map fst result)) outchan
+ ~body:(pp_result req (List.map fst result)) outchan
| "/getpage" ->
(* TODO implement "is_permitted" *)
let _ = prerr_endline
| page -> Http_daemon.respond_forbidden ~url:page outchan))
(* OLD | "/ask_uwobo" -> proxy (req#param "url") outchan *)
| "/ask_uwobo" -> build_uwobo_request req outchan
- | "/hint"
- | "/match"
- | "/elim" ->
- exec_action mqi_handle req outchan
+ | "/hint" | "/match" | "/elim" -> exec_action mqi_handle req outchan
| invalid_request ->
Http_daemon.respond_error ~code:(`Status (`Client_error `Bad_request))
outchan);
| Chat_unfinished -> prerr_endline "Chat unfinished, Try again!"
| Http_types.Param_not_found attr_name ->
bad_request (sprintf "Parameter '%s' is missing" attr_name) outchan
- | exc ->
- let msg = sprintf "Uncaught exception: %s" (Printexc.to_string exc) in
- debug_print msg ;
- Http_daemon.respond ~body:(pp_error msg) outchan
-in
-printf "%s started and listening on port %d\n" daemon_name port;
-printf "Current directory is %s\n" (Sys.getcwd ());
-printf "HTML directory is %s\n" pages_dir;
-flush stdout;
-Unix.putenv "http_proxy" "";
-let mqi_handle = C.init ~log:debug_print () in
-Http_daemon.start' ~port (callback mqi_handle);
-C.close mqi_handle;
-printf "%s is terminating, bye!\n" daemon_name
+ | CicTextualParser2.Parse_error msg ->
+ send_results (`Error (pp_error "Parse_error" msg)) req outchan
+ | Unbound_identifier id ->
+ send_results (`Error (pp_error "Unbound identifier" id)) req outchan
+ | exn ->
+ let exn_string = Printexc.to_string exn in
+ debug_print exn_string;
+ let msg = pp_error "Uncaught exception" exn_string in
+ send_results (`Error msg) req outchan
+
+let _ =
+ printf "%s started and listening on port %d\n" daemon_name port;
+ printf "Current directory is %s\n" (Sys.getcwd ());
+ printf "HTML directory is %s\n" pages_dir;
+ flush stdout;
+ Unix.putenv "http_proxy" "";
+ let mqi_handle = C.init ~log:debug_print () in
+ Http_daemon.start' ~port (callback mqi_handle);
+ C.close mqi_handle;
+ printf "%s is terminating, bye!\n" daemon_name
+