forked from modethirteen/HyperPlug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Headers.php
320 lines (276 loc) · 9.59 KB
/
Headers.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php declare(strict_types=1);
/**
* HyperPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace modethirteen\Http;
use InvalidArgumentException;
/**
* Class Headers
*
* @package modethirteen\Http
*/
class Headers implements IMutableHeaders {
const HEADER_CONTENT_TYPE = 'Content-Type';
const HEADER_AUTHORIZATION = 'Authorization';
const HEADER_CONTENT_LENGTH = 'Content-Length';
const HEADER_SET_COOKIE = 'Set-Cookie';
/**
* Header names that should not have multiple values
*
* @var string[]
*/
protected static array $singleValueHeaders = [
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17
'Content-Type',
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
'Location'
];
/**
* Header names that are sent as separate headers, not a single header with multiple values
*
* @var string[]
*/
protected static array $multipleNameValuePairHeaders = [
/**
* http://tools.ietf.org/html/rfc6265: Origin servers SHOULD NOT fold multiple Set-Cookie header fields into a single header field. The usual
* mechanism for folding HTTP headers fields (i.e., as defined in [RFC2616]) might change the semantics of the Set-Cookie header field because
* the %x2C (",") character is used by Set-Cookie in a way that conflicts with such folding.
*/
'Set-Cookie'
];
/**
* Return an instance from a name/value pair structure: [ [ name, value ], [ name, value ], ...]
*
* @param string[][] $pairs
* @return self
*/
public static function newFromHeaderNameValuePairs(array $pairs) : self {
$headers = new Headers();
foreach($pairs as $pair) {
if(!isset($pair[0])) {
throw new InvalidArgumentException('Invalid name/value pair structure');
}
$value = isset($pair[1]) ? $pair[1] : null;
$headers->addHeader($pair[0], $value);
}
return $headers;
}
/**
* Return a raw header string
*
* @param string $name
* @param string|null $value
* @return string
*/
private static function newRawHeader(string $name, string $value = null) : string {
return !StringUtil::isNullOrEmpty($value) ? "{$name}: {$value}" : "{$name}:";
}
/**
* Retrieve HTTP header name in capitalized, hyphenated format (Content-Type, Set-Cookie, ...)
*
* @param string $name
* @return string
*/
private static function getFormattedHeaderName(string $name) : string {
return implode('-', array_map('ucfirst', explode('-', strtolower($name))));
}
/**
* @var array<string, array<string>>
* @structure name => [ value, ... ]
*/
private array $headers = [];
/**
* @var array<mixed> - list of keys in the map
*/
private array $names = [];
/**
* @var mixed - current key
*/
private $name;
/**
* @var bool - split added raw headers on commas?
*/
private bool $isRawHeaderCommaSeparationEnabled = false;
public function __clone() {
// deep copy internal data objects and arrays
$this->headers = unserialize(serialize($this->headers));
$this->names = unserialize(serialize($this->names));
}
public function withRawHeaderCommaSeparationEnabled() : IHeaders {
$headers = clone $this;
$headers->isRawHeaderCommaSeparationEnabled = true;
return $headers;
}
public function getHeaderLine(string $name) : ?string {
$values = $this->getHeader(self::getFormattedHeaderName($name));
return !empty($values) ? implode(', ', $values) : null;
}
public function getHeader(string $name) : array {
$name = self::getFormattedHeaderName($name);
return isset($this->headers[$name]) ? $this->headers[$name] : [];
}
public function getSetCookieHeaderLine(string $cookieName) : ?string {
$headers = $this->getHeader('Set-Cookie');
if(empty($headers)) {
return null;
}
foreach($headers as $header) {
if(strpos($header, $cookieName) === 0) {
return $header;
}
}
return null;
}
public function addHeaders(IHeaders $headers) : void {
foreach($headers as $name => $values) {
foreach($values as $value) {
$this->addHeader($name, $value);
}
}
}
public function addHeader(string $name, $value) : void {
$name = self::getFormattedHeaderName($name);
$this->set($name, $this->getValuesHelper($value), false);
}
public function setHeader(string $name, $value) : void {
$name = self::getFormattedHeaderName($name);
$this->set($name, $this->getValuesHelper($value), true);
}
public function addRawHeader(string $header) : void {
$this->setRawHeaderHelper($header, false);
}
public function setRawHeader(string $header) : void {
$this->setRawHeaderHelper($header, true);
}
public function hasHeader(string $name) : bool {
return isset($this->headers[self::getFormattedHeaderName($name)]);
}
public function removeHeader(string $name) : void {
unset($this->headers[self::getFormattedHeaderName($name)]);
$this->names = array_keys($this->headers);
$this->rewind();
}
public function isEmpty() : bool {
return empty($this->headers);
}
public function rewind() {
$this->name = reset($this->names);
}
public function key() {
return $this->name;
}
/** @noinspection PhpMissingReturnTypeInspection */
public function current() {
return $this->headers[$this->name];
}
public function next() {
$this->name = next($this->names);
}
/** @noinspection PhpMissingReturnTypeInspection */
public function valid() {
return $this->name !== false;
}
public function toRawHeaders() : array {
$headers = [];
foreach($this as $name => $values) {
if(in_array($name, static::$multipleNameValuePairHeaders)) {
foreach($values as $value) {
$headers[] = self::newRawHeader($name, $value);
}
} else {
$headers[] = self::newRawHeader($name, implode(', ', $values));
}
}
return $headers;
}
public function toFlattenedArray() : array {
$headers = [];
foreach($this as $name => $values) {
$headers[$name] = implode(', ', $values);
}
return $headers;
}
public function toArray() : array {
return $this->headers;
}
public function toMutableHeaders() : IMutableHeaders {
return $this;
}
public function toMergedHeaders(IHeaders $headers) : IHeaders {
$instance = clone $this;
$instance->addHeaders($headers);
return $instance;
}
/**
* Build a header values array
*
* @param mixed $value
* @return array
*/
protected function getValuesHelper($value) : array {
if(is_string($value)) {
return [trim($value)];
}
if(is_array($value)) {
return array_map(function($value) {
return trim(StringUtil::stringify($value));
}, $value);
}
return [trim(StringUtil::stringify($value))];
}
/**
* @param string $name
* @param string[] $values
* @param bool $overwrite
* @return void
*/
protected function set(string $name, array $values, bool $overwrite) : void {
if(in_array($name, static::$singleValueHeaders)) {
// enforce headers that can only hold one value
if(!empty($values)) {
$values = [$values[0]];
}
$overwrite = true;
}
$this->headers[$name] = isset($this->headers[$name])
? ($overwrite ? $values : array_merge($this->headers[$name], $values))
: $values;
// rebuild map and move iterator back to first element
$values = array_values(array_filter($this->headers[$name], 'strlen'));
if(empty($values)) {
$values = [''];
}
$this->headers[$name] = $values;
$this->names = array_keys($this->headers);
$this->rewind();
}
/**
* @param string $header
* @param bool $overwrite
* @return void
*/
protected function setRawHeaderHelper(string $header, bool $overwrite) : void {
if(strpos($header, ':') === false) {
throw new InvalidArgumentException('Invalid HTTP header: ' . $header);
}
list($name, $value) = explode(':', $header, 2);
$name = self::getFormattedHeaderName($name);
$values = !$this->isRawHeaderCommaSeparationEnabled || in_array($name, static::$multipleNameValuePairHeaders)
? [trim($value)]
// split multiple header values
: array_filter(array_map('trim', explode(',', $value)), 'strlen');
$this->set($name, $values, $overwrite);
}
}