]> cat aescling's git repositories - httpd-execline.git/commitdiff
Add minimal cacheing support
authorcat æscling <aescling@cat.family>
Tue, 30 Apr 2024 02:00:51 +0000 (22:00 -0400)
committercat æscling <aescling@cat.family>
Tue, 30 Apr 2024 02:33:15 +0000 (22:33 -0400)
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

README.md
visible-to-httpd/binaries/httpd.execline

index f330ed00bf987ce4d5085d611bf6b5edfb82e55b..315acdba61bb02a58d57ae5e50685710d9e8dfd8 100644 (file)
--- 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&#x2019;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
index 27ec66fc214a47e4571dd9695ac371ed6ec2a7cd..cf55de16169b307791661eea82630df5e82f6b10 100755 (executable)
@@ -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
 "