apchk.ml 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. (*
  2. * _ _ ____ _
  3. * _| || |_/ ___| ___ _ __ _ __ ___ | |
  4. * |_ .. _\___ \ / _ \ '_ \| '_ \ / _ \| |
  5. * |_ _|___) | __/ |_) | |_) | (_) |_|
  6. * |_||_| |____/ \___| .__/| .__/ \___/(_)
  7. * |_| |_|
  8. *
  9. * Personal Social Web.
  10. *
  11. * Copyright (C) The #Seppo contributors. All rights reserved.
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *)
  26. let () =
  27. let module Cg = Seppo_lib.Cgi in
  28. let module E = Seppo_lib.E in
  29. let module Http = Seppo_lib.Http in
  30. let module L = Seppo_lib.Logr in
  31. Mirage_crypto_rng_unix.use_default ();
  32. let uuid = () |> (() |> Random.State.make_self_init |> Uuidm.v4_gen) in
  33. let resp_err ec ?(hdrs = [Http.H.ct_plain]) status msg =
  34. (try L.err (fun m -> m "%a %s %s" Uuidm.pp uuid ec msg);
  35. with _ -> ());
  36. Cg.Response.flush uuid stdout (status, hdrs, fun oc ->
  37. let s = status |> Cohttp.Code.string_of_status in
  38. Printf.eprintf "FATAL: %s\r\nsee %s\r\n%s\r\n" s ec msg;
  39. Printf.fprintf oc "Status: %s\r\n\r\n%s\r\n%s" s ec msg ) in
  40. (try match Cg.Request.(from_env () |> consolidate |> proxy) with
  41. | Error _ -> Sys.argv
  42. |> Array.to_list
  43. |> Shell.exec
  44. | Ok req ->
  45. let uuid = Uuidm.v4_gen (Random.State.make_self_init ()) () in
  46. req
  47. |> Cgi.handle uuid stdin
  48. |> Cg.Response.flush uuid stdout
  49. with
  50. | Unix.Unix_error(ENOENT, "chdir", dir) -> resp_err E.e1037 `Internal_server_error ("cd: The directory '" ^ dir ^ "' does not exist")
  51. | Sys_error msg -> resp_err E.e1034 `Internal_server_error msg
  52. | e -> resp_err E.e1005 `Internal_server_error (Printexc.to_string e) )
  53. |> exit