-
Notifications
You must be signed in to change notification settings - Fork 2
/
ResponseEmitter.php
296 lines (264 loc) · 8.68 KB
/
ResponseEmitter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.3.5
* @license https://opensource.org/licenses/mit-license.php MIT License
*
* Parts of this file are derived from Zend-Diactoros
* @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (https://www.zend.com/)
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
*/
namespace Cake\Http;
use Cake\Http\Cookie\Cookie;
use Laminas\Diactoros\RelativeStream;
use Laminas\HttpHandlerRunner\Emitter\EmitterInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Emits a Response to the PHP Server API.
*
* This emitter offers a few changes from the emitters offered by
* diactoros:
*
* - It logs headers sent using CakePHP's logging tools.
* - Cookies are emitted using setcookie() to not conflict with ext/session
*/
class ResponseEmitter implements EmitterInterface
{
/**
* Maximum output buffering size for each iteration.
*
* @var int
*/
protected $maxBufferLength;
/**
* Constructor
*
* @param int $maxBufferLength Maximum output buffering size for each iteration.
*/
public function __construct(int $maxBufferLength = 8192)
{
$this->maxBufferLength = $maxBufferLength;
}
/**
* Emit a response.
*
* Emits a response, including status line, headers, and the message body,
* according to the environment.
*
* @param \Psr\Http\Message\ResponseInterface $response The response to emit.
* @return bool
*/
public function emit(ResponseInterface $response): bool
{
$file = '';
$line = 0;
if (headers_sent($file, $line)) {
$message = "Unable to emit headers. Headers sent in file=$file line=$line";
trigger_error($message, E_USER_WARNING);
}
$this->emitStatusLine($response);
$this->emitHeaders($response);
$this->flush();
$range = $this->parseContentRange($response->getHeaderLine('Content-Range'));
if (is_array($range)) {
$this->emitBodyRange($range, $response);
} else {
$this->emitBody($response);
}
if (function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
}
return true;
}
/**
* Emit the message body.
*
* @param \Psr\Http\Message\ResponseInterface $response The response to emit
* @return void
*/
protected function emitBody(ResponseInterface $response): void
{
if (in_array($response->getStatusCode(), [204, 304], true)) {
return;
}
$body = $response->getBody();
if (!$body->isSeekable()) {
echo $body;
return;
}
$body->rewind();
while (!$body->eof()) {
echo $body->read($this->maxBufferLength);
}
}
/**
* Emit a range of the message body.
*
* @param array $range The range data to emit
* @param \Psr\Http\Message\ResponseInterface $response The response to emit
* @return void
*/
protected function emitBodyRange(array $range, ResponseInterface $response): void
{
[, $first, $last] = $range;
$body = $response->getBody();
if (!$body->isSeekable()) {
$contents = $body->getContents();
echo substr($contents, $first, $last - $first + 1);
return;
}
$body = new RelativeStream($body, $first);
$body->rewind();
$pos = 0;
$length = $last - $first + 1;
while (!$body->eof() && $pos < $length) {
if ($pos + $this->maxBufferLength > $length) {
echo $body->read($length - $pos);
break;
}
echo $body->read($this->maxBufferLength);
$pos = $body->tell();
}
}
/**
* Emit the status line.
*
* Emits the status line using the protocol version and status code from
* the response; if a reason phrase is available, it, too, is emitted.
*
* @param \Psr\Http\Message\ResponseInterface $response The response to emit
* @return void
*/
protected function emitStatusLine(ResponseInterface $response): void
{
$reasonPhrase = $response->getReasonPhrase();
header(sprintf(
'HTTP/%s %d%s',
$response->getProtocolVersion(),
$response->getStatusCode(),
($reasonPhrase ? ' ' . $reasonPhrase : '')
));
}
/**
* Emit response headers.
*
* Loops through each header, emitting each; if the header value
* is an array with multiple values, ensures that each is sent
* in such a way as to create aggregate headers (instead of replace
* the previous).
*
* @param \Psr\Http\Message\ResponseInterface $response The response to emit
* @return void
*/
protected function emitHeaders(ResponseInterface $response): void
{
$cookies = [];
if (method_exists($response, 'getCookieCollection')) {
$cookies = iterator_to_array($response->getCookieCollection());
}
foreach ($response->getHeaders() as $name => $values) {
if (strtolower($name) === 'set-cookie') {
$cookies = array_merge($cookies, $values);
continue;
}
$first = true;
foreach ($values as $value) {
header(sprintf(
'%s: %s',
$name,
$value
), $first);
$first = false;
}
}
$this->emitCookies($cookies);
}
/**
* Emit cookies using setcookie()
*
* @param array<\Cake\Http\Cookie\CookieInterface|string> $cookies An array of cookies.
* @return void
*/
protected function emitCookies(array $cookies): void
{
foreach ($cookies as $cookie) {
$this->setCookie($cookie);
}
}
/**
* Helper methods to set cookie.
*
* @param \Cake\Http\Cookie\CookieInterface|string $cookie Cookie.
* @return bool
*/
protected function setCookie($cookie): bool
{
if (is_string($cookie)) {
$cookie = Cookie::createFromHeaderString($cookie, ['path' => '']);
}
if (PHP_VERSION_ID >= 70300) {
return setcookie($cookie->getName(), $cookie->getScalarValue(), $cookie->getOptions());
}
$path = $cookie->getPath();
$sameSite = $cookie->getSameSite();
if ($sameSite !== null) {
// Temporary hack for PHP 7.2 to set "SameSite" attribute
// https://stackoverflow.com/questions/39750906/php-setcookie-samesite-strict
$path .= '; samesite=' . $sameSite;
}
return setcookie(
$cookie->getName(),
$cookie->getScalarValue(),
$cookie->getExpiresTimestamp() ?: 0,
$path,
$cookie->getDomain(),
$cookie->isSecure(),
$cookie->isHttpOnly()
);
}
/**
* Loops through the output buffer, flushing each, before emitting
* the response.
*
* @param int|null $maxBufferLevel Flush up to this buffer level.
* @return void
*/
protected function flush(?int $maxBufferLevel = null): void
{
if ($maxBufferLevel === null) {
$maxBufferLevel = ob_get_level();
}
while (ob_get_level() > $maxBufferLevel) {
ob_end_flush();
}
}
/**
* Parse content-range header
* https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.16
*
* @param string $header The Content-Range header to parse.
* @return array|false [unit, first, last, length]; returns false if no
* content range or an invalid content range is provided
*/
protected function parseContentRange(string $header)
{
if (preg_match('/(?P<unit>[\w]+)\s+(?P<first>\d+)-(?P<last>\d+)\/(?P<length>\d+|\*)/', $header, $matches)) {
return [
$matches['unit'],
(int)$matches['first'],
(int)$matches['last'],
$matches['length'] === '*' ? '*' : (int)$matches['length'],
];
}
return false;
}
}