1 | <?php |
||
39 | class CorsService |
||
40 | { |
||
41 | /** |
||
42 | * @var CorsOptions |
||
43 | */ |
||
44 | protected $options; |
||
45 | |||
46 | /** |
||
47 | * @param CorsOptions $options |
||
48 | */ |
||
49 | public function __construct(CorsOptions $options) |
||
53 | |||
54 | /** |
||
55 | * Check if the HTTP request is a CORS request by checking if the Origin header is present and that the |
||
56 | * request URI is not the same as the one in the Origin |
||
57 | * |
||
58 | * @param HttpRequest $request |
||
59 | * @return bool |
||
60 | */ |
||
61 | public function isCorsRequest(HttpRequest $request) |
||
85 | |||
86 | /** |
||
87 | * Check if the CORS request is a preflight request |
||
88 | * |
||
89 | * @param HttpRequest $request |
||
90 | * @return bool |
||
91 | */ |
||
92 | public function isPreflightRequest(HttpRequest $request) |
||
98 | |||
99 | /** |
||
100 | * Create a preflight response by adding the corresponding headers |
||
101 | * |
||
102 | * @param HttpRequest $request |
||
103 | * @return HttpResponse |
||
104 | */ |
||
105 | public function createPreflightCorsResponse(HttpRequest $request) |
||
124 | |||
125 | /** |
||
126 | * Create a preflight response by adding the correspoding headers which are merged with per-route configuration |
||
127 | * |
||
128 | * @param HttpRequest $request |
||
129 | * @param RouteMatch|DeprecatedRouteMatch|null $routeMatch |
||
130 | * |
||
131 | * @return HttpResponse |
||
132 | */ |
||
133 | public function createPreflightCorsResponseWithRouteOptions(HttpRequest $request, $routeMatch = null) |
||
143 | |||
144 | /** |
||
145 | * Populate a simple CORS response |
||
146 | * |
||
147 | * @param HttpRequest $request |
||
148 | * @param HttpResponse $response |
||
149 | * @param null|RouteMatch $routeMatch |
||
150 | * @return HttpResponse |
||
151 | * @throws DisallowedOriginException If the origin is not allowed |
||
152 | */ |
||
153 | public function populateCorsResponse(HttpRequest $request, HttpResponse $response, $routeMatch = null) |
||
154 | { |
||
155 | if ($routeMatch instanceof RouteMatch || $routeMatch instanceof DeprecatedRouteMatch) { |
||
156 | $this->options->setFromArray($routeMatch->getParam(CorsOptions::ROUTE_PARAM) ?: []); |
||
157 | } |
||
158 | |||
159 | $origin = $this->getAllowedOriginValue($request); |
||
160 | |||
161 | // If $origin is "null", then it means that the origin is not allowed. As this is |
||
162 | // a simple request, it is useless to continue the processing as it will be refused |
||
163 | // by the browser anyway, so we throw an exception |
||
164 | if ($origin === 'null') { |
||
165 | $origin = $request->getHeader('Origin'); |
||
166 | $originHeader = $origin ? $origin->getFieldValue() : ''; |
||
167 | throw new DisallowedOriginException( |
||
168 | sprintf( |
||
169 | 'The origin "%s" is not authorized', |
||
170 | $originHeader |
||
171 | ) |
||
172 | ); |
||
173 | } |
||
174 | |||
175 | $headers = $response->getHeaders(); |
||
176 | $headers->addHeaderLine('Access-Control-Allow-Origin', $origin); |
||
177 | $headers->addHeaderLine('Access-Control-Expose-Headers', implode(', ', $this->options->getExposedHeaders())); |
||
178 | |||
179 | $headers = $this->ensureVaryHeader($response); |
||
180 | |||
181 | if ($this->options->getAllowedCredentials()) { |
||
182 | $headers->addHeaderLine('Access-Control-Allow-Credentials', 'true'); |
||
183 | } |
||
184 | |||
185 | $response->setHeaders($headers); |
||
186 | |||
187 | return $response; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Get a single value for the "Access-Control-Allow-Origin" header |
||
192 | * |
||
193 | * According to the spec, it is not valid to set multiple origins separated by commas. Only accepted |
||
194 | * value are wildcard ("*"), an exact domain or a null string. |
||
195 | * |
||
196 | * @link http://www.w3.org/TR/cors/#access-control-allow-origin-response-header |
||
197 | * @param HttpRequest $request |
||
198 | * @return string |
||
199 | */ |
||
200 | protected function getAllowedOriginValue(HttpRequest $request) |
||
220 | |||
221 | /** |
||
222 | * Ensure that the Vary header is set. |
||
223 | * |
||
224 | * |
||
225 | * @link http://www.w3.org/TR/cors/#resource-implementation |
||
226 | * @param HttpResponse $response |
||
227 | * @return \Zend\Http\Headers |
||
228 | */ |
||
229 | public function ensureVaryHeader(HttpResponse $response) |
||
251 | } |
||
252 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.