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