]> cat aescling's git repositories - httpd-execline.git/commitdiff
Add optional HTTP to HTTPS redirection script
authorcat æscling <11325618-aescling@users.noreply.gitlab.com>
Mon, 31 Jul 2023 20:10:34 +0000 (16:10 -0400)
committercat æscling <11325618-aescling@users.noreply.gitlab.com>
Mon, 31 Jul 2023 20:14:23 +0000 (16:14 -0400)
visible-to-httpd/binaries/redirect.execline [new file with mode: 0644]

diff --git a/visible-to-httpd/binaries/redirect.execline b/visible-to-httpd/binaries/redirect.execline
new file mode 100644 (file)
index 0000000..dff964c
--- /dev/null
@@ -0,0 +1,76 @@
+#!/command/execlineb -WP
+
+importas -i httpd_execline_jail_directory httpd_execline_jail_directory
+
+export PATH /binaries
+chroot ${httpd_execline_jail_directory}
+s6-applyuidgid -U -z
+
+export program_name redirect.execline
+
+# see end of script: handle crashes cleanly
+if -X -n -t {
+       http-start-line-parse.execline
+       multisubstitute {
+               importas -i -u method http_start_line_parse_method
+               importas -i requested_resource http_start_line_parse_resource
+       }
+       ifelse -n { eltest \${method} =~ HEAD|GET }
+       {
+               http-error-response.execline
+                       501
+                       "method not implemented"
+                       "unsupported method: \""${method}\"
+       }
+
+       http-header-parse.execline
+               supported-hostname-test.execline
+       importas -i hostname http_header_parse_host
+
+       fdclose 0
+
+       foreground {
+               log.execline
+                       "info:"
+                       "client request:"
+                       "for \""${hostname}\"":"
+                       \"${method}\"
+                       \"${requested_resource}\"":"
+                       "redirecting"
+       }
+
+       # TODO: file length in bytes: SHOULD be provided
+       backtick -E -n Date { date -u "+%a, %d %b %Y %T GMT" }
+       backtick -E -n Location { s6-echo -n -- "https://"${hostname}${requested_resource} }
+       if {
+               s6-echo -n -- "HTTP/1.1 301 moved permanently\r
+Location: "${Location}"\r
+Content-Type: "application/xhtml+xml; charset=utf-8"\r
+Date: "${Date}"\r
+" }
+       if -t { eltest \${method} = GET }
+               if {
+                       s6-echo -n -- "<!DOCTYPE html>
+<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">
+<head>
+       <meta charset=\"utf-8\" />
+       <title>moved permanently</title>
+       <style>html {
+       background: #FFFFEA;
+       color: #303020;
+       font-family: Tinos, serif;
+       text-align: center;
+}</style></head>
+<body>
+<h1>moved permanently</h1>
+<p>this website <a href=\""${Location}"\">uses https</a></p>
+</body>
+</html>\n"
+               }
+                       # hack
+                       s6-sleep -m 500
+}
+       http-error-response.execline
+               500
+               "internal server error"
+               "(i/o error? timeout?)"