4 exception Emphasized_error of string
5 exception Ambiguous of string
7 module Stack = Continuationals.Stack
9 let rt_path () = Helm_registry.get "matita.rt_base_dir"
11 let libdir uid = (rt_path ()) ^ "/users/" ^ uid
13 let utf8_length = Netconversion.ustring_length `Enc_utf8
15 let mutex = Mutex.create ();;
17 let to_be_committed = ref [];;
19 (* adds a user to the commit queue; concurrent instances possible, so we
20 * enclose the update in a CS
22 let add_user_for_commit uid =
24 to_be_committed := uid::List.filter (fun x -> x <> uid) !to_be_committed;
28 let do_global_commit () =
29 prerr_endline ("to be committed: " ^ String.concat " " !to_be_committed);
32 let ft = MatitaAuthentication.read_ft u in
34 (* first we add new files/dirs to the repository *)
35 (* must take the reverse because svn requires the add to be performed in
37 (otherwise run with --parents option) *)
38 let to_be_added = List.rev (List.map fst
39 (List.filter (fun (_,flag) -> flag = MatitaFilesystem.MAdd) ft))
41 prerr_endline ("@@@ ADDING files: " ^ String.concat ", " to_be_added);
44 let newout = MatitaFilesystem.add_files u to_be_added in
47 | MatitaFilesystem.SvnError outstr ->
48 prerr_endline ("ADD OF " ^ u ^ "FAILED:" ^ outstr);
52 (* now we update the local copy (to merge updates from other users) *)
54 let files,anomalies,(added,conflict,del,upd,merged) =
55 MatitaFilesystem.update_user u
57 let anomalies = String.concat "\n" anomalies in
58 let details = Printf.sprintf
60 "%d deleted files\n"^^
61 "%d updated files\n"^^
63 "%d conflicting files\n\n" ^^
64 "Anomalies:\n%s") added del upd merged conflict anomalies
66 prerr_endline ("update details:\n" ^ details);
67 MatitaAuthentication.set_file_flag u files;
70 | MatitaFilesystem.SvnError outstr ->
71 prerr_endline ("UPDATE OF " ^ u ^ "FAILED:" ^ outstr);
75 (* we re-read the file table after updating *)
76 let ft = MatitaAuthentication.read_ft u in
78 (* finally we perform the real commit *)
79 let modified = (List.map fst
80 (List.filter (fun (_,flag) -> flag = MatitaFilesystem.MModified) ft))
82 let to_be_committed = to_be_added @ modified
85 let newout = MatitaFilesystem.commit u to_be_committed in
88 | MatitaFilesystem.SvnError outstr ->
89 prerr_endline ("COMMIT OF " ^ u ^ "FAILED:" ^ outstr);
93 (* call stat to get the final status *)
94 let files, anomalies = MatitaFilesystem.stat_user u in
95 let added,not_added = List.fold_left
96 (fun (a_acc, na_acc) fname ->
97 if List.mem fname (List.map fst files) then
100 fname::a_acc, na_acc)
103 let committed,not_committed = List.fold_left
104 (fun (c_acc, nc_acc) fname ->
105 if List.mem fname (List.map fst files) then
108 fname::c_acc, nc_acc)
111 let conflicts = List.map fst (List.filter
112 (fun (_,f) -> f = Some MatitaFilesystem.MConflict) files)
114 MatitaAuthentication.set_file_flag u
115 (List.map (fun x -> x, Some MatitaFilesystem.MSynchronized) (added@committed));
116 MatitaAuthentication.set_file_flag u files;
117 out ^ "\n\n" ^ (Printf.sprintf
118 ("COMMIT RESULTS for %s\n" ^^
119 "==============\n" ^^
120 "added and committed (%d of %d): %s\n" ^^
121 "modified and committed (%d of %d): %s\n" ^^
123 "not committed: %s\n" ^^
125 u (List.length added) (List.length to_be_added) (String.concat ", " added)
126 (List.length committed) (List.length modified) (String.concat ", " committed)
127 (String.concat ", " not_added)
128 (String.concat ", " not_committed) (String.concat ", " conflicts)))
130 (* XXX: at the moment, we don't keep track of the order in which users have
131 scheduled their commits, but we should, otherwise we will get a
132 "first come, random served" policy *)
133 "" (* (List.rev !to_be_committed) *) (MatitaAuthentication.get_users ())
136 (*** from matitaScript.ml ***)
137 (* let only_dust_RE = Pcre.regexp "^(\\s|\n|%%[^\n]*\n)*$" *)
139 let eval_statement include_paths (* (buffer : GText.buffer) *) status (* script *)
142 let ast,unparsed_text =
145 (* if Pcre.pmatch ~rex:only_dust_RE text then raise Margin; *)
147 GrafiteParser.parsable_statement status
148 (Ulexing.from_utf8_string text) in
149 let ast = MatitaEngine.get_ast status include_paths strm in
151 | `Ast (st, text) -> st, text
153 let floc = match ast with
154 | GrafiteAst.Executable (loc, _)
155 | GrafiteAst.Comment (loc, _) -> loc in
157 let _,lend = HExtlib.loc_of_floc floc in
158 let parsed_text, _parsed_text_len =
159 HExtlib.utf8_parsed_text unparsed_text (HExtlib.floc_of_loc (0,lend)) in
160 let byte_parsed_text_len = String.length parsed_text in
162 String.sub unparsed_text byte_parsed_text_len
163 (String.length unparsed_text - byte_parsed_text_len)
167 MatitaEngine.eval_ast ~include_paths ~do_heavy_checks:false status ("",0,ast)
169 (status, parsed_text, unparsed_txt'),"",(*parsed_text_len*)
170 utf8_length parsed_text
172 (*let save_moo status =
173 let script = MatitaScript.current () in
174 let baseuri = status#baseuri in
175 match script#bos, script#eos with
178 GrafiteTypes.Serializer.serialize ~baseuri:(NUri.uri_of_string baseuri)
180 | _ -> clean_current_baseuri status
183 let sequent_size = ref 40;;
185 let include_paths = ref [];;
188 * <meta number="...">
189 * <metaname>...</metaname>
195 let output_status s =
196 let _,_,metasenv,subst,_ = s#obj in
197 let render_switch = function
198 | Stack.Open i -> "?" ^ (string_of_int i)
199 | Stack.Closed i -> "<S>?" ^ (string_of_int i) ^ "</S>"
201 let int_of_switch = function
202 | Stack.Open i | Stack.Closed i -> i
204 let sequent = function
206 let meta = List.assoc i metasenv in
207 snd (ApplyTransformation.ntxt_of_cic_sequent
208 ~metasenv ~subst ~map_unicode_to_tex:false !sequent_size s (i,meta))
209 | Stack.Closed _ -> "This goal has already been closed."
211 let render_sequent is_loc acc depth tag (pos,sw) =
212 let metano = int_of_switch sw in
215 (match depth, pos with
216 | 0, 0 -> "<span class=\"activegoal\">" ^ (render_switch sw) ^ "</span>"
218 Printf.sprintf "<span class=\"activegoal\">|<SUB>%d</SUB>: %s</span>" pos (render_switch sw)
219 | 1, pos when Stack.head_tag s#stack = `BranchTag ->
220 Printf.sprintf "<span class=\"passivegoal\">|<SUB>%d</SUB> : %s</span>" pos (render_switch sw)
221 | _ -> render_switch sw)
222 else render_switch sw
225 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () markup in
226 let markup = "<metaname>" ^ markup ^ "</metaname>" in
228 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () (sequent sw)
230 let txt0 = "<goal>" ^ sequent ^ "</goal>" in
231 "<meta number=\"" ^ (string_of_int metano) ^ "\">" ^ markup ^
232 txt0 ^ "</meta>" ^ acc
236 ~env:(render_sequent true) ~cont:(render_sequent false)
237 ~todo:(render_sequent false) "" s#stack) ^
239 (* prerr_endline ("sending metasenv:\n" ^ res); res *)
242 let html_of_matita s =
243 let patt1 = Str.regexp "\005" in
244 let patt2 = Str.regexp "\006" in
245 let patt3 = Str.regexp "<" in
246 let patt4 = Str.regexp ">" in
247 let res = Str.global_replace patt4 ">" s in
248 let res = Str.global_replace patt3 "<" res in
249 let res = Str.global_replace patt2 ">" res in
250 let res = Str.global_replace patt1 "<" res in
254 let heading_nl_RE = Pcre.regexp "^\\s*\n\\s*";;
257 let s = Pcre.replace ~rex:heading_nl_RE s in
259 let nl_pos = String.index s '\n' in
260 String.sub s 0 nl_pos
264 let read_file fname =
265 let chan = open_in fname in
266 let lines = ref [] in
269 lines := input_line chan :: !lines
271 with End_of_file -> close_in chan);
272 String.concat "\n" (List.rev !lines)
275 let load_index outchan =
276 let s = read_file "index.html" in
277 Http_daemon.respond ~headers:["Content-Type", "text/html"] ~code:(`Code 200) ~body:s outchan
280 let load_doc filename outchan =
281 let s = read_file filename in
283 try String.sub filename (String.length filename - 4) 4 = ".png"
284 with Invalid_argument _ -> false
286 let contenttype = if is_png then "image/png" else "text/html" in
287 Http_daemon.respond ~headers:["Content-Type", contenttype] ~code:(`Code 200) ~body:s outchan
290 let retrieve (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
291 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
292 let env = cgi#environment in
294 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
295 let sid = HExtlib.unopt sid in
296 let uid = MatitaAuthentication.user_of_session sid in
300 ~content_type:"text/xml; charset=\"utf-8\""
303 let filename = libdir uid ^ "/" ^ (cgi # argument_value "file") in
304 (* prerr_endline ("reading file " ^ filename); *)
306 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false ()
307 (html_of_matita (read_file filename)) in
309 (* html_of_matita (read_file filename) in *)
310 (* prerr_endline ("sending:\nBEGIN\n" ^ body ^ "\nEND"); *)
311 let body = "<response><file>" ^ body ^ "</file></response>" in
312 let baseuri, incpaths =
314 let root, baseuri, _fname, _tgt =
315 Librarian.baseuri_of_script ~include_paths:[] filename in
318 Str.split (Str.regexp " ")
319 (List.assoc "include_paths" (Librarian.load_root_file (root^"/root")))
322 let rc = root :: includes in
323 List.iter (HLog.debug) rc; baseuri, rc
325 Librarian.NoRootFor _ | Librarian.FileNotFound _ -> "",[] in
326 include_paths := incpaths;
327 let status = (MatitaAuthentication.get_status sid)#set_baseuri baseuri in
328 let history = [status] in
329 MatitaAuthentication.set_status sid status;
330 MatitaAuthentication.set_history sid history;
333 ~content_type:"text/xml; charset=\"utf-8\""
335 cgi#out_channel#output_string body;
339 ~status:`Internal_server_error
341 ~content_type:"text/html; charset=\"utf-8\""
343 cgi#out_channel#commit_work()
346 let advance0 sid text =
347 let status = MatitaAuthentication.get_status sid in
348 let status = status#reset_disambiguate_db () in
349 let (st,new_statements,new_unparsed),(* newtext TODO *) _,parsed_len =
351 eval_statement !include_paths (*buffer*) status (`Raw text)
353 | HExtlib.Localized (floc,e) as exn ->
354 let x, y = HExtlib.loc_of_floc floc in
355 let pre = Netconversion.ustring_sub `Enc_utf8 0 x text in
356 let err = Netconversion.ustring_sub `Enc_utf8 x (y-x) text in
357 let post = Netconversion.ustring_sub `Enc_utf8 y
358 (Netconversion.ustring_length `Enc_utf8 text - y) text in
359 let _,title = MatitaExcPp.to_string exn in
360 (* let title = "" in *)
362 pre ^ "\005span class=\"error\" title=\"" ^ title ^ "\"\006" ^ err ^ "\005/span\006" ^ post in
363 let marked = Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
364 () (html_of_matita marked) in
365 raise (Emphasized_error marked)
366 | GrafiteDisambiguate.Ambiguous_input (loc,choices) ->
367 let x,y = HExtlib.loc_of_floc loc in
368 let choice_of_alias = function
369 | GrafiteAst.Ident_alias (_,uri) -> uri, None, uri
370 | GrafiteAst.Number_alias (None,desc)
371 | GrafiteAst.Symbol_alias (_,None,desc) -> "cic:/fakeuri.def(1)", Some desc, desc
372 | GrafiteAst.Number_alias (Some uri,desc)
373 | GrafiteAst.Symbol_alias (_,Some uri,desc) -> uri, Some desc, desc
375 let tag_of_choice (uri,title,desc) =
377 | None -> Printf.sprintf "<choice href=\"%s\">%s</choice>"
379 (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () desc)
380 | Some t -> Printf.sprintf "<choice href=\"%s\" title=\"%s\">%s</choice>"
382 (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () t)
383 (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () desc)
387 (List.map (fun x -> tag_of_choice (choice_of_alias x)) choices)
389 prerr_endline (Printf.sprintf
390 "@@@ Ambiguous input at (%d,%d). Possible choices:\n\n%s\n\n@@@ End."
393 let pre = Netconversion.ustring_sub `Enc_utf8 0 x text in
394 let err = Netconversion.ustring_sub `Enc_utf8 x (y-x) text in
395 let post = Netconversion.ustring_sub `Enc_utf8 y
396 (Netconversion.ustring_length `Enc_utf8 text - y) text in
397 let title = "Disambiguation Error" in
398 (* let title = "" in *)
400 pre ^ "\005span class=\"error\" title=\"" ^ title ^ "\"\006" ^ err ^ "\005/span\006" ^ post in
401 let marked = Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
402 () (html_of_matita marked) in
404 let strchoices = Printf.sprintf
405 "<ambiguity start=\"%d\" stop=\"%d\">%s</ambiguity>" x y strchoices
406 in raise (Ambiguous strchoices)
407 (* | End_of_file -> ... *)
409 let stringbuf = Ulexing.from_utf8_string new_statements in
410 let interpr = GrafiteDisambiguate.get_interpr st#disambiguate_db in
411 let outstr = ref "" in
412 ignore (SmallLexer.mk_small_printer interpr outstr stringbuf);
413 prerr_endline ("baseuri after advance = " ^ st#baseuri);
414 (* prerr_endline ("parser output: " ^ !outstr); *)
415 MatitaAuthentication.set_status sid st;
417 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
418 () (html_of_matita !outstr), new_unparsed, st
420 let register (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
421 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
422 let _env = cgi#environment in
424 assert (cgi#arguments <> []);
425 let uid = cgi#argument_value "userid" in
426 let userpw = cgi#argument_value "password" in
428 MatitaAuthentication.add_user uid userpw;
429 (* env#set_output_header_field "Location" "/index.html" *)
430 cgi#out_channel#output_string
431 ("<html><head><meta http-equiv=\"refresh\" content=\"2;url=/login.html\">"
432 ^ "</head><body>Redirecting to login page...</body></html>")
434 | MatitaAuthentication.UsernameCollision _ ->
437 ~content_type:"text/html; charset=\"utf-8\""
439 cgi#out_channel#output_string
440 "<html><head></head><body>Error: User id collision!</body></html>"
441 | MatitaFilesystem.SvnError msg ->
444 ~content_type:"text/html; charset=\"utf-8\""
446 cgi#out_channel#output_string
447 ("<html><head></head><body><p>Error: Svn checkout failed!<p><p><textarea>"
448 ^ msg ^ "</textarea></p></body></html>"));
449 cgi#out_channel#commit_work()
452 let login (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
453 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
454 let env = cgi#environment in
456 assert (cgi#arguments <> []);
457 let uid = cgi#argument_value "userid" in
458 let userpw = cgi#argument_value "password" in
459 let pw,_ = MatitaAuthentication.lookup_user uid in
463 let ft = MatitaAuthentication.read_ft uid in
464 let _ = MatitaFilesystem.html_of_library uid ft in
465 let sid = MatitaAuthentication.create_session uid in
466 (* let cookie = Netcgi.Cookie.make "session" (Uuidm.to_string sid) in
467 cgi#set_header ~set_cookies:[cookie] (); *)
468 env#set_output_header_field
469 "Set-Cookie" ("session=" ^ (Uuidm.to_string sid));
470 (* env#set_output_header_field "Location" "/index.html" *)
471 cgi#out_channel#output_string
472 ("<html><head><meta http-equiv=\"refresh\" content=\"2;url=/index.html\">"
473 ^ "</head><body>Redirecting to Matita page...</body></html>")
479 ~content_type:"text/html; charset=\"utf-8\""
481 cgi#out_channel#output_string
482 "<html><head></head><body>Authentication error</body></html>"
485 cgi#out_channel#commit_work()
489 let logout (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
490 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
491 let env = cgi#environment in
493 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
494 let sid = HExtlib.unopt sid in
495 MatitaAuthentication.logout_user sid;
498 ~content_type:"text/html; charset=\"utf-8\""
500 let text = read_file (rt_path () ^ "/logout.html") in
501 cgi#out_channel#output_string text
505 ~status:`Internal_server_error
507 ~content_type:"text/html; charset=\"utf-8\""
509 cgi#out_channel#commit_work()
512 exception File_already_exists;;
514 let save (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
515 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
516 let env = cgi#environment in
518 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
519 let sid = HExtlib.unopt sid in
520 let status = MatitaAuthentication.get_status sid in
521 let uid = MatitaAuthentication.user_of_session sid in
522 assert (cgi#arguments <> []);
523 let locked = cgi#argument_value "locked" in
524 let unlocked = cgi#argument_value "unlocked" in
525 let dir = cgi#argument_value "dir" in
526 let rel_filename = cgi # argument_value "file" in
527 let filename = libdir uid ^ "/" ^ rel_filename in
528 let force = bool_of_string (cgi#argument_value "force") in
529 let already_exists = Sys.file_exists filename in
531 if ((not force) && already_exists) then
532 raise File_already_exists;
535 Unix.mkdir filename 0o744
538 let oc = open_out filename in
539 output_string oc (locked ^ unlocked);
541 if MatitaEngine.eos status unlocked then
543 (* prerr_endline ("serializing proof objects..."); *)
544 GrafiteTypes.Serializer.serialize
545 ~baseuri:(NUri.uri_of_string status#baseuri) status;
546 (* prerr_endline ("done."); *)
551 List.assoc rel_filename (MatitaAuthentication.read_ft uid)
552 with Not_found -> MatitaFilesystem.MUnversioned
554 (if old_flag <> MatitaFilesystem.MConflict &&
555 old_flag <> MatitaFilesystem.MAdd then
557 if already_exists then MatitaFilesystem.MModified
558 else MatitaFilesystem.MAdd
560 MatitaAuthentication.set_file_flag uid [rel_filename, Some newflag]);
563 ~content_type:"text/xml; charset=\"utf-8\""
565 cgi#out_channel#output_string "<response>ok</response>"
567 | File_already_exists ->
568 cgi#out_channel#output_string "<response>cancelled</response>"
571 ~status:`Internal_server_error
573 ~content_type:"text/xml; charset=\"utf-8\""
576 let estr = Printexc.to_string e in
577 cgi#out_channel#output_string ("<response>" ^ estr ^ "</response>"));
578 cgi#out_channel#commit_work()
581 let initiate_commit (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
582 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
583 let _env = cgi#environment in
585 let out = do_global_commit () in
588 ~content_type:"text/xml; charset=\"utf-8\""
590 cgi#out_channel#output_string "<commit>";
591 cgi#out_channel#output_string "<response>ok</response>";
592 cgi#out_channel#output_string ("<details>" ^ out ^ "</details>");
593 cgi#out_channel#output_string "</commit>"
597 ~status:`Internal_server_error
599 ~content_type:"text/xml; charset=\"utf-8\""
601 cgi#out_channel#commit_work()
604 let svn_update (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
605 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
606 let env = cgi#environment in
607 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
608 let sid = HExtlib.unopt sid in
609 let uid = MatitaAuthentication.user_of_session sid in
611 let files,anomalies,(added,conflict,del,upd,merged) =
612 MatitaFilesystem.update_user uid
614 let anomalies = String.concat "\n" anomalies in
615 let details = Printf.sprintf
617 "%d deleted files\n"^^
618 "%d updated files\n"^^
619 "%d merged files\n"^^
620 "%d conflicting files\n\n" ^^
621 "Anomalies:\n%s") added del upd merged conflict anomalies
623 prerr_endline ("update details:\n" ^ details);
625 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () details
627 MatitaAuthentication.set_file_flag uid files;
630 ~content_type:"text/xml; charset=\"utf-8\""
632 cgi#out_channel#output_string "<update>";
633 cgi#out_channel#output_string "<response>ok</response>";
634 cgi#out_channel#output_string ("<details>" ^ details ^ "</details>");
635 cgi#out_channel#output_string "</update>";
639 ~status:`Internal_server_error
641 ~content_type:"text/xml; charset=\"utf-8\""
643 cgi#out_channel#commit_work()
646 (* returns the length of the executed text and an html representation of the
648 let advance (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
649 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
650 let env = cgi#environment in
652 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
653 let sid = HExtlib.unopt sid in
657 ~content_type:"text/xml; charset=\"utf-8\""
660 let text = cgi#argument_value "body" in
661 (* prerr_endline ("body =\n" ^ text); *)
662 let history = MatitaAuthentication.get_history sid in
663 let parsed_len, new_parsed, new_unparsed, new_status = advance0 sid text in
664 MatitaAuthentication.set_history sid (new_status::history);
665 let txt = output_status new_status in
667 "<response><parsed length=\"" ^ (string_of_int parsed_len) ^ "\">" ^
668 new_parsed ^ "</parsed>" ^ txt
671 (* prerr_endline ("sending advance response:\n" ^ body); *)
674 ~content_type:"text/xml; charset=\"utf-8\""
676 cgi#out_channel#output_string body
678 | Emphasized_error text ->
679 (* | MultiPassDisambiguator.DisambiguationError (offset,errorll) -> *)
680 let body = "<response><error>" ^ text ^ "</error></response>" in
683 ~content_type:"text/xml; charset=\"utf-8\""
685 cgi#out_channel#output_string body
687 let body = "<response>" ^ text ^ "</response>" in
690 ~content_type:"text/xml; charset=\"utf-8\""
692 cgi#out_channel#output_string body
695 ~status:`Internal_server_error
697 ~content_type:"text/xml; charset=\"utf-8\""
699 cgi#out_channel#commit_work()
702 let gotoBottom (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
703 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
704 let env = cgi#environment in
706 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
707 let sid = HExtlib.unopt sid in
708 let history = MatitaAuthentication.get_history sid in
710 let rec aux parsed_len parsed_txt text =
712 prerr_endline ("evaluating: " ^ first_line text);
713 let plen,new_parsed,new_unparsed,_new_status = advance0 sid text in
714 aux (parsed_len+plen) (parsed_txt ^ new_parsed) new_unparsed
717 let status = MatitaAuthentication.get_status sid in
718 GrafiteTypes.Serializer.serialize
719 ~baseuri:(NUri.uri_of_string status#baseuri) status;
720 if parsed_len > 0 then
721 MatitaAuthentication.set_history sid (status::history);
722 parsed_len, parsed_txt
723 | _ -> parsed_len, parsed_txt
728 ~content_type:"text/xml; charset=\"utf-8\""
731 let text = cgi#argument_value "body" in
732 (* prerr_endline ("body =\n" ^ text); *)
733 let parsed_len, new_parsed = aux 0 "" text in
734 let status = MatitaAuthentication.get_status sid in
735 let txt = output_status status in
737 "<response><parsed length=\"" ^ (string_of_int parsed_len) ^ "\">" ^
738 new_parsed ^ "</parsed>" ^ txt
742 "<response><parsed length=\"" ^ (string_of_int parsed_len) ^ "\" />" ^ txt
745 (* prerr_endline ("sending goto bottom response:\n" ^ body); *)
748 ~content_type:"text/xml; charset=\"utf-8\""
750 cgi#out_channel#output_string body
751 with Not_found -> cgi#set_header ~status:`Internal_server_error
753 ~content_type:"text/xml; charset=\"utf-8\"" ());
754 cgi#out_channel#commit_work()
757 let gotoTop (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
758 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
759 let env = cgi#environment in
760 prerr_endline "executing goto Top";
762 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
763 let sid = HExtlib.unopt sid in
767 ~content_type:"text/xml; charset=\"utf-8\""
770 let status = MatitaAuthentication.get_status sid in
771 let uid = MatitaAuthentication.user_of_session sid in
772 let baseuri = status#baseuri in
773 let new_status = new MatitaEngine.status (Some uid) baseuri in
774 prerr_endline "gototop prima della time travel";
775 NCicLibrary.time_travel new_status;
776 prerr_endline "gototop dopo della time travel";
777 let new_history = [new_status] in
778 MatitaAuthentication.set_history sid new_history;
779 MatitaAuthentication.set_status sid new_status;
780 NCicLibrary.time_travel new_status;
783 ~content_type:"text/xml; charset=\"utf-8\""
785 cgi#out_channel#output_string "<response>ok</response>"
787 (cgi#set_header ~status:`Internal_server_error
789 ~content_type:"text/xml; charset=\"utf-8\"" ();
790 cgi#out_channel#output_string "<response>ok</response>"));
791 cgi#out_channel#commit_work()
794 let retract (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
795 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
796 let env = cgi#environment in
798 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
799 let sid = HExtlib.unopt sid in
803 ~content_type:"text/xml; charset=\"utf-8\""
806 let history = MatitaAuthentication.get_history sid in
807 let new_history,new_status =
809 _::(status::_ as history) ->
811 | [_] -> (prerr_endline "singleton";failwith "retract")
812 | _ -> (prerr_endline "nil"; assert false) in
813 prerr_endline ("prima della time travel");
814 NCicLibrary.time_travel new_status;
815 prerr_endline ("dopo della time travel");
816 MatitaAuthentication.set_history sid new_history;
817 MatitaAuthentication.set_status sid new_status;
818 prerr_endline ("baseuri after retract = " ^ new_status#baseuri);
819 let body = output_status new_status in
822 ~content_type:"text/xml; charset=\"utf-8\""
824 cgi#out_channel#output_string body
825 with _ -> cgi#set_header ~status:`Internal_server_error
827 ~content_type:"text/xml; charset=\"utf-8\"" ());
828 cgi#out_channel#commit_work()
832 let viewLib (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
833 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
834 let env = cgi#environment in
836 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
837 let sid = HExtlib.unopt sid in
841 ~content_type:"text/html; charset=\"utf-8\""
844 let uid = MatitaAuthentication.user_of_session sid in
846 let ft = MatitaAuthentication.read_ft uid in
847 let html = MatitaFilesystem.html_of_library uid ft in
850 ~content_type:"text/html; charset=\"utf-8\""
852 cgi#out_channel#output_string
855 "<title>XML Tree Control</title>\n" ^
856 "<link href=\"treeview/xmlTree.css\" type=\"text/css\" rel=\"stylesheet\">\n" ^
857 "<script src=\"treeview/xmlTree.js\" type=\"text/javascript\"></script>\n" ^
859 html (* ^ "\n</body></html>" *) );
861 let files,anomalies = MatitaFilesystem.stat_user uid in
862 let changed = HExtlib.filter_map
863 (fun (n,f) -> if (f = Some MatitaFilesystem.MModified) then Some n else None) files
865 let changed = String.concat "\n" changed in
866 let anomalies = String.concat "\n" anomalies in
867 prerr_endline ("Changed:\n" ^ changed ^ "\n\nAnomalies:\n" ^ anomalies);
868 cgi#out_channel#commit_work()
872 let resetLib (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
873 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
874 MatitaAuthentication.reset ();
877 ~content_type:"text/html; charset=\"utf-8\""
880 cgi#out_channel#output_string
882 "<title>Matitaweb Reset</title>\n" ^
883 "<body><H1>Reset completed</H1></body></html>");
884 cgi#out_channel#commit_work()
886 open Netcgi1_compat.Netcgi_types;;
888 (**********************************************************************)
889 (* Create the webserver *)
890 (**********************************************************************)
894 let (opt_list, cmdline_cfg) = Netplex_main.args() in
896 let use_mt = ref true in
899 [ "-mt", Arg.Set use_mt,
900 " Use multi-threading instead of multi-processing"
905 (fun s -> raise (Arg.Bad ("Don't know what to do with: " ^ s)))
906 "usage: netplex [options]";
909 Netplex_mt.mt() (* multi-threading *)
911 Netplex_mp.mp() in (* multi-processing *)
914 { Nethttpd_services.dyn_handler = (fun _ -> process1);
915 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
916 dyn_uri = None; (* not needed *)
917 dyn_translator = (fun _ -> ""); (* not needed *)
918 dyn_accept_all_conditionals = false;
922 { Nethttpd_services.dyn_handler = (fun _ -> advance);
923 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
924 dyn_uri = None; (* not needed *)
925 dyn_translator = (fun _ -> ""); (* not needed *)
926 dyn_accept_all_conditionals = false;
929 { Nethttpd_services.dyn_handler = (fun _ -> retract);
930 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
931 dyn_uri = None; (* not needed *)
932 dyn_translator = (fun _ -> ""); (* not needed *)
933 dyn_accept_all_conditionals = false;
936 { Nethttpd_services.dyn_handler = (fun _ -> gotoBottom);
937 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
938 dyn_uri = None; (* not needed *)
939 dyn_translator = (fun _ -> ""); (* not needed *)
940 dyn_accept_all_conditionals = false;
943 { Nethttpd_services.dyn_handler = (fun _ -> gotoTop);
944 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
945 dyn_uri = None; (* not needed *)
946 dyn_translator = (fun _ -> ""); (* not needed *)
947 dyn_accept_all_conditionals = false;
950 { Nethttpd_services.dyn_handler = (fun _ -> retrieve);
951 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
952 dyn_uri = None; (* not needed *)
953 dyn_translator = (fun _ -> ""); (* not needed *)
954 dyn_accept_all_conditionals = false;
957 { Nethttpd_services.dyn_handler = (fun _ -> register);
958 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
959 dyn_uri = None; (* not needed *)
960 dyn_translator = (fun _ -> ""); (* not needed *)
961 dyn_accept_all_conditionals = false;
964 { Nethttpd_services.dyn_handler = (fun _ -> login);
965 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
966 dyn_uri = None; (* not needed *)
967 dyn_translator = (fun _ -> ""); (* not needed *)
968 dyn_accept_all_conditionals = false;
971 { Nethttpd_services.dyn_handler = (fun _ -> logout);
972 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
973 dyn_uri = None; (* not needed *)
974 dyn_translator = (fun _ -> ""); (* not needed *)
975 dyn_accept_all_conditionals = false;
978 { Nethttpd_services.dyn_handler = (fun _ -> viewLib);
979 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
980 dyn_uri = None; (* not needed *)
981 dyn_translator = (fun _ -> ""); (* not needed *)
982 dyn_accept_all_conditionals = false;
985 { Nethttpd_services.dyn_handler = (fun _ -> resetLib);
986 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
987 dyn_uri = None; (* not needed *)
988 dyn_translator = (fun _ -> ""); (* not needed *)
989 dyn_accept_all_conditionals = false;
992 { Nethttpd_services.dyn_handler = (fun _ -> save);
993 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
994 dyn_uri = None; (* not needed *)
995 dyn_translator = (fun _ -> ""); (* not needed *)
996 dyn_accept_all_conditionals = false;
999 { Nethttpd_services.dyn_handler = (fun _ -> initiate_commit);
1000 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1001 dyn_uri = None; (* not needed *)
1002 dyn_translator = (fun _ -> ""); (* not needed *)
1003 dyn_accept_all_conditionals = false;
1006 { Nethttpd_services.dyn_handler = (fun _ -> svn_update);
1007 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1008 dyn_uri = None; (* not needed *)
1009 dyn_translator = (fun _ -> ""); (* not needed *)
1010 dyn_accept_all_conditionals = false;
1014 let nethttpd_factory =
1015 Nethttpd_plex.nethttpd_factory
1016 ~handlers:[ "advance", do_advance
1017 ; "retract", do_retract
1018 ; "bottom", goto_bottom
1021 ; "register", do_register
1023 ; "logout", do_logout
1024 ; "reset", do_resetlib
1025 ; "viewlib", do_viewlib
1027 ; "commit", do_commit
1028 ; "update", do_update]
1030 MatitaInit.initialize_all ();
1031 MatitaAuthentication.deserialize ();
1032 Netplex_main.startup
1034 Netplex_log.logger_factories (* allow all built-in logging styles *)
1035 Netplex_workload.workload_manager_factories (* ... all ways of workload management *)
1036 [ nethttpd_factory ] (* make this nethttpd available *)
1040 Sys.set_signal Sys.sigpipe Sys.Signal_ignore;