-
Notifications
You must be signed in to change notification settings - Fork 2
/
ContentType.php
196 lines (163 loc) · 4.8 KB
/
ContentType.php
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
<?php
namespace Middlewares;
use Interop\Http\Server\MiddlewareInterface;
use Interop\Http\Server\RequestHandlerInterface;
use Middlewares\Utils\Factory;
use Negotiation\CharsetNegotiator;
use Negotiation\Negotiator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class ContentType implements MiddlewareInterface
{
use Utils\NegotiationTrait;
/**
* @var bool Whether use the first format as default
*/
private $useDefault = true;
/**
* @var array Available formats with the mime types
*/
private $formats;
/**
* @var array Available charsets
*/
private $charsets = ['UTF-8'];
/**
* @var bool Include X-Content-Type-Options: nosniff
*/
private $nosniff = true;
/**
* Return the default formats.
*
* @return array
*/
public static function getDefaultFormats()
{
return require __DIR__.'/formats_defaults.php';
}
/**
* Define de available formats.
*
* @param array|null $formats
*/
public function __construct(array $formats = null)
{
$this->formats = $formats ?: static::getDefaultFormats();
}
/**
* Whether use the first format as default.
*
* @param bool $useDefault
*
* @return self
*/
public function useDefault($useDefault = true)
{
$this->useDefault = (bool) $useDefault;
return $this;
}
/**
* Set the available charsets. The first value will be used as default
*
* @param array $charsets
*
* @return self
*/
public function charsets(array $charsets)
{
$this->charsets = $charsets;
return $this;
}
/**
* Configure the nosniff option.
*
* @param bool $nosniff
*
* @return self
*/
public function nosniff($nosniff = true)
{
$this->nosniff = $nosniff;
return $this;
}
/**
* Process a server request and return a response.
*
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
*
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler)
{
$format = $this->detectFromExtension($request) ?: $this->detectFromHeader($request);
if ($format === null) {
if (!$this->useDefault) {
return Factory::createResponse(406);
}
$format = key($this->formats);
}
$contentType = $this->formats[$format]['mime-type'][0];
$charset = $this->detectCharset($request) ?: current($this->charsets);
$request = $request
->withHeader('Accept', $contentType)
->withHeader('Accept-Charset', $charset);
$response = $handler->handle($request);
if (!$response->hasHeader('Content-Type')) {
$needCharset = !empty($this->formats[$format]['charset']);
if ($needCharset) {
$contentType .= '; charset='.$charset;
}
$response = $response->withHeader('Content-Type', $contentType);
}
if ($this->nosniff && !$response->hasHeader('X-Content-Type-Options')) {
$response = $response->withHeader('X-Content-Type-Options', 'nosniff');
}
return $response;
}
/**
* Returns the format using the file extension.
*
* @return null|string
*/
private function detectFromExtension(ServerRequestInterface $request)
{
$extension = strtolower(pathinfo($request->getUri()->getPath(), PATHINFO_EXTENSION));
if (empty($extension)) {
return;
}
foreach ($this->formats as $format => $data) {
if (in_array($extension, $data['extension'], true)) {
return $format;
}
}
}
/**
* Returns the format using the Accept header.
*
* @return null|string
*/
private function detectFromHeader(ServerRequestInterface $request)
{
$headers = call_user_func_array('array_merge', array_column($this->formats, 'mime-type'));
$accept = $request->getHeaderLine('Accept');
$mime = $this->negotiateHeader($accept, new Negotiator(), $headers);
if ($mime !== null) {
foreach ($this->formats as $format => $data) {
if (in_array($mime, $data['mime-type'], true)) {
return $format;
}
}
}
}
/**
* Returns the charset accepted.
*
* @return null|string
*/
private function detectCharset(ServerRequestInterface $request)
{
$accept = $request->getHeaderLine('Accept-Charset');
return $this->negotiateHeader($accept, new CharsetNegotiator(), $this->charsets);
}
}