/
Headers.php
240 lines (220 loc) · 6.51 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
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @link https://github.com/slimphp/Slim-Psr7
* @copyright Copyright (c) 2011-2017 Josh Lockhart
* @license https://github.com/slimphp/Slim-Psr7/blob/master/LICENSE (MIT License)
*/
namespace Slim\Psr7;
/**
* Headers
*
* This class represents a collection of HTTP headers
* that is used in both the HTTP request and response objects.
* It also enables header name case-insensitivity when
* getting or setting a header value.
*
* Each HTTP header can have multiple values. This class
* stores values into an array for each header name. When
* you request a header value, you receive an array of values
* for that header.
*/
class Headers extends Collection implements HeadersInterface
{
/**
* Special HTTP headers that do not have the "HTTP_" prefix
*
* @var array
*/
protected static $special = [
'CONTENT_TYPE' => 1,
'CONTENT_LENGTH' => 1,
'PHP_AUTH_USER' => 1,
'PHP_AUTH_PW' => 1,
'PHP_AUTH_DIGEST' => 1,
'AUTH_TYPE' => 1,
];
/**
* Create new headers collection with data extracted from
* the PHP global environment
*
* @param array $globals Global server variables
*
* @return self
*/
public static function createFromGlobals(array $globals)
{
$data = [];
$globals = self::determineAuthorization($globals);
foreach ($globals as $key => $value) {
$key = strtoupper($key);
if (isset(static::$special[$key]) || strpos($key, 'HTTP_') === 0) {
if ($key !== 'HTTP_CONTENT_LENGTH') {
$data[self::reconstructOriginalKey($key)] = $value;
}
}
}
return new static($data);
}
/**
* If HTTP_AUTHORIZATION does not exist tries to get it from
* getallheaders() when available.
*
* @param array $globals The Slim application Environment
*
* @return array
*/
public static function determineAuthorization(array $globals)
{
$authorization = isset($globals['HTTP_AUTHORIZATION']) ? $globals['HTTP_AUTHORIZATION'] : null;
if (empty($authorization) && is_callable('getallheaders')) {
$headers = getallheaders();
$headers = array_change_key_case($headers, CASE_LOWER);
if (isset($headers['authorization'])) {
$globals['HTTP_AUTHORIZATION'] = $headers['authorization'];
}
}
return $globals;
}
/**
* Return array of HTTP header names and values.
* This method returns the _original_ header name
* as specified by the end user.
*
* @return array
*/
public function all()
{
$all = parent::all();
$out = [];
foreach ($all as $key => $props) {
$out[$props['originalKey']] = $props['value'];
}
return $out;
}
/**
* Set HTTP header value
*
* This method sets a header value. It replaces
* any values that may already exist for the header name.
*
* @param string $key The case-insensitive header name
* @param string $value The header value
*/
public function set($key, $value)
{
if (!is_array($value)) {
$value = [$value];
}
parent::set($this->normalizeKey($key), [
'value' => $value,
'originalKey' => $key
]);
}
/**
* Get HTTP header value
*
* @param string $key The case-insensitive header name
* @param mixed $default The default value if key does not exist
*
* @return string[]
*/
public function get($key, $default = null)
{
if ($this->has($key)) {
return parent::get($this->normalizeKey($key))['value'];
}
return $default;
}
/**
* Get HTTP header key as originally specified
*
* @param string $key The case-insensitive header name
* @param mixed $default The default value if key does not exist
*
* @return string
*/
public function getOriginalKey($key, $default = null)
{
if ($this->has($key)) {
return parent::get($this->normalizeKey($key))['originalKey'];
}
return $default;
}
/**
* Add HTTP header value
*
* This method appends a header value. Unlike the set() method,
* this method _appends_ this new value to any values
* that already exist for this header name.
*
* @param string $key The case-insensitive header name
* @param array|string $value The new header value(s)
*/
public function add($key, $value)
{
$oldValues = $this->get($key, []);
$newValues = is_array($value) ? $value : [$value];
$this->set($key, array_merge($oldValues, array_values($newValues)));
}
/**
* Does this collection have a given header?
*
* @param string $key The case-insensitive header name
*
* @return bool
*/
public function has($key)
{
return parent::has($this->normalizeKey($key));
}
/**
* Remove header from collection
*
* @param string $key The case-insensitive header name
*/
public function remove($key)
{
parent::remove($this->normalizeKey($key));
}
/**
* Normalize header name
*
* This method transforms header names into a
* normalized form. This is how we enable case-insensitive
* header names in the other methods in this class.
*
* @param string $key The case-insensitive header name
*
* @return string Normalized header name
*/
public function normalizeKey($key)
{
$key = strtr(strtolower($key), '_', '-');
if (strpos($key, 'http-') === 0) {
$key = substr($key, 5);
}
return $key;
}
/**
* Reconstruct original header name
*
* This method takes an HTTP header name from the Environment
* and returns it as it was probably formatted by the actual client.
*
* @param string $key An HTTP header key from the $_SERVER global variable
*
* @return string The reconstructed key
*
* @example CONTENT_TYPE => Content-Type
* @example HTTP_USER_AGENT => User-Agent
*/
private static function reconstructOriginalKey($key)
{
if (strpos($key, 'HTTP_') === 0) {
$key = substr($key, 5);
}
return strtr(ucwords(strtr(strtolower($key), '_', ' ')), ' ', '-');
}
}