Last active
August 29, 2015 14:18
-
-
Save dratini0/9f4695a50e6913b9adff to your computer and use it in GitHub Desktop.
Backport of sabre-io/dav#623 for owncloud 8.0.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- /var/www/owncloud/3rdparty/sabre/dav/lib/Sabre/DAV/Server.php 2015-04-02 16:50:45.805577094 +0200 | |
+++ Server.php_ 2015-04-02 17:37:08.709819071 +0200 | |
@@ -616,25 +616,23 @@ | |
} | |
- // New read/write stream | |
- $newStream = fopen('php://temp','r+'); | |
- | |
- // stream_copy_to_stream() has a bug/feature: the `whence` argument | |
- // is interpreted as SEEK_SET (count from absolute offset 0), while | |
- // for a stream it should be SEEK_CUR (count from current offset). | |
- // If a stream is nonseekable, the function fails. So we *emulate* | |
- // the correct behaviour with fseek(): | |
- if ($start > 0) { | |
- if (($curOffs = ftell($body)) === false) $curOffs = 0; | |
- fseek($body, $start - $curOffs, SEEK_CUR); | |
+ // for a seekable $body stream we simply set the pointer | |
+ // for a non-seekable $body stream we read and discard just the | |
+ // right amount of data | |
+ if (stream_get_meta_data($body)['seekable']) { | |
+ fseek($body, $start, SEEK_SET); | |
+ } else { | |
+ $consumeBlock = 8192; | |
+ for($consumed = 0; $start - $consumed > 0; ){ | |
+ if(feof($body)) throw new Exception\RequestedRangeNotSatisfiable('The start offset (' . $start . ') exceeded the size of the entity (' . $consumed . ')'); | |
+ $consumed += strlen(fread($body, min($start - $consumed, $consumeBlock))); | |
+ } | |
} | |
- stream_copy_to_stream($body, $newStream, $end-$start+1); | |
- rewind($newStream); | |
$this->httpResponse->setHeader('Content-Length', $end-$start+1); | |
$this->httpResponse->setHeader('Content-Range','bytes ' . $start . '-' . $end . '/' . $nodeSize); | |
$this->httpResponse->sendStatus(206); | |
- $this->httpResponse->sendBody($newStream); | |
+ $this->httpResponse->sendBody($body); | |
} else { | |
--- /var/www/owncloud/3rdparty/sabre/dav/lib/Sabre/HTTP/Response.php 2015-04-02 16:50:41.545595448 +0200 | |
+++ Response.php_ 2015-04-02 17:33:25.842749460 +0200 | |
@@ -115,6 +115,7 @@ | |
} | |
+ private $contentLength = null; | |
/** | |
* Sets an HTTP header for the response | |
* | |
@@ -126,6 +127,7 @@ | |
public function setHeader($name, $value, $replace = true) { | |
$value = str_replace(array("\r","\n"),array('\r','\n'),$value); | |
+ if(strtolower($name) == "content-length") $this->contentLength = $value; | |
if (!headers_sent()) | |
return header($name . ': ' . $value, $replace); | |
else return false; | |
@@ -159,15 +161,15 @@ | |
*/ | |
public function sendBody($body) { | |
- if (is_resource($body)) { | |
- | |
- file_put_contents('php://output', $body); | |
- | |
+ if($this->contentLength !== null) { | |
+ $output = fopen('php://output', 'wb'); | |
+ if(is_resource($body) && get_resource_type($body) == 'stream') { | |
+ stream_copy_to_stream($body, $output, $this->contentLength); | |
+ } else { | |
+ fwrite($output, $body, $this->contentLength); | |
+ } | |
} else { | |
- | |
- // We assume a string | |
- echo $body; | |
- | |
+ file_put_contents('php://output', $body); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment