Skip to content

Commit

Permalink
Issue #11291: Add X-Sendfile support for high performance file downloads
Browse files Browse the repository at this point in the history
X-Sendfile is a header that is added to the output response from PHP
that points to a local file on the disk. Upon receiving the header once
the PHP script has terminated, the web server proceeds to replace all
content in the HTTP response with the contents of the file served up
locally from the disk. Headers set in the PHP script are usually kept
in-tact (depending on the web server implementation as to what gets
changed).

This technique is meant to improve performance as it now becomes
possible for the web server to use highly efficient file serving
functions to serve up the static content. For example, a web server
could use a zero-copy splice() call on Linux platforms to prevent
unnecessary buffering, drastically improving performance.

Servers that support X-Sendfile (or equivalent, as determined by the
$g_file_download_xsendfile_header_name option) include:
* Lighttpd
* Cherokee
* Apache (with mod_xsendfile)
* nginx
  • Loading branch information
davidhicks committed Jan 5, 2010
1 parent 45771c6 commit b50a52a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
20 changes: 20 additions & 0 deletions config_defaults_inc.php
Expand Up @@ -1655,6 +1655,26 @@
*/
$g_absolute_path_default_upload_folder = '';

/**
* Enable support for sending files to users via a more efficient X-Sendfile
* method. HTTP server software supporting this technique includes Lighttpd,
* Cherokee, Apache with mod_xsendfile and nginx. You may need to set the
* proceeding file_download_xsendfile_header_name option to suit the server you
* are using.
* @global int $g_file_download_method
*/
$g_file_download_xsendfile_enabled = OFF;

/**
* The name of the X-Sendfile header to use. Each server tends to implement
* this functionality in a slightly different way and thus the naming
* conventions for the header differ between each server. Lighttpd from v1.5,
* Apache with mod_xsendfile and Cherokee web servers use X-Sendfile. nginx
* uses X-Accel-Redirect and Lighttpd v1.4 uses X-LIGHTTPD-send-file.
* @global string $g_file_download_xsendfile_header_name
*/
$g_file_download_xsendfile_header_name = 'X-Sendfile';

/**************************
* MantisBT HTML Settings *
**************************/
Expand Down
43 changes: 33 additions & 10 deletions docbook/adminguide/en/configuration.sgml
Expand Up @@ -1353,12 +1353,12 @@
<section id="admin.config.uploads">
<title>File Upload</title>

<para>MantisBT allows users to upload file attachements and
associated them with bugs as well as projects. Bug attachments /
project documents can be uploaded to the webserver, database, or an
<para>MantisBT allows users to upload file attachments and
associate them with bugs as well as projects. Bug attachments /
project documents can be uploaded to the webserver, database or an
FTP server. When bugs are uploaded to the webserver they are
uploaded to the path that is configured in the project
properties.In case of problems getting the file upload feature to
properties. In case of problems getting the file upload feature to
work, check the following resources:
<ulink url="http://www.php.net/manual/en/features.file-upload.php">PHP
Manual
Expand Down Expand Up @@ -1478,12 +1478,35 @@
<term>$g_fileinfo_magic_db_file</term>
<listitem>
<para>Specify the filename of the magic database file.
This is used by PHP 5.3.0 (or earlier versions with the
fileinfo PECL extension) to guess what the MIME type of a
file is. Usually it is safe to leave this setting as the
default (blank) as PHP is usually able to find this file
by itself.
</para>
This is used by PHP 5.3.0 (or earlier versions with the
fileinfo PECL extension) to guess what the MIME type of a
file is. Usually it is safe to leave this setting as the
default (blank) as PHP is usually able to find this file
by itself.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>$g_file_download_xsendfile_enabled</term>
<listitem>
<para>Enable support for sending files to users via a more efficient
X-Sendfile method. HTTP server software supporting this technique
includes Lighttpd, Cherokee, Apache with mod_xsendfile and nginx.
You may need to set the proceeding file_download_xsendfile_header_name
option to suit the server you are using.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>$g_file_download_xsendfile_header_name</term>
<listitem>
<para>The name of the X-Sendfile header to use. Each server tends to
implement this functionality in a slightly different way and thus
the naming conventions for the header differ between each server.
Lighttpd from v1.5, Apache with mod_xsendfile and Cherokee web
servers use X-Sendfile. nginx uses X-Accel-Redirect and Lighttpd
v1.4 uses X-LIGHTTPD-send-file.
</para>
</listitem>
</varlistentry>
</variablelist>
Expand Down
7 changes: 6 additions & 1 deletion file_download.php
Expand Up @@ -155,7 +155,12 @@
}

header( 'Content-Type: ' . $t_content_type );
file_send_chunk( $t_local_disk_file );
if ( config_get( 'file_download_xsendfile_enabled' ) ) {
$t_xsendfile_header_name = config_get( 'file_download_xsendfile_header_name' );
header( $t_xsendfile_header_name . ': ' . $t_local_disk_file );
} else {
file_send_chunk( $t_local_disk_file );
}
}
break;
case FTP:
Expand Down

0 comments on commit b50a52a

Please sign in to comment.