]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/searchEngine/searchEngine.ml
1. param.name=value parameters added to the getpage method
[helm.git] / helm / searchEngine / searchEngine.ml
index 9bc3ef2c95eafc12ac2de858bf0a807040ece0ba..77280d77a32aa63f3df6973a95f71fbc017007c8 100644 (file)
@@ -29,7 +29,7 @@ Http_common.debug := true;;
 (* Http_common.debug := true;; *)
 
   (** accepted HTTP servers for ask_uwobo method forwarding *)
-let valid_servers = [ "mowgli.cs.unibo.it:58080" ] ;;
+let valid_servers = [ "mowgli.cs.unibo.it:58080" ; "localhost:58080" ] ;;
 
 open Printf;;
 
@@ -98,7 +98,8 @@ let fold_file f init fname =
 let iter_file f = fold_file (fun _ line -> f line) ()
 
 let (title_tag_RE, choices_tag_RE, msg_tag_RE, id_to_uris_RE, id_RE,
-    interpretations_RE, interpretations_labels_RE, results_RE, new_aliases_RE) =
+    interpretations_RE, interpretations_labels_RE, results_RE, new_aliases_RE)
+  =
   (Pcre.regexp "@TITLE@", Pcre.regexp "@CHOICES@", Pcre.regexp "@MSG@",
   Pcre.regexp "@ID_TO_URIS@", Pcre.regexp "@ID@",
   Pcre.regexp "@INTERPRETATIONS@", Pcre.regexp "@INTERPRETATIONS_LABELS@",
@@ -148,11 +149,38 @@ let callback (req: Http_types.request) outchan =
         (let is_permitted _ = true in
         let remove_fragment uri = Pcre.replace ~pat:"#.*" uri in
         let page = remove_fragment (req#param "url") in
-        match page with
+        let preprocess =
+          (try
+            bool_of_string (req#param "preprocess")
+          with Invalid_argument _ | Http_types.Param_not_found _ -> false)
+        in
+        (match page with
         | page when is_permitted page ->
-            Http_daemon.respond_file
-              ~fname:(sprintf "%s/%s" pages_dir (remove_fragment page)) outchan
-        | page -> Http_daemon.respond_forbidden ~url:page outchan)
+            let fname = sprintf "%s/%s" pages_dir (remove_fragment page) in
+            if preprocess then begin
+              Http_daemon.send_basic_headers ~code:200 outchan;
+              Http_daemon.send_CRLF outchan;
+              iter_file
+                (fun line ->
+                  output_string outchan
+                    ((apply_substs
+                       (List.map
+                         (function (key,value) ->
+                           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)
+                       )
+                       line) ^
+                    "\n"))
+                fname
+            end else
+              Http_daemon.respond_file ~fname outchan
+        | page -> Http_daemon.respond_forbidden ~url:page outchan))
     | "/ask_uwobo" ->
       let url = req#param "url" in
       let server_and_port =