|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Slim Framework (https://slimframework.com) |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/slimphp/Slim |
|
6
|
|
|
* @copyright Copyright (c) 2011-2016 Josh Lockhart |
|
7
|
|
|
* @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License) |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace Slim\Http; |
|
10
|
|
|
|
|
11
|
|
|
use Slim\Collection; |
|
12
|
|
|
use Slim\Interfaces\Http\HeadersInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Headers |
|
16
|
|
|
* |
|
17
|
|
|
* This class represents a collection of HTTP headers |
|
18
|
|
|
* that is used in both the HTTP request and response objects. |
|
19
|
|
|
* It also enables header name case-insensitivity when |
|
20
|
|
|
* getting or setting a header value. |
|
21
|
|
|
* |
|
22
|
|
|
* Each HTTP header can have multiple values. This class |
|
23
|
|
|
* stores values into an array for each header name. When |
|
24
|
|
|
* you request a header value, you receive an array of values |
|
25
|
|
|
* for that header. |
|
26
|
|
|
*/ |
|
27
|
|
|
class Headers extends Collection implements HeadersInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Special HTTP headers that do not have the "HTTP_" prefix |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected static $special = [ |
|
35
|
|
|
'CONTENT_TYPE' => 1, |
|
36
|
|
|
'CONTENT_LENGTH' => 1, |
|
37
|
|
|
'PHP_AUTH_USER' => 1, |
|
38
|
|
|
'PHP_AUTH_PW' => 1, |
|
39
|
|
|
'PHP_AUTH_DIGEST' => 1, |
|
40
|
|
|
'AUTH_TYPE' => 1, |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Create new headers collection with data extracted from |
|
45
|
|
|
* the application Environment object |
|
46
|
|
|
* |
|
47
|
|
|
* @param Environment $environment The Slim application Environment |
|
48
|
|
|
* |
|
49
|
|
|
* @return self |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function createFromEnvironment(Environment $environment) |
|
52
|
|
|
{ |
|
53
|
|
|
$data = []; |
|
54
|
|
|
$environment = self::determineAuthorization($environment); |
|
55
|
|
|
foreach ($environment as $key => $value) { |
|
56
|
|
|
$key = strtoupper($key); |
|
57
|
|
|
if (isset(static::$special[$key]) || strpos($key, 'HTTP_') === 0) { |
|
58
|
|
|
if ($key !== 'HTTP_CONTENT_LENGTH') { |
|
59
|
|
|
$data[self::reconstructOriginalKey($key)] = $value; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return new static($data); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* If HTTP_AUTHORIZATION does not exist tries to get it from |
|
69
|
|
|
* getallheaders() when available. |
|
70
|
|
|
* |
|
71
|
|
|
* @param Environment $environment The Slim application Environment |
|
72
|
|
|
* |
|
73
|
|
|
* @return Environment |
|
74
|
|
|
*/ |
|
75
|
|
|
|
|
76
|
|
|
public static function determineAuthorization(Environment $environment) |
|
77
|
|
|
{ |
|
78
|
|
|
$authorization = $environment->get('HTTP_AUTHORIZATION'); |
|
79
|
|
|
|
|
80
|
|
|
if (null === $authorization && is_callable('getallheaders')) { |
|
81
|
|
|
$headers = getallheaders(); |
|
82
|
|
|
$headers = array_change_key_case($headers, CASE_LOWER); |
|
83
|
|
|
if (isset($headers['authorization'])) { |
|
84
|
|
|
$environment->set('HTTP_AUTHORIZATION', $headers['authorization']); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $environment; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Return array of HTTP header names and values. |
|
93
|
|
|
* This method returns the _original_ header name |
|
94
|
|
|
* as specified by the end user. |
|
95
|
|
|
* |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
|
|
public function all() |
|
99
|
|
|
{ |
|
100
|
|
|
$all = parent::all(); |
|
101
|
|
|
$out = []; |
|
102
|
|
|
foreach ($all as $key => $props) { |
|
103
|
|
|
$out[$props['originalKey']] = $props['value']; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $out; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Set HTTP header value |
|
111
|
|
|
* |
|
112
|
|
|
* This method sets a header value. It replaces |
|
113
|
|
|
* any values that may already exist for the header name. |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $key The case-insensitive header name |
|
116
|
|
|
* @param string $value The header value |
|
117
|
|
|
*/ |
|
118
|
|
|
public function set($key, $value) |
|
119
|
|
|
{ |
|
120
|
|
|
if (!is_array($value)) { |
|
121
|
|
|
$value = [$value]; |
|
122
|
|
|
} |
|
123
|
|
|
parent::set($this->normalizeKey($key), [ |
|
124
|
|
|
'value' => $value, |
|
125
|
|
|
'originalKey' => $key |
|
126
|
|
|
]); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Get HTTP header value |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $key The case-insensitive header name |
|
133
|
|
|
* @param mixed $default The default value if key does not exist |
|
134
|
|
|
* |
|
135
|
|
|
* @return string[] |
|
136
|
|
|
*/ |
|
137
|
|
|
public function get($key, $default = null) |
|
138
|
|
|
{ |
|
139
|
|
|
if ($this->has($key)) { |
|
140
|
|
|
return parent::get($this->normalizeKey($key))['value']; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $default; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Get HTTP header key as originally specified |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $key The case-insensitive header name |
|
150
|
|
|
* @param mixed $default The default value if key does not exist |
|
151
|
|
|
* |
|
152
|
|
|
* @return string |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getOriginalKey($key, $default = null) |
|
155
|
|
|
{ |
|
156
|
|
|
if ($this->has($key)) { |
|
157
|
|
|
return parent::get($this->normalizeKey($key))['originalKey']; |
|
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return $default; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Add HTTP header value |
|
165
|
|
|
* |
|
166
|
|
|
* This method appends a header value. Unlike the set() method, |
|
167
|
|
|
* this method _appends_ this new value to any values |
|
168
|
|
|
* that already exist for this header name. |
|
169
|
|
|
* |
|
170
|
|
|
* @param string $key The case-insensitive header name |
|
171
|
|
|
* @param array|string $value The new header value(s) |
|
172
|
|
|
*/ |
|
173
|
|
|
public function add($key, $value) |
|
174
|
|
|
{ |
|
175
|
|
|
$oldValues = $this->get($key, []); |
|
176
|
|
|
$newValues = is_array($value) ? $value : [$value]; |
|
177
|
|
|
$this->set($key, array_merge($oldValues, array_values($newValues))); |
|
|
|
|
|
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Does this collection have a given header? |
|
182
|
|
|
* |
|
183
|
|
|
* @param string $key The case-insensitive header name |
|
184
|
|
|
* |
|
185
|
|
|
* @return bool |
|
186
|
|
|
*/ |
|
187
|
|
|
public function has($key) |
|
188
|
|
|
{ |
|
189
|
|
|
return parent::has($this->normalizeKey($key)); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Remove header from collection |
|
194
|
|
|
* |
|
195
|
|
|
* @param string $key The case-insensitive header name |
|
196
|
|
|
*/ |
|
197
|
|
|
public function remove($key) |
|
198
|
|
|
{ |
|
199
|
|
|
parent::remove($this->normalizeKey($key)); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Normalize header name |
|
204
|
|
|
* |
|
205
|
|
|
* This method transforms header names into a |
|
206
|
|
|
* normalized form. This is how we enable case-insensitive |
|
207
|
|
|
* header names in the other methods in this class. |
|
208
|
|
|
* |
|
209
|
|
|
* @param string $key The case-insensitive header name |
|
210
|
|
|
* |
|
211
|
|
|
* @return string Normalized header name |
|
212
|
|
|
*/ |
|
213
|
|
|
public function normalizeKey($key) |
|
214
|
|
|
{ |
|
215
|
|
|
$key = strtr(strtolower($key), '_', '-'); |
|
216
|
|
|
if (strpos($key, 'http-') === 0) { |
|
217
|
|
|
$key = substr($key, 5); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
return $key; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Reconstruct original header name |
|
225
|
|
|
* |
|
226
|
|
|
* This method takes an HTTP header name from the Environment |
|
227
|
|
|
* and returns it as it was probably formatted by the actual client. |
|
228
|
|
|
* |
|
229
|
|
|
* @param string $key An HTTP header key from the $_SERVER global variable |
|
230
|
|
|
* |
|
231
|
|
|
* @return string The reconstructed key |
|
232
|
|
|
* |
|
233
|
|
|
* @example CONTENT_TYPE => Content-Type |
|
234
|
|
|
* @example HTTP_USER_AGENT => User-Agent |
|
235
|
|
|
*/ |
|
236
|
|
|
private static function reconstructOriginalKey($key) |
|
237
|
|
|
{ |
|
238
|
|
|
if (strpos($key, 'HTTP_') === 0) { |
|
239
|
|
|
$key = substr($key, 5); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
return strtr(ucwords(strtr(strtolower($key), '_', ' ')), ' ', '-'); |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.