|
1
|
|
|
<?php namespace Neomerx\JsonApi\Http\Headers; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015 [email protected] (www.neomerx.com) |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use \Neomerx\JsonApi\Exceptions\JsonApiException as E; |
|
20
|
|
|
use \Neomerx\JsonApi\Contracts\Codec\CodecMatcherInterface; |
|
21
|
|
|
use \Neomerx\JsonApi\Contracts\Http\Headers\HeadersCheckerInterface; |
|
22
|
|
|
use \Neomerx\JsonApi\Contracts\Http\Headers\HeaderParametersInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @package Neomerx\JsonApi |
|
26
|
|
|
*/ |
|
27
|
|
|
class RestrictiveHeadersChecker implements HeadersCheckerInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var CodecMatcherInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $codecMatcher; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param CodecMatcherInterface $codecMatcher |
|
36
|
|
|
*/ |
|
37
|
6 |
|
public function __construct(CodecMatcherInterface $codecMatcher) |
|
38
|
|
|
{ |
|
39
|
6 |
|
$this->codecMatcher = $codecMatcher; |
|
40
|
6 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param HeaderParametersInterface $parameters |
|
44
|
|
|
* |
|
45
|
|
|
* @return void |
|
46
|
|
|
*/ |
|
47
|
5 |
|
public function checkHeaders(HeaderParametersInterface $parameters) |
|
48
|
|
|
{ |
|
49
|
|
|
// Note: for these checks the order is specified by spec. See details inside. |
|
50
|
5 |
|
$this->checkAcceptHeader($parameters); |
|
51
|
4 |
|
$this->checkContentTypeHeader($parameters); |
|
52
|
2 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param HeaderParametersInterface $parameters |
|
56
|
|
|
* |
|
57
|
|
|
* @return void |
|
58
|
|
|
*/ |
|
59
|
5 |
|
protected function checkAcceptHeader(HeaderParametersInterface $parameters) |
|
60
|
|
|
{ |
|
61
|
5 |
|
$this->codecMatcher->matchEncoder($parameters->getAcceptHeader()); |
|
62
|
|
|
|
|
63
|
|
|
// From spec: Servers MUST respond with a 406 Not Acceptable status code |
|
64
|
|
|
// if a request's Accept header contains the JSON API media type and all |
|
65
|
|
|
// instances of that media type are modified with media type parameters. |
|
66
|
|
|
|
|
67
|
|
|
// We return 406 if no match found for encoder (media type with or wo parameters) |
|
68
|
|
|
// If no encoders were configured for media types with parameters we return 406 anyway |
|
69
|
5 |
|
if ($this->codecMatcher->getEncoderHeaderMatchedType() === null) { |
|
70
|
1 |
|
throw new E([], E::HTTP_CODE_NOT_ACCEPTABLE); |
|
71
|
|
|
} |
|
72
|
4 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param HeaderParametersInterface $parameters |
|
76
|
|
|
* |
|
77
|
|
|
* @return void |
|
78
|
|
|
*/ |
|
79
|
4 |
|
protected function checkContentTypeHeader(HeaderParametersInterface $parameters) |
|
80
|
|
|
{ |
|
81
|
|
|
// Do not allow specify more than 1 media type for input data. Otherwise which one is correct? |
|
82
|
4 |
|
if (count($parameters->getContentTypeHeader()->getMediaTypes()) > 1) { |
|
83
|
1 |
|
throw new E([], E::HTTP_CODE_BAD_REQUEST); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
3 |
|
$this->codecMatcher->matchDecoder($parameters->getContentTypeHeader()); |
|
87
|
|
|
|
|
88
|
|
|
// From spec: Servers MUST respond with a 415 Unsupported Media Type status code |
|
89
|
|
|
// if a request specifies the header Content-Type: application/vnd.api+json with |
|
90
|
|
|
// any media type parameters. |
|
91
|
|
|
|
|
92
|
|
|
// We return 415 if no match found for decoder (media type with or wo parameters) |
|
93
|
|
|
// If no decoders were configured for media types with parameters we return 415 anyway |
|
94
|
3 |
|
if ($this->codecMatcher->getDecoderHeaderMatchedType() === null) { |
|
95
|
1 |
|
throw new E([], E::HTTP_CODE_UNSUPPORTED_MEDIA_TYPE); |
|
96
|
|
|
} |
|
97
|
2 |
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|