-
Notifications
You must be signed in to change notification settings - Fork 610
/
Response.php
356 lines (316 loc) · 10.4 KB
/
Response.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\web;
use Craft;
use craft\helpers\ArrayHelper;
use craft\helpers\UrlHelper;
use Throwable;
use yii\base\Application as BaseApplication;
use yii\web\Cookie;
use yii\web\CookieCollection;
use yii\web\HttpException;
/**
* @inheritdoc
* @mixin CpScreenResponseBehavior
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class Response extends \yii\web\Response
{
/**
* @since 3.4.0
*/
public const FORMAT_CSV = 'csv';
/**
* Default response formatter configurations.
*
* This could be set from `config/app.web.php` to append additional default response formatters, or modify existing ones.
*
* ```php
* use craft\helpers\App;
* use craft\helpers\ArrayHelper;
* use craft\web\Response;
*
* return [
* 'components' => [
* 'response' => fn() => Craft::createObject(ArrayHelper::merge(
* App::webResponseConfig(),
* [
* 'defaultFormatters' => [
* Response::FORMAT_CSV => [
* 'delimiter' => chr(9),
* ],
* ],
* ]
* )),
* ],
* ];
* ```
*
* @see defaultFormatters()
* @since 4.5.0
*/
public array $defaultFormatters = [];
/**
* @var bool whether the response has been prepared.
*/
private bool $_isPrepared = false;
/**
* @var CookieCollection Collection of raw cookies
* @see getRawCookies()
*/
private CookieCollection $_rawCookies;
/**
* Returns the Content-Type header (sans `charset=X`) that the response will most likely include.
*
* @return string|null
*/
public function getContentType(): ?string
{
// If the response hasn't been prepared yet, go with what the formatter is going to set
if (!$this->_isPrepared) {
switch ($this->format) {
case self::FORMAT_HTML:
return 'text/html';
case self::FORMAT_XML:
return 'application/xml';
case self::FORMAT_JSON:
return 'application/json';
case self::FORMAT_JSONP:
return 'application/javascript';
case self::FORMAT_CSV:
return 'text/csv';
}
}
// Otherwise check the Content-Type header
if (($header = $this->getHeaders()->get('content-type')) === null) {
return null;
}
if (($pos = strpos($header, ';')) !== false) {
$header = substr($header, 0, $pos);
}
return strtolower(trim($header));
}
/**
* Sets headers that will instruct the client to cache this response.
*
* @param int $duration The total cache duration, in seconds. Defaults to 1 year.
* @param bool $overwrite Whether the headers should overwrite existing headers, if already set
* @return self self reference
*/
public function setCacheHeaders(int $duration = 31536000, bool $overwrite = true): self
{
if ($duration <= 0) {
$this->setNoCacheHeaders($overwrite);
return $this;
}
$this->setHeader('Expires', sprintf('%s GMT', gmdate('D, d M Y H:i:s', time() + $duration)), $overwrite);
$this->setHeader('Pragma', 'cache', $overwrite);
$this->setHeader('Cache-Control', "public, max-age=$duration", $overwrite);
return $this;
}
/**
* Sets headers that will instruct the client to not cache this response.
*
* @param bool $overwrite Whether the headers should overwrite existing headers, if already set
* @return self self reference
* @since 3.5.0
*/
public function setNoCacheHeaders(bool $overwrite = true): self
{
$this->setHeader('Expires', '0', $overwrite);
$this->setHeader('Pragma', 'no-cache', $overwrite);
$this->setHeader('Cache-Control', 'no-cache, no-store, must-revalidate', $overwrite);
return $this;
}
private function setHeader(string $name, string $value, bool $overwrite): void
{
if ($overwrite) {
$this->getHeaders()->set($name, $value);
} else {
$this->getHeaders()->setDefault($name, $value);
}
}
/**
* Sets a Last-Modified header based on a given file path.
*
* @param string $path The file to read the last modified date from.
* @return self self reference
*/
public function setLastModifiedHeader(string $path): self
{
$modifiedTime = filemtime($path);
if ($modifiedTime) {
$this->getHeaders()->set('Last-Modified', gmdate('D, d M Y H:i:s', $modifiedTime) . ' GMT');
}
return $this;
}
/**
* Returns the “raw” cookie collection.
*
* Works similar to [[getCookies()]], but these cookies won’t go through validation, and their values won’t
* be hashed.
*
* @return CookieCollection the cookie collection.
* @since 3.5.0
*/
public function getRawCookies(): CookieCollection
{
if (!isset($this->_rawCookies)) {
$this->_rawCookies = new CookieCollection();
}
return $this->_rawCookies;
}
/**
* @inheritdoc
* @since 3.5.0
*/
protected function sendCookies(): void
{
parent::sendCookies();
if (!isset($this->_rawCookies)) {
return;
}
foreach ($this->getRawCookies() as $cookie) {
/** @var Cookie $cookie */
setcookie($cookie->name, $cookie->value, [
'expires' => $cookie->expire,
'path' => $cookie->path,
'domain' => $cookie->domain,
'secure' => $cookie->secure,
'httpOnly' => $cookie->httpOnly,
'sameSite' => !empty($cookie->sameSite) ? $cookie->sameSite : null,
]);
}
}
/**
* @inheritdoc
*/
public function redirect($url, $statusCode = 302, $checkAjax = true): self
{
if (is_string($url)) {
$url = UrlHelper::url($url);
}
if ($this->format === TemplateResponseFormatter::FORMAT) {
$this->format = self::FORMAT_HTML;
}
parent::redirect($url, $statusCode, $checkAjax);
if (Craft::$app->state === BaseApplication::STATE_SENDING_RESPONSE) {
$this->send();
}
return $this;
}
/**
* @inheritdoc \yii\web\Response::sendFile()
* @param string $filePath
* @param string|null $attachmentName
* @param array $options
* @return self self reference
*/
public function sendFile($filePath, $attachmentName = null, $options = []): self
{
$this->_clearOutputBuffer();
parent::sendFile($filePath, $attachmentName, $options);
return $this;
}
/**
* @inheritdoc \yii\web\Response::sendContentAsFile()
* @param string $content
* @param string $attachmentName
* @param array $options
* @return self self reference
* @throws HttpException
*/
public function sendContentAsFile($content, $attachmentName, $options = []): self
{
$this->_clearOutputBuffer();
parent::sendContentAsFile($content, $attachmentName, $options);
return $this;
}
/**
* Attempts to closes the connection with the HTTP client, without ending PHP script execution.
*
* This method relies on [flush()](https://php.net/manual/en/function.flush.php), which may not actually work if
* mod_deflate or mod_gzip is installed, or if this is a Win32 server.
*
* @see http://stackoverflow.com/a/141026
* @throws Throwable An exception will be thrown if content has already been output.
*/
public function sendAndClose(): void
{
// Make sure nothing has been output yet
if (headers_sent()) {
return;
}
// Get the active user before headers are sent
Craft::$app->getUser()->getIdentity();
// Prevent the script from ending when the browser closes the connection
ignore_user_abort(true);
// Prepend any current OB content
while (ob_get_length() !== false) {
// If ob_start() didn't have the PHP_OUTPUT_HANDLER_CLEANABLE flag, ob_get_clean() will cause a PHP notice
// and return false.
$obContent = @ob_get_clean();
if ($obContent !== false) {
$this->content = $obContent . $this->content;
} else {
break;
}
}
// Tell the browser to close the connection
$length = isset($this->content) ? strlen($this->content) : 0;
$this->getHeaders()
->set('Connection', 'close')
->set('Content-Length', (string)$length);
$this->send();
// Close the session.
Craft::$app->getSession()->close();
// In case we're running on php-fpm (https://secure.php.net/manual/en/book.fpm.php)
if (function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
}
}
/**
* @inheritdoc
* @since 3.4.0
*/
protected function defaultFormatters(): array
{
return ArrayHelper::merge(
parent::defaultFormatters(),
[
self::FORMAT_CSV => [
'class' => CsvResponseFormatter::class,
],
],
$this->defaultFormatters,
);
}
/**
* @inheritdoc
*/
protected function prepare(): void
{
parent::prepare();
$this->_isPrepared = true;
}
/**
* Clear the output buffer to prevent corrupt downloads.
*
* Need to check the OB status first, or else some PHP versions will throw an E_NOTICE
* since we have a custom error handler (http://pear.php.net/bugs/bug.php?id=9670).
*
*/
private function _clearOutputBuffer(): void
{
if (ob_get_length() !== false) {
// If zlib.output_compression is enabled, then ob_clean() will corrupt the results of output buffering.
// ob_end_clean is what we want.
ob_end_clean();
}
}
}