From 46f97f4c389572eb1edac30d8cfa7086835c58fb Mon Sep 17 00:00:00 2001 From: mvn23 Date: Wed, 19 Jun 2013 15:36:48 +0200 Subject: [PATCH 1/4] Implement X-Sendfile2 for resume support in LigHTTPd LigHTTPd does not support HTTP Range headers with the X-Sendfile header in the way Apache does. Instead, it needs to be handled in the backend. This commit does exactly that, using the X-Sendfile2 header to send ranges of files. To accomplish this without breaking web servers that don't support X-Sendfile2, a new variable MOD_X_SENDFILE2_ENABLED was introduced to separate this method from X-Sendfile and X-Accel-Redirect. --- lib/files.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/files.php b/lib/files.php index abb1617c25..5dd65e85a4 100644 --- a/lib/files.php +++ b/lib/files.php @@ -45,7 +45,7 @@ class OC_Files { */ public static function get($dir, $files, $only_header = false) { $xsendfile = false; - if (isset($_SERVER['MOD_X_SENDFILE_ENABLED']) || + if (isset($_SERVER['MOD_X_SENDFILE_ENABLED']) || isset($_SERVER['MOD_X_SENDFILE2_ENABLED']) || isset($_SERVER['MOD_X_ACCEL_REDIRECT_ENABLED'])) { $xsendfile = true; } @@ -170,6 +170,18 @@ class OC_Files { private static function addSendfileHeader($filename) { if (isset($_SERVER['MOD_X_SENDFILE_ENABLED'])) { header("X-Sendfile: " . $filename); + } + if (isset($_SERVER['MOD_X_SENDFILE2_ENABLED'])) { + if (isset($_SERVER['HTTP_RANGE']) && preg_match('/\Abytes=(?P[0-9]+)-(?P[0-9]*)\z/', $_SERVER['HTTP_RANGE'], $range)) { + if ($range['end'] == "") { + $range['end'] = filesize($filename) - 1; + } + header("Content-Range: bytes " . $range['start'] . "-" . $range['end'] . "/" . filesize($filename)); + header("HTTP/1.1 206 Partial content"); + header("X-Sendfile2: " . str_replace(",", "%2c", rawurlencode($filename)) . " " . $range['start'] . "-" . $range['end']); + } else { + header("X-Sendfile: " . $filename); + } } if (isset($_SERVER['MOD_X_ACCEL_REDIRECT_ENABLED'])) { header("X-Accel-Redirect: " . $filename); From a25bfa92917075d1ab26649c4d6d5bea6150e623 Mon Sep 17 00:00:00 2001 From: mvn23 Date: Wed, 19 Jun 2013 23:44:45 +0200 Subject: [PATCH 2/4] Update files.php --- lib/files.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/files.php b/lib/files.php index 5dd65e85a4..123c87eb5f 100644 --- a/lib/files.php +++ b/lib/files.php @@ -45,7 +45,8 @@ class OC_Files { */ public static function get($dir, $files, $only_header = false) { $xsendfile = false; - if (isset($_SERVER['MOD_X_SENDFILE_ENABLED']) || isset($_SERVER['MOD_X_SENDFILE2_ENABLED']) || + if (isset($_SERVER['MOD_X_SENDFILE_ENABLED']) || + isset($_SERVER['MOD_X_SENDFILE2_ENABLED']) || isset($_SERVER['MOD_X_ACCEL_REDIRECT_ENABLED'])) { $xsendfile = true; } @@ -172,7 +173,8 @@ class OC_Files { header("X-Sendfile: " . $filename); } if (isset($_SERVER['MOD_X_SENDFILE2_ENABLED'])) { - if (isset($_SERVER['HTTP_RANGE']) && preg_match('/\Abytes=(?P[0-9]+)-(?P[0-9]*)\z/', $_SERVER['HTTP_RANGE'], $range)) { + if (isset($_SERVER['HTTP_RANGE']) && + preg_match("/\Abytes=(?P[0-9]+)-(?P[0-9]*)\z/", $_SERVER['HTTP_RANGE'], $range)) { if ($range['end'] == "") { $range['end'] = filesize($filename) - 1; } From 3f20a080fedd47e2614c44ebe1824f2ee46bb767 Mon Sep 17 00:00:00 2001 From: mvn23 Date: Thu, 20 Jun 2013 12:23:25 +0300 Subject: [PATCH 3/4] Revert most changes for testing --- lib/files.php | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/files.php b/lib/files.php index 123c87eb5f..20ad2405fc 100644 --- a/lib/files.php +++ b/lib/files.php @@ -173,18 +173,20 @@ class OC_Files { header("X-Sendfile: " . $filename); } if (isset($_SERVER['MOD_X_SENDFILE2_ENABLED'])) { - if (isset($_SERVER['HTTP_RANGE']) && - preg_match("/\Abytes=(?P[0-9]+)-(?P[0-9]*)\z/", $_SERVER['HTTP_RANGE'], $range)) { - if ($range['end'] == "") { - $range['end'] = filesize($filename) - 1; - } - header("Content-Range: bytes " . $range['start'] . "-" . $range['end'] . "/" . filesize($filename)); - header("HTTP/1.1 206 Partial content"); - header("X-Sendfile2: " . str_replace(",", "%2c", rawurlencode($filename)) . " " . $range['start'] . "-" . $range['end']); - } else { - header("X-Sendfile: " . $filename); - } + /* if (isset($_SERVER['HTTP_RANGE']) && + * preg_match("/\Abytes=(?P[0-9]+)-(?P[0-9]*)\z/", $_SERVER['HTTP_RANGE'], $range)) { + * if ($range['end'] == "") { + * $range['end'] = filesize($filename) - 1; + * } + * header("Content-Range: bytes " . $range['start'] . "-" . $range['end'] . "/" . filesize($filename)); + * header("HTTP/1.1 206 Partial content"); + * header("X-Sendfile2: " . str_replace(",", "%2c", rawurlencode($filename)) . " " . $range['start'] . "-" . $range['end']); + * } else { + */ + header("X-Sendfile: " . $filename); + // } } + if (isset($_SERVER['MOD_X_ACCEL_REDIRECT_ENABLED'])) { header("X-Accel-Redirect: " . $filename); } From 59fa3055e1e2f4f070a0443bcc0f06fcd6e892eb Mon Sep 17 00:00:00 2001 From: mvn23 Date: Thu, 20 Jun 2013 17:46:36 +0300 Subject: [PATCH 4/4] Reviewed code for X-Sendfile2 Made some small changes which might have caused a segfault on ci.tmit.eu earlier. --- lib/files.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/files.php b/lib/files.php index 20ad2405fc..f5dffd970d 100644 --- a/lib/files.php +++ b/lib/files.php @@ -173,18 +173,18 @@ class OC_Files { header("X-Sendfile: " . $filename); } if (isset($_SERVER['MOD_X_SENDFILE2_ENABLED'])) { - /* if (isset($_SERVER['HTTP_RANGE']) && - * preg_match("/\Abytes=(?P[0-9]+)-(?P[0-9]*)\z/", $_SERVER['HTTP_RANGE'], $range)) { - * if ($range['end'] == "") { - * $range['end'] = filesize($filename) - 1; - * } - * header("Content-Range: bytes " . $range['start'] . "-" . $range['end'] . "/" . filesize($filename)); - * header("HTTP/1.1 206 Partial content"); - * header("X-Sendfile2: " . str_replace(",", "%2c", rawurlencode($filename)) . " " . $range['start'] . "-" . $range['end']); - * } else { - */ - header("X-Sendfile: " . $filename); - // } + if (isset($_SERVER['HTTP_RANGE']) && + preg_match("/^bytes=([0-9]+)-([0-9]*)$/", $_SERVER['HTTP_RANGE'], $range)) { + $filelength = filesize($filename); + if ($range[2] == "") { + $range[2] = $filelength - 1; + } + header("Content-Range: bytes $range[1]-$range[2]/" . $filelength); + header("HTTP/1.1 206 Partial content"); + header("X-Sendfile2: " . str_replace(",", "%2c", rawurlencode($filename)) . " $range[1]-$range[2]"); + } else { + header("X-Sendfile: " . $filename); + } } if (isset($_SERVER['MOD_X_ACCEL_REDIRECT_ENABLED'])) {