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
|
<?php
namespace Sabre\HTTP;
/**
* This is the abstract base class for both the Request and Response objects.
*
* This object contains a few simple methods that are shared by both.
*
* @copyright Copyright (C) fruux GmbH (https://fruux.com/)
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/
abstract class Message implements MessageInterface {
/**
* Request body
*
* This should be a stream resource
*
* @var resource
*/
protected $body;
/**
* Contains the list of HTTP headers
*
* @var array
*/
protected $headers = [];
/**
* HTTP message version (1.0 or 1.1)
*
* @var string
*/
protected $httpVersion = '1.1';
/**
* Returns the body as a readable stream resource.
*
* Note that the stream may not be rewindable, and therefore may only be
* read once.
*
* @return resource
*/
function getBodyAsStream() {
$body = $this->getBody();
if (is_string($body) || is_null($body)) {
$stream = fopen('php://temp', 'r+');
fwrite($stream, $body);
rewind($stream);
return $stream;
}
return $body;
}
/**
* Returns the body as a string.
*
* Note that because the underlying data may be based on a stream, this
* method could only work correctly the first time.
*
* @return string
*/
function getBodyAsString() {
$body = $this->getBody();
if (is_string($body)) {
return $body;
}
if (is_null($body)) {
return '';
}
$contentLength = $this->getHeader('Content-Length');
if (null === $contentLength) {
return stream_get_contents($body);
} else {
return stream_get_contents($body, $contentLength);
}
}
/**
* Returns the message body, as it's internal representation.
*
* This could be either a string or a stream.
*
* @return resource|string
*/
function getBody() {
return $this->body;
}
/**
* Replaces the body resource with a new stream or string.
*
* @param resource|string $body
*/
function setBody($body) {
$this->body = $body;
}
/**
* Returns all the HTTP headers as an array.
*
* Every header is returned as an array, with one or more values.
*
* @return array
*/
function getHeaders() {
$result = [];
foreach ($this->headers as $headerInfo) {
$result[$headerInfo[0]] = $headerInfo[1];
}
return $result;
}
/**
* Will return true or false, depending on if a HTTP header exists.
*
* @param string $name
* @return bool
*/
function hasHeader($name) {
return isset($this->headers[strtolower($name)]);
}
/**
* Returns a specific HTTP header, based on it's name.
*
* The name must be treated as case-insensitive.
* If the header does not exist, this method must return null.
*
* If a header appeared more than once in a HTTP request, this method will
* concatenate all the values with a comma.
*
* Note that this not make sense for all headers. Some, such as
* `Set-Cookie` cannot be logically combined with a comma. In those cases
* you *should* use getHeaderAsArray().
*
* @param string $name
* @return string|null
*/
function getHeader($name) {
$name = strtolower($name);
if (isset($this->headers[$name])) {
return implode(',', $this->headers[$name][1]);
}
return null;
}
/**
* Returns a HTTP header as an array.
*
* For every time the HTTP header appeared in the request or response, an
* item will appear in the array.
*
* If the header did not exists, this method will return an empty array.
*
* @param string $name
* @return string[]
*/
function getHeaderAsArray($name) {
$name = strtolower($name);
if (isset($this->headers[$name])) {
return $this->headers[$name][1];
}
return [];
}
/**
* Updates a HTTP header.
*
* The case-sensitity of the name value must be retained as-is.
*
* If the header already existed, it will be overwritten.
*
* @param string $name
* @param string|string[] $value
* @return void
*/
function setHeader($name, $value) {
$this->headers[strtolower($name)] = [$name, (array)$value];
}
/**
* Sets a new set of HTTP headers.
*
* The headers array should contain headernames for keys, and their value
* should be specified as either a string or an array.
*
* Any header that already existed will be overwritten.
*
* @param array $headers
* @return void
*/
function setHeaders(array $headers) {
foreach ($headers as $name => $value) {
$this->setHeader($name, $value);
}
}
/**
* Adds a HTTP header.
*
* This method will not overwrite any existing HTTP header, but instead add
* another value. Individual values can be retrieved with
* getHeadersAsArray.
*
* @param string $name
* @param string $value
* @return void
*/
function addHeader($name, $value) {
$lName = strtolower($name);
if (isset($this->headers[$lName])) {
$this->headers[$lName][1] = array_merge(
$this->headers[$lName][1],
(array)$value
);
} else {
$this->headers[$lName] = [
$name,
(array)$value
];
}
}
/**
* Adds a new set of HTTP headers.
*
* Any existing headers will not be overwritten.
*
* @param array $headers
* @return void
*/
function addHeaders(array $headers) {
foreach ($headers as $name => $value) {
$this->addHeader($name, $value);
}
}
/**
* Removes a HTTP header.
*
* The specified header name must be treated as case-insenstive.
* This method should return true if the header was successfully deleted,
* and false if the header did not exist.
*
* @return bool
*/
function removeHeader($name) {
$name = strtolower($name);
if (!isset($this->headers[$name])) {
return false;
}
unset($this->headers[$name]);
return true;
}
/**
* Sets the HTTP version.
*
* Should be 1.0 or 1.1.
*
* @param string $version
* @return void
*/
function setHttpVersion($version) {
$this->httpVersion = $version;
}
/**
* Returns the HTTP version.
*
* @return string
*/
function getHttpVersion() {
return $this->httpVersion;
}
}
|