X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=vendor%2Fzendframework%2Fzend-diactoros%2Fsrc%2FResponse%2FSapiStreamEmitter.php;fp=vendor%2Fzendframework%2Fzend-diactoros%2Fsrc%2FResponse%2FSapiStreamEmitter.php;h=02095e50b41162806cd57fe06b3746eae9d2aeda;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/vendor/zendframework/zend-diactoros/src/Response/SapiStreamEmitter.php b/vendor/zendframework/zend-diactoros/src/Response/SapiStreamEmitter.php new file mode 100644 index 000000000..02095e50b --- /dev/null +++ b/vendor/zendframework/zend-diactoros/src/Response/SapiStreamEmitter.php @@ -0,0 +1,135 @@ +injectContentLength($response); + + $this->emitStatusLine($response); + $this->emitHeaders($response); + $this->flush(); + + $range = $this->parseContentRange($response->getHeaderLine('Content-Range')); + + if (is_array($range) && $range[0] === 'bytes') { + $this->emitBodyRange($range, $response, $maxBufferLength); + return; + } + + $this->emitBody($response, $maxBufferLength); + } + + /** + * Emit the message body. + * + * @param ResponseInterface $response + * @param int $maxBufferLength + */ + private function emitBody(ResponseInterface $response, $maxBufferLength) + { + $body = $response->getBody(); + + if ($body->isSeekable()) { + $body->rewind(); + } + + if (! $body->isReadable()) { + echo $body; + return; + } + + while (! $body->eof()) { + echo $body->read($maxBufferLength); + } + } + + /** + * Emit a range of the message body. + * + * @param array $range + * @param ResponseInterface $response + * @param int $maxBufferLength + */ + private function emitBodyRange(array $range, ResponseInterface $response, $maxBufferLength) + { + list($unit, $first, $last, $length) = $range; + + $body = $response->getBody(); + + $length = $last - $first + 1; + + if ($body->isSeekable()) { + $body->seek($first); + + $first = 0; + } + + if (! $body->isReadable()) { + echo substr($body->getContents(), $first, $length); + return; + } + + $remaining = $length; + + while ($remaining >= $maxBufferLength && ! $body->eof()) { + $contents = $body->read($maxBufferLength); + $remaining -= strlen($contents); + + echo $contents; + } + + if ($remaining > 0 && ! $body->eof()) { + echo $body->read($remaining); + } + } + + /** + * Parse content-range header + * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.16 + * + * @param string $header + * @return false|array [unit, first, last, length]; returns false if no + * content range or an invalid content range is provided + */ + private function parseContentRange($header) + { + if (preg_match('/(?P[\w]+)\s+(?P\d+)-(?P\d+)\/(?P\d+|\*)/', $header, $matches)) { + return [ + $matches['unit'], + (int) $matches['first'], + (int) $matches['last'], + $matches['length'] === '*' ? '*' : (int) $matches['length'], + ]; + } + return false; + } +}