seppo_bin.ml 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. module C = Cgi
  27. open Seppo_lib
  28. open Astring
  29. (*
  30. * Being a CGI[1] means #Seppo is a binary executable reading from stdin
  31. * writing to stdout in the first place. The Webserver puts incoming HTTP Header
  32. * variables into the environment.
  33. *
  34. * Serving dynamic content via CGI can be as simple as putting the executable
  35. * into the web directory of e.g. an Apache webserver and entering the according
  36. * http url into your browser.
  37. *
  38. * Besides that, seppo.cgi has housekeeping commands for the commandline.
  39. *
  40. * [1] RFC3875 The Common Gateway Interface https://www.rfc-editor.org/rfc/rfc3875.html
  41. *)
  42. let () =
  43. Mirage_crypto_rng_lwt.initialize (module Mirage_crypto_rng.Fortuna);
  44. let uuid = Uuidm.v `V4 in
  45. let resp_err ec ?(hdrs = [Http.H.ct_plain]) status msg =
  46. (try Logr.err (fun m -> m "%a %s %s" Uuidm.pp uuid ec msg);
  47. with _ -> ());
  48. Cgi.Response.flush uuid stdout (status, hdrs, fun oc ->
  49. let s = status |> Cohttp.Code.string_of_status in
  50. Printf.eprintf "FATAL: %s\r\nsee %s\r\n%s\r\n" s ec msg;
  51. Printf.fprintf oc "Status: %s\r\n\r\n%s\r\n%s" s ec msg ) in
  52. (try match Cgi.Request.(from_env () |> consolidate |> proxy) with
  53. | Error "Not Found." ->
  54. (* some CLI commands are fine without logging. At least -h and -V have to. *)
  55. Sys.argv
  56. |> Array.to_list
  57. |> Shell.exec
  58. | Error msg ->
  59. resp_err E.e1036 `Internal_server_error msg
  60. | Ok req ->
  61. req.script_name
  62. |> Cgi.cd_cgi_bin_twin_path
  63. |> Unix.chdir;
  64. match "app/var/log/" |> File.mkdir_p 0o770 with
  65. | Error msg -> resp_err E.e1035 `Internal_server_error msg
  66. | Ok log_dir ->
  67. let tnow = Ptime_clock.now () in
  68. assert (log_dir |> St.is_suffix ~affix:"/");
  69. Logr.open_out (log_dir ^ "seppo.log");
  70. let r = req
  71. |> C.handle uuid tnow stdin
  72. |> Cgi.Response.flush uuid stdout in
  73. close_in stdin;
  74. (* closing kills the cgi: close_out stdout; *)
  75. flush stderr;
  76. (* L.close_out (); *)
  77. r
  78. with
  79. | Unix.(Unix_error(ENOENT, "chdir", dir)) -> resp_err E.e1037 `Internal_server_error ("cd: The directory '" ^ dir ^ "' does not exist")
  80. | Sys_error msg -> resp_err E.e1034 `Internal_server_error msg
  81. | e -> resp_err E.e1005 `Internal_server_error (Printexc.to_string e) )
  82. |> exit