/
Request.php
160 lines (149 loc) · 3.76 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
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Network\Http;
use Cake\Error;
use Cake\Network\Http\Message;
/**
* Implements methods for HTTP requests.
*
* Used by Cake\Network\Http\Client to contain request information
* for making requests.
*/
class Request extends Message {
/**
* The HTTP method to use.
*
* @var string
*/
protected $_method = self::METHOD_GET;
/**
* Request body to send.
*
* @var mixed
*/
protected $_body;
/**
* The URL to request.
*
* @var string
*/
protected $_url;
/**
* Headers to be sent.
*
* @var array
*/
protected $_headers = [
'Connection' => 'close',
'User-Agent' => 'CakePHP'
];
/**
* Get/Set the HTTP method.
*
* @param string|null $method The method for the request.
* @return mixed Either this or the current method.
* @throws Cake\Error\Exception On invalid methods.
*/
public function method($method = null) {
if ($method === null) {
return $this->_method;
}
$name = get_called_class() . '::METHOD_' . strtoupper($method);
if (!defined($name)) {
throw new Error\Exception('Invalid method type');
}
$this->_method = $method;
return $this;
}
/**
* Get/Set the url for the request.
*
* @param string|null $url The url for the request. Leave null for get
* @return mixed Either $this or the url value.
*/
public function url($url = null) {
if ($url === null) {
return $this->_url;
}
$this->_url = $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']);`
*
* @param string|array $name The name to get, or array of multiple values to set.
* @param string $value The value to set for the header.
* @return mixed Either $this when setting or header value when getting.
*/
public function header($name = null, $value = null) {
if ($value === null && is_string($name)) {
$name = $this->_normalizeHeader($name);
return isset($this->_headers[$name]) ? $this->_headers[$name] : null;
}
if ($value !== null && !is_array($name)) {
$name = [$name => $value];
}
foreach ($name as $key => $val) {
$key = $this->_normalizeHeader($key);
$this->_headers[$key] = $val;
}
return $this;
}
/**
* 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.
*/
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;
}
}