annotate vendor/zendframework/zend-diactoros/src/Response/SapiStreamEmitter.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children c2387f117808
rev   line source
Chris@0 1 <?php
Chris@0 2 /**
Chris@12 3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
Chris@12 4 * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
Chris@0 5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
Chris@0 6 */
Chris@0 7
Chris@0 8 namespace Zend\Diactoros\Response;
Chris@0 9
Chris@0 10 use Psr\Http\Message\ResponseInterface;
Chris@0 11 use RuntimeException;
Chris@0 12 use Zend\Diactoros\RelativeStream;
Chris@0 13
Chris@0 14 class SapiStreamEmitter implements EmitterInterface
Chris@0 15 {
Chris@0 16 use SapiEmitterTrait;
Chris@0 17
Chris@0 18 /**
Chris@0 19 * Emits a response for a PHP SAPI environment.
Chris@0 20 *
Chris@0 21 * Emits the status line and headers via the header() function, and the
Chris@0 22 * body content via the output buffer.
Chris@0 23 *
Chris@0 24 * @param ResponseInterface $response
Chris@0 25 * @param int $maxBufferLength Maximum output buffering size for each iteration
Chris@0 26 */
Chris@0 27 public function emit(ResponseInterface $response, $maxBufferLength = 8192)
Chris@0 28 {
Chris@12 29 $this->assertNoPreviousOutput();
Chris@12 30 $this->emitHeaders($response);
Chris@0 31 $this->emitStatusLine($response);
Chris@0 32
Chris@0 33 $range = $this->parseContentRange($response->getHeaderLine('Content-Range'));
Chris@0 34
Chris@0 35 if (is_array($range) && $range[0] === 'bytes') {
Chris@0 36 $this->emitBodyRange($range, $response, $maxBufferLength);
Chris@0 37 return;
Chris@0 38 }
Chris@0 39
Chris@0 40 $this->emitBody($response, $maxBufferLength);
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@0 44 * Emit the message body.
Chris@0 45 *
Chris@0 46 * @param ResponseInterface $response
Chris@0 47 * @param int $maxBufferLength
Chris@0 48 */
Chris@0 49 private function emitBody(ResponseInterface $response, $maxBufferLength)
Chris@0 50 {
Chris@0 51 $body = $response->getBody();
Chris@0 52
Chris@0 53 if ($body->isSeekable()) {
Chris@0 54 $body->rewind();
Chris@0 55 }
Chris@0 56
Chris@0 57 if (! $body->isReadable()) {
Chris@0 58 echo $body;
Chris@0 59 return;
Chris@0 60 }
Chris@0 61
Chris@0 62 while (! $body->eof()) {
Chris@0 63 echo $body->read($maxBufferLength);
Chris@0 64 }
Chris@0 65 }
Chris@0 66
Chris@0 67 /**
Chris@0 68 * Emit a range of the message body.
Chris@0 69 *
Chris@0 70 * @param array $range
Chris@0 71 * @param ResponseInterface $response
Chris@0 72 * @param int $maxBufferLength
Chris@0 73 */
Chris@0 74 private function emitBodyRange(array $range, ResponseInterface $response, $maxBufferLength)
Chris@0 75 {
Chris@0 76 list($unit, $first, $last, $length) = $range;
Chris@0 77
Chris@0 78 $body = $response->getBody();
Chris@0 79
Chris@0 80 $length = $last - $first + 1;
Chris@0 81
Chris@0 82 if ($body->isSeekable()) {
Chris@0 83 $body->seek($first);
Chris@0 84
Chris@0 85 $first = 0;
Chris@0 86 }
Chris@0 87
Chris@0 88 if (! $body->isReadable()) {
Chris@0 89 echo substr($body->getContents(), $first, $length);
Chris@0 90 return;
Chris@0 91 }
Chris@0 92
Chris@0 93 $remaining = $length;
Chris@0 94
Chris@0 95 while ($remaining >= $maxBufferLength && ! $body->eof()) {
Chris@0 96 $contents = $body->read($maxBufferLength);
Chris@0 97 $remaining -= strlen($contents);
Chris@0 98
Chris@0 99 echo $contents;
Chris@0 100 }
Chris@0 101
Chris@0 102 if ($remaining > 0 && ! $body->eof()) {
Chris@0 103 echo $body->read($remaining);
Chris@0 104 }
Chris@0 105 }
Chris@0 106
Chris@0 107 /**
Chris@0 108 * Parse content-range header
Chris@0 109 * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.16
Chris@0 110 *
Chris@0 111 * @param string $header
Chris@0 112 * @return false|array [unit, first, last, length]; returns false if no
Chris@0 113 * content range or an invalid content range is provided
Chris@0 114 */
Chris@0 115 private function parseContentRange($header)
Chris@0 116 {
Chris@0 117 if (preg_match('/(?P<unit>[\w]+)\s+(?P<first>\d+)-(?P<last>\d+)\/(?P<length>\d+|\*)/', $header, $matches)) {
Chris@0 118 return [
Chris@0 119 $matches['unit'],
Chris@0 120 (int) $matches['first'],
Chris@0 121 (int) $matches['last'],
Chris@0 122 $matches['length'] === '*' ? '*' : (int) $matches['length'],
Chris@0 123 ];
Chris@0 124 }
Chris@0 125 return false;
Chris@0 126 }
Chris@0 127 }