4 module Stack = Continuationals.Stack
6 let rt_path () = Helm_registry.get "matita.rt_base_dir"
8 let utf8_length = Netconversion.ustring_length `Enc_utf8
10 let utf8_parsed_text s floc =
11 let start, stop = HExtlib.loc_of_floc floc in
12 let len = stop - start in
13 let res = Netconversion.ustring_sub `Enc_utf8 start len s in
14 res, String.length res
16 (*** from matitaScript.ml ***)
17 (* let only_dust_RE = Pcre.regexp "^(\\s|\n|%%[^\n]*\n)*$" *)
19 let eval_statement include_paths (* (buffer : GText.buffer) *) status (* script *)
22 let ast,unparsed_text =
25 (* if Pcre.pmatch ~rex:only_dust_RE text then raise Margin; *)
27 GrafiteParser.parsable_statement status
28 (Ulexing.from_utf8_string text) in
29 let ast = MatitaEngine.get_ast status include_paths strm in
31 | `Ast (st, text) -> st, text
33 let floc = match ast with
34 | GrafiteAst.Executable (loc, _)
35 | GrafiteAst.Comment (loc, _) -> loc in
37 let _,lend = HExtlib.loc_of_floc floc in
38 let parsed_text, _parsed_text_len =
39 utf8_parsed_text unparsed_text (HExtlib.floc_of_loc (0,lend)) in
40 let byte_parsed_text_len = String.length parsed_text in
42 String.sub unparsed_text byte_parsed_text_len
43 (String.length unparsed_text - byte_parsed_text_len)
47 MatitaEngine.eval_ast ~include_paths ~do_heavy_checks:false status ("",0,ast)
49 (status, parsed_text, unparsed_txt'),"",(*parsed_text_len*)
50 utf8_length parsed_text
52 let sequent_size = ref 40;;
54 let include_paths = ref [];;
58 * <metaname>...</metaname>
65 let _,_,metasenv,subst,_ = s#obj in
66 let render_switch = function
67 | Stack.Open i -> "?" ^ (string_of_int i)
68 | Stack.Closed i -> "<S>?" ^ (string_of_int i) ^ "</S>"
70 let int_of_switch = function
71 | Stack.Open i | Stack.Closed i -> i
73 let sequent = function
75 let meta = List.assoc i metasenv in
76 snd (ApplyTransformation.ntxt_of_cic_sequent
77 ~metasenv ~subst ~map_unicode_to_tex:false !sequent_size s (i,meta))
78 | Stack.Closed _ -> "This goal has already been closed."
80 let render_sequent is_loc acc depth tag (pos,sw) =
81 let metano = int_of_switch sw in
84 (match depth, pos with
85 | 0, 0 -> "<B>" ^ (render_switch sw) ^ "</B>"
87 Printf.sprintf "<B>|<SUB>%d</SUB>: %s</B>" pos (render_switch sw)
88 | 1, pos when Stack.head_tag s#stack = `BranchTag ->
89 Printf.sprintf "|<SUB>%d</SUB> : %s" pos (render_switch sw)
90 | _ -> render_switch sw)
93 prerr_endline "pippo1";
95 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () markup in
96 let markup = "<metaname>" ^ markup ^ "</metaname>" in
97 prerr_endline "pippo2";
99 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () (sequent sw)
101 let txt0 = "<goal>" ^ sequent ^ "</goal>" in
102 "<meta number=\"" ^ (string_of_int metano) ^ "\">" ^ markup ^
103 txt0 ^ "</meta>" ^ acc
105 let res = "<metasenv>" ^
107 ~env:(render_sequent true) ~cont:(render_sequent false)
108 ~todo:(render_sequent false) "" s#stack) ^
111 prerr_endline ("sending metasenv:\n" ^ res); res
114 (* let html_of_status s =
115 let _,_,metasenv,subst,_ = s#obj in
116 let txt = List.fold_left
117 (fun acc (nmeta,_ as meta) ->
118 let txt0 = snd (ApplyTransformation.ntxt_of_cic_sequent
119 ~metasenv ~subst ~map_unicode_to_tex:false 80 s meta)
121 prerr_endline ("### txt0 = " ^ txt0);
122 ("<B>Goal ?" ^ (string_of_int nmeta) ^ "</B>\n" ^ txt0)::acc)
125 String.concat "\n\n" txt
128 let heading_nl_RE = Pcre.regexp "^\\s*\n\\s*";;
131 let s = Pcre.replace ~rex:heading_nl_RE s in
133 let nl_pos = String.index s '\n' in
134 String.sub s 0 nl_pos
138 let read_file fname =
139 let chan = open_in fname in
140 let lines = ref [] in
143 lines := input_line chan :: !lines
145 with End_of_file -> close_in chan);
146 String.concat "\n" (List.rev !lines)
149 let load_index outchan =
150 let s = read_file "index.html" in
151 Http_daemon.respond ~headers:["Content-Type", "text/html"] ~code:(`Code 200) ~body:s outchan
154 let load_doc filename outchan =
155 let s = read_file filename in
157 try String.sub filename (String.length filename - 4) 4 = ".png"
158 with Invalid_argument _ -> false
160 let contenttype = if is_png then "image/png" else "text/html" in
161 Http_daemon.respond ~headers:["Content-Type", contenttype] ~code:(`Code 200) ~body:s outchan
164 let retrieve (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
165 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
166 let env = cgi#environment in
168 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
169 let sid = HExtlib.unopt sid in
172 ~content_type:"text/xml; charset=\"utf-8\""
174 let filename = cgi # argument_value "file" in
175 prerr_endline ("reading file " ^ filename);
177 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false ()
178 (read_file filename) in
179 prerr_endline ("sending:\nBEGIN\n" ^ body ^ "\nEND");
180 let body = "<file>" ^ body ^ "</file>" in
181 let baseuri, incpaths =
183 let root, baseuri, _fname, _tgt =
184 Librarian.baseuri_of_script ~include_paths:[] filename in
187 Str.split (Str.regexp " ")
188 (List.assoc "include_paths" (Librarian.load_root_file (root^"/root")))
191 let rc = root :: includes in
192 List.iter (HLog.debug) rc; baseuri, rc
194 Librarian.NoRootFor _ | Librarian.FileNotFound _ -> "",[] in
195 include_paths := incpaths;
196 let status = MatitaAuthentication.get_status sid in
197 MatitaAuthentication.set_status sid (status#set_baseuri baseuri);
198 cgi#out_channel#output_string body;
202 ~status:`Internal_server_error
204 ~content_type:"text/xml; charset=\"utf-8\""
206 cgi#out_channel#commit_work()
209 let advance0 sid text =
210 let status = MatitaAuthentication.get_status sid in
211 let history = MatitaAuthentication.get_history sid in
212 let (st,new_statements,new_unparsed),(* newtext TODO *) _,parsed_len =
214 eval_statement !include_paths (*buffer*) status (`Raw text)
215 (* with End_of_file -> raise Margin *)
217 MatitaAuthentication.set_status sid st;
218 MatitaAuthentication.set_history sid (st::history);
219 parsed_len, new_unparsed, st
221 let register (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
222 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
223 let env = cgi#environment in
225 assert (cgi#arguments <> []);
226 let uid = cgi#argument_value "userid" in
227 let userpw = cgi#argument_value "password" in
229 MatitaAuthentication.add_user uid userpw;
230 env#set_output_header_field "Location" "/index.html"
231 with MatitaAuthentication.UsernameCollision _ ->
234 ~content_type:"text/html; charset=\"utf-8\""
236 cgi#out_channel#output_string
237 "<html><head></head><body>Error: User id collision!</body></html>");
238 cgi#out_channel#commit_work()
241 let login (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
242 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
243 let env = cgi#environment in
246 assert (cgi#arguments <> []);
247 let uid = cgi#argument_value "userid" in
248 let userpw = cgi#argument_value "password" in
249 prerr_endline ("2: user = " ^ uid);
250 let pw,_ = MatitaAuthentication.lookup_user uid in
256 let sid = MatitaAuthentication.create_session uid in
257 (* let cookie = Netcgi.Cookie.make "session" (Uuidm.to_string sid) in
258 cgi#set_header ~set_cookies:[cookie] (); *)
259 env#set_output_header_field
260 "Set-Cookie" ("session=" ^ (Uuidm.to_string sid));
261 env#set_output_header_field "Location" "/index.html"
267 ~content_type:"text/html; charset=\"utf-8\""
269 cgi#out_channel#output_string
270 "<html><head></head><body>Authentication error</body></html>"
273 cgi#out_channel#commit_work()
277 let logout (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
278 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
279 let env = cgi#environment in
281 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
282 let sid = HExtlib.unopt sid in
283 MatitaAuthentication.logout_user sid;
286 ~content_type:"text/html; charset=\"utf-8\""
288 let text = read_file (rt_path () ^ "/logout.html") in
289 cgi#out_channel#output_string text
293 ~status:`Internal_server_error
295 ~content_type:"text/xml; charset=\"utf-8\""
297 cgi#out_channel#commit_work()
300 (* returns the length of the executed text and an html representation of the
302 let advance (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
303 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
304 let env = cgi#environment in
306 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
307 let sid = HExtlib.unopt sid in
310 ~content_type:"text/xml; charset=\"utf-8\""
312 let text = cgi#argument_value "body" in
313 prerr_endline ("body =\n" ^ text);
314 let parsed_len, new_unparsed, new_status = advance0 sid text in
315 let txt = output_status new_status in
317 "<response><parsed length=\"" ^ (string_of_int parsed_len) ^ "\" />" ^ txt
320 prerr_endline ("sending advance response:\n" ^ body);
321 cgi#out_channel#output_string body
325 ~status:`Internal_server_error
327 ~content_type:"text/xml; charset=\"utf-8\""
329 cgi#out_channel#commit_work()
332 let gotoBottom (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
333 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
334 let env = cgi#environment in
336 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
337 let sid = HExtlib.unopt sid in
339 let rec aux parsed_len text =
341 prerr_endline ("evaluating: " ^ first_line text);
342 let plen,new_unparsed,_new_status = advance0 sid text in
343 aux (parsed_len+plen) new_unparsed
349 ~content_type:"text/xml; charset=\"utf-8\""
351 let text = cgi#argument_value "body" in
352 prerr_endline ("body =\n" ^ text);
353 let parsed_len = aux 0 text in
354 let status = MatitaAuthentication.get_status sid in
355 let txt = output_status status in
357 "<response><parsed length=\"" ^ (string_of_int parsed_len) ^ "\" />" ^ txt
360 prerr_endline ("sending goto bottom response:\n" ^ body);
361 cgi#out_channel#output_string body
362 with Not_found -> cgi#set_header ~status:`Internal_server_error
364 ~content_type:"text/xml; charset=\"utf-8\"" ());
365 cgi#out_channel#commit_work()
368 let retract (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
369 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
370 let env = cgi#environment in
372 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
373 let sid = HExtlib.unopt sid in
376 ~content_type:"text/xml; charset=\"utf-8\""
378 let history = MatitaAuthentication.get_history sid in
379 let new_history,new_status =
381 _::(status::_ as history) ->
383 | [_] -> (prerr_endline "singleton";failwith "retract")
384 | _ -> (prerr_endline "nil"; assert false) in
385 NCicLibrary.time_travel new_status;
386 MatitaAuthentication.set_history sid new_history;
387 MatitaAuthentication.set_status sid new_status;
388 prerr_endline ("after retract history.length = " ^
389 string_of_int (List.length new_history));
390 let body = output_status new_status in
391 cgi#out_channel#output_string body
392 with _ -> cgi#set_header ~status:`Internal_server_error
394 ~content_type:"text/xml; charset=\"utf-8\"" ());
395 cgi#out_channel#commit_work()
399 open Netcgi1_compat.Netcgi_types;;
401 (**********************************************************************)
402 (* Create the webserver *)
403 (**********************************************************************)
407 let (opt_list, cmdline_cfg) = Netplex_main.args() in
409 let use_mt = ref true in
412 [ "-mt", Arg.Set use_mt,
413 " Use multi-threading instead of multi-processing"
418 (fun s -> raise (Arg.Bad ("Don't know what to do with: " ^ s)))
419 "usage: netplex [options]";
422 Netplex_mt.mt() (* multi-threading *)
424 Netplex_mp.mp() in (* multi-processing *)
427 { Nethttpd_services.dyn_handler = (fun _ -> process1);
428 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
429 dyn_uri = None; (* not needed *)
430 dyn_translator = (fun _ -> ""); (* not needed *)
431 dyn_accept_all_conditionals = false;
435 { Nethttpd_services.dyn_handler = (fun _ -> advance);
436 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
437 dyn_uri = None; (* not needed *)
438 dyn_translator = (fun _ -> ""); (* not needed *)
439 dyn_accept_all_conditionals = false;
442 { Nethttpd_services.dyn_handler = (fun _ -> retract);
443 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
444 dyn_uri = None; (* not needed *)
445 dyn_translator = (fun _ -> ""); (* not needed *)
446 dyn_accept_all_conditionals = false;
449 { Nethttpd_services.dyn_handler = (fun _ -> gotoBottom);
450 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
451 dyn_uri = None; (* not needed *)
452 dyn_translator = (fun _ -> ""); (* not needed *)
453 dyn_accept_all_conditionals = false;
456 { Nethttpd_services.dyn_handler = (fun _ -> retrieve);
457 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
458 dyn_uri = None; (* not needed *)
459 dyn_translator = (fun _ -> ""); (* not needed *)
460 dyn_accept_all_conditionals = false;
463 { Nethttpd_services.dyn_handler = (fun _ -> register);
464 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
465 dyn_uri = None; (* not needed *)
466 dyn_translator = (fun _ -> ""); (* not needed *)
467 dyn_accept_all_conditionals = false;
470 { Nethttpd_services.dyn_handler = (fun _ -> login);
471 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
472 dyn_uri = None; (* not needed *)
473 dyn_translator = (fun _ -> ""); (* not needed *)
474 dyn_accept_all_conditionals = false;
477 { Nethttpd_services.dyn_handler = (fun _ -> logout);
478 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
479 dyn_uri = None; (* not needed *)
480 dyn_translator = (fun _ -> ""); (* not needed *)
481 dyn_accept_all_conditionals = false;
484 let nethttpd_factory =
485 Nethttpd_plex.nethttpd_factory
486 ~handlers:[ "advance", do_advance
487 ; "retract", do_retract
488 ; "bottom", goto_bottom
490 ; "register", do_register
492 ; "logout", do_logout ]
494 MatitaInit.initialize_all ();
495 prerr_endline (MatitaFilesystem.checkout "ricciott");
496 MatitaAuthentication.deserialize ();
499 Netplex_log.logger_factories (* allow all built-in logging styles *)
500 Netplex_workload.workload_manager_factories (* ... all ways of workload management *)
501 [ nethttpd_factory ] (* make this nethttpd available *)
505 Sys.set_signal Sys.sigpipe Sys.Signal_ignore;