forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageTrait.php
297 lines (248 loc) · 7.79 KB
/
MessageTrait.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
<?php
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\HTTP;
use CodeIgniter\HTTP\Exceptions\HTTPException;
use InvalidArgumentException;
/**
* Message Trait
* Additional methods to make a PSR-7 Message class
* compliant with the framework's own MessageInterface.
*
* @see https://github.com/php-fig/http-message/blob/master/src/MessageInterface.php
*/
trait MessageTrait
{
/**
* List of all HTTP request headers.
*
* [name => Header]
* or
* [name => [Header1, Header2]]
*
* @var array<string, Header|list<Header>>
*/
protected $headers = [];
/**
* Holds a map of lower-case header names
* and their normal-case key as it is in $headers.
* Used for case-insensitive header access.
*
* @var array
*/
protected $headerMap = [];
// --------------------------------------------------------------------
// Body
// --------------------------------------------------------------------
/**
* Sets the body of the current message.
*
* @param string $data
*
* @return $this
*/
public function setBody($data): self
{
$this->body = $data;
return $this;
}
/**
* Appends data to the body of the current message.
*
* @param string $data
*
* @return $this
*/
public function appendBody($data): self
{
$this->body .= (string) $data;
return $this;
}
// --------------------------------------------------------------------
// Headers
// --------------------------------------------------------------------
/**
* Populates the $headers array with any headers the server knows about.
*/
public function populateHeaders(): void
{
$contentType = $_SERVER['CONTENT_TYPE'] ?? getenv('CONTENT_TYPE');
if (! empty($contentType)) {
$this->setHeader('Content-Type', $contentType);
}
unset($contentType);
foreach (array_keys($_SERVER) as $key) {
if (sscanf($key, 'HTTP_%s', $header) === 1) {
// take SOME_HEADER and turn it into Some-Header
$header = str_replace('_', ' ', strtolower($header));
$header = str_replace(' ', '-', ucwords($header));
$this->setHeader($header, $_SERVER[$key]);
// Add us to the header map, so we can find them case-insensitively
$this->headerMap[strtolower($header)] = $header;
}
}
}
/**
* Returns an array containing all Headers.
*
* @return array<string, Header|list<Header>> An array of the Header objects
*/
public function headers(): array
{
// If no headers are defined, but the user is
// requesting it, then it's likely they want
// it to be populated so do that...
if (empty($this->headers)) {
$this->populateHeaders();
}
return $this->headers;
}
/**
* Returns a single Header object. If multiple headers with the same
* name exist, then will return an array of header objects.
*
* @param string $name
*
* @return Header|list<Header>|null
*/
public function header($name)
{
$origName = $this->getHeaderName($name);
return $this->headers[$origName] ?? null;
}
/**
* Sets a header and it's value.
*
* @param array|string|null $value
*
* @return $this
*/
public function setHeader(string $name, $value): self
{
$this->checkMultipleHeaders($name);
$origName = $this->getHeaderName($name);
if (
isset($this->headers[$origName])
&& is_array($this->headers[$origName]->getValue())
) {
if (! is_array($value)) {
$value = [$value];
}
foreach ($value as $v) {
$this->appendHeader($origName, $v);
}
} else {
$this->headers[$origName] = new Header($origName, $value);
$this->headerMap[strtolower($origName)] = $origName;
}
return $this;
}
private function hasMultipleHeaders(string $name): bool
{
$origName = $this->getHeaderName($name);
return isset($this->headers[$origName]) && is_array($this->headers[$origName]);
}
private function checkMultipleHeaders(string $name): void
{
if ($this->hasMultipleHeaders($name)) {
throw new InvalidArgumentException(
'The header "' . $name . '" already has multiple headers.'
. ' You cannot change them. If you really need to change, remove the header first.'
);
}
}
/**
* Removes a header from the list of headers we track.
*
* @return $this
*/
public function removeHeader(string $name): self
{
$origName = $this->getHeaderName($name);
unset($this->headers[$origName], $this->headerMap[strtolower($name)]);
return $this;
}
/**
* Adds an additional header value to any headers that accept
* multiple values (i.e. are an array or implement ArrayAccess)
*
* @return $this
*/
public function appendHeader(string $name, ?string $value): self
{
$this->checkMultipleHeaders($name);
$origName = $this->getHeaderName($name);
array_key_exists($origName, $this->headers)
? $this->headers[$origName]->appendValue($value)
: $this->setHeader($name, $value);
return $this;
}
/**
* Adds a header (not a header value) with the same name.
* Use this only when you set multiple headers with the same name,
* typically, for `Set-Cookie`.
*
* @return $this
*/
public function addHeader(string $name, string $value): static
{
$origName = $this->getHeaderName($name);
if (! isset($this->headers[$origName])) {
$this->setHeader($name, $value);
return $this;
}
if (! $this->hasMultipleHeaders($name) && isset($this->headers[$origName])) {
$this->headers[$origName] = [$this->headers[$origName]];
}
// Add the header.
$this->headers[$origName][] = new Header($origName, $value);
return $this;
}
/**
* Adds an additional header value to any headers that accept
* multiple values (i.e. are an array or implement ArrayAccess)
*
* @return $this
*/
public function prependHeader(string $name, string $value): self
{
$this->checkMultipleHeaders($name);
$origName = $this->getHeaderName($name);
$this->headers[$origName]->prependValue($value);
return $this;
}
/**
* Takes a header name in any case, and returns the
* normal-case version of the header.
*/
protected function getHeaderName(string $name): string
{
return $this->headerMap[strtolower($name)] ?? $name;
}
/**
* Sets the HTTP protocol version.
*
* @return $this
*
* @throws HTTPException For invalid protocols
*/
public function setProtocolVersion(string $version): self
{
if (! is_numeric($version)) {
$version = substr($version, strpos($version, '/') + 1);
}
// Make sure that version is in the correct format
$version = number_format((float) $version, 1);
if (! in_array($version, $this->validProtocolVersions, true)) {
throw HTTPException::forInvalidHTTPProtocol($version);
}
$this->protocolVersion = $version;
return $this;
}
}