mod_xsendfile is a small Apache2 module that processes X-SENDFILE headers registered by the original output handler.
If it encounters the presence of such header it will discard all output and send the file specified by that header instead using Apache internals including all optimizations like caching-headers and sendfile or mmap if configured.
It is useful for processing script-output of e.g. php, perl or any cgi.
Yep, it is useful.
apxs -cia mod_xsendfile.c
X-SENDFILE
- Send the file referenced by this headers instead of the current response bodyX-SENDFILE-TEMPORARY
- Like X-SENDFILE
, but the file will be deleted afterwards. The file must originate from a path that has the AllowFileDelete
flag enabled.Description | Enables or disables header processing |
---|---|
Syntax | XSendFile on|off |
Default | XSendFile off |
Context | server config, virtual host, directory, .htaccess |
Setting XSendFile on
will enable
processing.
The file specified in X-SENDFILE
header will be sent instead of the handler output.
The value (file name) given by the header is assmumed to be url-encoded, i.e. unescaping/url-decoding will be performed. See XSendFileUnescape.
If you happen to store files using already url-encoded file names, you must "double" encode the names... %20 -> %2520
If the response lacks the X-SENDFILE
header nothing is done.
Description | Ignore script provided Etag headers |
---|---|
Syntax | XSendFileIgnoreEtag on|off |
Default | XSendFileIgnoreEtag off |
Context | server config, virtual host, directory, .htaccess |
Setting XSendFileIgnoreEtag on
will ignore all ETag headers the original output handler may have set.
This is helpful for applications that will generate such headers even for empty content.
Description | Ignore script provided LastModified headers |
---|---|
Syntax | XSendFileIgnoreLastModified on|off |
Default | XSendFileIgnoreLastModified off |
Context | server config, virtual host, directory, .htaccess |
Setting XSendFileIgnoreLastModified on
will ignore all Last-Modified headers the original output handler may have set.
This is helpful for applications that will generate such headers even for empty content.
Description | Unescape/url-decode the value of the header |
---|---|
Syntax | XSendFileUnescape on|off |
Default | XSendFileUnescape on |
Context | server config, virtual host, directory, .htaccess |
Setting XSendFileUnescape off
will restore the pre-1.0 behavior of using the raw header value, instead of trying to unescape/url-decode first.
Headers may only contain a certain ASCII subset, as dictated by the corresponding RFCs/protocol. Hence you should escape/url-encode (and have XSendFile unescape/url-decode) the header value. Failing to keep within the bounds of that ASCII subset might cause errors, depending on your application framework.
Hence this setting is meant only for backwards-compatibility with legacy applications expecting the old behavior; new application should url-encode the value correctly and leave XSendFileUnescape on
.
Description | White-list more paths |
---|---|
Syntax | XSendFilePath <absolute path> [AllowFileDelete ] |
Default | None |
Context | server config, virtual host, directory |
XSendFilePath allow you to add additional paths to some kind of white list. All files within these paths are allowed to get served through mod_xsendfile.
Provide an absolute path as Parameter to this directive.
If the optional AllowFileDelete
flag is specified, then files under this path can be served using the X-SENDFILE-TEMPORARY
header, and will then be deleted once the file is delievered.
Hence you should only set the AllowFileDelete
flag for paths that do not hold any files that shouldn't be deleted!
You may provide more than one path.
The current working directory (if it can be determined) will be always checked first.
If you provide relative paths via the X-SendFile header, then all whitelist items will be checked until a seamingly valid combination is found, i.e. the result is within the bounds of the whitelist item; it isn't checked at this point if the path in question actually exists.
Considering you whitelisted /tmp/pool
and /tmp/pool2
and your script working directory is /var/www
.
X-SendFile: file
/var/www/file
- Within bounds of /var/www
, OKX-SendFile: ../pool2/file
/var/www/../pool2/file = /var/pool2/file
- Not within bounds of /var/www
/tmp/pool/../pool2/file = /tmp/pool2/file
- Not within bounds of /tmp/pool
/tmp/pool2/../pool2/file = /tmp/pool2/file
- Within bounds of /tmp/pool2
, OKYou still can only access paths that are whitelisted. However you have might expect a different behavior here, hence the documentation.
The white list "inherits" entries from higher level configuration.
XSendFilePath /tmp <VirtualHost *> ServerName someserver XSendFilePath /home/userxyz </VirtualHost> <VirtualHost *> ServerName anotherserver XSendFilePath /var/www/somesite/ <Directory /var/www/somesite/fastcgis> XSendFilePath /var/www/shared </Directory> </VirtualHost>
Above example will give:
/tmp
/tmp
/home/userxyz
/tmp
/var/www/somesite
/var/www/shared
(for scripts* located in /var/www/somesite/fastcgis)*) Scripts, in this context, mean the actual script-starters. E.g. PHP as a handler will use the .php itself, while in CGI mode refers to the starter.
Windows users should take care to include the drive letter to those paths as well. Tests show that it has to be in upper-case.
.htaccess
<Files out.php>
XSendFile on
</Files>
out.php
<?php
...
if ($user->isLoggedIn())
{
header("X-Sendfile: $path_to_somefile");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$somefile\"");
exit;
}
?>
<h1>Permission denied</h1>
<p>Login first!</p>
Content-Encoding
header - if present - will be dropped, as the module
cannot know if it was set by intention of the programmer or the
handler. E.g. php with output compression enabled will set this header,
but the replacement file send via mod_xsendfile is most likely not
compressed.The idea comes from lighttpd - A fast web server with minimal memory footprint.
The module itself was inspired by many other Apache2 modules such as mod_rewrite, mod_headers and obviously core.c.
Copyright 2006-2010 by Nils Maier
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
XSendFileUnescape
setting, to support legacy applicationsX-SENDFILE-TEMPORARY
header and corresponding AllowFileDelete
flag