+(* returns the length of the executed text and an html representation of the
+ * current metasenv*)
+(*let advance =*)
+let advance (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
+ let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
+ let env = cgi#environment in
+ (try
+ let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
+ let sid = HExtlib.unopt sid in
+ (*
+ cgi # set_header
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\""
+ ();
+ *)
+ let text = cgi#argument_value "body" in
+ (* prerr_endline ("body =\n" ^ text); *)
+ let parsed_len, new_parsed, new_unparsed, new_status = advance0 sid text in
+ let txt = output_status new_status in
+ let body =
+ "<response><parsed length=\"" ^ (string_of_int parsed_len) ^ "\">" ^
+ new_parsed ^ "</parsed>" ^ txt
+ ^ "</response>"
+ in
+ (* prerr_endline ("sending advance response:\n" ^ body); *)
+ cgi # set_header
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\""
+ ();
+ cgi#out_channel#output_string body
+ with
+ | Generic_error text ->
+ let body = "<response><error>" ^ text ^ "</error></response>" in
+ cgi # set_header
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\""
+ ();
+ cgi#out_channel#output_string body
+ | Emphasized_error text ->
+(* | MultiPassDisambiguator.DisambiguationError (offset,errorll) -> *)
+ let body = "<response><localized>" ^ text ^ "</localized></response>" in
+ cgi # set_header
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\""
+ ();
+ cgi#out_channel#output_string body
+ | Disamb_error text ->
+ let body = "<response>" ^ text ^ "</response>" in
+ cgi # set_header
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\""
+ ();
+ cgi#out_channel#output_string body
+ | End_of_file _ ->
+ let body = "<response><parsed length=\"0\"></parsed></response>" in
+ cgi # set_header
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\""
+ ();
+ cgi#out_channel#output_string body
+ | Not_found _ ->
+ cgi # set_header
+ ~status:`Internal_server_error
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\""
+ ()
+ );
+ cgi#out_channel#commit_work()
+;;
+
+let gotoBottom (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
+ let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
+ let env = cgi#environment in
+(* (try *)
+ let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
+ let sid = HExtlib.unopt sid in
+
+ let error_msg = function
+ | Emphasized_error text -> "<localized>" ^ text ^ "</localized>"
+ | Disamb_error text -> text
+ | End_of_file _ -> (* not an error *) ""
+ | e -> (* unmanaged error *)
+ "<error>" ^
+ (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false ()
+ (Printexc.to_string e)) ^ "</error>"
+ in
+
+ let rec aux acc text =
+ try
+ prerr_endline ("evaluating: " ^ first_line text);
+ let plen,new_parsed,new_unparsed,_new_status = advance0 sid text in
+ aux ((plen,new_parsed)::acc) new_unparsed
+ with e -> acc, error_msg e
+ (* DON'T SERIALIZE NOW!!!
+ let status = MatitaAuthentication.get_status sid in
+ GrafiteTypes.Serializer.serialize
+ ~baseuri:(NUri.uri_of_string status#baseuri) status;
+ acc, error_msg e *)
+ in
+ (*
+ cgi # set_header
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\""
+ ();
+ *)
+ let text = cgi#argument_value "body" in
+ (* prerr_endline ("body =\n" ^ text); *)
+ let len_parsedlist, err_msg = aux [] text in
+ let status = MatitaAuthentication.get_status sid in
+ let txt = output_status status in
+ let parsed_tag (len,txt) =
+ "<parsed length=\"" ^ (string_of_int len) ^ "\">" ^ txt ^ "</parsed>"
+ in
+ (* List.rev: the list begins with the older parsed txt *)
+ let body =
+ "<response>" ^
+ String.concat "" (List.rev (List.map parsed_tag len_parsedlist)) ^
+ txt ^ err_msg ^ "</response>"
+ in
+ (* prerr_endline ("sending goto bottom response:\n" ^ body); *)
+ cgi # set_header
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\""
+ ();
+ cgi#out_channel#output_string body;
+(* with Not_found -> cgi#set_header ~status:`Internal_server_error
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\"" ()); *)
+ cgi#out_channel#commit_work()
+;;
+
+let gotoTop (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
+ let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
+ let env = cgi#environment in
+ prerr_endline "executing goto Top";
+ (try
+ let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
+ let sid = HExtlib.unopt sid in
+ (*
+ cgi # set_header
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\""
+ ();
+ *)
+ let status = MatitaAuthentication.get_status sid in
+ let uid = MatitaAuthentication.user_of_session sid in
+ let baseuri = status#baseuri in
+ let new_status = new MatitaEngine.status (Some uid) baseuri in
+ prerr_endline "gototop prima della time travel";
+ (* NCicLibrary.time_travel new_status; *)
+ prerr_endline "gototop dopo della time travel";
+ let new_history = [new_status] in
+ MatitaAuthentication.set_history sid new_history;
+ MatitaAuthentication.set_status sid new_status;
+ (* NCicLibrary.time_travel new_status; *)
+ cgi # set_header
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\""
+ ();
+ cgi#out_channel#output_string "<response>ok</response>"
+ with _ ->
+ (cgi#set_header ~status:`Internal_server_error
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\"" ();
+ cgi#out_channel#output_string "<response>ok</response>"));
+ cgi#out_channel#commit_work()
+;;
+
+let retract (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
+ let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
+ let env = cgi#environment in
+ (try
+ let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
+ let sid = HExtlib.unopt sid in
+ (*
+ cgi # set_header
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\""
+ ();
+ *)
+ let history = MatitaAuthentication.get_history sid in
+ let old_status = MatitaAuthentication.get_status sid in
+ let new_history,new_status =
+ match history with
+ _::(status::_ as history) ->
+ history, status
+ | [_] -> (prerr_endline "singleton";failwith "retract")
+ | _ -> (prerr_endline "nil"; assert false) in
+(* prerr_endline "timestamp prima della retract";
+ old_status#print_timestamp ();
+ prerr_endline "timestamp della retract";
+ new_status#print_timestamp ();
+ prerr_endline ("prima della time travel"); *)
+ NCicLibrary.time_travel new_status;
+ prerr_endline ("dopo della time travel");
+ MatitaAuthentication.set_history sid new_history;
+ MatitaAuthentication.set_status sid new_status;
+ prerr_endline ("baseuri after retract = " ^ new_status#baseuri);
+ let body = output_status new_status in
+ cgi # set_header
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\""
+ ();
+ cgi#out_channel#output_string body
+ with e ->
+ prerr_endline ("error in retract: " ^ Printexc.to_string e);
+ cgi#set_header ~status:`Internal_server_error
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\"" ());
+ cgi#out_channel#commit_work()
+;;
+
+
+let viewLib (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
+ let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
+ let env = cgi#environment in
+
+ let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
+ let sid = HExtlib.unopt sid in
+ (*
+ cgi # set_header
+ ~cache:`No_cache
+ ~content_type:"text/html; charset=\"utf-8\""
+ ();
+ *)
+ let uid = MatitaAuthentication.user_of_session sid in
+
+ let ft = MatitaAuthentication.read_ft uid in
+ let html = MatitaFilesystem.html_of_library uid ft in
+ cgi # set_header
+ ~cache:`No_cache
+ ~content_type:"text/html; charset=\"utf-8\""
+ ();
+ cgi#out_channel#output_string
+ ((*
+ "<html><head>\n" ^
+ "<title>XML Tree Control</title>\n" ^
+ "<link href=\"treeview/xmlTree.css\" type=\"text/css\" rel=\"stylesheet\">\n" ^
+ "<script src=\"treeview/xmlTree.js\" type=\"text/javascript\"></script>\n" ^
+ "<body>\n" ^ *)
+ html (* ^ "\n</body></html>" *) );
+
+ let files,anomalies = MatitaFilesystem.stat_user uid in
+ let changed = HExtlib.filter_map
+ (fun (n,f) -> if (f = Some MatitaFilesystem.MModified) then Some n else None) files
+ in
+ let changed = String.concat "\n" changed in
+ let anomalies = String.concat "\n" anomalies in
+ prerr_endline ("Changed:\n" ^ changed ^ "\n\nAnomalies:\n" ^ anomalies);
+ cgi#out_channel#commit_work()
+
+;;
+
+let resetLib (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
+ let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
+ MatitaAuthentication.reset ();
+ cgi # set_header
+ ~cache:`No_cache
+ ~content_type:"text/html; charset=\"utf-8\""
+ ();
+
+ cgi#out_channel#output_string
+ ("<html><head>\n" ^
+ "<title>Matitaweb Reset</title>\n" ^
+ "<body><H1>Reset completed</H1></body></html>");
+ cgi#out_channel#commit_work()
+
+open Netcgi1_compat.Netcgi_types;;
+
+(**********************************************************************)
+(* Create the webserver *)
+(**********************************************************************)
+
+
+let start() =
+ let (opt_list, cmdline_cfg) = Netplex_main.args() in
+
+ let use_mt = ref true in
+
+ let opt_list' =
+ [ "-mt", Arg.Set use_mt,
+ " Use multi-threading instead of multi-processing"
+ ] @ opt_list in
+
+ Arg.parse
+ opt_list'
+ (fun s -> raise (Arg.Bad ("Don't know what to do with: " ^ s)))
+ "usage: netplex [options]";
+ let parallelizer =
+ if !use_mt then
+ Netplex_mt.mt() (* multi-threading *)
+ else
+ Netplex_mp.mp() in (* multi-processing *)
+(*
+ let adder =
+ { Nethttpd_services.dyn_handler = (fun _ -> process1);
+ dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
+ dyn_uri = None; (* not needed *)
+ dyn_translator = (fun _ -> ""); (* not needed *)
+ dyn_accept_all_conditionals = false;
+ } in
+*)
+ let do_advance =
+ { Nethttpd_services.dyn_handler = (fun _ -> advance);
+ dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
+ dyn_uri = None; (* not needed *)
+ dyn_translator = (fun _ -> ""); (* not needed *)
+ dyn_accept_all_conditionals = false;
+ } in
+ let do_retract =
+ { Nethttpd_services.dyn_handler = (fun _ -> retract);
+ dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
+ dyn_uri = None; (* not needed *)
+ dyn_translator = (fun _ -> ""); (* not needed *)
+ dyn_accept_all_conditionals = false;
+ } in
+ let goto_bottom =
+ { Nethttpd_services.dyn_handler = (fun _ -> gotoBottom);
+ dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
+ dyn_uri = None; (* not needed *)
+ dyn_translator = (fun _ -> ""); (* not needed *)
+ dyn_accept_all_conditionals = false;
+ } in
+ let goto_top =
+ { Nethttpd_services.dyn_handler = (fun _ -> gotoTop);
+ dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
+ dyn_uri = None; (* not needed *)
+ dyn_translator = (fun _ -> ""); (* not needed *)
+ dyn_accept_all_conditionals = false;
+ } in
+ let retrieve =
+ { Nethttpd_services.dyn_handler = (fun _ -> retrieve);
+ dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
+ dyn_uri = None; (* not needed *)
+ dyn_translator = (fun _ -> ""); (* not needed *)
+ dyn_accept_all_conditionals = false;
+ } in
+ let do_register =
+ { Nethttpd_services.dyn_handler = (fun _ -> register);
+ dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
+ dyn_uri = None; (* not needed *)
+ dyn_translator = (fun _ -> ""); (* not needed *)
+ dyn_accept_all_conditionals = false;
+ } in
+ let do_login =
+ { Nethttpd_services.dyn_handler = (fun _ -> login);
+ dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
+ dyn_uri = None; (* not needed *)
+ dyn_translator = (fun _ -> ""); (* not needed *)
+ dyn_accept_all_conditionals = false;
+ } in
+ let do_logout =
+ { Nethttpd_services.dyn_handler = (fun _ -> logout);
+ dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
+ dyn_uri = None; (* not needed *)
+ dyn_translator = (fun _ -> ""); (* not needed *)
+ dyn_accept_all_conditionals = false;
+ } in
+ let do_viewlib =
+ { Nethttpd_services.dyn_handler = (fun _ -> viewLib);
+ dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
+ dyn_uri = None; (* not needed *)
+ dyn_translator = (fun _ -> ""); (* not needed *)
+ dyn_accept_all_conditionals = false;
+ } in
+ let do_resetlib =
+ { Nethttpd_services.dyn_handler = (fun _ -> resetLib);
+ dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
+ dyn_uri = None; (* not needed *)
+ dyn_translator = (fun _ -> ""); (* not needed *)
+ dyn_accept_all_conditionals = false;
+ } in
+ let do_save =
+ { Nethttpd_services.dyn_handler = (fun _ -> save);
+ dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
+ dyn_uri = None; (* not needed *)
+ dyn_translator = (fun _ -> ""); (* not needed *)
+ dyn_accept_all_conditionals = false;
+ } in
+ let do_commit =
+ { Nethttpd_services.dyn_handler = (fun _ -> initiate_commit);
+ dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
+ dyn_uri = None; (* not needed *)
+ dyn_translator = (fun _ -> ""); (* not needed *)
+ dyn_accept_all_conditionals = false;
+ } in
+ let do_update =
+ { Nethttpd_services.dyn_handler = (fun _ -> svn_update);
+ dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
+ dyn_uri = None; (* not needed *)
+ dyn_translator = (fun _ -> ""); (* not needed *)
+ dyn_accept_all_conditionals = false;
+ } in
+
+
+ let nethttpd_factory =
+ Nethttpd_plex.nethttpd_factory
+ ~handlers:[ "advance", do_advance
+ ; "retract", do_retract
+ ; "bottom", goto_bottom
+ ; "top", goto_top
+ ; "open", retrieve
+ ; "register", do_register
+ ; "login", do_login
+ ; "logout", do_logout
+ ; "reset", do_resetlib
+ ; "viewlib", do_viewlib
+ ; "save", do_save
+ ; "commit", do_commit
+ ; "update", do_update]
+ () in