]> matita.cs.unibo.it Git - helm.git/blobdiff - matitaB/matita/matitadaemon.ml
(Almost) working multi-user matitaweb.
[helm.git] / matitaB / matita / matitadaemon.ml
index 07b03207a7ec4b7774af7bd19ad889b7019bb0ab..492edd56e425ac73b9fcbee296021942f288aadb 100644 (file)
@@ -218,6 +218,25 @@ let advance0 sid text =
   MatitaAuthentication.set_history sid (st::history);
   parsed_len, new_unparsed, st
 
+let register (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
+  let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
+  let env = cgi#environment in
+  
+  assert (cgi#arguments <> []);
+  let uid = cgi#argument_value "userid" in
+  let userpw = cgi#argument_value "password" in
+  (try 
+    MatitaAuthentication.add_user uid userpw;
+    env#set_output_header_field "Location" "/index.html"
+   with MatitaAuthentication.UsernameCollision _ ->
+    cgi#set_header
+      ~cache:`No_cache 
+      ~content_type:"text/html; charset=\"utf-8\""
+      ();
+    cgi#out_channel#output_string
+      "<html><head></head><body>Error: User id collision!</body></html>");
+  cgi#out_channel#commit_work()
+;;
 
 let login (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
   let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
@@ -235,8 +254,10 @@ let login (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
    begin
     prerr_endline "4";
     let sid = MatitaAuthentication.create_session uid in
-    let cookie = Netcgi.Cookie.make "session" (Uuidm.to_string sid) in
-    cgi#set_header ~status:`See_other ~cache:`No_cache ~set_cookies:[cookie] ();
+    (* let cookie = Netcgi.Cookie.make "session" (Uuidm.to_string sid) in
+       cgi#set_header ~set_cookies:[cookie] (); *)
+    env#set_output_header_field 
+      "Set-Cookie" ("session=" ^ (Uuidm.to_string sid));
     env#set_output_header_field "Location" "/index.html"
    end
   else
@@ -438,6 +459,13 @@ let start() =
       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;
@@ -445,13 +473,13 @@ let start() =
       dyn_translator = (fun _ -> ""); (* not needed *)
       dyn_accept_all_conditionals = false;
     } in
-  (*let do_logout =
+  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 *)
+    } in 
   
   let nethttpd_factory = 
     Nethttpd_plex.nethttpd_factory
@@ -459,14 +487,12 @@ let start() =
                 ; "retract", do_retract
                 ; "bottom", goto_bottom
                 ; "open", retrieve 
+                ; "register", do_register
                 ; "login", do_login 
-                (*; "logout", do_logout *)]
+                ; "logout", do_logout ]
       () in
   MatitaInit.initialize_all ();
-  (* test begin *)
-  MatitaAuthentication.add_user "ricciott" "pippo123";
-  MatitaAuthentication.add_user "asperti" "pluto456";
-  (* test end *)
+  MatitaAuthentication.deserialize ();
   Netplex_main.startup
     parallelizer
     Netplex_log.logger_factories   (* allow all built-in logging styles *)