Repository navigation
Expand file tree
/
Copy pathRequestHandlerComponent.php
More file actions
523 lines (465 loc) · 18 KB
/
Copy pathRequestHandlerComponent.php
File metadata and controls
523 lines (465 loc) · 18 KB
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<?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 0.10.4
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Controller\Component;
use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
use Cake\Controller\Controller;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Event\EventInterface;
use Cake\Http\Exception\NotFoundException;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Cake\Routing\Router;
use Cake\Utility\Inflector;
/**
* Request object handling for alternative HTTP requests.
*
* This Component checks for requests for different content types like JSON, XML,
* XMLHttpRequest(AJAX) and configures the response object and view builder accordingly.
*
* It can also check for HTTP caching headers like `Last-Modified`, `If-Modified-Since`
* etc. and return a response accordingly.
*
* @link https://book.cakephp.org/4/en/controllers/components/request-handling.html
*/
class RequestHandlerComponent extends Component
{
/**
* Contains the file extension parsed out by the Router
*
* @var string|null
* @see \Cake\Routing\Router::extensions()
*/
protected $ext;
/**
* The template type to use when rendering the given content type.
*
* @var string|null
*/
protected $_renderType;
/**
* Default config
*
* These are merged with user-provided config when the component is used.
*
* - `checkHttpCache` - Whether to check for HTTP cache. Default `true`.
* - `viewClassMap` - Mapping between type and view classes. If undefined
* json, xml, and ajax will be mapped. Defining any types will omit the defaults.
*
* @var array
*/
protected $_defaultConfig = [
'checkHttpCache' => true,
'viewClassMap' => [],
];
/**
* Constructor. Parses the accepted content types accepted by the client using HTTP_ACCEPT
*
* @param \Cake\Controller\ComponentRegistry $registry ComponentRegistry object.
* @param array $config Array of config.
*/
public function __construct(ComponentRegistry $registry, array $config = [])
{
$config += [
'viewClassMap' => [
'json' => 'Json',
'xml' => 'Xml',
'ajax' => 'Ajax',
],
];
parent::__construct($registry, $config);
}
/**
* Events supported by this component.
*
* @return array
*/
public function implementedEvents(): array
{
return [
'Controller.startup' => 'startup',
'Controller.beforeRender' => 'beforeRender',
];
}
/**
* Set the extension based on the `Accept` header or URL extension.
*
* Compares the accepted types and configured extensions.
* If there is one common type, that is assigned as the ext/content type for the response.
* The type with the highest weight will be set. If the highest weight has more
* than one type matching the extensions, the order in which extensions are specified
* determines which type will be set.
*
* If html is one of the preferred types, no content type will be set, this
* is to avoid issues with browsers that prefer HTML and several other content types.
*
* @param \Cake\Http\ServerRequest $request The request instance.
* @param \Cake\Http\Response $response The response instance.
* @return void
*/
protected function _setExtension(ServerRequest $request, Response $response): void
{
$accept = $request->parseAccept();
if (empty($accept) || current($accept)[0] === 'text/html') {
return;
}
/** @var array $accepts */
$accepts = $response->mapType($accept);
$preferredTypes = current($accepts);
if (array_intersect($preferredTypes, ['html', 'xhtml'])) {
return;
}
$extensions = array_unique(
array_merge(Router::extensions(), array_keys($this->getConfig('viewClassMap')))
);
foreach ($accepts as $types) {
$ext = array_intersect($extensions, $types);
if ($ext) {
$this->ext = current($ext);
break;
}
}
}
/**
* The startup method of the RequestHandler enables several automatic behaviors
* related to the detection of certain properties of the HTTP request, including:
*
* If the XML data is POSTed, the data is parsed into an XML object, which is assigned
* to the $data property of the controller, which can then be saved to a model object.
*
* @param \Cake\Event\EventInterface $event The startup event that was fired.
* @return void
*/
public function startup(EventInterface $event): void
{
$controller = $this->getController();
$request = $controller->getRequest();
$response = $controller->getResponse();
$this->ext = $request->getParam('_ext');
if (!$this->ext || in_array($this->ext, ['html', 'htm'], true)) {
$this->_setExtension($request, $response);
}
$isAjax = $request->is('ajax');
$controller->setRequest($request->withAttribute('isAjax', $isAjax));
if (!$this->ext && $isAjax) {
$this->ext = 'ajax';
}
}
/**
* Checks if the response can be considered different according to the request
* headers, and the caching response headers. If it was not modified, then the
* render process is skipped. And the client will get a blank response with a
* "304 Not Modified" header.
*
* - If Router::extensions() is enabled, the layout and template type are
* switched based on the parsed extension or `Accept` header. For example,
* if `controller/action.xml` is requested, the view path becomes
* `templates/Controller/xml/action.php`. Also if `controller/action` is
* requested with `Accept: application/xml` in the headers the view
* path will become `templates/Controller/xml/action.php`. Layout and template
* types will only switch to mime-types recognized by Cake\Http\Response.
* If you need to declare additional mime-types, you can do so using
* Cake\Http\Response::type() in your controller's beforeFilter() method.
* - If a helper with the same name as the extension exists, it is added to
* the controller.
* - If the extension is of a type that RequestHandler understands, it will
* set that Content-type in the response header.
*
* @param \Cake\Event\EventInterface $event The Controller.beforeRender event.
* @return void
* @throws \Cake\Http\Exception\NotFoundException If invoked extension is not configured.
*/
public function beforeRender(EventInterface $event): void
{
$controller = $this->getController();
$response = $controller->getResponse();
if ($this->ext && !in_array($this->ext, ['html', 'htm'], true)) {
if (!$response->getMimeType($this->ext)) {
throw new NotFoundException('Invoked extension not recognized/configured: ' . $this->ext);
}
$this->renderAs($controller, $this->ext);
$response = $controller->getResponse();
} else {
$response = $response->withCharset(Configure::read('App.encoding'));
}
if (
$this->_config['checkHttpCache'] &&
$response->checkNotModified($controller->getRequest())
) {
$controller->setResponse($response);
$event->stopPropagation();
return;
}
$controller->setResponse($response);
}
/**
* Determines which content types the client accepts. Acceptance is based on
* the file extension parsed by the Router (if present), and by the HTTP_ACCEPT
* header. Unlike Cake\Http\ServerRequest::accepts() this method deals entirely with mapped content types.
*
* Usage:
*
* ```
* $this->RequestHandler->accepts(['xml', 'html', 'json']);
* ```
*
* Returns true if the client accepts any of the supplied types.
*
* ```
* $this->RequestHandler->accepts('xml');
* ```
*
* Returns true if the client accepts xml.
*
* @param string|array|null $type Can be null (or no parameter), a string type name, or an
* array of types
* @return mixed If null or no parameter is passed, returns an array of content
* types the client accepts. If a string is passed, returns true
* if the client accepts it. If an array is passed, returns true
* if the client accepts one or more elements in the array.
*/
public function accepts($type = null)
{
$controller = $this->getController();
/** @var array $accepted */
$accepted = $controller->getRequest()->accepts();
if (!$type) {
return $controller->getResponse()->mapType($accepted);
}
if (is_array($type)) {
foreach ($type as $t) {
$t = $this->mapAlias($t);
if (in_array($t, $accepted, true)) {
return true;
}
}
return false;
}
if (is_string($type)) {
return in_array($this->mapAlias($type), $accepted, true);
}
return false;
}
/**
* Determines the content type of the data the client has sent (i.e. in a POST request)
*
* @param string|array|null $type Can be null (or no parameter), a string type name, or an array of types
* @return mixed If a single type is supplied a boolean will be returned. If no type is provided
* The mapped value of CONTENT_TYPE will be returned. If an array is supplied the first type
* in the request content type will be returned.
*/
public function requestedWith($type = null)
{
$controller = $this->getController();
$request = $controller->getRequest();
if (
!$request->is('post') &&
!$request->is('put') &&
!$request->is('patch') &&
!$request->is('delete')
) {
return null;
}
if (is_array($type)) {
foreach ($type as $t) {
if ($this->requestedWith($t)) {
return $t;
}
}
return false;
}
[$contentType] = explode(';', $request->contentType() ?? '');
if ($type === null) {
return $controller->getResponse()->mapType($contentType);
}
if (is_string($type)) {
return $type === $controller->getResponse()->mapType($contentType);
}
}
/**
* Determines which content-types the client prefers. If no parameters are given,
* the single content-type that the client most likely prefers is returned. If $type is
* an array, the first item in the array that the client accepts is returned.
* Preference is determined primarily by the file extension parsed by the Router
* if provided, and secondarily by the list of content-types provided in
* HTTP_ACCEPT.
*
* @param string|array|null $type An optional array of 'friendly' content-type names, i.e.
* 'html', 'xml', 'js', etc.
* @return string|bool|null If $type is null or not provided, the first content-type in the
* list, based on preference, is returned. If a single type is provided
* a boolean will be returned if that type is preferred.
* If an array of types are provided then the first preferred type is returned.
* If no type is provided the first preferred type is returned.
*/
public function prefers($type = null)
{
$controller = $this->getController();
$acceptRaw = $controller->getRequest()->parseAccept();
if (empty($acceptRaw)) {
return $type ? $type === $this->ext : $this->ext;
}
/** @var array $accepts */
$accepts = $controller->getResponse()->mapType(array_shift($acceptRaw));
if (!$type) {
if (empty($this->ext) && !empty($accepts)) {
return $accepts[0];
}
return $this->ext;
}
$types = (array)$type;
if (count($types) === 1) {
if ($this->ext) {
return in_array($this->ext, $types, true);
}
return in_array($types[0], $accepts, true);
}
$intersect = array_values(array_intersect($accepts, $types));
if (!$intersect) {
return false;
}
return $intersect[0];
}
/**
* Sets either the view class if one exists or the layout and template path of the view.
* The names of these are derived from the $type input parameter.
*
* ### Usage:
*
* Render the response as an 'ajax' response.
*
* ```
* $this->RequestHandler->renderAs($this, 'ajax');
* ```
*
* Render the response as an xml file and force the result as a file download.
*
* ```
* $this->RequestHandler->renderAs($this, 'xml', ['attachment' => 'myfile.xml'];
* ```
*
* @param \Cake\Controller\Controller $controller A reference to a controller object
* @param string $type Type of response to send (e.g: 'ajax')
* @param array $options Array of options to use
* @return void
* @see \Cake\Controller\Component\RequestHandlerComponent::respondAs()
*/
public function renderAs(Controller $controller, string $type, array $options = []): void
{
$defaults = ['charset' => 'UTF-8'];
$viewClassMap = $this->getConfig('viewClassMap');
if (Configure::read('App.encoding') !== null) {
$defaults['charset'] = Configure::read('App.encoding');
}
$options += $defaults;
$builder = $controller->viewBuilder();
if (array_key_exists($type, $viewClassMap)) {
$view = $viewClassMap[$type];
} else {
$view = Inflector::classify($type);
}
$viewClass = null;
if ($builder->getClassName() === null) {
$viewClass = App::className($view, 'View', 'View');
}
if ($viewClass) {
$builder->setClassName($viewClass);
} else {
if (!$this->_renderType) {
$builder->setTemplatePath((string)$builder->getTemplatePath() . DIRECTORY_SEPARATOR . $type);
} else {
$builder->setTemplatePath(preg_replace(
"/([\/\\\\]{$this->_renderType})$/",
DIRECTORY_SEPARATOR . $type,
(string)$builder->getTemplatePath()
));
}
$this->_renderType = $type;
$builder->setLayoutPath($type);
}
if ($controller->getResponse()->getMimeType($type)) {
$this->respondAs($type, $options);
}
}
/**
* Sets the response header based on type map index name. This wraps several methods
* available on Cake\Http\Response. It also allows you to use Content-Type aliases.
*
* @param string $type Friendly type name, i.e. 'html' or 'xml', or a full content-type,
* like 'application/x-shockwave'.
* @param array $options If $type is a friendly type name that is associated with
* more than one type of content, $index is used to select which content-type to use.
* @return bool Returns false if the friendly type name given in $type does
* not exist in the type map, or if the Content-type header has
* already been set by this method.
*/
public function respondAs($type, array $options = []): bool
{
$defaults = ['index' => null, 'charset' => null, 'attachment' => false];
$options += $defaults;
$cType = $type;
$controller = $this->getController();
$response = $controller->getResponse();
if (strpos($type, '/') === false) {
$cType = $response->getMimeType($type);
}
if (is_array($cType)) {
if (isset($cType[$options['index']])) {
$cType = $cType[$options['index']];
}
if ($this->prefers($cType)) {
$cType = $this->prefers($cType);
} else {
$cType = $cType[0];
}
}
if (!$cType) {
return false;
}
$response = $response->withType($cType);
if (!empty($options['charset'])) {
$response = $response->withCharset($options['charset']);
}
if (!empty($options['attachment'])) {
$response = $response->withDownload($options['attachment']);
}
$controller->setResponse($response);
return true;
}
/**
* Maps a content type alias back to its mime-type(s)
*
* @param string|array $alias String alias to convert back into a content type. Or an array of aliases to map.
* @return string|array|null Null on an undefined alias. String value of the mapped alias type. If an
* alias maps to more than one content type, the first one will be returned. If an array is provided
* for $alias, an array of mapped types will be returned.
*/
public function mapAlias($alias)
{
if (is_array($alias)) {
return array_map([$this, 'mapAlias'], $alias);
}
$type = $this->getController()->getResponse()->getMimeType($alias);
if ($type) {
if (is_array($type)) {
return $type[0];
}
return $type;
}
return null;
}
}