From 2043d29fa12ad5f58cc288f49818028f5fefd363 Mon Sep 17 00:00:00 2001 From: =?utf8?q?cat=20=C3=A6scling?= <11325618-aescling@users.noreply.gitlab.com> Date: Tue, 25 Jul 2023 19:33:07 -0400 Subject: [PATCH] Implement Content-Type override Document accordingly Ignore any Content-Type headers in `configuration/override/${resource}/headers` * Remove empty line comments --- README.md | 10 +++++++--- .../binaries/http-print-header-files.execline | 11 +++++++++++ visible-to-httpd/binaries/httpd.execline | 10 ++++++---- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7b51078..a209adc 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ will be served with a `Content-Type` of `{1}/${2}` (with colons in `index.text=x:market` will always be served with a `Content-Type` of `text/x.market`. 2. using the per-resource `overrides` folder (see below) to specify a -`Content-Type` header explicitly. +`Content-Type` header explicitly (but not in the `headers` subdirectory). #### `./visible-to-httpd/configuration/default_headers/` ### @@ -147,9 +147,13 @@ this directory allows you to override the extra headers sent along with a resource, and attach a status code other than 200 with them. a folder named after the specific resource (including a prepended hostname) should may contain a `status_code` file containing a -numerical status code and optional textual message, as well as a +numerical status code and optional textual message, a `headers` folder, which specifies headers using the `default_headers` -scheme. +scheme, and a `Content-Type` file, to override any other mechanism for +determining the Content-Type to send to the client. + +Note that a `Content-Type` file in the `headers` folder is always +ignored. a former official website for `httpd-execline.eerie.garden` used to redirect to this github repository, thanks to diff --git a/visible-to-httpd/binaries/http-print-header-files.execline b/visible-to-httpd/binaries/http-print-header-files.execline index 230d976..9886081 100755 --- a/visible-to-httpd/binaries/http-print-header-files.execline +++ b/visible-to-httpd/binaries/http-print-header-files.execline @@ -6,6 +6,8 @@ # instances of ${hostname} and ${resource} are substituted with the provided # host and resource from the request # +# the script will refuse to override the Content-Type header +# # execs into self in order to retain an environment variable which marks # whether a certain header has been overridden or not @@ -31,6 +33,15 @@ backtick -E -n header_name { sed "s@.*/([^/]*)@\\1@" } +ifelse { + pipeline { s6-echo -n -- \${header_name} } + grep -s -i '^[ ]*Content-Type[ ]*$' # paranoid whitespace matching +} +{ + foreground { log.execline "refusing to override Content-Type header" } + http-print-header-directories.execline ${@} +} + # short circuit on overridden header ifelse { s6-test -v http_print_header_directories_${header_name} } { diff --git a/visible-to-httpd/binaries/httpd.execline b/visible-to-httpd/binaries/httpd.execline index b38f3dd..2860e6e 100755 --- a/visible-to-httpd/binaries/httpd.execline +++ b/visible-to-httpd/binaries/httpd.execline @@ -147,8 +147,13 @@ if -X -n -t { #### 4. send response ### ##### 4.1. determine found resource's Content-Type ### - # backtick -i -n Content-Type { + define Content_Type_override_file configuration/overrides/${resourse}/Content-Type + ifelse { s6-test -f ${Content_Type_override_file} -a -r ${Content_Type_override_file} } + { + cat ${Content_Type_override_file} + } + backtick -D "no.extension" -n extension { pipeline { printenv resource } # strip everything up to the non-periods after the final @@ -162,12 +167,9 @@ if -X -n -t { # with `Content-Type` `${1}/${2}`. colons in the extension are # transformed into periods, allowing files like # `index.text=x:market` being served as `text/x.market` - # - # this overrides any other Content-Type determination mechanism ifelse { pipeline { printenv extension } # this regex matches exactly what `publicfile` does - # grep -s "[a-zA-Z0-9]+=[^=]+$" } { -- 2.47.3