From b50a52a4963c7243cee912f36714acd64a1b9f42 Mon Sep 17 00:00:00 2001 From: David Hicks Date: Tue, 5 Jan 2010 19:36:00 +1100 Subject: [PATCH] Issue #11291: Add X-Sendfile support for high performance file downloads 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 --- config_defaults_inc.php | 20 +++++++++++ docbook/adminguide/en/configuration.sgml | 43 ++++++++++++++++++------ file_download.php | 7 +++- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/config_defaults_inc.php b/config_defaults_inc.php index 4aad19eacc..66380e3758 100644 --- a/config_defaults_inc.php +++ b/config_defaults_inc.php @@ -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 * **************************/ diff --git a/docbook/adminguide/en/configuration.sgml b/docbook/adminguide/en/configuration.sgml index 842fdc81d8..7ab743f669 100644 --- a/docbook/adminguide/en/configuration.sgml +++ b/docbook/adminguide/en/configuration.sgml @@ -1353,12 +1353,12 @@
File Upload - 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 + 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: PHP Manual @@ -1478,12 +1478,35 @@ $g_fileinfo_magic_db_file 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. - + 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. + + + + + $g_file_download_xsendfile_enabled + + 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. + + + + + $g_file_download_xsendfile_header_name + + 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. + diff --git a/file_download.php b/file_download.php index 8edd4294ba..ef3937e0a0 100644 --- a/file_download.php +++ b/file_download.php @@ -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: