Forum

X-Sendfile support

Trex-512
24 January 2016, 14:59
Hello. Is there anyway to bail out of a CGI application and make the server serve a static file instead? Other servers have the X-Sendfile header, but this does not seem to work. I have seen an old post in the subject, but there was no followup there: https://www.hiawatha-webserver.org/forum/topic/948

A huge use-case for this is caching CGI responses like in this example hello.cgi:

#!/bin/bash
CACHENAME=cache/hello_cached.html""

#If cache is older than 30 minutes
if [ "`find -name $CACHENAME -mmin +30`" ]; then
printf '<html>' >> $CACHENAME
printf '<head>' >> $CACHENAME
printf '<title>Hello World</title>' >> $CACHENAME
printf '</head>' >> $CACHENAME
printf '<body>' >> $CACHENAME
printf 'Hello World' >> $CACHENAME
printf '</body>' >> $CACHENAME
printf '</html>' >> $CACHENAME
fi


printf "x-sendfile: $CACHENAME\r\n\r\n"
exit 0


exit 0
Trex-512
24 January 2016, 15:00
Note that this is just an exmaple. a cpu-heavy operation can be done inside the if block.
Hugo Leisink
24 January 2016, 15:01
You can redirect to that file via a Location header.
printf "Location: /path/to/file.ext\r\n\r\n"
Trex-512
24 January 2016, 15:11
But this redirects the client. I don't want the client to know the difference, and in fact the cache folder can be outside the server root dir.
Trex-512
24 January 2016, 15:24
Note for everyone, for the record: For the specific usage of cgi caching, X-Hiawatha-Cache is available.

But X-Sendfile would still be nice for other use cass.
Hugo Leisink
24 January 2016, 15:44
I implemented support for it. It will be available in v10.1.
Trex-512
24 January 2016, 19:58
Thank you for the fast response
Note that it may be subject to the same problem as this one:
https://www.hiawatha-webserver.org/forum/topic/2156
This topic has been closed.