-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathHeaderTrait.php
270 lines (265 loc) · 10.6 KB
/
HeaderTrait.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php declare(strict_types=1);
/*
* This file is part of Aplus Framework HTTP Library.
*
* (c) Natan Felles <natanfelles@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Framework\HTTP;
/**
* Trait HeaderTrait.
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers
*
* @package http
*/
trait HeaderTrait
{
// -------------------------------------------------------------------------
// General headers (Request and Response)
// -------------------------------------------------------------------------
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
*/
public const string CACHE_CONTROL = 'Cache-Control';
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection
*/
public const string CONNECTION = 'Connection';
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
*/
public const string CONTENT_DISPOSITION = 'Content-Disposition';
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Date
*/
public const string DATE = 'Date';
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Keep-Alive
*/
public const string KEEP_ALIVE = 'Keep-Alive';
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Pragma
*/
public const string PRAGMA = 'Pragma';
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Via
*/
public const string VIA = 'Via';
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Warning
*/
public const string WARNING = 'Warning';
// -------------------------------------------------------------------------
// Representation headers (Request and Response)
// -------------------------------------------------------------------------
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding
*/
public const string CONTENT_ENCODING = 'Content-Encoding';
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Language
*/
public const string CONTENT_LANGUAGE = 'Content-Language';
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Location
*/
public const string CONTENT_LOCATION = 'Content-Location';
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
*/
public const string CONTENT_TYPE = 'Content-Type';
// -------------------------------------------------------------------------
// Payload headers (Request and Response)
// -------------------------------------------------------------------------
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Length
*/
public const string CONTENT_LENGTH = 'Content-Length';
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Range
*/
public const string CONTENT_RANGE = 'Content-Range';
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link
*/
public const string LINK = 'Link';
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Trailer
*/
public const string TRAILER = 'Trailer';
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding
*/
public const string TRANSFER_ENCODING = 'Transfer-Encoding';
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Upgrade
*/
public const string UPGRADE = 'Upgrade';
// -------------------------------------------------------------------------
// Custom
// -------------------------------------------------------------------------
/**
* @see https://riptutorial.com/http-headers/topic/10581/x-request-id
*/
public const string X_REQUEST_ID = 'X-Request-ID';
/**
* Header names.
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers
*
* @var array<string,string>
*/
protected static array $headers = [
// ---------------------------------------------------------------------
// General headers (Request and Response)
// ---------------------------------------------------------------------
'cache-control' => 'Cache-Control',
'connection' => 'Connection',
'content-disposition' => 'Content-Disposition',
'date' => 'Date',
'keep-alive' => 'Keep-Alive',
'link' => 'Link',
'pragma' => 'Pragma',
'via' => 'Via',
'warning' => 'Warning',
// ---------------------------------------------------------------------
// Representation headers (Request and Response)
// ---------------------------------------------------------------------
'content-encoding' => 'Content-Encoding',
'content-language' => 'Content-Language',
'content-location' => 'Content-Location',
'content-type' => 'Content-Type',
// ---------------------------------------------------------------------
// Payload headers (Request and Response)
// ---------------------------------------------------------------------
'content-length' => 'Content-Length',
'content-range' => 'Content-Range',
'trailer' => 'Trailer',
'transfer-encoding' => 'Transfer-Encoding',
// ---------------------------------------------------------------------
// Request headers
// ---------------------------------------------------------------------
'accept' => 'Accept',
'accept-charset' => 'Accept-Charset',
'accept-encoding' => 'Accept-Encoding',
'accept-language' => 'Accept-Language',
'access-control-request-headers' => 'Access-Control-Request-Headers',
'access-control-request-method' => 'Access-Control-Request-Method',
'authorization' => 'Authorization',
'cdn-loop' => 'CDN-Loop',
'cookie' => 'Cookie',
'dnt' => 'DNT',
'expect' => 'Expect',
'forwarded' => 'Forwarded',
'from' => 'From',
'host' => 'Host',
'if-match' => 'If-Match',
'if-modified-since' => 'If-Modified-Since',
'if-none-match' => 'If-None-Match',
'if-range' => 'If-Range',
'if-unmodified-since' => 'If-Unmodified-Since',
'origin' => 'Origin',
'priority' => 'Priority',
'proxy-authorization' => 'Proxy-Authorization',
'range' => 'Range',
'referer' => 'Referer',
'sec-fetch-dest' => 'Sec-Fetch-Dest',
'sec-fetch-mode' => 'Sec-Fetch-Mode',
'sec-fetch-site' => 'Sec-Fetch-Site',
'sec-fetch-user' => 'Sec-Fetch-User',
'te' => 'TE',
'upgrade-insecure-requests' => 'Upgrade-Insecure-Requests',
'user-agent' => 'User-Agent',
'x-forwarded-for' => 'X-Forwarded-For',
'x-forwarded-host' => 'X-Forwarded-Host',
'x-forwarded-proto' => 'X-Forwarded-Proto',
'x-real-ip' => 'X-Real-IP',
'x-requested-with' => 'X-Requested-With',
// ---------------------------------------------------------------------
// Response headers
// ---------------------------------------------------------------------
'accept-ranges' => 'Accept-Ranges',
'access-control-allow-credentials' => 'Access-Control-Allow-Credentials',
'access-control-allow-headers' => 'Access-Control-Allow-Headers',
'access-control-allow-methods' => 'Access-Control-Allow-Methods',
'access-control-allow-origin' => 'Access-Control-Allow-Origin',
'access-control-expose-headers' => 'Access-Control-Expose-Headers',
'access-control-max-age' => 'Access-Control-Max-Age',
'age' => 'Age',
'allow' => 'Allow',
'clear-site-data' => 'Clear-Site-Data',
'content-security-policy' => 'Content-Security-Policy',
'content-security-policy-report-only' => 'Content-Security-Policy-Report-Only',
'etag' => 'ETag',
'expect-ct' => 'Expect-CT',
'expires' => 'Expires',
'feature-policy' => 'Feature-Policy',
'last-modified' => 'Last-Modified',
'location' => 'Location',
'proxy-authenticate' => 'Proxy-Authenticate',
'public-key-pins' => 'Public-Key-Pins',
'public-key-pins-report-only' => 'Public-Key-Pins-Report-Only',
'referrer-policy' => 'Referrer-Policy',
'retry-after' => 'Retry-After',
'server' => 'Server',
'set-cookie' => 'Set-Cookie',
'sourcemap' => 'SourceMap',
'strict-transport-security' => 'Strict-Transport-Security',
'timing-allow-origin' => 'Timing-Allow-Origin',
'tk' => 'Tk',
'vary' => 'Vary',
'www-authenticate' => 'WWW-Authenticate',
'x-content-type-options' => 'X-Content-Type-Options',
'x-dns-prefetch-control' => 'X-DNS-Prefetch-Control',
'x-frame-options' => 'X-Frame-Options',
'x-robots-tag' => 'X-Robots-Tag',
'x-xss-protection' => 'X-XSS-Protection',
// ---------------------------------------------------------------------
// Custom (Response)
// ---------------------------------------------------------------------
'x-request-id' => 'X-Request-ID',
'x-powered-by' => 'X-Powered-By',
// ---------------------------------------------------------------------
// WebSocket
// ---------------------------------------------------------------------
'sec-websocket-extensions' => 'Sec-WebSocket-Extensions',
'sec-websocket-key' => 'Sec-WebSocket-Key',
'sec-websocket-protocol' => 'Sec-WebSocket-Protocol',
'sec-websocket-version' => 'Sec-WebSocket-Version',
];
public static function getName(string $name) : string
{
return static::$headers[\strtolower($name)] ?? $name;
}
public static function setName(string $name) : void
{
static::$headers[\strtolower($name)] = $name;
}
/**
* @return array<string>
*/
public static function getMultilines() : array
{
return [
'cdn-loop',
'date',
'expires',
'if-modified-since',
'if-range',
'if-unmodified-since',
'last-modified',
'proxy-authenticate',
'retry-after',
'set-cookie',
'x-robots-tag',
'www-authenticate',
];
}
public static function isMultiline(string $name) : bool
{
return \in_array(\strtolower($name), static::getMultilines(), true);
}
}