From: cat æscling Date: Tue, 30 Apr 2024 02:00:51 +0000 (-0400) Subject: Add minimal cacheing support X-Git-Url: https://git.xn--scling-oua.cat.family/?a=commitdiff_plain;h=88944f026bfb5030c038ec26a3cdf49861af477e;p=httpd-execline.git Add minimal cacheing support If configured to do so, generate ETags on the fly fur every successfully found resource with `cksum(1)`. * Typos * Fix strange syntax error * Fix missing `"` * Typos * Wrap ETag generation in a conditional --- diff --git a/README.md b/README.md index f330ed0..315acdb 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,8 @@ feasible configuration is to place statically linked dependencies into - some 9base commands do not build on my system, but thankfully none of the tools required by httpd.execline fail. + [toybox](http://www.landley.net/toybox/): `wc(1)`, -`date(1p)`, `stat(1)`, `cat(1)`, `timeout(1)` +`date(1p)`, `stat(1)`, `cat(1)`, `timeout(1)`, `cksum(1)` (optional; used to +generate ETags) - toybox’s `tr(1)` is pending but seems to work + [busybox](https://www.busybox.net/): `gzip(1)` (optional; required for gzip encoding) @@ -211,3 +212,7 @@ containing `https://github.com/single-right-quote/httpd.execline` + `httpd_execline_default_index`: the file to default to if a resource matches a directory. defaults to `index.html` + `HTTPD_EXECLINE_GZIP`: if set, use `gzip(1)` to compress found resources. ++ `HTTPD_EXECLINE_GENERATE_ETAGS`: if set, use `cksum(1)` to generate `ETag`s + for all resources on the fly ++ `HTTPD_EXECLINE_MAX_AGE`: if `HTTPD_EXECLINE_CACHE` is set, this variable + controls the max-age directive. defaults to `604800` \ No newline at end of file diff --git a/visible-to-httpd/binaries/httpd.execline b/visible-to-httpd/binaries/httpd.execline index 27ec66f..cf55de1 100755 --- a/visible-to-httpd/binaries/httpd.execline +++ b/visible-to-httpd/binaries/httpd.execline @@ -114,6 +114,27 @@ if -X -n -t { s6-echo -n -- identity } + backtick -D "no-cache" Cache-Control { + if { eltest -v HTTPD_EXECLINE_GENERATE_ETAGS } + importas -D "604800" -u HTTPD_EXECLINE_MAX_AGE HTTPD_EXECLINE_MAX_AGE + s6-echo -n -- ${HTTPD_EXECLINE_MAX_AGE} + } + + backtick -D "" optional_etag_line { + if { eltest -v HTTPD_EXECLINE_GENERATE_ETAGS } + backtick -i -E -n cksum { + redirfd -r 0 supported_domains/${resource} + cksum + } + case -N -- ${cksum} { + "^([^ ]+) .*$" { + importas -i -u actual_cksum 1 + s6-echo -n -- "Etag: "${actual_cksum}"\r\n" + } + } + s6-echo -n -- '' + } + ##### 4.1. determine found resource's Content-Type ### backtick -i -n Content-Type { define Content_Type_override_file configuration/overrides/${resource}/Content-Type @@ -192,19 +213,23 @@ if -X -n -t { multisubstitute { importas -i -u output_executable output_executable importas -i -u status_code_and_message status_code_and_message + importas -i -u Cache-Control Cache-Control importas -i -u Content-Encoding Content-Encoding importas -i -u Content-Length Content-Length importas -i -u Content-Type Content-Type + importas -i -u optional_etag_line optional_etag_line importas -i -u Date Date importas -i -u Last-Modified Last-Modified importas -i -u extra_headers extra_headers } + if { log.execline line is ${optional_etag_line} } if { s6-echo -n -- "HTTP/1.1 "${status_code_and_message}"\r Content-Encoding: "${Content-Encoding}"\r Content-Type: "${Content-Type}"\r Content-Length: "${Content-Length}"\r -Last-Modified: "${Last-Modified}"\r +Cache-Control: "${Cache-Control}"\r +"${optional_etag_line}"\r\nLast-Modified: "${Last-Modified}"\r Date: "${Date}${extra_headers}" \r "