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