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) |
||
50 | { |
||
51 | $this->options = $options; |
||
52 | } |
||
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) |
||
62 | { |
||
63 | $headers = $request->getHeaders(); |
||
64 | |||
65 | if (! $headers->has('Origin')) { |
||
66 | return false; |
||
67 | } |
||
68 | |||
69 | try { |
||
70 | $origin = $headers->get('Origin'); |
||
71 | } catch (Header\Exception\InvalidArgumentException $exception) { |
||
72 | throw InvalidOriginException::fromInvalidHeaderValue(); |
||
73 | } |
||
74 | |||
75 | $originUri = UriFactory::factory($origin->getFieldValue()); |
||
|
|||
76 | $requestUri = $request->getUri(); |
||
77 | |||
78 | // According to the spec (http://tools.ietf.org/html/rfc6454#section-4), we should check host, port and scheme |
||
79 | |||
80 | return (! ($originUri->getHost() === $requestUri->getHost()) |
||
81 | || ! ($originUri->getPort() === $requestUri->getPort()) |
||
82 | || ! ($originUri->getScheme() === $requestUri->getScheme()) |
||
83 | ); |
||
84 | } |
||
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 | * @return HttpResponse |
||
150 | * @throws DisallowedOriginException If the origin is not allowed |
||
151 | */ |
||
152 | public function populateCorsResponse(HttpRequest $request, HttpResponse $response) |
||
184 | |||
185 | /** |
||
186 | * Get a single value for the "Access-Control-Allow-Origin" header |
||
187 | * |
||
188 | * According to the spec, it is not valid to set multiple origins separated by commas. Only accepted |
||
189 | * value are wildcard ("*"), an exact domain or a null string. |
||
190 | * |
||
191 | * @link http://www.w3.org/TR/cors/#access-control-allow-origin-response-header |
||
192 | * @param HttpRequest $request |
||
193 | * @return string |
||
194 | */ |
||
195 | protected function getAllowedOriginValue(HttpRequest $request) |
||
215 | |||
216 | /** |
||
217 | * Ensure that the Vary header is set. |
||
218 | * |
||
219 | * |
||
220 | * @link http://www.w3.org/TR/cors/#resource-implementation |
||
221 | * @param HttpResponse $response |
||
222 | * @return \Zend\Http\Headers |
||
223 | */ |
||
224 | public function ensureVaryHeader(HttpResponse $response) |
||
246 | } |
||
247 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: