]> cat aescling's git repositories - httpd-execline.git/commitdiff
allow less specific headers to take effect origin/better-headers
authorsingle-right-quote <34298117+single-right-quote@users.noreply.github.com>
Sun, 12 Sep 2021 03:53:10 +0000 (23:53 -0400)
committersingle-right-quote <34298117+single-right-quote@users.noreply.github.com>
Sun, 12 Sep 2021 04:44:38 +0000 (00:44 -0400)
visible-to-httpd/binaries/http-get-extra-headers.execline
visible-to-httpd/binaries/http-print-header-directories.execline [new file with mode: 0644]

index 03432b6840902bc8e333f0b0b4d38499b75df8fb..e48c8bad74cf8ab6708c4b6d18d0f70850f9ec5c 100755 (executable)
 
 multisubstitute {
        importas -D -no_hostname_parsed hostname http_header_parse_Host
-       importas -i requested_resource http_start_line_parse_resource
-       importas -i resource resource
+       importas -D -no_resource_parsed resource resource
 }
-define header_substitution_script
-"multisubstitute {
-       define hostname "${hostname}"
-       define resource "${requested_resource}"
-}
-s6-echo -n -- "
-
-backtick -x -n header-directory {
-       # for status code pages specifically
-       ifelse {
-               s6-test \${#} != 0
-                       -a -d configuration/error_response_pages/${1}/headers
-                       -a -r configuration/error_response_pages/${1}/headers
-       }
-       {
-               s6-echo -n -- configuration/error_response_pages/${1}/headers
-       }
-
-       # otherwise: prioritize header specifications in order of specificity:
-       # by resource, then by hostname, then the fallback (and finally nothing)
-       ifelse {
-               s6-test -d configuration/overrides/${resource}/headers
-                       -a -r configuration/overrides/${resource}/headers
-       }
-       {
-               s6-echo -n -- configuration/overrides/${resource}/headers
-       }
-       ifelse {
-               s6-test -d configuration/default_headers/${hostname}
-                       -a -r configuration/default_headers/${hostname}
-       }
-       {
-               s6-echo -n -- configuration/default_headers/${hostname}
-       }
-       # the preferred name for a DNS lookup should begin with an alphanumeric,
-       # so “-fallback”, since it begins with a hyphen, should never conflict
-       # with a hostname anybody would ever actually use
-       ifelse {
-               s6-test -d configuration/default_headers/-fallback
-                       -a -r configuration/default_headers/-fallback
-       }
-       {
-               s6-echo -n -- configuration/default_headers/-fallback
-       }
-               exit 1
-}
-
-# (we output nothing if there is no applicable header directory, which is fine)
-if -t { s6-test -v header-directory }
-       # to avoid double \r\n in case we don't output anything
-       if { s6-echo -n -- "\r\n" }
-
-       importas -i -u header-directory header-directory
-
-       foreground { log.execline "header-directory: "${header-directory} }
-
-       cd ${header-directory}
-       elglob -0 -s header_names *
-
-       # we’ll strip out `\r`s and `\n`s from filenames and file contents, in
-       # case the configuration should ever be made in a mischevious way
-       forx header_name { ${header_names} }
-               importas -i -u header_name header_name
-               if {
-                       pipeline { s6-echo -n -- ${header_name}": " }
-                       tr -d "\r\n"
-               }
-               if {
-                       backtick -E -n unparsed_header_contents {
-                               pipeline { cat ${header_name} }
-                               tr -d "\r\n"
-                       }
-                       execlineb -WPc ${header_substitution_script}${unparsed_header_contents}
-               }
-
-               s6-echo -n -- "\r\n"
-       # do remember to add the terminating `\r\n`!
 
+elglob -0 -s error_response_page_headers configuration/error_response_pages/${1}/headers/*
+elglob -0 -s resource_headers configuration/overrides/${resource}/headers/*
+elglob -0 -s hostname_headers configuration/default_headers/${hostname}/*
+elglob -0 -s fallback_headers configuration/default_headers/-fallback/*
+
+if { s6-echo -n -- "\r\n" }
+http-print-header-directories.execline
+       ${error_response_page_headers}
+       ${resource_headers}
+       ${hostname_headers}
+       ${fallback_headers}
diff --git a/visible-to-httpd/binaries/http-print-header-directories.execline b/visible-to-httpd/binaries/http-print-header-directories.execline
new file mode 100644 (file)
index 0000000..9506735
--- /dev/null
@@ -0,0 +1,61 @@
+#!/binaries/execlineb -W
+
+# quit if there are no arguments
+ifelse {
+       importas "#" "#"
+       s6-test ${#} = 0
+}
+{
+       exit 0
+}
+
+# otherwise, print the contents of the current header directory
+importas header_file 1
+shift
+elgetpositionals
+emptyenv -P
+
+backtick -E -n header_name {
+       pipeline { s6-echo -n -- ${header_file} }
+       sed "s@.*/([^/]*)@\\1@"
+}
+
+if { log.execline \"${header_file}\" \"${header_name}\" }
+
+# short circuit on a duplicate header name
+ifelse { s6-test -v http_print_header_directories_${header_name} }
+{
+       foreground { log.execline "ignoring duplicate header_name: "\"${header_name}\" }
+       http-print-header-directories.execline ${@}
+}
+
+# otherwise, print out the header line
+multisubstitute {
+       importas -D -no_hostname_parsed hostname http_header_parse_Host
+       importas -D -no_resource_parsed requested_resource http_start_line_parse_resource
+}
+define header_substitution_script
+"multisubstitute {
+       define hostname "${hostname}"
+       define resource "${requested_resource}"
+}
+s6-echo -n -- "
+
+# we’ll strip out `\r`s and `\n`s from filenames and file contents, in
+# case the configuration should ever be made in a mischevious way
+if {
+       pipeline { s6-echo -n -- ${header_name}": " }
+       tr -d "\r\n"
+}
+if {
+       backtick -E -n unparsed_header_contents {
+               pipeline { cat ${header_file} }
+                       tr -d "\r\n"
+       }
+       execlineb -WPc ${header_substitution_script}${unparsed_header_contents}
+}
+if { s6-echo -n -- "\r\n" }
+# do remember to add the terminating `\r\n`!
+
+export http_print_header_directories_${header_name} ""
+http-print-header-directories.execline ${@}