|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Zend Framework (http://framework.zend.com/) |
| 4 | + * |
| 5 | + * @see http://github.com/zendframework/zend-diactoros for the canonical source repository |
| 6 | + * @copyright Copyright (c) 2015 Oscar Otero (http://oscarotero.com) / Zend Technologies USA Inc. (http://www.zend.com) |
| 7 | + * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Zend\Diactoros\Response; |
| 11 | + |
| 12 | +use Psr\Http\Message\ResponseInterface; |
| 13 | +use RuntimeException; |
| 14 | + |
| 15 | +class SapiStreamEmitter implements EmitterInterface |
| 16 | +{ |
| 17 | + use SapiEmitterTrait; |
| 18 | + |
| 19 | + /** |
| 20 | + * Emits a response for a PHP SAPI environment. |
| 21 | + * |
| 22 | + * Emits the status line and headers via the header() function, and the |
| 23 | + * body content via the output buffer. |
| 24 | + * |
| 25 | + * @param ResponseInterface $response |
| 26 | + * @param int $maxBufferLength Maximum output buffering size for each iteration |
| 27 | + */ |
| 28 | + public function emit(ResponseInterface $response, $maxBufferLength = 8192) |
| 29 | + { |
| 30 | + if (headers_sent()) { |
| 31 | + throw new RuntimeException('Unable to emit response; headers already sent'); |
| 32 | + } |
| 33 | + |
| 34 | + $response = $this->injectContentLength($response); |
| 35 | + |
| 36 | + $this->emitStatusLine($response); |
| 37 | + $this->emitHeaders($response); |
| 38 | + $this->flush(); |
| 39 | + |
| 40 | + $range = $this->parseContentRange($response->getHeaderLine('Content-Range')); |
| 41 | + |
| 42 | + if (is_array($range)) { |
| 43 | + $this->emitBodyRange($range, $response, $maxBufferLength); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + $this->emitBody($response, $maxBufferLength); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Emit the message body. |
| 52 | + * |
| 53 | + * @param ResponseInterface $response |
| 54 | + * @param int $maxBufferLength |
| 55 | + */ |
| 56 | + private function emitBody(ResponseInterface $response, $maxBufferLength) |
| 57 | + { |
| 58 | + $body = $response->getBody(); |
| 59 | + $body->rewind(); |
| 60 | + |
| 61 | + while (! $body->eof()) { |
| 62 | + echo $body->read($maxBufferLength); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Emit a range of the message body. |
| 68 | + * |
| 69 | + * @param array $range |
| 70 | + * @param ResponseInterface $response |
| 71 | + * @param int $maxBufferLength |
| 72 | + */ |
| 73 | + private function emitBodyRange(array $range, ResponseInterface $response, $maxBufferLength) |
| 74 | + { |
| 75 | + list($unit, $first, $last, $lenght) = $range; |
| 76 | + |
| 77 | + ++$last; //zero-based position |
| 78 | + $body = $response->getBody(); |
| 79 | + $body->seek($first); |
| 80 | + $pos = $first; |
| 81 | + |
| 82 | + while (! $body->eof() && $pos < $last) { |
| 83 | + if (($pos + $maxBufferLength) > $last) { |
| 84 | + echo $body->read($last - $pos); |
| 85 | + break; |
| 86 | + } |
| 87 | + |
| 88 | + echo $body->read($maxBufferLength); |
| 89 | + $pos = $body->tell(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Parse content-range header |
| 95 | + * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.16 |
| 96 | + * |
| 97 | + * @param string $header |
| 98 | + * @return false|array [unit, first, last, length]; returns false if no |
| 99 | + * content range or an invalid content range is provided |
| 100 | + */ |
| 101 | + private function parseContentRange($header) |
| 102 | + { |
| 103 | + if (preg_match('/(?P<unit>[\w]+)\s+(?P<first>\d+)-(?P<last>\d+)\/(?P<length>\d+|\*)/', $header, $matches)) { |
| 104 | + return [ |
| 105 | + $matches['unit'], |
| 106 | + (int) $matches['first'], |
| 107 | + (int) $matches['last'], |
| 108 | + $matches['length'] === '*' ? '*' : (int) $matches['length'], |
| 109 | + ]; |
| 110 | + } |
| 111 | + return false; |
| 112 | + } |
| 113 | +} |
0 commit comments