-
-
Notifications
You must be signed in to change notification settings - Fork 963
Expand file tree
/
Copy pathHttpResponseHeadersTrait.php
More file actions
132 lines (113 loc) · 5.33 KB
/
HttpResponseHeadersTrait.php
File metadata and controls
132 lines (113 loc) · 5.33 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
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\State\Util;
use ApiPlatform\Metadata\Exception\HttpExceptionInterface;
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
use ApiPlatform\Metadata\Exception\ItemNotFoundException;
use ApiPlatform\Metadata\Exception\RuntimeException;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\IriConverterInterface;
use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface;
use ApiPlatform\Metadata\UrlGeneratorInterface;
use ApiPlatform\Metadata\Util\ClassInfoTrait;
use ApiPlatform\Metadata\Util\CloneTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface as SymfonyHttpExceptionInterface;
use Uri\Rfc3986\Uri;
/**
* Shares the logic to create API Platform's headers.
*
* @internal
*/
trait HttpResponseHeadersTrait
{
use ClassInfoTrait;
use CloneTrait;
private ?IriConverterInterface $iriConverter;
private ?OperationMetadataFactoryInterface $operationMetadataFactory;
/**
* @param array<string, mixed> $context
*
* @return array<string, string|string[]>
*/
private function getHeaders(Request $request, HttpOperation $operation, array $context): array
{
$status = $this->getStatus($request, $operation, $context);
$headers = [
'Content-Type' => \sprintf('%s; charset=utf-8', $request->getMimeType($request->getRequestFormat())),
'Vary' => 'Accept',
'X-Content-Type-Options' => 'nosniff',
'X-Frame-Options' => 'deny',
];
$exception = $request->attributes->get('exception');
if (($exception instanceof HttpExceptionInterface || $exception instanceof SymfonyHttpExceptionInterface) && $exceptionHeaders = $exception->getHeaders()) {
$headers = array_merge($headers, $exceptionHeaders);
}
if ($operationHeaders = $operation->getHeaders()) {
$headers = array_merge($headers, $operationHeaders);
}
if ($sunset = $operation->getSunset()) {
$headers['Sunset'] = (new \DateTimeImmutable($sunset))->format(\DateTimeInterface::RFC1123);
}
if ($acceptPatch = $operation->getAcceptPatch()) {
$headers['Accept-Patch'] = $acceptPatch;
}
$method = $request->getMethod();
$originalData = $context['original_data'] ?? null;
$outputMetadata = $operation->getOutput() ?? ['class' => $operation->getClass()];
$hasOutput = \is_array($outputMetadata) && \array_key_exists('class', $outputMetadata) && null !== $outputMetadata['class'];
$hasData = !$hasOutput ? false : ($this->resourceClassResolver && $originalData && \is_object($originalData) && $this->resourceClassResolver->isResourceClass($this->getObjectClass($originalData)));
if ($hasData) {
$isAlternateResourceMetadata = $operation->getExtraProperties()['is_alternate_resource_metadata'] ?? false;
$canonicalUriTemplate = $operation->getExtraProperties()['canonical_uri_template'] ?? null;
if (
!isset($headers['Location'])
&& 300 <= $status && $status < 400
&& ($isAlternateResourceMetadata || $canonicalUriTemplate)
) {
$canonicalOperation = $operation;
if ($this->operationMetadataFactory && null !== $canonicalUriTemplate) {
$canonicalOperation = $this->operationMetadataFactory->create($canonicalUriTemplate, $context);
}
if ($this->iriConverter) {
$headers['Location'] = $this->iriConverter->getIriFromResource($originalData, UrlGeneratorInterface::ABS_PATH, $canonicalOperation);
}
}
}
$query = PHP_VERSION_ID >= 80500 && \class_exists(Uri::class)
? Uri::parse($context['request_uri'] ?? '')?->getQuery()
: $requestParts['query'] ?? null
;
if ($this->iriConverter && !isset($headers['Content-Location'])) {
try {
$iri = null;
if ($hasData) {
$iri = $this->iriConverter->getIriFromResource($originalData);
} elseif ($operation->getClass()) {
$iri = $this->iriConverter->getIriFromResource($operation->getClass(), UrlGeneratorInterface::ABS_PATH, $operation);
}
if ($iri && 'GET' !== $method) {
$location = \sprintf('%s.%s', $iri, $request->getRequestFormat());
if (isset($query)) {
$location .= '?'.$query;
}
$headers['Content-Location'] = $location;
if ((Response::HTTP_CREATED === $status || (300 <= $status && $status < 400)) && 'POST' === $method && !isset($headers['Location'])) {
$headers['Location'] = $iri;
}
}
} catch (InvalidArgumentException|ItemNotFoundException|RuntimeException) {
}
}
return $headers;
}
}