/
Request.php
262 lines (241 loc) · 7.63 KB
/
Request.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
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Http\Client;
use Cake\Core\Exception\Exception;
use Psr\Http\Message\RequestInterface;
use Zend\Diactoros\MessageTrait;
use Zend\Diactoros\RequestTrait;
use Zend\Diactoros\Stream;
/**
* Implements methods for HTTP requests.
*
* Used by Cake\Http\Client to contain request information
* for making requests.
*/
class Request extends Message implements RequestInterface
{
use MessageTrait;
use RequestTrait;
/**
* Constructor
*
* Provides backwards compatible defaults for some properties.
*
* @param string $url The request URL
* @param string $method The HTTP method to use.
* @param array $headers The HTTP headers to set.
* @param array|string|null $data The request body to use.
*/
public function __construct($url = '', $method = self::METHOD_GET, array $headers = [], $data = null)
{
$this->validateMethod($method);
$this->method = $method;
$this->uri = $this->createUri($url);
$headers += [
'Connection' => 'close',
'User-Agent' => 'CakePHP'
];
$this->addHeaders($headers);
$this->body($data);
}
/**
* Get/Set the HTTP method.
*
* *Warning* This method mutates the request in-place for backwards
* compatibility reasons, and is not part of the PSR7 interface.
*
* @param string|null $method The method for the request.
* @return $this|string Either this or the current method.
* @throws \Cake\Core\Exception\Exception On invalid methods.
* @deprecated 3.3.0 Use getMethod() and withMethod() instead.
*/
public function method($method = null)
{
if ($method === null) {
return $this->method;
}
$name = get_called_class() . '::METHOD_' . strtoupper($method);
if (!defined($name)) {
throw new Exception('Invalid method type');
}
$this->method = $method;
return $this;
}
/**
* Get/Set the url for the request.
*
* *Warning* This method mutates the request in-place for backwards
* compatibility reasons, and is not part of the PSR7 interface.
*
* @param string|null $url The url for the request. Leave null for get
* @return $this|string Either $this or the url value.
* @deprecated 3.3.0 Use getUri() and withUri() instead.
*/
public function url($url = null)
{
if ($url === null) {
return '' . $this->getUri();
}
$this->uri = $this->createUri($url);
return $this;
}
/**
* Get/Set headers into the request.
*
* You can get the value of a header, or set one/many headers.
* Headers are set / fetched in a case insensitive way.
*
* ### Getting headers
*
* ```
* $request->header('Content-Type');
* ```
*
* ### Setting one header
*
* ```
* $request->header('Content-Type', 'application/json');
* ```
*
* ### Setting multiple headers
*
* ```
* $request->header(['Connection' => 'close', 'User-Agent' => 'CakePHP']);
* ```
*
* *Warning* This method mutates the request in-place for backwards
* compatibility reasons, and is not part of the PSR7 interface.
*
* @param string|array|null $name The name to get, or array of multiple values to set.
* @param string|null $value The value to set for the header.
* @return mixed Either $this when setting or header value when getting.
* @deprecated 3.3.0 Use withHeader() and getHeaderLine() instead.
*/
public function header($name = null, $value = null)
{
if ($value === null && is_string($name)) {
$val = $this->getHeaderLine($name);
if ($val === '') {
return null;
}
return $val;
}
if ($value !== null && !is_array($name)) {
$name = [$name => $value];
}
$this->addHeaders($name);
return $this;
}
/**
* Add an array of headers to the request.
*
* @param array $headers The headers to add.
* @return void
*/
protected function addHeaders($headers)
{
foreach ($headers as $key => $val) {
$normalized = strtolower($key);
$this->headers[$key] = (array)$val;
$this->headerNames[$normalized] = $key;
}
}
/**
* Get/Set cookie values.
*
* ### Getting a cookie
*
* ```
* $request->cookie('session');
* ```
*
* ### Setting one cookie
*
* ```
* $request->cookie('session', '123456');
* ```
*
* ### Setting multiple headers
*
* ```
* $request->cookie(['test' => 'value', 'split' => 'banana']);
* ```
*
* @param string $name The name of the cookie to get/set
* @param string|null $value Either the value or null when getting values.
* @return mixed Either $this or the cookie value.
* @deprected 3.5.0 No longer used. CookieCollections now add `Cookie` header to the request
* before sending. Use Cake\Http\Cookie\CookieCollection::addToRequest() to make adding cookies
* to a request easier.
*/
public function cookie($name, $value = null)
{
if ($value === null && is_string($name)) {
return isset($this->_cookies[$name]) ? $this->_cookies[$name] : null;
}
if (is_string($name) && is_string($value)) {
$name = [$name => $value];
}
foreach ($name as $key => $val) {
$this->_cookies[$key] = $val;
}
return $this;
}
/**
* Get/Set HTTP version.
*
* *Warning* This method mutates the request in-place for backwards
* compatibility reasons, and is not part of the PSR7 interface.
*
* @param string|null $version The HTTP version.
* @return $this|string Either $this or the HTTP version.
* @deprecated 3.3.0 Use getProtocolVersion() and withProtocolVersion() instead.
*/
public function version($version = null)
{
if ($version === null) {
return $this->protocol;
}
$this->protocol = $version;
return $this;
}
/**
* Get/set the body/payload for the message.
*
* Array data will be serialized with Cake\Http\FormData,
* and the content-type will be set.
*
* @param string|array|null $body The body for the request. Leave null for get
* @return mixed Either $this or the body value.
*/
public function body($body = null)
{
if ($body === null) {
$body = $this->getBody();
return $body ? $body->__toString() : '';
}
if (is_array($body)) {
$formData = new FormData();
$formData->addMany($body);
$this->header('Content-Type', $formData->contentType());
$body = (string)$formData;
}
$stream = new Stream('php://memory', 'rw');
$stream->write($body);
$this->stream = $stream;
return $this;
}
}
// @deprecated Add backwards compat alias.
class_alias('Cake\Http\Client\Request', 'Cake\Network\Http\Request');