From d3f37c0f84828226a29c64316beffc9681409066 Mon Sep 17 00:00:00 2001 From: FusionPBX Date: Fri, 27 Oct 2023 23:56:57 -0600 Subject: [PATCH] Add Accept-Ranges for HTTP content delivery --- app/provision/index.php | 50 +++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/app/provision/index.php b/app/provision/index.php index 1a18826115..e19ffa8ec7 100644 --- a/app/provision/index.php +++ b/app/provision/index.php @@ -476,29 +476,53 @@ $cfg_ext = ".cfg"; if ($device_vendor === "aastra" && strrpos($file, $cfg_ext, 0) === strlen($file) - strlen($cfg_ext)) { header("Content-Type: text/plain"); - header("Content-Length: ".strlen($file_contents)); } else if ($device_vendor === "yealink" || $device_vendor === "flyingvoice") { header("Content-Type: text/plain"); - header("Content-Length: ".strval(strlen($file_contents))); } else if ($device_vendor === "snom" && $device_template === "snom/m3") { $file_contents = utf8_decode($file_contents); header("Content-Type: text/plain; charset=iso-8859-1"); - header("Content-Length: ".strlen($file_contents)); + } + elseif (!empty($file_contents) && is_xml($file_contents)) { + header("Content-Type: text/xml; charset=utf-8"); } else { - if (!empty($file_contents) && is_xml($file_contents)) { - header("Content-Type: text/xml; charset=utf-8"); - header("Content-Length: ".strlen($file_contents)); - } - else { - header("Content-Type: text/plain"); - header("Content-Length: ".strval(strlen($file_contents))); - } + header("Content-Type: text/plain"); } } - echo $file_contents; + +//send the content + $file_size = strlen($file_contents); + if (isset($_SERVER['HTTP_RANGE'])) { + $ranges = $_SERVER['HTTP_RANGE']; + list($unit, $range) = explode('=', $ranges, 2); + list($start, $end) = explode('-', $range, 2); + + $start = empty($start) ? 0 : (int)$start; + $end = empty($end) ? $file_size - 1 : min((int)$end, $file_size - 1); + + $length = $end - $start + 1; + + //add additional headers + header('HTTP/1.1 206 Partial Content'); + header("Content-Length: $length"); + header("Content-Range: bytes $start-$end/$file_size"); + + //output the requested range from the content variable + echo substr($file_contents, $start, $length); + } + else { + //add additional headers + header('HTTP/1.1 200 OK'); + header("Content-Length: $file_size"); + header('Accept-Ranges: bytes'); + + //send the entire content + echo $file_contents; + } + +//close the closelog(); //device logs @@ -507,3 +531,5 @@ } ?> + +