@@ -17,300 +17,300 @@ |
||
17 | 17 | */ |
18 | 18 | class Request extends Message implements RequestInterface { |
19 | 19 | |
20 | - /** |
|
21 | - * HTTP Method |
|
22 | - * |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - protected $method; |
|
26 | - |
|
27 | - /** |
|
28 | - * Request Url |
|
29 | - * |
|
30 | - * @var string |
|
31 | - */ |
|
32 | - protected $url; |
|
33 | - |
|
34 | - /** |
|
35 | - * Creates the request object |
|
36 | - * |
|
37 | - * @param string $method |
|
38 | - * @param string $url |
|
39 | - * @param array $headers |
|
40 | - * @param resource $body |
|
41 | - */ |
|
42 | - public function __construct($method = null, $url = null, array $headers = null, $body = null) { |
|
43 | - |
|
44 | - if (is_array($method)) { |
|
45 | - throw new InvalidArgumentException('The first argument for this constructor should be a string or null, not an array. Did you upgrade from sabre/http 1.0 to 2.0?'); |
|
46 | - } |
|
47 | - if (!is_null($method)) $this->setMethod($method); |
|
48 | - if (!is_null($url)) $this->setUrl($url); |
|
49 | - if (!is_null($headers)) $this->setHeaders($headers); |
|
50 | - if (!is_null($body)) $this->setBody($body); |
|
51 | - |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * Returns the current HTTP method |
|
56 | - * |
|
57 | - * @return string |
|
58 | - */ |
|
59 | - public function getMethod() { |
|
60 | - |
|
61 | - return $this->method; |
|
62 | - |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * Sets the HTTP method |
|
67 | - * |
|
68 | - * @param string $method |
|
69 | - * @return void |
|
70 | - */ |
|
71 | - public function setMethod($method) { |
|
72 | - |
|
73 | - $this->method = $method; |
|
74 | - |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Returns the request url. |
|
79 | - * |
|
80 | - * @return string |
|
81 | - */ |
|
82 | - public function getUrl() { |
|
83 | - |
|
84 | - return $this->url; |
|
85 | - |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Sets the request url. |
|
90 | - * |
|
91 | - * @param string $url |
|
92 | - * @return void |
|
93 | - */ |
|
94 | - public function setUrl($url) { |
|
95 | - |
|
96 | - $this->url = $url; |
|
97 | - |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Returns the list of query parameters. |
|
102 | - * |
|
103 | - * This is equivalent to PHP's $_GET superglobal. |
|
104 | - * |
|
105 | - * @return array |
|
106 | - */ |
|
107 | - public function getQueryParameters() { |
|
108 | - |
|
109 | - $url = $this->getUrl(); |
|
110 | - if (($index = strpos($url, '?')) === false) { |
|
111 | - return []; |
|
112 | - } else { |
|
113 | - parse_str(substr($url, $index + 1), $queryParams); |
|
114 | - return $queryParams; |
|
115 | - } |
|
116 | - |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Sets the absolute url. |
|
121 | - * |
|
122 | - * @param string $url |
|
123 | - * @return void |
|
124 | - */ |
|
125 | - public function setAbsoluteUrl($url) { |
|
126 | - |
|
127 | - $this->absoluteUrl = $url; |
|
128 | - |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Returns the absolute url. |
|
133 | - * |
|
134 | - * @return string |
|
135 | - */ |
|
136 | - public function getAbsoluteUrl() { |
|
137 | - |
|
138 | - return $this->absoluteUrl; |
|
139 | - |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Base url |
|
144 | - * |
|
145 | - * @var string |
|
146 | - */ |
|
147 | - protected $baseUrl = '/'; |
|
148 | - |
|
149 | - /** |
|
150 | - * Sets a base url. |
|
151 | - * |
|
152 | - * This url is used for relative path calculations. |
|
153 | - * |
|
154 | - * @param string $url |
|
155 | - * @return void |
|
156 | - */ |
|
157 | - public function setBaseUrl($url) { |
|
158 | - |
|
159 | - $this->baseUrl = $url; |
|
160 | - |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Returns the current base url. |
|
165 | - * |
|
166 | - * @return string |
|
167 | - */ |
|
168 | - public function getBaseUrl() { |
|
169 | - |
|
170 | - return $this->baseUrl; |
|
171 | - |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Returns the relative path. |
|
176 | - * |
|
177 | - * This is being calculated using the base url. This path will not start |
|
178 | - * with a slash, so it will always return something like |
|
179 | - * 'example/path.html'. |
|
180 | - * |
|
181 | - * If the full path is equal to the base url, this method will return an |
|
182 | - * empty string. |
|
183 | - * |
|
184 | - * This method will also urldecode the path, and if the url was incoded as |
|
185 | - * ISO-8859-1, it will convert it to UTF-8. |
|
186 | - * |
|
187 | - * If the path is outside of the base url, a LogicException will be thrown. |
|
188 | - * |
|
189 | - * @return string |
|
190 | - */ |
|
191 | - public function getPath() { |
|
192 | - |
|
193 | - // Removing duplicated slashes. |
|
194 | - $uri = str_replace('//', '/', $this->getUrl()); |
|
195 | - |
|
196 | - $uri = Uri\normalize($uri); |
|
197 | - $baseUri = Uri\normalize($this->getBaseUrl()); |
|
198 | - |
|
199 | - if (strpos($uri, $baseUri) === 0) { |
|
200 | - |
|
201 | - // We're not interested in the query part (everything after the ?). |
|
202 | - list($uri) = explode('?', $uri); |
|
203 | - return trim(URLUtil::decodePath(substr($uri, strlen($baseUri))), '/'); |
|
204 | - |
|
205 | - } |
|
206 | - // A special case, if the baseUri was accessed without a trailing |
|
207 | - // slash, we'll accept it as well. |
|
208 | - elseif ($uri . '/' === $baseUri) { |
|
209 | - |
|
210 | - return ''; |
|
211 | - |
|
212 | - } |
|
213 | - |
|
214 | - throw new \LogicException('Requested uri (' . $this->getUrl() . ') is out of base uri (' . $this->getBaseUrl() . ')'); |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Equivalent of PHP's $_POST. |
|
219 | - * |
|
220 | - * @var array |
|
221 | - */ |
|
222 | - protected $postData = []; |
|
223 | - |
|
224 | - /** |
|
225 | - * Sets the post data. |
|
226 | - * |
|
227 | - * This is equivalent to PHP's $_POST superglobal. |
|
228 | - * |
|
229 | - * This would not have been needed, if POST data was accessible as |
|
230 | - * php://input, but unfortunately we need to special case it. |
|
231 | - * |
|
232 | - * @param array $postData |
|
233 | - * @return void |
|
234 | - */ |
|
235 | - public function setPostData(array $postData) { |
|
236 | - |
|
237 | - $this->postData = $postData; |
|
238 | - |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * Returns the POST data. |
|
243 | - * |
|
244 | - * This is equivalent to PHP's $_POST superglobal. |
|
245 | - * |
|
246 | - * @return array |
|
247 | - */ |
|
248 | - public function getPostData() { |
|
249 | - |
|
250 | - return $this->postData; |
|
251 | - |
|
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * An array containing the raw _SERVER array. |
|
256 | - * |
|
257 | - * @var array |
|
258 | - */ |
|
259 | - protected $rawServerData; |
|
260 | - |
|
261 | - /** |
|
262 | - * Returns an item from the _SERVER array. |
|
263 | - * |
|
264 | - * If the value does not exist in the array, null is returned. |
|
265 | - * |
|
266 | - * @param string $valueName |
|
267 | - * @return string|null |
|
268 | - */ |
|
269 | - public function getRawServerValue($valueName) { |
|
270 | - |
|
271 | - if (isset($this->rawServerData[$valueName])) { |
|
272 | - return $this->rawServerData[$valueName]; |
|
273 | - } |
|
274 | - |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * Sets the _SERVER array. |
|
279 | - * |
|
280 | - * @param array $data |
|
281 | - * @return void |
|
282 | - */ |
|
283 | - public function setRawServerData(array $data) { |
|
284 | - |
|
285 | - $this->rawServerData = $data; |
|
286 | - |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * Serializes the request object as a string. |
|
291 | - * |
|
292 | - * This is useful for debugging purposes. |
|
293 | - * |
|
294 | - * @return string |
|
295 | - */ |
|
296 | - public function __toString() { |
|
297 | - |
|
298 | - $out = $this->getMethod() . ' ' . $this->getUrl() . ' HTTP/' . $this->getHTTPVersion() . "\r\n"; |
|
299 | - |
|
300 | - foreach ($this->getHeaders() as $key => $value) { |
|
301 | - foreach ($value as $v) { |
|
302 | - if ($key === 'Authorization') { |
|
303 | - list($v) = explode(' ', $v, 2); |
|
304 | - $v .= ' REDACTED'; |
|
305 | - } |
|
306 | - $out .= $key . ": " . $v . "\r\n"; |
|
307 | - } |
|
308 | - } |
|
309 | - $out .= "\r\n"; |
|
310 | - $out .= $this->getBodyAsString(); |
|
311 | - |
|
312 | - return $out; |
|
313 | - |
|
314 | - } |
|
20 | + /** |
|
21 | + * HTTP Method |
|
22 | + * |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + protected $method; |
|
26 | + |
|
27 | + /** |
|
28 | + * Request Url |
|
29 | + * |
|
30 | + * @var string |
|
31 | + */ |
|
32 | + protected $url; |
|
33 | + |
|
34 | + /** |
|
35 | + * Creates the request object |
|
36 | + * |
|
37 | + * @param string $method |
|
38 | + * @param string $url |
|
39 | + * @param array $headers |
|
40 | + * @param resource $body |
|
41 | + */ |
|
42 | + public function __construct($method = null, $url = null, array $headers = null, $body = null) { |
|
43 | + |
|
44 | + if (is_array($method)) { |
|
45 | + throw new InvalidArgumentException('The first argument for this constructor should be a string or null, not an array. Did you upgrade from sabre/http 1.0 to 2.0?'); |
|
46 | + } |
|
47 | + if (!is_null($method)) $this->setMethod($method); |
|
48 | + if (!is_null($url)) $this->setUrl($url); |
|
49 | + if (!is_null($headers)) $this->setHeaders($headers); |
|
50 | + if (!is_null($body)) $this->setBody($body); |
|
51 | + |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * Returns the current HTTP method |
|
56 | + * |
|
57 | + * @return string |
|
58 | + */ |
|
59 | + public function getMethod() { |
|
60 | + |
|
61 | + return $this->method; |
|
62 | + |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * Sets the HTTP method |
|
67 | + * |
|
68 | + * @param string $method |
|
69 | + * @return void |
|
70 | + */ |
|
71 | + public function setMethod($method) { |
|
72 | + |
|
73 | + $this->method = $method; |
|
74 | + |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Returns the request url. |
|
79 | + * |
|
80 | + * @return string |
|
81 | + */ |
|
82 | + public function getUrl() { |
|
83 | + |
|
84 | + return $this->url; |
|
85 | + |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Sets the request url. |
|
90 | + * |
|
91 | + * @param string $url |
|
92 | + * @return void |
|
93 | + */ |
|
94 | + public function setUrl($url) { |
|
95 | + |
|
96 | + $this->url = $url; |
|
97 | + |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Returns the list of query parameters. |
|
102 | + * |
|
103 | + * This is equivalent to PHP's $_GET superglobal. |
|
104 | + * |
|
105 | + * @return array |
|
106 | + */ |
|
107 | + public function getQueryParameters() { |
|
108 | + |
|
109 | + $url = $this->getUrl(); |
|
110 | + if (($index = strpos($url, '?')) === false) { |
|
111 | + return []; |
|
112 | + } else { |
|
113 | + parse_str(substr($url, $index + 1), $queryParams); |
|
114 | + return $queryParams; |
|
115 | + } |
|
116 | + |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Sets the absolute url. |
|
121 | + * |
|
122 | + * @param string $url |
|
123 | + * @return void |
|
124 | + */ |
|
125 | + public function setAbsoluteUrl($url) { |
|
126 | + |
|
127 | + $this->absoluteUrl = $url; |
|
128 | + |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Returns the absolute url. |
|
133 | + * |
|
134 | + * @return string |
|
135 | + */ |
|
136 | + public function getAbsoluteUrl() { |
|
137 | + |
|
138 | + return $this->absoluteUrl; |
|
139 | + |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Base url |
|
144 | + * |
|
145 | + * @var string |
|
146 | + */ |
|
147 | + protected $baseUrl = '/'; |
|
148 | + |
|
149 | + /** |
|
150 | + * Sets a base url. |
|
151 | + * |
|
152 | + * This url is used for relative path calculations. |
|
153 | + * |
|
154 | + * @param string $url |
|
155 | + * @return void |
|
156 | + */ |
|
157 | + public function setBaseUrl($url) { |
|
158 | + |
|
159 | + $this->baseUrl = $url; |
|
160 | + |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Returns the current base url. |
|
165 | + * |
|
166 | + * @return string |
|
167 | + */ |
|
168 | + public function getBaseUrl() { |
|
169 | + |
|
170 | + return $this->baseUrl; |
|
171 | + |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Returns the relative path. |
|
176 | + * |
|
177 | + * This is being calculated using the base url. This path will not start |
|
178 | + * with a slash, so it will always return something like |
|
179 | + * 'example/path.html'. |
|
180 | + * |
|
181 | + * If the full path is equal to the base url, this method will return an |
|
182 | + * empty string. |
|
183 | + * |
|
184 | + * This method will also urldecode the path, and if the url was incoded as |
|
185 | + * ISO-8859-1, it will convert it to UTF-8. |
|
186 | + * |
|
187 | + * If the path is outside of the base url, a LogicException will be thrown. |
|
188 | + * |
|
189 | + * @return string |
|
190 | + */ |
|
191 | + public function getPath() { |
|
192 | + |
|
193 | + // Removing duplicated slashes. |
|
194 | + $uri = str_replace('//', '/', $this->getUrl()); |
|
195 | + |
|
196 | + $uri = Uri\normalize($uri); |
|
197 | + $baseUri = Uri\normalize($this->getBaseUrl()); |
|
198 | + |
|
199 | + if (strpos($uri, $baseUri) === 0) { |
|
200 | + |
|
201 | + // We're not interested in the query part (everything after the ?). |
|
202 | + list($uri) = explode('?', $uri); |
|
203 | + return trim(URLUtil::decodePath(substr($uri, strlen($baseUri))), '/'); |
|
204 | + |
|
205 | + } |
|
206 | + // A special case, if the baseUri was accessed without a trailing |
|
207 | + // slash, we'll accept it as well. |
|
208 | + elseif ($uri . '/' === $baseUri) { |
|
209 | + |
|
210 | + return ''; |
|
211 | + |
|
212 | + } |
|
213 | + |
|
214 | + throw new \LogicException('Requested uri (' . $this->getUrl() . ') is out of base uri (' . $this->getBaseUrl() . ')'); |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Equivalent of PHP's $_POST. |
|
219 | + * |
|
220 | + * @var array |
|
221 | + */ |
|
222 | + protected $postData = []; |
|
223 | + |
|
224 | + /** |
|
225 | + * Sets the post data. |
|
226 | + * |
|
227 | + * This is equivalent to PHP's $_POST superglobal. |
|
228 | + * |
|
229 | + * This would not have been needed, if POST data was accessible as |
|
230 | + * php://input, but unfortunately we need to special case it. |
|
231 | + * |
|
232 | + * @param array $postData |
|
233 | + * @return void |
|
234 | + */ |
|
235 | + public function setPostData(array $postData) { |
|
236 | + |
|
237 | + $this->postData = $postData; |
|
238 | + |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * Returns the POST data. |
|
243 | + * |
|
244 | + * This is equivalent to PHP's $_POST superglobal. |
|
245 | + * |
|
246 | + * @return array |
|
247 | + */ |
|
248 | + public function getPostData() { |
|
249 | + |
|
250 | + return $this->postData; |
|
251 | + |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * An array containing the raw _SERVER array. |
|
256 | + * |
|
257 | + * @var array |
|
258 | + */ |
|
259 | + protected $rawServerData; |
|
260 | + |
|
261 | + /** |
|
262 | + * Returns an item from the _SERVER array. |
|
263 | + * |
|
264 | + * If the value does not exist in the array, null is returned. |
|
265 | + * |
|
266 | + * @param string $valueName |
|
267 | + * @return string|null |
|
268 | + */ |
|
269 | + public function getRawServerValue($valueName) { |
|
270 | + |
|
271 | + if (isset($this->rawServerData[$valueName])) { |
|
272 | + return $this->rawServerData[$valueName]; |
|
273 | + } |
|
274 | + |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * Sets the _SERVER array. |
|
279 | + * |
|
280 | + * @param array $data |
|
281 | + * @return void |
|
282 | + */ |
|
283 | + public function setRawServerData(array $data) { |
|
284 | + |
|
285 | + $this->rawServerData = $data; |
|
286 | + |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * Serializes the request object as a string. |
|
291 | + * |
|
292 | + * This is useful for debugging purposes. |
|
293 | + * |
|
294 | + * @return string |
|
295 | + */ |
|
296 | + public function __toString() { |
|
297 | + |
|
298 | + $out = $this->getMethod() . ' ' . $this->getUrl() . ' HTTP/' . $this->getHTTPVersion() . "\r\n"; |
|
299 | + |
|
300 | + foreach ($this->getHeaders() as $key => $value) { |
|
301 | + foreach ($value as $v) { |
|
302 | + if ($key === 'Authorization') { |
|
303 | + list($v) = explode(' ', $v, 2); |
|
304 | + $v .= ' REDACTED'; |
|
305 | + } |
|
306 | + $out .= $key . ": " . $v . "\r\n"; |
|
307 | + } |
|
308 | + } |
|
309 | + $out .= "\r\n"; |
|
310 | + $out .= $this->getBodyAsString(); |
|
311 | + |
|
312 | + return $out; |
|
313 | + |
|
314 | + } |
|
315 | 315 | |
316 | 316 | } |
@@ -11,35 +11,35 @@ |
||
11 | 11 | */ |
12 | 12 | interface ResponseInterface extends MessageInterface { |
13 | 13 | |
14 | - /** |
|
15 | - * Returns the current HTTP status code. |
|
16 | - * |
|
17 | - * @return int |
|
18 | - */ |
|
19 | - public function getStatus(); |
|
14 | + /** |
|
15 | + * Returns the current HTTP status code. |
|
16 | + * |
|
17 | + * @return int |
|
18 | + */ |
|
19 | + public function getStatus(); |
|
20 | 20 | |
21 | - /** |
|
22 | - * Returns the human-readable status string. |
|
23 | - * |
|
24 | - * In the case of a 200, this may for example be 'OK'. |
|
25 | - * |
|
26 | - * @return string |
|
27 | - */ |
|
28 | - public function getStatusText(); |
|
21 | + /** |
|
22 | + * Returns the human-readable status string. |
|
23 | + * |
|
24 | + * In the case of a 200, this may for example be 'OK'. |
|
25 | + * |
|
26 | + * @return string |
|
27 | + */ |
|
28 | + public function getStatusText(); |
|
29 | 29 | |
30 | - /** |
|
31 | - * Sets the HTTP status code. |
|
32 | - * |
|
33 | - * This can be either the full HTTP status code with human readable string, |
|
34 | - * for example: "403 I can't let you do that, Dave". |
|
35 | - * |
|
36 | - * Or just the code, in which case the appropriate default message will be |
|
37 | - * added. |
|
38 | - * |
|
39 | - * @param string|int $status |
|
40 | - * @throws \InvalidArgumentExeption |
|
41 | - * @return void |
|
42 | - */ |
|
43 | - public function setStatus($status); |
|
30 | + /** |
|
31 | + * Sets the HTTP status code. |
|
32 | + * |
|
33 | + * This can be either the full HTTP status code with human readable string, |
|
34 | + * for example: "403 I can't let you do that, Dave". |
|
35 | + * |
|
36 | + * Or just the code, in which case the appropriate default message will be |
|
37 | + * added. |
|
38 | + * |
|
39 | + * @param string|int $status |
|
40 | + * @throws \InvalidArgumentExeption |
|
41 | + * @return void |
|
42 | + */ |
|
43 | + public function setStatus($status); |
|
44 | 44 | |
45 | 45 | } |
@@ -11,137 +11,137 @@ |
||
11 | 11 | */ |
12 | 12 | interface RequestInterface extends MessageInterface { |
13 | 13 | |
14 | - /** |
|
15 | - * Returns the current HTTP method |
|
16 | - * |
|
17 | - * @return string |
|
18 | - */ |
|
19 | - public function getMethod(); |
|
20 | - |
|
21 | - /** |
|
22 | - * Sets the HTTP method |
|
23 | - * |
|
24 | - * @param string $method |
|
25 | - * @return void |
|
26 | - */ |
|
27 | - public function setMethod($method); |
|
28 | - |
|
29 | - /** |
|
30 | - * Returns the request url. |
|
31 | - * |
|
32 | - * @return string |
|
33 | - */ |
|
34 | - public function getUrl(); |
|
35 | - |
|
36 | - /** |
|
37 | - * Sets the request url. |
|
38 | - * |
|
39 | - * @param string $url |
|
40 | - * @return void |
|
41 | - */ |
|
42 | - public function setUrl($url); |
|
43 | - |
|
44 | - /** |
|
45 | - * Returns the absolute url. |
|
46 | - * |
|
47 | - * @return string |
|
48 | - */ |
|
49 | - public function getAbsoluteUrl(); |
|
50 | - |
|
51 | - /** |
|
52 | - * Sets the absolute url. |
|
53 | - * |
|
54 | - * @param string $url |
|
55 | - * @return void |
|
56 | - */ |
|
57 | - public function setAbsoluteUrl($url); |
|
58 | - |
|
59 | - /** |
|
60 | - * Returns the current base url. |
|
61 | - * |
|
62 | - * @return string |
|
63 | - */ |
|
64 | - public function getBaseUrl(); |
|
65 | - |
|
66 | - /** |
|
67 | - * Sets a base url. |
|
68 | - * |
|
69 | - * This url is used for relative path calculations. |
|
70 | - * |
|
71 | - * The base url should default to / |
|
72 | - * |
|
73 | - * @param string $url |
|
74 | - * @return void |
|
75 | - */ |
|
76 | - public function setBaseUrl($url); |
|
77 | - |
|
78 | - /** |
|
79 | - * Returns the relative path. |
|
80 | - * |
|
81 | - * This is being calculated using the base url. This path will not start |
|
82 | - * with a slash, so it will always return something like |
|
83 | - * 'example/path.html'. |
|
84 | - * |
|
85 | - * If the full path is equal to the base url, this method will return an |
|
86 | - * empty string. |
|
87 | - * |
|
88 | - * This method will also urldecode the path, and if the url was incoded as |
|
89 | - * ISO-8859-1, it will convert it to UTF-8. |
|
90 | - * |
|
91 | - * If the path is outside of the base url, a LogicException will be thrown. |
|
92 | - * |
|
93 | - * @return string |
|
94 | - */ |
|
95 | - public function getPath(); |
|
96 | - |
|
97 | - /** |
|
98 | - * Returns the list of query parameters. |
|
99 | - * |
|
100 | - * This is equivalent to PHP's $_GET superglobal. |
|
101 | - * |
|
102 | - * @return array |
|
103 | - */ |
|
104 | - public function getQueryParameters(); |
|
105 | - |
|
106 | - /** |
|
107 | - * Returns the POST data. |
|
108 | - * |
|
109 | - * This is equivalent to PHP's $_POST superglobal. |
|
110 | - * |
|
111 | - * @return array |
|
112 | - */ |
|
113 | - public function getPostData(); |
|
114 | - |
|
115 | - /** |
|
116 | - * Sets the post data. |
|
117 | - * |
|
118 | - * This is equivalent to PHP's $_POST superglobal. |
|
119 | - * |
|
120 | - * This would not have been needed, if POST data was accessible as |
|
121 | - * php://input, but unfortunately we need to special case it. |
|
122 | - * |
|
123 | - * @param array $postData |
|
124 | - * @return void |
|
125 | - */ |
|
126 | - public function setPostData(array $postData); |
|
127 | - |
|
128 | - /** |
|
129 | - * Returns an item from the _SERVER array. |
|
130 | - * |
|
131 | - * If the value does not exist in the array, null is returned. |
|
132 | - * |
|
133 | - * @param string $valueName |
|
134 | - * @return string|null |
|
135 | - */ |
|
136 | - public function getRawServerValue($valueName); |
|
137 | - |
|
138 | - /** |
|
139 | - * Sets the _SERVER array. |
|
140 | - * |
|
141 | - * @param array $data |
|
142 | - * @return void |
|
143 | - */ |
|
144 | - public function setRawServerData(array $data); |
|
14 | + /** |
|
15 | + * Returns the current HTTP method |
|
16 | + * |
|
17 | + * @return string |
|
18 | + */ |
|
19 | + public function getMethod(); |
|
20 | + |
|
21 | + /** |
|
22 | + * Sets the HTTP method |
|
23 | + * |
|
24 | + * @param string $method |
|
25 | + * @return void |
|
26 | + */ |
|
27 | + public function setMethod($method); |
|
28 | + |
|
29 | + /** |
|
30 | + * Returns the request url. |
|
31 | + * |
|
32 | + * @return string |
|
33 | + */ |
|
34 | + public function getUrl(); |
|
35 | + |
|
36 | + /** |
|
37 | + * Sets the request url. |
|
38 | + * |
|
39 | + * @param string $url |
|
40 | + * @return void |
|
41 | + */ |
|
42 | + public function setUrl($url); |
|
43 | + |
|
44 | + /** |
|
45 | + * Returns the absolute url. |
|
46 | + * |
|
47 | + * @return string |
|
48 | + */ |
|
49 | + public function getAbsoluteUrl(); |
|
50 | + |
|
51 | + /** |
|
52 | + * Sets the absolute url. |
|
53 | + * |
|
54 | + * @param string $url |
|
55 | + * @return void |
|
56 | + */ |
|
57 | + public function setAbsoluteUrl($url); |
|
58 | + |
|
59 | + /** |
|
60 | + * Returns the current base url. |
|
61 | + * |
|
62 | + * @return string |
|
63 | + */ |
|
64 | + public function getBaseUrl(); |
|
65 | + |
|
66 | + /** |
|
67 | + * Sets a base url. |
|
68 | + * |
|
69 | + * This url is used for relative path calculations. |
|
70 | + * |
|
71 | + * The base url should default to / |
|
72 | + * |
|
73 | + * @param string $url |
|
74 | + * @return void |
|
75 | + */ |
|
76 | + public function setBaseUrl($url); |
|
77 | + |
|
78 | + /** |
|
79 | + * Returns the relative path. |
|
80 | + * |
|
81 | + * This is being calculated using the base url. This path will not start |
|
82 | + * with a slash, so it will always return something like |
|
83 | + * 'example/path.html'. |
|
84 | + * |
|
85 | + * If the full path is equal to the base url, this method will return an |
|
86 | + * empty string. |
|
87 | + * |
|
88 | + * This method will also urldecode the path, and if the url was incoded as |
|
89 | + * ISO-8859-1, it will convert it to UTF-8. |
|
90 | + * |
|
91 | + * If the path is outside of the base url, a LogicException will be thrown. |
|
92 | + * |
|
93 | + * @return string |
|
94 | + */ |
|
95 | + public function getPath(); |
|
96 | + |
|
97 | + /** |
|
98 | + * Returns the list of query parameters. |
|
99 | + * |
|
100 | + * This is equivalent to PHP's $_GET superglobal. |
|
101 | + * |
|
102 | + * @return array |
|
103 | + */ |
|
104 | + public function getQueryParameters(); |
|
105 | + |
|
106 | + /** |
|
107 | + * Returns the POST data. |
|
108 | + * |
|
109 | + * This is equivalent to PHP's $_POST superglobal. |
|
110 | + * |
|
111 | + * @return array |
|
112 | + */ |
|
113 | + public function getPostData(); |
|
114 | + |
|
115 | + /** |
|
116 | + * Sets the post data. |
|
117 | + * |
|
118 | + * This is equivalent to PHP's $_POST superglobal. |
|
119 | + * |
|
120 | + * This would not have been needed, if POST data was accessible as |
|
121 | + * php://input, but unfortunately we need to special case it. |
|
122 | + * |
|
123 | + * @param array $postData |
|
124 | + * @return void |
|
125 | + */ |
|
126 | + public function setPostData(array $postData); |
|
127 | + |
|
128 | + /** |
|
129 | + * Returns an item from the _SERVER array. |
|
130 | + * |
|
131 | + * If the value does not exist in the array, null is returned. |
|
132 | + * |
|
133 | + * @param string $valueName |
|
134 | + * @return string|null |
|
135 | + */ |
|
136 | + public function getRawServerValue($valueName); |
|
137 | + |
|
138 | + /** |
|
139 | + * Sets the _SERVER array. |
|
140 | + * |
|
141 | + * @param array $data |
|
142 | + * @return void |
|
143 | + */ |
|
144 | + public function setRawServerData(array $data); |
|
145 | 145 | |
146 | 146 | |
147 | 147 | } |
@@ -12,166 +12,166 @@ |
||
12 | 12 | */ |
13 | 13 | interface MessageInterface { |
14 | 14 | |
15 | - /** |
|
16 | - * Returns the body as a readable stream resource. |
|
17 | - * |
|
18 | - * Note that the stream may not be rewindable, and therefore may only be |
|
19 | - * read once. |
|
20 | - * |
|
21 | - * @return resource |
|
22 | - */ |
|
23 | - public function getBodyAsStream(); |
|
24 | - |
|
25 | - /** |
|
26 | - * Returns the body as a string. |
|
27 | - * |
|
28 | - * Note that because the underlying data may be based on a stream, this |
|
29 | - * method could only work correctly the first time. |
|
30 | - * |
|
31 | - * @return string |
|
32 | - */ |
|
33 | - public function getBodyAsString(); |
|
34 | - |
|
35 | - /** |
|
36 | - * Returns the message body, as it's internal representation. |
|
37 | - * |
|
38 | - * This could be either a string or a stream. |
|
39 | - * |
|
40 | - * @return resource|string |
|
41 | - */ |
|
42 | - public function getBody(); |
|
43 | - |
|
44 | - /** |
|
45 | - * Updates the body resource with a new stream. |
|
46 | - * |
|
47 | - * @param resource $body |
|
48 | - * @return void |
|
49 | - */ |
|
50 | - public function setBody($body); |
|
51 | - |
|
52 | - /** |
|
53 | - * Returns all the HTTP headers as an array. |
|
54 | - * |
|
55 | - * Every header is returned as an array, with one or more values. |
|
56 | - * |
|
57 | - * @return array |
|
58 | - */ |
|
59 | - public function getHeaders(); |
|
60 | - |
|
61 | - /** |
|
62 | - * Will return true or false, depending on if a HTTP header exists. |
|
63 | - * |
|
64 | - * @param string $name |
|
65 | - * @return bool |
|
66 | - */ |
|
67 | - public function hasHeader($name); |
|
68 | - |
|
69 | - /** |
|
70 | - * Returns a specific HTTP header, based on it's name. |
|
71 | - * |
|
72 | - * The name must be treated as case-insensitive. |
|
73 | - * If the header does not exist, this method must return null. |
|
74 | - * |
|
75 | - * If a header appeared more than once in a HTTP request, this method will |
|
76 | - * concatenate all the values with a comma. |
|
77 | - * |
|
78 | - * Note that this not make sense for all headers. Some, such as |
|
79 | - * `Set-Cookie` cannot be logically combined with a comma. In those cases |
|
80 | - * you *should* use getHeaderAsArray(). |
|
81 | - * |
|
82 | - * @param string $name |
|
83 | - * @return string|null |
|
84 | - */ |
|
85 | - public function getHeader($name); |
|
86 | - |
|
87 | - /** |
|
88 | - * Returns a HTTP header as an array. |
|
89 | - * |
|
90 | - * For every time the HTTP header appeared in the request or response, an |
|
91 | - * item will appear in the array. |
|
92 | - * |
|
93 | - * If the header did not exists, this method will return an empty array. |
|
94 | - * |
|
95 | - * @param string $name |
|
96 | - * @return string[] |
|
97 | - */ |
|
98 | - public function getHeaderAsArray($name); |
|
99 | - |
|
100 | - /** |
|
101 | - * Updates a HTTP header. |
|
102 | - * |
|
103 | - * The case-sensitity of the name value must be retained as-is. |
|
104 | - * |
|
105 | - * If the header already existed, it will be overwritten. |
|
106 | - * |
|
107 | - * @param string $name |
|
108 | - * @param string|string[] $value |
|
109 | - * @return void |
|
110 | - */ |
|
111 | - public function setHeader($name, $value); |
|
112 | - |
|
113 | - /** |
|
114 | - * Sets a new set of HTTP headers. |
|
115 | - * |
|
116 | - * The headers array should contain headernames for keys, and their value |
|
117 | - * should be specified as either a string or an array. |
|
118 | - * |
|
119 | - * Any header that already existed will be overwritten. |
|
120 | - * |
|
121 | - * @param array $headers |
|
122 | - * @return void |
|
123 | - */ |
|
124 | - public function setHeaders(array $headers); |
|
125 | - |
|
126 | - /** |
|
127 | - * Adds a HTTP header. |
|
128 | - * |
|
129 | - * This method will not overwrite any existing HTTP header, but instead add |
|
130 | - * another value. Individual values can be retrieved with |
|
131 | - * getHeadersAsArray. |
|
132 | - * |
|
133 | - * @param string $name |
|
134 | - * @param string $value |
|
135 | - * @return void |
|
136 | - */ |
|
137 | - public function addHeader($name, $value); |
|
138 | - |
|
139 | - /** |
|
140 | - * Adds a new set of HTTP headers. |
|
141 | - * |
|
142 | - * Any existing headers will not be overwritten. |
|
143 | - * |
|
144 | - * @param array $headers |
|
145 | - * @return void |
|
146 | - */ |
|
147 | - public function addHeaders(array $headers); |
|
148 | - |
|
149 | - /** |
|
150 | - * Removes a HTTP header. |
|
151 | - * |
|
152 | - * The specified header name must be treated as case-insenstive. |
|
153 | - * This method should return true if the header was successfully deleted, |
|
154 | - * and false if the header did not exist. |
|
155 | - * |
|
156 | - * @return bool |
|
157 | - */ |
|
158 | - public function removeHeader($name); |
|
159 | - |
|
160 | - /** |
|
161 | - * Sets the HTTP version. |
|
162 | - * |
|
163 | - * Should be 1.0 or 1.1. |
|
164 | - * |
|
165 | - * @param string $version |
|
166 | - * @return void |
|
167 | - */ |
|
168 | - public function setHttpVersion($version); |
|
169 | - |
|
170 | - /** |
|
171 | - * Returns the HTTP version. |
|
172 | - * |
|
173 | - * @return string |
|
174 | - */ |
|
175 | - public function getHttpVersion(); |
|
15 | + /** |
|
16 | + * Returns the body as a readable stream resource. |
|
17 | + * |
|
18 | + * Note that the stream may not be rewindable, and therefore may only be |
|
19 | + * read once. |
|
20 | + * |
|
21 | + * @return resource |
|
22 | + */ |
|
23 | + public function getBodyAsStream(); |
|
24 | + |
|
25 | + /** |
|
26 | + * Returns the body as a string. |
|
27 | + * |
|
28 | + * Note that because the underlying data may be based on a stream, this |
|
29 | + * method could only work correctly the first time. |
|
30 | + * |
|
31 | + * @return string |
|
32 | + */ |
|
33 | + public function getBodyAsString(); |
|
34 | + |
|
35 | + /** |
|
36 | + * Returns the message body, as it's internal representation. |
|
37 | + * |
|
38 | + * This could be either a string or a stream. |
|
39 | + * |
|
40 | + * @return resource|string |
|
41 | + */ |
|
42 | + public function getBody(); |
|
43 | + |
|
44 | + /** |
|
45 | + * Updates the body resource with a new stream. |
|
46 | + * |
|
47 | + * @param resource $body |
|
48 | + * @return void |
|
49 | + */ |
|
50 | + public function setBody($body); |
|
51 | + |
|
52 | + /** |
|
53 | + * Returns all the HTTP headers as an array. |
|
54 | + * |
|
55 | + * Every header is returned as an array, with one or more values. |
|
56 | + * |
|
57 | + * @return array |
|
58 | + */ |
|
59 | + public function getHeaders(); |
|
60 | + |
|
61 | + /** |
|
62 | + * Will return true or false, depending on if a HTTP header exists. |
|
63 | + * |
|
64 | + * @param string $name |
|
65 | + * @return bool |
|
66 | + */ |
|
67 | + public function hasHeader($name); |
|
68 | + |
|
69 | + /** |
|
70 | + * Returns a specific HTTP header, based on it's name. |
|
71 | + * |
|
72 | + * The name must be treated as case-insensitive. |
|
73 | + * If the header does not exist, this method must return null. |
|
74 | + * |
|
75 | + * If a header appeared more than once in a HTTP request, this method will |
|
76 | + * concatenate all the values with a comma. |
|
77 | + * |
|
78 | + * Note that this not make sense for all headers. Some, such as |
|
79 | + * `Set-Cookie` cannot be logically combined with a comma. In those cases |
|
80 | + * you *should* use getHeaderAsArray(). |
|
81 | + * |
|
82 | + * @param string $name |
|
83 | + * @return string|null |
|
84 | + */ |
|
85 | + public function getHeader($name); |
|
86 | + |
|
87 | + /** |
|
88 | + * Returns a HTTP header as an array. |
|
89 | + * |
|
90 | + * For every time the HTTP header appeared in the request or response, an |
|
91 | + * item will appear in the array. |
|
92 | + * |
|
93 | + * If the header did not exists, this method will return an empty array. |
|
94 | + * |
|
95 | + * @param string $name |
|
96 | + * @return string[] |
|
97 | + */ |
|
98 | + public function getHeaderAsArray($name); |
|
99 | + |
|
100 | + /** |
|
101 | + * Updates a HTTP header. |
|
102 | + * |
|
103 | + * The case-sensitity of the name value must be retained as-is. |
|
104 | + * |
|
105 | + * If the header already existed, it will be overwritten. |
|
106 | + * |
|
107 | + * @param string $name |
|
108 | + * @param string|string[] $value |
|
109 | + * @return void |
|
110 | + */ |
|
111 | + public function setHeader($name, $value); |
|
112 | + |
|
113 | + /** |
|
114 | + * Sets a new set of HTTP headers. |
|
115 | + * |
|
116 | + * The headers array should contain headernames for keys, and their value |
|
117 | + * should be specified as either a string or an array. |
|
118 | + * |
|
119 | + * Any header that already existed will be overwritten. |
|
120 | + * |
|
121 | + * @param array $headers |
|
122 | + * @return void |
|
123 | + */ |
|
124 | + public function setHeaders(array $headers); |
|
125 | + |
|
126 | + /** |
|
127 | + * Adds a HTTP header. |
|
128 | + * |
|
129 | + * This method will not overwrite any existing HTTP header, but instead add |
|
130 | + * another value. Individual values can be retrieved with |
|
131 | + * getHeadersAsArray. |
|
132 | + * |
|
133 | + * @param string $name |
|
134 | + * @param string $value |
|
135 | + * @return void |
|
136 | + */ |
|
137 | + public function addHeader($name, $value); |
|
138 | + |
|
139 | + /** |
|
140 | + * Adds a new set of HTTP headers. |
|
141 | + * |
|
142 | + * Any existing headers will not be overwritten. |
|
143 | + * |
|
144 | + * @param array $headers |
|
145 | + * @return void |
|
146 | + */ |
|
147 | + public function addHeaders(array $headers); |
|
148 | + |
|
149 | + /** |
|
150 | + * Removes a HTTP header. |
|
151 | + * |
|
152 | + * The specified header name must be treated as case-insenstive. |
|
153 | + * This method should return true if the header was successfully deleted, |
|
154 | + * and false if the header did not exist. |
|
155 | + * |
|
156 | + * @return bool |
|
157 | + */ |
|
158 | + public function removeHeader($name); |
|
159 | + |
|
160 | + /** |
|
161 | + * Sets the HTTP version. |
|
162 | + * |
|
163 | + * Should be 1.0 or 1.1. |
|
164 | + * |
|
165 | + * @param string $version |
|
166 | + * @return void |
|
167 | + */ |
|
168 | + public function setHttpVersion($version); |
|
169 | + |
|
170 | + /** |
|
171 | + * Returns the HTTP version. |
|
172 | + * |
|
173 | + * @return string |
|
174 | + */ |
|
175 | + public function getHttpVersion(); |
|
176 | 176 | |
177 | 177 | } |
@@ -13,302 +13,302 @@ |
||
13 | 13 | */ |
14 | 14 | abstract class Message implements MessageInterface { |
15 | 15 | |
16 | - /** |
|
17 | - * Request body |
|
18 | - * |
|
19 | - * This should be a stream resource |
|
20 | - * |
|
21 | - * @var resource |
|
22 | - */ |
|
23 | - protected $body; |
|
24 | - |
|
25 | - /** |
|
26 | - * Contains the list of HTTP headers |
|
27 | - * |
|
28 | - * @var array |
|
29 | - */ |
|
30 | - protected $headers = []; |
|
31 | - |
|
32 | - /** |
|
33 | - * HTTP message version (1.0 or 1.1) |
|
34 | - * |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - protected $httpVersion = '1.1'; |
|
38 | - |
|
39 | - /** |
|
40 | - * Returns the body as a readable stream resource. |
|
41 | - * |
|
42 | - * Note that the stream may not be rewindable, and therefore may only be |
|
43 | - * read once. |
|
44 | - * |
|
45 | - * @return resource |
|
46 | - */ |
|
47 | - public function getBodyAsStream() { |
|
48 | - |
|
49 | - $body = $this->getBody(); |
|
50 | - if (is_string($body) || is_null($body)) { |
|
51 | - $stream = fopen('php://temp', 'r+'); |
|
52 | - fwrite($stream, $body); |
|
53 | - rewind($stream); |
|
54 | - return $stream; |
|
55 | - } |
|
56 | - return $body; |
|
57 | - |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Returns the body as a string. |
|
62 | - * |
|
63 | - * Note that because the underlying data may be based on a stream, this |
|
64 | - * method could only work correctly the first time. |
|
65 | - * |
|
66 | - * @return string |
|
67 | - */ |
|
68 | - public function getBodyAsString() { |
|
69 | - |
|
70 | - $body = $this->getBody(); |
|
71 | - if (is_string($body)) { |
|
72 | - return $body; |
|
73 | - } |
|
74 | - if (is_null($body)) { |
|
75 | - return ''; |
|
76 | - } |
|
77 | - $contentLength = $this->getHeader('Content-Length'); |
|
78 | - if (null === $contentLength) { |
|
79 | - return stream_get_contents($body); |
|
80 | - } else { |
|
81 | - return stream_get_contents($body, $contentLength); |
|
82 | - } |
|
83 | - |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Returns the message body, as it's internal representation. |
|
88 | - * |
|
89 | - * This could be either a string or a stream. |
|
90 | - * |
|
91 | - * @return resource|string |
|
92 | - */ |
|
93 | - public function getBody() { |
|
94 | - |
|
95 | - return $this->body; |
|
96 | - |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Replaces the body resource with a new stream or string. |
|
101 | - * |
|
102 | - * @param resource|string $body |
|
103 | - */ |
|
104 | - public function setBody($body) { |
|
105 | - |
|
106 | - $this->body = $body; |
|
107 | - |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Returns all the HTTP headers as an array. |
|
112 | - * |
|
113 | - * Every header is returned as an array, with one or more values. |
|
114 | - * |
|
115 | - * @return array |
|
116 | - */ |
|
117 | - public function getHeaders() { |
|
118 | - |
|
119 | - $result = []; |
|
120 | - foreach ($this->headers as $headerInfo) { |
|
121 | - $result[$headerInfo[0]] = $headerInfo[1]; |
|
122 | - } |
|
123 | - return $result; |
|
124 | - |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Will return true or false, depending on if a HTTP header exists. |
|
129 | - * |
|
130 | - * @param string $name |
|
131 | - * @return bool |
|
132 | - */ |
|
133 | - public function hasHeader($name) { |
|
134 | - |
|
135 | - return isset($this->headers[strtolower($name)]); |
|
136 | - |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Returns a specific HTTP header, based on it's name. |
|
141 | - * |
|
142 | - * The name must be treated as case-insensitive. |
|
143 | - * If the header does not exist, this method must return null. |
|
144 | - * |
|
145 | - * If a header appeared more than once in a HTTP request, this method will |
|
146 | - * concatenate all the values with a comma. |
|
147 | - * |
|
148 | - * Note that this not make sense for all headers. Some, such as |
|
149 | - * `Set-Cookie` cannot be logically combined with a comma. In those cases |
|
150 | - * you *should* use getHeaderAsArray(). |
|
151 | - * |
|
152 | - * @param string $name |
|
153 | - * @return string|null |
|
154 | - */ |
|
155 | - public function getHeader($name) { |
|
156 | - |
|
157 | - $name = strtolower($name); |
|
158 | - |
|
159 | - if (isset($this->headers[$name])) { |
|
160 | - return implode(',', $this->headers[$name][1]); |
|
161 | - } |
|
162 | - return null; |
|
163 | - |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Returns a HTTP header as an array. |
|
168 | - * |
|
169 | - * For every time the HTTP header appeared in the request or response, an |
|
170 | - * item will appear in the array. |
|
171 | - * |
|
172 | - * If the header did not exists, this method will return an empty array. |
|
173 | - * |
|
174 | - * @param string $name |
|
175 | - * @return string[] |
|
176 | - */ |
|
177 | - public function getHeaderAsArray($name) { |
|
178 | - |
|
179 | - $name = strtolower($name); |
|
180 | - |
|
181 | - if (isset($this->headers[$name])) { |
|
182 | - return $this->headers[$name][1]; |
|
183 | - } |
|
184 | - |
|
185 | - return []; |
|
186 | - |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Updates a HTTP header. |
|
191 | - * |
|
192 | - * The case-sensitity of the name value must be retained as-is. |
|
193 | - * |
|
194 | - * If the header already existed, it will be overwritten. |
|
195 | - * |
|
196 | - * @param string $name |
|
197 | - * @param string|string[] $value |
|
198 | - * @return void |
|
199 | - */ |
|
200 | - public function setHeader($name, $value) { |
|
201 | - |
|
202 | - $this->headers[strtolower($name)] = [$name, (array)$value]; |
|
203 | - |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * Sets a new set of HTTP headers. |
|
208 | - * |
|
209 | - * The headers array should contain headernames for keys, and their value |
|
210 | - * should be specified as either a string or an array. |
|
211 | - * |
|
212 | - * Any header that already existed will be overwritten. |
|
213 | - * |
|
214 | - * @param array $headers |
|
215 | - * @return void |
|
216 | - */ |
|
217 | - public function setHeaders(array $headers) { |
|
218 | - |
|
219 | - foreach ($headers as $name => $value) { |
|
220 | - $this->setHeader($name, $value); |
|
221 | - } |
|
222 | - |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * Adds a HTTP header. |
|
227 | - * |
|
228 | - * This method will not overwrite any existing HTTP header, but instead add |
|
229 | - * another value. Individual values can be retrieved with |
|
230 | - * getHeadersAsArray. |
|
231 | - * |
|
232 | - * @param string $name |
|
233 | - * @param string $value |
|
234 | - * @return void |
|
235 | - */ |
|
236 | - public function addHeader($name, $value) { |
|
237 | - |
|
238 | - $lName = strtolower($name); |
|
239 | - if (isset($this->headers[$lName])) { |
|
240 | - $this->headers[$lName][1] = array_merge( |
|
241 | - $this->headers[$lName][1], |
|
242 | - (array)$value |
|
243 | - ); |
|
244 | - } else { |
|
245 | - $this->headers[$lName] = [ |
|
246 | - $name, |
|
247 | - (array)$value |
|
248 | - ]; |
|
249 | - } |
|
250 | - |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * Adds a new set of HTTP headers. |
|
255 | - * |
|
256 | - * Any existing headers will not be overwritten. |
|
257 | - * |
|
258 | - * @param array $headers |
|
259 | - * @return void |
|
260 | - */ |
|
261 | - public function addHeaders(array $headers) { |
|
262 | - |
|
263 | - foreach ($headers as $name => $value) { |
|
264 | - $this->addHeader($name, $value); |
|
265 | - } |
|
266 | - |
|
267 | - } |
|
268 | - |
|
269 | - |
|
270 | - /** |
|
271 | - * Removes a HTTP header. |
|
272 | - * |
|
273 | - * The specified header name must be treated as case-insenstive. |
|
274 | - * This method should return true if the header was successfully deleted, |
|
275 | - * and false if the header did not exist. |
|
276 | - * |
|
277 | - * @return bool |
|
278 | - */ |
|
279 | - public function removeHeader($name) { |
|
280 | - |
|
281 | - $name = strtolower($name); |
|
282 | - if (!isset($this->headers[$name])) { |
|
283 | - return false; |
|
284 | - } |
|
285 | - unset($this->headers[$name]); |
|
286 | - return true; |
|
287 | - |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Sets the HTTP version. |
|
292 | - * |
|
293 | - * Should be 1.0 or 1.1. |
|
294 | - * |
|
295 | - * @param string $version |
|
296 | - * @return void |
|
297 | - */ |
|
298 | - public function setHttpVersion($version) { |
|
299 | - |
|
300 | - $this->httpVersion = $version; |
|
301 | - |
|
302 | - } |
|
303 | - |
|
304 | - /** |
|
305 | - * Returns the HTTP version. |
|
306 | - * |
|
307 | - * @return string |
|
308 | - */ |
|
309 | - public function getHttpVersion() { |
|
310 | - |
|
311 | - return $this->httpVersion; |
|
312 | - |
|
313 | - } |
|
16 | + /** |
|
17 | + * Request body |
|
18 | + * |
|
19 | + * This should be a stream resource |
|
20 | + * |
|
21 | + * @var resource |
|
22 | + */ |
|
23 | + protected $body; |
|
24 | + |
|
25 | + /** |
|
26 | + * Contains the list of HTTP headers |
|
27 | + * |
|
28 | + * @var array |
|
29 | + */ |
|
30 | + protected $headers = []; |
|
31 | + |
|
32 | + /** |
|
33 | + * HTTP message version (1.0 or 1.1) |
|
34 | + * |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + protected $httpVersion = '1.1'; |
|
38 | + |
|
39 | + /** |
|
40 | + * Returns the body as a readable stream resource. |
|
41 | + * |
|
42 | + * Note that the stream may not be rewindable, and therefore may only be |
|
43 | + * read once. |
|
44 | + * |
|
45 | + * @return resource |
|
46 | + */ |
|
47 | + public function getBodyAsStream() { |
|
48 | + |
|
49 | + $body = $this->getBody(); |
|
50 | + if (is_string($body) || is_null($body)) { |
|
51 | + $stream = fopen('php://temp', 'r+'); |
|
52 | + fwrite($stream, $body); |
|
53 | + rewind($stream); |
|
54 | + return $stream; |
|
55 | + } |
|
56 | + return $body; |
|
57 | + |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Returns the body as a string. |
|
62 | + * |
|
63 | + * Note that because the underlying data may be based on a stream, this |
|
64 | + * method could only work correctly the first time. |
|
65 | + * |
|
66 | + * @return string |
|
67 | + */ |
|
68 | + public function getBodyAsString() { |
|
69 | + |
|
70 | + $body = $this->getBody(); |
|
71 | + if (is_string($body)) { |
|
72 | + return $body; |
|
73 | + } |
|
74 | + if (is_null($body)) { |
|
75 | + return ''; |
|
76 | + } |
|
77 | + $contentLength = $this->getHeader('Content-Length'); |
|
78 | + if (null === $contentLength) { |
|
79 | + return stream_get_contents($body); |
|
80 | + } else { |
|
81 | + return stream_get_contents($body, $contentLength); |
|
82 | + } |
|
83 | + |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Returns the message body, as it's internal representation. |
|
88 | + * |
|
89 | + * This could be either a string or a stream. |
|
90 | + * |
|
91 | + * @return resource|string |
|
92 | + */ |
|
93 | + public function getBody() { |
|
94 | + |
|
95 | + return $this->body; |
|
96 | + |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Replaces the body resource with a new stream or string. |
|
101 | + * |
|
102 | + * @param resource|string $body |
|
103 | + */ |
|
104 | + public function setBody($body) { |
|
105 | + |
|
106 | + $this->body = $body; |
|
107 | + |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Returns all the HTTP headers as an array. |
|
112 | + * |
|
113 | + * Every header is returned as an array, with one or more values. |
|
114 | + * |
|
115 | + * @return array |
|
116 | + */ |
|
117 | + public function getHeaders() { |
|
118 | + |
|
119 | + $result = []; |
|
120 | + foreach ($this->headers as $headerInfo) { |
|
121 | + $result[$headerInfo[0]] = $headerInfo[1]; |
|
122 | + } |
|
123 | + return $result; |
|
124 | + |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Will return true or false, depending on if a HTTP header exists. |
|
129 | + * |
|
130 | + * @param string $name |
|
131 | + * @return bool |
|
132 | + */ |
|
133 | + public function hasHeader($name) { |
|
134 | + |
|
135 | + return isset($this->headers[strtolower($name)]); |
|
136 | + |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Returns a specific HTTP header, based on it's name. |
|
141 | + * |
|
142 | + * The name must be treated as case-insensitive. |
|
143 | + * If the header does not exist, this method must return null. |
|
144 | + * |
|
145 | + * If a header appeared more than once in a HTTP request, this method will |
|
146 | + * concatenate all the values with a comma. |
|
147 | + * |
|
148 | + * Note that this not make sense for all headers. Some, such as |
|
149 | + * `Set-Cookie` cannot be logically combined with a comma. In those cases |
|
150 | + * you *should* use getHeaderAsArray(). |
|
151 | + * |
|
152 | + * @param string $name |
|
153 | + * @return string|null |
|
154 | + */ |
|
155 | + public function getHeader($name) { |
|
156 | + |
|
157 | + $name = strtolower($name); |
|
158 | + |
|
159 | + if (isset($this->headers[$name])) { |
|
160 | + return implode(',', $this->headers[$name][1]); |
|
161 | + } |
|
162 | + return null; |
|
163 | + |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Returns a HTTP header as an array. |
|
168 | + * |
|
169 | + * For every time the HTTP header appeared in the request or response, an |
|
170 | + * item will appear in the array. |
|
171 | + * |
|
172 | + * If the header did not exists, this method will return an empty array. |
|
173 | + * |
|
174 | + * @param string $name |
|
175 | + * @return string[] |
|
176 | + */ |
|
177 | + public function getHeaderAsArray($name) { |
|
178 | + |
|
179 | + $name = strtolower($name); |
|
180 | + |
|
181 | + if (isset($this->headers[$name])) { |
|
182 | + return $this->headers[$name][1]; |
|
183 | + } |
|
184 | + |
|
185 | + return []; |
|
186 | + |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Updates a HTTP header. |
|
191 | + * |
|
192 | + * The case-sensitity of the name value must be retained as-is. |
|
193 | + * |
|
194 | + * If the header already existed, it will be overwritten. |
|
195 | + * |
|
196 | + * @param string $name |
|
197 | + * @param string|string[] $value |
|
198 | + * @return void |
|
199 | + */ |
|
200 | + public function setHeader($name, $value) { |
|
201 | + |
|
202 | + $this->headers[strtolower($name)] = [$name, (array)$value]; |
|
203 | + |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * Sets a new set of HTTP headers. |
|
208 | + * |
|
209 | + * The headers array should contain headernames for keys, and their value |
|
210 | + * should be specified as either a string or an array. |
|
211 | + * |
|
212 | + * Any header that already existed will be overwritten. |
|
213 | + * |
|
214 | + * @param array $headers |
|
215 | + * @return void |
|
216 | + */ |
|
217 | + public function setHeaders(array $headers) { |
|
218 | + |
|
219 | + foreach ($headers as $name => $value) { |
|
220 | + $this->setHeader($name, $value); |
|
221 | + } |
|
222 | + |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * Adds a HTTP header. |
|
227 | + * |
|
228 | + * This method will not overwrite any existing HTTP header, but instead add |
|
229 | + * another value. Individual values can be retrieved with |
|
230 | + * getHeadersAsArray. |
|
231 | + * |
|
232 | + * @param string $name |
|
233 | + * @param string $value |
|
234 | + * @return void |
|
235 | + */ |
|
236 | + public function addHeader($name, $value) { |
|
237 | + |
|
238 | + $lName = strtolower($name); |
|
239 | + if (isset($this->headers[$lName])) { |
|
240 | + $this->headers[$lName][1] = array_merge( |
|
241 | + $this->headers[$lName][1], |
|
242 | + (array)$value |
|
243 | + ); |
|
244 | + } else { |
|
245 | + $this->headers[$lName] = [ |
|
246 | + $name, |
|
247 | + (array)$value |
|
248 | + ]; |
|
249 | + } |
|
250 | + |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * Adds a new set of HTTP headers. |
|
255 | + * |
|
256 | + * Any existing headers will not be overwritten. |
|
257 | + * |
|
258 | + * @param array $headers |
|
259 | + * @return void |
|
260 | + */ |
|
261 | + public function addHeaders(array $headers) { |
|
262 | + |
|
263 | + foreach ($headers as $name => $value) { |
|
264 | + $this->addHeader($name, $value); |
|
265 | + } |
|
266 | + |
|
267 | + } |
|
268 | + |
|
269 | + |
|
270 | + /** |
|
271 | + * Removes a HTTP header. |
|
272 | + * |
|
273 | + * The specified header name must be treated as case-insenstive. |
|
274 | + * This method should return true if the header was successfully deleted, |
|
275 | + * and false if the header did not exist. |
|
276 | + * |
|
277 | + * @return bool |
|
278 | + */ |
|
279 | + public function removeHeader($name) { |
|
280 | + |
|
281 | + $name = strtolower($name); |
|
282 | + if (!isset($this->headers[$name])) { |
|
283 | + return false; |
|
284 | + } |
|
285 | + unset($this->headers[$name]); |
|
286 | + return true; |
|
287 | + |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Sets the HTTP version. |
|
292 | + * |
|
293 | + * Should be 1.0 or 1.1. |
|
294 | + * |
|
295 | + * @param string $version |
|
296 | + * @return void |
|
297 | + */ |
|
298 | + public function setHttpVersion($version) { |
|
299 | + |
|
300 | + $this->httpVersion = $version; |
|
301 | + |
|
302 | + } |
|
303 | + |
|
304 | + /** |
|
305 | + * Returns the HTTP version. |
|
306 | + * |
|
307 | + * @return string |
|
308 | + */ |
|
309 | + public function getHttpVersion() { |
|
310 | + |
|
311 | + return $this->httpVersion; |
|
312 | + |
|
313 | + } |
|
314 | 314 | } |
@@ -43,557 +43,557 @@ |
||
43 | 43 | */ |
44 | 44 | class Client extends EventEmitter { |
45 | 45 | |
46 | - /** |
|
47 | - * List of curl settings |
|
48 | - * |
|
49 | - * @var array |
|
50 | - */ |
|
51 | - protected $curlSettings = []; |
|
46 | + /** |
|
47 | + * List of curl settings |
|
48 | + * |
|
49 | + * @var array |
|
50 | + */ |
|
51 | + protected $curlSettings = []; |
|
52 | 52 | |
53 | - /** |
|
54 | - * Wether or not exceptions should be thrown when a HTTP error is returned. |
|
55 | - * |
|
56 | - * @var bool |
|
57 | - */ |
|
58 | - protected $throwExceptions = false; |
|
53 | + /** |
|
54 | + * Wether or not exceptions should be thrown when a HTTP error is returned. |
|
55 | + * |
|
56 | + * @var bool |
|
57 | + */ |
|
58 | + protected $throwExceptions = false; |
|
59 | 59 | |
60 | - /** |
|
61 | - * The maximum number of times we'll follow a redirect. |
|
62 | - * |
|
63 | - * @var int |
|
64 | - */ |
|
65 | - protected $maxRedirects = 5; |
|
60 | + /** |
|
61 | + * The maximum number of times we'll follow a redirect. |
|
62 | + * |
|
63 | + * @var int |
|
64 | + */ |
|
65 | + protected $maxRedirects = 5; |
|
66 | 66 | |
67 | - /** |
|
68 | - * Initializes the client. |
|
69 | - * |
|
70 | - * @return void |
|
71 | - */ |
|
72 | - public function __construct() { |
|
67 | + /** |
|
68 | + * Initializes the client. |
|
69 | + * |
|
70 | + * @return void |
|
71 | + */ |
|
72 | + public function __construct() { |
|
73 | 73 | |
74 | - $this->curlSettings = [ |
|
75 | - CURLOPT_RETURNTRANSFER => true, |
|
76 | - CURLOPT_HEADER => true, |
|
77 | - CURLOPT_NOBODY => false, |
|
78 | - CURLOPT_USERAGENT => 'sabre-http/' . Version::VERSION . ' (http://sabre.io/)', |
|
79 | - ]; |
|
74 | + $this->curlSettings = [ |
|
75 | + CURLOPT_RETURNTRANSFER => true, |
|
76 | + CURLOPT_HEADER => true, |
|
77 | + CURLOPT_NOBODY => false, |
|
78 | + CURLOPT_USERAGENT => 'sabre-http/' . Version::VERSION . ' (http://sabre.io/)', |
|
79 | + ]; |
|
80 | 80 | |
81 | - } |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Sends a request to a HTTP server, and returns a response. |
|
85 | - * |
|
86 | - * @param RequestInterface $request |
|
87 | - * @return ResponseInterface |
|
88 | - */ |
|
89 | - public function send(RequestInterface $request) { |
|
83 | + /** |
|
84 | + * Sends a request to a HTTP server, and returns a response. |
|
85 | + * |
|
86 | + * @param RequestInterface $request |
|
87 | + * @return ResponseInterface |
|
88 | + */ |
|
89 | + public function send(RequestInterface $request) { |
|
90 | 90 | |
91 | - $this->emit('beforeRequest', [$request]); |
|
91 | + $this->emit('beforeRequest', [$request]); |
|
92 | 92 | |
93 | - $retryCount = 0; |
|
94 | - $redirects = 0; |
|
93 | + $retryCount = 0; |
|
94 | + $redirects = 0; |
|
95 | 95 | |
96 | - do { |
|
97 | - |
|
98 | - $doRedirect = false; |
|
99 | - $retry = false; |
|
96 | + do { |
|
97 | + |
|
98 | + $doRedirect = false; |
|
99 | + $retry = false; |
|
100 | 100 | |
101 | - try { |
|
102 | - |
|
103 | - $response = $this->doRequest($request); |
|
104 | - |
|
105 | - $code = (int)$response->getStatus(); |
|
106 | - |
|
107 | - // We are doing in-PHP redirects, because curl's |
|
108 | - // FOLLOW_LOCATION throws errors when PHP is configured with |
|
109 | - // open_basedir. |
|
110 | - // |
|
111 | - // https://github.com/fruux/sabre-http/issues/12 |
|
112 | - if (in_array($code, [301, 302, 307, 308]) && $redirects < $this->maxRedirects) { |
|
113 | - |
|
114 | - $oldLocation = $request->getUrl(); |
|
115 | - |
|
116 | - // Creating a new instance of the request object. |
|
117 | - $request = clone $request; |
|
118 | - |
|
119 | - // Setting the new location |
|
120 | - $request->setUrl(Uri\resolve( |
|
121 | - $oldLocation, |
|
122 | - $response->getHeader('Location') |
|
123 | - )); |
|
101 | + try { |
|
102 | + |
|
103 | + $response = $this->doRequest($request); |
|
104 | + |
|
105 | + $code = (int)$response->getStatus(); |
|
106 | + |
|
107 | + // We are doing in-PHP redirects, because curl's |
|
108 | + // FOLLOW_LOCATION throws errors when PHP is configured with |
|
109 | + // open_basedir. |
|
110 | + // |
|
111 | + // https://github.com/fruux/sabre-http/issues/12 |
|
112 | + if (in_array($code, [301, 302, 307, 308]) && $redirects < $this->maxRedirects) { |
|
113 | + |
|
114 | + $oldLocation = $request->getUrl(); |
|
115 | + |
|
116 | + // Creating a new instance of the request object. |
|
117 | + $request = clone $request; |
|
118 | + |
|
119 | + // Setting the new location |
|
120 | + $request->setUrl(Uri\resolve( |
|
121 | + $oldLocation, |
|
122 | + $response->getHeader('Location') |
|
123 | + )); |
|
124 | 124 | |
125 | - $doRedirect = true; |
|
126 | - $redirects++; |
|
127 | - |
|
128 | - } |
|
129 | - |
|
130 | - // This was a HTTP error |
|
131 | - if ($code >= 400) { |
|
132 | - |
|
133 | - $this->emit('error', [$request, $response, &$retry, $retryCount]); |
|
134 | - $this->emit('error:' . $code, [$request, $response, &$retry, $retryCount]); |
|
135 | - |
|
136 | - } |
|
137 | - |
|
138 | - } catch (ClientException $e) { |
|
139 | - |
|
140 | - $this->emit('exception', [$request, $e, &$retry, $retryCount]); |
|
141 | - |
|
142 | - // If retry was still set to false, it means no event handler |
|
143 | - // dealt with the problem. In this case we just re-throw the |
|
144 | - // exception. |
|
145 | - if (!$retry) { |
|
146 | - throw $e; |
|
147 | - } |
|
148 | - |
|
149 | - } |
|
150 | - |
|
151 | - if ($retry) { |
|
152 | - $retryCount++; |
|
153 | - } |
|
125 | + $doRedirect = true; |
|
126 | + $redirects++; |
|
127 | + |
|
128 | + } |
|
129 | + |
|
130 | + // This was a HTTP error |
|
131 | + if ($code >= 400) { |
|
132 | + |
|
133 | + $this->emit('error', [$request, $response, &$retry, $retryCount]); |
|
134 | + $this->emit('error:' . $code, [$request, $response, &$retry, $retryCount]); |
|
135 | + |
|
136 | + } |
|
137 | + |
|
138 | + } catch (ClientException $e) { |
|
139 | + |
|
140 | + $this->emit('exception', [$request, $e, &$retry, $retryCount]); |
|
141 | + |
|
142 | + // If retry was still set to false, it means no event handler |
|
143 | + // dealt with the problem. In this case we just re-throw the |
|
144 | + // exception. |
|
145 | + if (!$retry) { |
|
146 | + throw $e; |
|
147 | + } |
|
148 | + |
|
149 | + } |
|
150 | + |
|
151 | + if ($retry) { |
|
152 | + $retryCount++; |
|
153 | + } |
|
154 | 154 | |
155 | - } while ($retry || $doRedirect); |
|
155 | + } while ($retry || $doRedirect); |
|
156 | 156 | |
157 | - $this->emit('afterRequest', [$request, $response]); |
|
157 | + $this->emit('afterRequest', [$request, $response]); |
|
158 | 158 | |
159 | - if ($this->throwExceptions && $code >= 400) { |
|
160 | - throw new ClientHttpException($response); |
|
161 | - } |
|
159 | + if ($this->throwExceptions && $code >= 400) { |
|
160 | + throw new ClientHttpException($response); |
|
161 | + } |
|
162 | 162 | |
163 | - return $response; |
|
163 | + return $response; |
|
164 | 164 | |
165 | - } |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * Sends a HTTP request asynchronously. |
|
169 | - * |
|
170 | - * Due to the nature of PHP, you must from time to time poll to see if any |
|
171 | - * new responses came in. |
|
172 | - * |
|
173 | - * After calling sendAsync, you must therefore occasionally call the poll() |
|
174 | - * method, or wait(). |
|
175 | - * |
|
176 | - * @param RequestInterface $request |
|
177 | - * @param callable $success |
|
178 | - * @param callable $error |
|
179 | - * @return void |
|
180 | - */ |
|
181 | - public function sendAsync(RequestInterface $request, callable $success = null, callable $error = null) { |
|
167 | + /** |
|
168 | + * Sends a HTTP request asynchronously. |
|
169 | + * |
|
170 | + * Due to the nature of PHP, you must from time to time poll to see if any |
|
171 | + * new responses came in. |
|
172 | + * |
|
173 | + * After calling sendAsync, you must therefore occasionally call the poll() |
|
174 | + * method, or wait(). |
|
175 | + * |
|
176 | + * @param RequestInterface $request |
|
177 | + * @param callable $success |
|
178 | + * @param callable $error |
|
179 | + * @return void |
|
180 | + */ |
|
181 | + public function sendAsync(RequestInterface $request, callable $success = null, callable $error = null) { |
|
182 | 182 | |
183 | - $this->emit('beforeRequest', [$request]); |
|
184 | - $this->sendAsyncInternal($request, $success, $error); |
|
185 | - $this->poll(); |
|
183 | + $this->emit('beforeRequest', [$request]); |
|
184 | + $this->sendAsyncInternal($request, $success, $error); |
|
185 | + $this->poll(); |
|
186 | 186 | |
187 | - } |
|
187 | + } |
|
188 | 188 | |
189 | 189 | |
190 | - /** |
|
191 | - * This method checks if any http requests have gotten results, and if so, |
|
192 | - * call the appropriate success or error handlers. |
|
193 | - * |
|
194 | - * This method will return true if there are still requests waiting to |
|
195 | - * return, and false if all the work is done. |
|
196 | - * |
|
197 | - * @return bool |
|
198 | - */ |
|
199 | - public function poll() { |
|
190 | + /** |
|
191 | + * This method checks if any http requests have gotten results, and if so, |
|
192 | + * call the appropriate success or error handlers. |
|
193 | + * |
|
194 | + * This method will return true if there are still requests waiting to |
|
195 | + * return, and false if all the work is done. |
|
196 | + * |
|
197 | + * @return bool |
|
198 | + */ |
|
199 | + public function poll() { |
|
200 | 200 | |
201 | - // nothing to do? |
|
202 | - if (!$this->curlMultiMap) { |
|
203 | - return false; |
|
204 | - } |
|
201 | + // nothing to do? |
|
202 | + if (!$this->curlMultiMap) { |
|
203 | + return false; |
|
204 | + } |
|
205 | 205 | |
206 | - do { |
|
207 | - $r = curl_multi_exec( |
|
208 | - $this->curlMultiHandle, |
|
209 | - $stillRunning |
|
210 | - ); |
|
211 | - } while ($r === CURLM_CALL_MULTI_PERFORM); |
|
206 | + do { |
|
207 | + $r = curl_multi_exec( |
|
208 | + $this->curlMultiHandle, |
|
209 | + $stillRunning |
|
210 | + ); |
|
211 | + } while ($r === CURLM_CALL_MULTI_PERFORM); |
|
212 | 212 | |
213 | - do { |
|
213 | + do { |
|
214 | 214 | |
215 | - messageQueue: |
|
215 | + messageQueue: |
|
216 | 216 | |
217 | - $status = curl_multi_info_read( |
|
218 | - $this->curlMultiHandle, |
|
219 | - $messagesInQueue |
|
220 | - ); |
|
217 | + $status = curl_multi_info_read( |
|
218 | + $this->curlMultiHandle, |
|
219 | + $messagesInQueue |
|
220 | + ); |
|
221 | 221 | |
222 | - if ($status && $status['msg'] === CURLMSG_DONE) { |
|
222 | + if ($status && $status['msg'] === CURLMSG_DONE) { |
|
223 | 223 | |
224 | - $resourceId = intval($status['handle']); |
|
225 | - list( |
|
226 | - $request, |
|
227 | - $successCallback, |
|
228 | - $errorCallback, |
|
229 | - $retryCount, |
|
230 | - ) = $this->curlMultiMap[$resourceId]; |
|
231 | - unset($this->curlMultiMap[$resourceId]); |
|
232 | - $curlResult = $this->parseCurlResult(curl_multi_getcontent($status['handle']), $status['handle']); |
|
233 | - $retry = false; |
|
224 | + $resourceId = intval($status['handle']); |
|
225 | + list( |
|
226 | + $request, |
|
227 | + $successCallback, |
|
228 | + $errorCallback, |
|
229 | + $retryCount, |
|
230 | + ) = $this->curlMultiMap[$resourceId]; |
|
231 | + unset($this->curlMultiMap[$resourceId]); |
|
232 | + $curlResult = $this->parseCurlResult(curl_multi_getcontent($status['handle']), $status['handle']); |
|
233 | + $retry = false; |
|
234 | 234 | |
235 | - if ($curlResult['status'] === self::STATUS_CURLERROR) { |
|
235 | + if ($curlResult['status'] === self::STATUS_CURLERROR) { |
|
236 | 236 | |
237 | - $e = new ClientException($curlResult['curl_errmsg'], $curlResult['curl_errno']); |
|
238 | - $this->emit('exception', [$request, $e, &$retry, $retryCount]); |
|
237 | + $e = new ClientException($curlResult['curl_errmsg'], $curlResult['curl_errno']); |
|
238 | + $this->emit('exception', [$request, $e, &$retry, $retryCount]); |
|
239 | 239 | |
240 | - if ($retry) { |
|
241 | - $retryCount++; |
|
242 | - $this->sendAsyncInternal($request, $successCallback, $errorCallback, $retryCount); |
|
243 | - goto messageQueue; |
|
244 | - } |
|
245 | - |
|
246 | - $curlResult['request'] = $request; |
|
240 | + if ($retry) { |
|
241 | + $retryCount++; |
|
242 | + $this->sendAsyncInternal($request, $successCallback, $errorCallback, $retryCount); |
|
243 | + goto messageQueue; |
|
244 | + } |
|
245 | + |
|
246 | + $curlResult['request'] = $request; |
|
247 | 247 | |
248 | - if ($errorCallback) { |
|
249 | - $errorCallback($curlResult); |
|
250 | - } |
|
251 | - |
|
252 | - } elseif ($curlResult['status'] === self::STATUS_HTTPERROR) { |
|
253 | - |
|
254 | - $this->emit('error', [$request, $curlResult['response'], &$retry, $retryCount]); |
|
255 | - $this->emit('error:' . $curlResult['http_code'], [$request, $curlResult['response'], &$retry, $retryCount]); |
|
256 | - |
|
257 | - if ($retry) { |
|
258 | - |
|
259 | - $retryCount++; |
|
260 | - $this->sendAsyncInternal($request, $successCallback, $errorCallback, $retryCount); |
|
261 | - goto messageQueue; |
|
248 | + if ($errorCallback) { |
|
249 | + $errorCallback($curlResult); |
|
250 | + } |
|
251 | + |
|
252 | + } elseif ($curlResult['status'] === self::STATUS_HTTPERROR) { |
|
253 | + |
|
254 | + $this->emit('error', [$request, $curlResult['response'], &$retry, $retryCount]); |
|
255 | + $this->emit('error:' . $curlResult['http_code'], [$request, $curlResult['response'], &$retry, $retryCount]); |
|
256 | + |
|
257 | + if ($retry) { |
|
258 | + |
|
259 | + $retryCount++; |
|
260 | + $this->sendAsyncInternal($request, $successCallback, $errorCallback, $retryCount); |
|
261 | + goto messageQueue; |
|
262 | 262 | |
263 | - } |
|
264 | - |
|
265 | - $curlResult['request'] = $request; |
|
266 | - |
|
267 | - if ($errorCallback) { |
|
268 | - $errorCallback($curlResult); |
|
269 | - } |
|
270 | - |
|
271 | - } else { |
|
272 | - |
|
273 | - $this->emit('afterRequest', [$request, $curlResult['response']]); |
|
274 | - |
|
275 | - if ($successCallback) { |
|
276 | - $successCallback($curlResult['response']); |
|
277 | - } |
|
278 | - |
|
279 | - } |
|
280 | - } |
|
281 | - |
|
282 | - } while ($messagesInQueue > 0); |
|
283 | - |
|
284 | - return count($this->curlMultiMap) > 0; |
|
285 | - |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * Processes every HTTP request in the queue, and waits till they are all |
|
290 | - * completed. |
|
291 | - * |
|
292 | - * @return void |
|
293 | - */ |
|
294 | - public function wait() { |
|
295 | - |
|
296 | - do { |
|
297 | - curl_multi_select($this->curlMultiHandle); |
|
298 | - $stillRunning = $this->poll(); |
|
299 | - } while ($stillRunning); |
|
300 | - |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * If this is set to true, the Client will automatically throw exceptions |
|
305 | - * upon HTTP errors. |
|
306 | - * |
|
307 | - * This means that if a response came back with a status code greater than |
|
308 | - * or equal to 400, we will throw a ClientHttpException. |
|
309 | - * |
|
310 | - * This only works for the send() method. Throwing exceptions for |
|
311 | - * sendAsync() is not supported. |
|
312 | - * |
|
313 | - * @param bool $throwExceptions |
|
314 | - * @return void |
|
315 | - */ |
|
316 | - public function setThrowExceptions($throwExceptions) { |
|
317 | - |
|
318 | - $this->throwExceptions = $throwExceptions; |
|
319 | - |
|
320 | - } |
|
321 | - |
|
322 | - /** |
|
323 | - * Adds a CURL setting. |
|
324 | - * |
|
325 | - * These settings will be included in every HTTP request. |
|
326 | - * |
|
327 | - * @param int $name |
|
328 | - * @param mixed $value |
|
329 | - * @return void |
|
330 | - */ |
|
331 | - public function addCurlSetting($name, $value) { |
|
332 | - |
|
333 | - $this->curlSettings[$name] = $value; |
|
334 | - |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * This method is responsible for performing a single request. |
|
339 | - * |
|
340 | - * @param RequestInterface $request |
|
341 | - * @return ResponseInterface |
|
342 | - */ |
|
343 | - protected function doRequest(RequestInterface $request) { |
|
344 | - |
|
345 | - $settings = $this->createCurlSettingsArray($request); |
|
346 | - |
|
347 | - if (!$this->curlHandle) { |
|
348 | - $this->curlHandle = curl_init(); |
|
349 | - } |
|
350 | - |
|
351 | - curl_setopt_array($this->curlHandle, $settings); |
|
352 | - $response = $this->curlExec($this->curlHandle); |
|
353 | - $response = $this->parseCurlResult($response, $this->curlHandle); |
|
354 | - |
|
355 | - if ($response['status'] === self::STATUS_CURLERROR) { |
|
356 | - throw new ClientException($response['curl_errmsg'], $response['curl_errno']); |
|
357 | - } |
|
358 | - |
|
359 | - return $response['response']; |
|
360 | - |
|
361 | - } |
|
362 | - |
|
363 | - /** |
|
364 | - * Cached curl handle. |
|
365 | - * |
|
366 | - * By keeping this resource around for the lifetime of this object, things |
|
367 | - * like persistent connections are possible. |
|
368 | - * |
|
369 | - * @var resource |
|
370 | - */ |
|
371 | - private $curlHandle; |
|
372 | - |
|
373 | - /** |
|
374 | - * Handler for curl_multi requests. |
|
375 | - * |
|
376 | - * The first time sendAsync is used, this will be created. |
|
377 | - * |
|
378 | - * @var resource |
|
379 | - */ |
|
380 | - private $curlMultiHandle; |
|
381 | - |
|
382 | - /** |
|
383 | - * Has a list of curl handles, as well as their associated success and |
|
384 | - * error callbacks. |
|
385 | - * |
|
386 | - * @var array |
|
387 | - */ |
|
388 | - private $curlMultiMap = []; |
|
389 | - |
|
390 | - /** |
|
391 | - * Turns a RequestInterface object into an array with settings that can be |
|
392 | - * fed to curl_setopt |
|
393 | - * |
|
394 | - * @param RequestInterface $request |
|
395 | - * @return array |
|
396 | - */ |
|
397 | - protected function createCurlSettingsArray(RequestInterface $request) { |
|
398 | - |
|
399 | - $settings = $this->curlSettings; |
|
400 | - |
|
401 | - switch ($request->getMethod()) { |
|
402 | - case 'HEAD' : |
|
403 | - $settings[CURLOPT_NOBODY] = true; |
|
404 | - $settings[CURLOPT_CUSTOMREQUEST] = 'HEAD'; |
|
405 | - $settings[CURLOPT_POSTFIELDS] = ''; |
|
406 | - $settings[CURLOPT_PUT] = false; |
|
407 | - break; |
|
408 | - case 'GET' : |
|
409 | - $settings[CURLOPT_CUSTOMREQUEST] = 'GET'; |
|
410 | - $settings[CURLOPT_POSTFIELDS] = ''; |
|
411 | - $settings[CURLOPT_PUT] = false; |
|
412 | - break; |
|
413 | - default : |
|
414 | - $body = $request->getBody(); |
|
415 | - if (is_resource($body)) { |
|
416 | - // This needs to be set to PUT, regardless of the actual |
|
417 | - // method used. Without it, INFILE will be ignored for some |
|
418 | - // reason. |
|
419 | - $settings[CURLOPT_PUT] = true; |
|
420 | - $settings[CURLOPT_INFILE] = $request->getBody(); |
|
421 | - } else { |
|
422 | - // For security we cast this to a string. If somehow an array could |
|
423 | - // be passed here, it would be possible for an attacker to use @ to |
|
424 | - // post local files. |
|
425 | - $settings[CURLOPT_POSTFIELDS] = (string)$body; |
|
426 | - } |
|
427 | - $settings[CURLOPT_CUSTOMREQUEST] = $request->getMethod(); |
|
428 | - break; |
|
429 | - |
|
430 | - } |
|
431 | - |
|
432 | - $nHeaders = []; |
|
433 | - foreach ($request->getHeaders() as $key => $values) { |
|
434 | - |
|
435 | - foreach ($values as $value) { |
|
436 | - $nHeaders[] = $key . ': ' . $value; |
|
437 | - } |
|
438 | - |
|
439 | - } |
|
440 | - $settings[CURLOPT_HTTPHEADER] = $nHeaders; |
|
441 | - $settings[CURLOPT_URL] = $request->getUrl(); |
|
442 | - if (defined('CURLOPT_PROTOCOLS')) { |
|
443 | - $settings[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; |
|
444 | - } |
|
445 | - if (defined('CURLOPT_REDIR_PROTOCOLS')) { |
|
446 | - $settings[CURLOPT_REDIR_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; |
|
447 | - } |
|
448 | - |
|
449 | - return $settings; |
|
450 | - |
|
451 | - } |
|
452 | - |
|
453 | - const STATUS_SUCCESS = 0; |
|
454 | - const STATUS_CURLERROR = 1; |
|
455 | - const STATUS_HTTPERROR = 2; |
|
456 | - |
|
457 | - /** |
|
458 | - * Parses the result of a curl call in a format that's a bit more |
|
459 | - * convenient to work with. |
|
460 | - * |
|
461 | - * The method returns an array with the following elements: |
|
462 | - * * status - one of the 3 STATUS constants. |
|
463 | - * * curl_errno - A curl error number. Only set if status is |
|
464 | - * STATUS_CURLERROR. |
|
465 | - * * curl_errmsg - A current error message. Only set if status is |
|
466 | - * STATUS_CURLERROR. |
|
467 | - * * response - Response object. Only set if status is STATUS_SUCCESS, or |
|
468 | - * STATUS_HTTPERROR. |
|
469 | - * * http_code - HTTP status code, as an int. Only set if Only set if |
|
470 | - * status is STATUS_SUCCESS, or STATUS_HTTPERROR |
|
471 | - * |
|
472 | - * @param string $response |
|
473 | - * @param resource $curlHandle |
|
474 | - * @return Response |
|
475 | - */ |
|
476 | - protected function parseCurlResult($response, $curlHandle) { |
|
477 | - |
|
478 | - list( |
|
479 | - $curlInfo, |
|
480 | - $curlErrNo, |
|
481 | - $curlErrMsg |
|
482 | - ) = $this->curlStuff($curlHandle); |
|
483 | - |
|
484 | - if ($curlErrNo) { |
|
485 | - return [ |
|
486 | - 'status' => self::STATUS_CURLERROR, |
|
487 | - 'curl_errno' => $curlErrNo, |
|
488 | - 'curl_errmsg' => $curlErrMsg, |
|
489 | - ]; |
|
490 | - } |
|
491 | - |
|
492 | - $headerBlob = substr($response, 0, $curlInfo['header_size']); |
|
493 | - // In the case of 204 No Content, strlen($response) == $curlInfo['header_size]. |
|
494 | - // This will cause substr($response, $curlInfo['header_size']) return FALSE instead of NULL |
|
495 | - // An exception will be thrown when calling getBodyAsString then |
|
496 | - $responseBody = substr($response, $curlInfo['header_size']) ?: null; |
|
497 | - |
|
498 | - unset($response); |
|
499 | - |
|
500 | - // In the case of 100 Continue, or redirects we'll have multiple lists |
|
501 | - // of headers for each separate HTTP response. We can easily split this |
|
502 | - // because they are separated by \r\n\r\n |
|
503 | - $headerBlob = explode("\r\n\r\n", trim($headerBlob, "\r\n")); |
|
504 | - |
|
505 | - // We only care about the last set of headers |
|
506 | - $headerBlob = $headerBlob[count($headerBlob) - 1]; |
|
507 | - |
|
508 | - // Splitting headers |
|
509 | - $headerBlob = explode("\r\n", $headerBlob); |
|
510 | - |
|
511 | - $response = new Response(); |
|
512 | - $response->setStatus($curlInfo['http_code']); |
|
513 | - |
|
514 | - foreach ($headerBlob as $header) { |
|
515 | - $parts = explode(':', $header, 2); |
|
516 | - if (count($parts) == 2) { |
|
517 | - $response->addHeader(trim($parts[0]), trim($parts[1])); |
|
518 | - } |
|
519 | - } |
|
520 | - |
|
521 | - $response->setBody($responseBody); |
|
522 | - |
|
523 | - $httpCode = intval($response->getStatus()); |
|
524 | - |
|
525 | - return [ |
|
526 | - 'status' => $httpCode >= 400 ? self::STATUS_HTTPERROR : self::STATUS_SUCCESS, |
|
527 | - 'response' => $response, |
|
528 | - 'http_code' => $httpCode, |
|
529 | - ]; |
|
530 | - |
|
531 | - } |
|
532 | - |
|
533 | - /** |
|
534 | - * Sends an asynchronous HTTP request. |
|
535 | - * |
|
536 | - * We keep this in a separate method, so we can call it without triggering |
|
537 | - * the beforeRequest event and don't do the poll(). |
|
538 | - * |
|
539 | - * @param RequestInterface $request |
|
540 | - * @param callable $success |
|
541 | - * @param callable $error |
|
542 | - * @param int $retryCount |
|
543 | - */ |
|
544 | - protected function sendAsyncInternal(RequestInterface $request, callable $success, callable $error, $retryCount = 0) { |
|
545 | - |
|
546 | - if (!$this->curlMultiHandle) { |
|
547 | - $this->curlMultiHandle = curl_multi_init(); |
|
548 | - } |
|
549 | - $curl = curl_init(); |
|
550 | - curl_setopt_array( |
|
551 | - $curl, |
|
552 | - $this->createCurlSettingsArray($request) |
|
553 | - ); |
|
554 | - curl_multi_add_handle($this->curlMultiHandle, $curl); |
|
555 | - $this->curlMultiMap[intval($curl)] = [ |
|
556 | - $request, |
|
557 | - $success, |
|
558 | - $error, |
|
559 | - $retryCount |
|
560 | - ]; |
|
561 | - |
|
562 | - } |
|
563 | - |
|
564 | - // @codeCoverageIgnoreStart |
|
565 | - |
|
566 | - /** |
|
567 | - * Calls curl_exec |
|
568 | - * |
|
569 | - * This method exists so it can easily be overridden and mocked. |
|
570 | - * |
|
571 | - * @param resource $curlHandle |
|
572 | - * @return string |
|
573 | - */ |
|
574 | - protected function curlExec($curlHandle) { |
|
575 | - |
|
576 | - return curl_exec($curlHandle); |
|
577 | - |
|
578 | - } |
|
579 | - |
|
580 | - /** |
|
581 | - * Returns a bunch of information about a curl request. |
|
582 | - * |
|
583 | - * This method exists so it can easily be overridden and mocked. |
|
584 | - * |
|
585 | - * @param resource $curlHandle |
|
586 | - * @return array |
|
587 | - */ |
|
588 | - protected function curlStuff($curlHandle) { |
|
589 | - |
|
590 | - return [ |
|
591 | - curl_getinfo($curlHandle), |
|
592 | - curl_errno($curlHandle), |
|
593 | - curl_error($curlHandle), |
|
594 | - ]; |
|
595 | - |
|
596 | - } |
|
597 | - // @codeCoverageIgnoreEnd |
|
263 | + } |
|
264 | + |
|
265 | + $curlResult['request'] = $request; |
|
266 | + |
|
267 | + if ($errorCallback) { |
|
268 | + $errorCallback($curlResult); |
|
269 | + } |
|
270 | + |
|
271 | + } else { |
|
272 | + |
|
273 | + $this->emit('afterRequest', [$request, $curlResult['response']]); |
|
274 | + |
|
275 | + if ($successCallback) { |
|
276 | + $successCallback($curlResult['response']); |
|
277 | + } |
|
278 | + |
|
279 | + } |
|
280 | + } |
|
281 | + |
|
282 | + } while ($messagesInQueue > 0); |
|
283 | + |
|
284 | + return count($this->curlMultiMap) > 0; |
|
285 | + |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * Processes every HTTP request in the queue, and waits till they are all |
|
290 | + * completed. |
|
291 | + * |
|
292 | + * @return void |
|
293 | + */ |
|
294 | + public function wait() { |
|
295 | + |
|
296 | + do { |
|
297 | + curl_multi_select($this->curlMultiHandle); |
|
298 | + $stillRunning = $this->poll(); |
|
299 | + } while ($stillRunning); |
|
300 | + |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * If this is set to true, the Client will automatically throw exceptions |
|
305 | + * upon HTTP errors. |
|
306 | + * |
|
307 | + * This means that if a response came back with a status code greater than |
|
308 | + * or equal to 400, we will throw a ClientHttpException. |
|
309 | + * |
|
310 | + * This only works for the send() method. Throwing exceptions for |
|
311 | + * sendAsync() is not supported. |
|
312 | + * |
|
313 | + * @param bool $throwExceptions |
|
314 | + * @return void |
|
315 | + */ |
|
316 | + public function setThrowExceptions($throwExceptions) { |
|
317 | + |
|
318 | + $this->throwExceptions = $throwExceptions; |
|
319 | + |
|
320 | + } |
|
321 | + |
|
322 | + /** |
|
323 | + * Adds a CURL setting. |
|
324 | + * |
|
325 | + * These settings will be included in every HTTP request. |
|
326 | + * |
|
327 | + * @param int $name |
|
328 | + * @param mixed $value |
|
329 | + * @return void |
|
330 | + */ |
|
331 | + public function addCurlSetting($name, $value) { |
|
332 | + |
|
333 | + $this->curlSettings[$name] = $value; |
|
334 | + |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * This method is responsible for performing a single request. |
|
339 | + * |
|
340 | + * @param RequestInterface $request |
|
341 | + * @return ResponseInterface |
|
342 | + */ |
|
343 | + protected function doRequest(RequestInterface $request) { |
|
344 | + |
|
345 | + $settings = $this->createCurlSettingsArray($request); |
|
346 | + |
|
347 | + if (!$this->curlHandle) { |
|
348 | + $this->curlHandle = curl_init(); |
|
349 | + } |
|
350 | + |
|
351 | + curl_setopt_array($this->curlHandle, $settings); |
|
352 | + $response = $this->curlExec($this->curlHandle); |
|
353 | + $response = $this->parseCurlResult($response, $this->curlHandle); |
|
354 | + |
|
355 | + if ($response['status'] === self::STATUS_CURLERROR) { |
|
356 | + throw new ClientException($response['curl_errmsg'], $response['curl_errno']); |
|
357 | + } |
|
358 | + |
|
359 | + return $response['response']; |
|
360 | + |
|
361 | + } |
|
362 | + |
|
363 | + /** |
|
364 | + * Cached curl handle. |
|
365 | + * |
|
366 | + * By keeping this resource around for the lifetime of this object, things |
|
367 | + * like persistent connections are possible. |
|
368 | + * |
|
369 | + * @var resource |
|
370 | + */ |
|
371 | + private $curlHandle; |
|
372 | + |
|
373 | + /** |
|
374 | + * Handler for curl_multi requests. |
|
375 | + * |
|
376 | + * The first time sendAsync is used, this will be created. |
|
377 | + * |
|
378 | + * @var resource |
|
379 | + */ |
|
380 | + private $curlMultiHandle; |
|
381 | + |
|
382 | + /** |
|
383 | + * Has a list of curl handles, as well as their associated success and |
|
384 | + * error callbacks. |
|
385 | + * |
|
386 | + * @var array |
|
387 | + */ |
|
388 | + private $curlMultiMap = []; |
|
389 | + |
|
390 | + /** |
|
391 | + * Turns a RequestInterface object into an array with settings that can be |
|
392 | + * fed to curl_setopt |
|
393 | + * |
|
394 | + * @param RequestInterface $request |
|
395 | + * @return array |
|
396 | + */ |
|
397 | + protected function createCurlSettingsArray(RequestInterface $request) { |
|
398 | + |
|
399 | + $settings = $this->curlSettings; |
|
400 | + |
|
401 | + switch ($request->getMethod()) { |
|
402 | + case 'HEAD' : |
|
403 | + $settings[CURLOPT_NOBODY] = true; |
|
404 | + $settings[CURLOPT_CUSTOMREQUEST] = 'HEAD'; |
|
405 | + $settings[CURLOPT_POSTFIELDS] = ''; |
|
406 | + $settings[CURLOPT_PUT] = false; |
|
407 | + break; |
|
408 | + case 'GET' : |
|
409 | + $settings[CURLOPT_CUSTOMREQUEST] = 'GET'; |
|
410 | + $settings[CURLOPT_POSTFIELDS] = ''; |
|
411 | + $settings[CURLOPT_PUT] = false; |
|
412 | + break; |
|
413 | + default : |
|
414 | + $body = $request->getBody(); |
|
415 | + if (is_resource($body)) { |
|
416 | + // This needs to be set to PUT, regardless of the actual |
|
417 | + // method used. Without it, INFILE will be ignored for some |
|
418 | + // reason. |
|
419 | + $settings[CURLOPT_PUT] = true; |
|
420 | + $settings[CURLOPT_INFILE] = $request->getBody(); |
|
421 | + } else { |
|
422 | + // For security we cast this to a string. If somehow an array could |
|
423 | + // be passed here, it would be possible for an attacker to use @ to |
|
424 | + // post local files. |
|
425 | + $settings[CURLOPT_POSTFIELDS] = (string)$body; |
|
426 | + } |
|
427 | + $settings[CURLOPT_CUSTOMREQUEST] = $request->getMethod(); |
|
428 | + break; |
|
429 | + |
|
430 | + } |
|
431 | + |
|
432 | + $nHeaders = []; |
|
433 | + foreach ($request->getHeaders() as $key => $values) { |
|
434 | + |
|
435 | + foreach ($values as $value) { |
|
436 | + $nHeaders[] = $key . ': ' . $value; |
|
437 | + } |
|
438 | + |
|
439 | + } |
|
440 | + $settings[CURLOPT_HTTPHEADER] = $nHeaders; |
|
441 | + $settings[CURLOPT_URL] = $request->getUrl(); |
|
442 | + if (defined('CURLOPT_PROTOCOLS')) { |
|
443 | + $settings[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; |
|
444 | + } |
|
445 | + if (defined('CURLOPT_REDIR_PROTOCOLS')) { |
|
446 | + $settings[CURLOPT_REDIR_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; |
|
447 | + } |
|
448 | + |
|
449 | + return $settings; |
|
450 | + |
|
451 | + } |
|
452 | + |
|
453 | + const STATUS_SUCCESS = 0; |
|
454 | + const STATUS_CURLERROR = 1; |
|
455 | + const STATUS_HTTPERROR = 2; |
|
456 | + |
|
457 | + /** |
|
458 | + * Parses the result of a curl call in a format that's a bit more |
|
459 | + * convenient to work with. |
|
460 | + * |
|
461 | + * The method returns an array with the following elements: |
|
462 | + * * status - one of the 3 STATUS constants. |
|
463 | + * * curl_errno - A curl error number. Only set if status is |
|
464 | + * STATUS_CURLERROR. |
|
465 | + * * curl_errmsg - A current error message. Only set if status is |
|
466 | + * STATUS_CURLERROR. |
|
467 | + * * response - Response object. Only set if status is STATUS_SUCCESS, or |
|
468 | + * STATUS_HTTPERROR. |
|
469 | + * * http_code - HTTP status code, as an int. Only set if Only set if |
|
470 | + * status is STATUS_SUCCESS, or STATUS_HTTPERROR |
|
471 | + * |
|
472 | + * @param string $response |
|
473 | + * @param resource $curlHandle |
|
474 | + * @return Response |
|
475 | + */ |
|
476 | + protected function parseCurlResult($response, $curlHandle) { |
|
477 | + |
|
478 | + list( |
|
479 | + $curlInfo, |
|
480 | + $curlErrNo, |
|
481 | + $curlErrMsg |
|
482 | + ) = $this->curlStuff($curlHandle); |
|
483 | + |
|
484 | + if ($curlErrNo) { |
|
485 | + return [ |
|
486 | + 'status' => self::STATUS_CURLERROR, |
|
487 | + 'curl_errno' => $curlErrNo, |
|
488 | + 'curl_errmsg' => $curlErrMsg, |
|
489 | + ]; |
|
490 | + } |
|
491 | + |
|
492 | + $headerBlob = substr($response, 0, $curlInfo['header_size']); |
|
493 | + // In the case of 204 No Content, strlen($response) == $curlInfo['header_size]. |
|
494 | + // This will cause substr($response, $curlInfo['header_size']) return FALSE instead of NULL |
|
495 | + // An exception will be thrown when calling getBodyAsString then |
|
496 | + $responseBody = substr($response, $curlInfo['header_size']) ?: null; |
|
497 | + |
|
498 | + unset($response); |
|
499 | + |
|
500 | + // In the case of 100 Continue, or redirects we'll have multiple lists |
|
501 | + // of headers for each separate HTTP response. We can easily split this |
|
502 | + // because they are separated by \r\n\r\n |
|
503 | + $headerBlob = explode("\r\n\r\n", trim($headerBlob, "\r\n")); |
|
504 | + |
|
505 | + // We only care about the last set of headers |
|
506 | + $headerBlob = $headerBlob[count($headerBlob) - 1]; |
|
507 | + |
|
508 | + // Splitting headers |
|
509 | + $headerBlob = explode("\r\n", $headerBlob); |
|
510 | + |
|
511 | + $response = new Response(); |
|
512 | + $response->setStatus($curlInfo['http_code']); |
|
513 | + |
|
514 | + foreach ($headerBlob as $header) { |
|
515 | + $parts = explode(':', $header, 2); |
|
516 | + if (count($parts) == 2) { |
|
517 | + $response->addHeader(trim($parts[0]), trim($parts[1])); |
|
518 | + } |
|
519 | + } |
|
520 | + |
|
521 | + $response->setBody($responseBody); |
|
522 | + |
|
523 | + $httpCode = intval($response->getStatus()); |
|
524 | + |
|
525 | + return [ |
|
526 | + 'status' => $httpCode >= 400 ? self::STATUS_HTTPERROR : self::STATUS_SUCCESS, |
|
527 | + 'response' => $response, |
|
528 | + 'http_code' => $httpCode, |
|
529 | + ]; |
|
530 | + |
|
531 | + } |
|
532 | + |
|
533 | + /** |
|
534 | + * Sends an asynchronous HTTP request. |
|
535 | + * |
|
536 | + * We keep this in a separate method, so we can call it without triggering |
|
537 | + * the beforeRequest event and don't do the poll(). |
|
538 | + * |
|
539 | + * @param RequestInterface $request |
|
540 | + * @param callable $success |
|
541 | + * @param callable $error |
|
542 | + * @param int $retryCount |
|
543 | + */ |
|
544 | + protected function sendAsyncInternal(RequestInterface $request, callable $success, callable $error, $retryCount = 0) { |
|
545 | + |
|
546 | + if (!$this->curlMultiHandle) { |
|
547 | + $this->curlMultiHandle = curl_multi_init(); |
|
548 | + } |
|
549 | + $curl = curl_init(); |
|
550 | + curl_setopt_array( |
|
551 | + $curl, |
|
552 | + $this->createCurlSettingsArray($request) |
|
553 | + ); |
|
554 | + curl_multi_add_handle($this->curlMultiHandle, $curl); |
|
555 | + $this->curlMultiMap[intval($curl)] = [ |
|
556 | + $request, |
|
557 | + $success, |
|
558 | + $error, |
|
559 | + $retryCount |
|
560 | + ]; |
|
561 | + |
|
562 | + } |
|
563 | + |
|
564 | + // @codeCoverageIgnoreStart |
|
565 | + |
|
566 | + /** |
|
567 | + * Calls curl_exec |
|
568 | + * |
|
569 | + * This method exists so it can easily be overridden and mocked. |
|
570 | + * |
|
571 | + * @param resource $curlHandle |
|
572 | + * @return string |
|
573 | + */ |
|
574 | + protected function curlExec($curlHandle) { |
|
575 | + |
|
576 | + return curl_exec($curlHandle); |
|
577 | + |
|
578 | + } |
|
579 | + |
|
580 | + /** |
|
581 | + * Returns a bunch of information about a curl request. |
|
582 | + * |
|
583 | + * This method exists so it can easily be overridden and mocked. |
|
584 | + * |
|
585 | + * @param resource $curlHandle |
|
586 | + * @return array |
|
587 | + */ |
|
588 | + protected function curlStuff($curlHandle) { |
|
589 | + |
|
590 | + return [ |
|
591 | + curl_getinfo($curlHandle), |
|
592 | + curl_errno($curlHandle), |
|
593 | + curl_error($curlHandle), |
|
594 | + ]; |
|
595 | + |
|
596 | + } |
|
597 | + // @codeCoverageIgnoreEnd |
|
598 | 598 | |
599 | 599 | } |
@@ -17,40 +17,40 @@ |
||
17 | 17 | */ |
18 | 18 | class Bearer extends AbstractAuth { |
19 | 19 | |
20 | - /** |
|
21 | - * This method returns a string with an access token. |
|
22 | - * |
|
23 | - * If no token was found, this method returns null. |
|
24 | - * |
|
25 | - * @return null|string |
|
26 | - */ |
|
27 | - public function getToken() { |
|
20 | + /** |
|
21 | + * This method returns a string with an access token. |
|
22 | + * |
|
23 | + * If no token was found, this method returns null. |
|
24 | + * |
|
25 | + * @return null|string |
|
26 | + */ |
|
27 | + public function getToken() { |
|
28 | 28 | |
29 | - $auth = $this->request->getHeader('Authorization'); |
|
29 | + $auth = $this->request->getHeader('Authorization'); |
|
30 | 30 | |
31 | - if (!$auth) { |
|
32 | - return null; |
|
33 | - } |
|
31 | + if (!$auth) { |
|
32 | + return null; |
|
33 | + } |
|
34 | 34 | |
35 | - if (strtolower(substr($auth, 0, 7)) !== 'bearer ') { |
|
36 | - return null; |
|
37 | - } |
|
35 | + if (strtolower(substr($auth, 0, 7)) !== 'bearer ') { |
|
36 | + return null; |
|
37 | + } |
|
38 | 38 | |
39 | - return substr($auth, 7); |
|
39 | + return substr($auth, 7); |
|
40 | 40 | |
41 | - } |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * This method sends the needed HTTP header and statuscode (401) to force |
|
45 | - * authentication. |
|
46 | - * |
|
47 | - * @return void |
|
48 | - */ |
|
49 | - public function requireLogin() { |
|
43 | + /** |
|
44 | + * This method sends the needed HTTP header and statuscode (401) to force |
|
45 | + * authentication. |
|
46 | + * |
|
47 | + * @return void |
|
48 | + */ |
|
49 | + public function requireLogin() { |
|
50 | 50 | |
51 | - $this->response->addHeader('WWW-Authenticate', 'Bearer realm="' . $this->realm . '"'); |
|
52 | - $this->response->setStatus(401); |
|
51 | + $this->response->addHeader('WWW-Authenticate', 'Bearer realm="' . $this->realm . '"'); |
|
52 | + $this->response->setStatus(401); |
|
53 | 53 | |
54 | - } |
|
54 | + } |
|
55 | 55 | |
56 | 56 | } |
@@ -29,203 +29,203 @@ |
||
29 | 29 | */ |
30 | 30 | class Digest extends AbstractAuth { |
31 | 31 | |
32 | - /** |
|
33 | - * These constants are used in setQOP(); |
|
34 | - */ |
|
35 | - const QOP_AUTH = 1; |
|
36 | - const QOP_AUTHINT = 2; |
|
37 | - |
|
38 | - protected $nonce; |
|
39 | - protected $opaque; |
|
40 | - protected $digestParts; |
|
41 | - protected $A1; |
|
42 | - protected $qop = self::QOP_AUTH; |
|
43 | - |
|
44 | - /** |
|
45 | - * Initializes the object |
|
46 | - */ |
|
47 | - public function __construct($realm = 'SabreTooth', RequestInterface $request, ResponseInterface $response) { |
|
48 | - |
|
49 | - $this->nonce = uniqid(); |
|
50 | - $this->opaque = md5($realm); |
|
51 | - parent::__construct($realm, $request, $response); |
|
52 | - |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * Gathers all information from the headers |
|
57 | - * |
|
58 | - * This method needs to be called prior to anything else. |
|
59 | - * |
|
60 | - * @return void |
|
61 | - */ |
|
62 | - public function init() { |
|
63 | - |
|
64 | - $digest = $this->getDigest(); |
|
65 | - $this->digestParts = $this->parseDigest($digest); |
|
66 | - |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Sets the quality of protection value. |
|
71 | - * |
|
72 | - * Possible values are: |
|
73 | - * Sabre\HTTP\DigestAuth::QOP_AUTH |
|
74 | - * Sabre\HTTP\DigestAuth::QOP_AUTHINT |
|
75 | - * |
|
76 | - * Multiple values can be specified using logical OR. |
|
77 | - * |
|
78 | - * QOP_AUTHINT ensures integrity of the request body, but this is not |
|
79 | - * supported by most HTTP clients. QOP_AUTHINT also requires the entire |
|
80 | - * request body to be md5'ed, which can put strains on CPU and memory. |
|
81 | - * |
|
82 | - * @param int $qop |
|
83 | - * @return void |
|
84 | - */ |
|
85 | - public function setQOP($qop) { |
|
86 | - |
|
87 | - $this->qop = $qop; |
|
88 | - |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Validates the user. |
|
93 | - * |
|
94 | - * The A1 parameter should be md5($username . ':' . $realm . ':' . $password); |
|
95 | - * |
|
96 | - * @param string $A1 |
|
97 | - * @return bool |
|
98 | - */ |
|
99 | - public function validateA1($A1) { |
|
100 | - |
|
101 | - $this->A1 = $A1; |
|
102 | - return $this->validate(); |
|
103 | - |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Validates authentication through a password. The actual password must be provided here. |
|
108 | - * It is strongly recommended not store the password in plain-text and use validateA1 instead. |
|
109 | - * |
|
110 | - * @param string $password |
|
111 | - * @return bool |
|
112 | - */ |
|
113 | - public function validatePassword($password) { |
|
114 | - |
|
115 | - $this->A1 = md5($this->digestParts['username'] . ':' . $this->realm . ':' . $password); |
|
116 | - return $this->validate(); |
|
117 | - |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Returns the username for the request |
|
122 | - * |
|
123 | - * @return string |
|
124 | - */ |
|
125 | - public function getUsername() { |
|
126 | - |
|
127 | - return $this->digestParts['username']; |
|
128 | - |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Validates the digest challenge |
|
133 | - * |
|
134 | - * @return bool |
|
135 | - */ |
|
136 | - protected function validate() { |
|
137 | - |
|
138 | - $A2 = $this->request->getMethod() . ':' . $this->digestParts['uri']; |
|
139 | - |
|
140 | - if ($this->digestParts['qop'] == 'auth-int') { |
|
141 | - // Making sure we support this qop value |
|
142 | - if (!($this->qop & self::QOP_AUTHINT)) return false; |
|
143 | - // We need to add an md5 of the entire request body to the A2 part of the hash |
|
144 | - $body = $this->request->getBody($asString = true); |
|
145 | - $this->request->setBody($body); |
|
146 | - $A2 .= ':' . md5($body); |
|
147 | - } else { |
|
148 | - |
|
149 | - // We need to make sure we support this qop value |
|
150 | - if (!($this->qop & self::QOP_AUTH)) return false; |
|
151 | - } |
|
152 | - |
|
153 | - $A2 = md5($A2); |
|
154 | - |
|
155 | - $validResponse = md5("{$this->A1}:{$this->digestParts['nonce']}:{$this->digestParts['nc']}:{$this->digestParts['cnonce']}:{$this->digestParts['qop']}:{$A2}"); |
|
156 | - |
|
157 | - return $this->digestParts['response'] == $validResponse; |
|
158 | - |
|
159 | - |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Returns an HTTP 401 header, forcing login |
|
164 | - * |
|
165 | - * This should be called when username and password are incorrect, or not supplied at all |
|
166 | - * |
|
167 | - * @return void |
|
168 | - */ |
|
169 | - public function requireLogin() { |
|
170 | - |
|
171 | - $qop = ''; |
|
172 | - switch ($this->qop) { |
|
173 | - case self::QOP_AUTH : |
|
174 | - $qop = 'auth'; |
|
175 | - break; |
|
176 | - case self::QOP_AUTHINT : |
|
177 | - $qop = 'auth-int'; |
|
178 | - break; |
|
179 | - case self::QOP_AUTH | self::QOP_AUTHINT : |
|
180 | - $qop = 'auth,auth-int'; |
|
181 | - break; |
|
182 | - } |
|
183 | - |
|
184 | - $this->response->addHeader('WWW-Authenticate', 'Digest realm="' . $this->realm . '",qop="' . $qop . '",nonce="' . $this->nonce . '",opaque="' . $this->opaque . '"'); |
|
185 | - $this->response->setStatus(401); |
|
186 | - |
|
187 | - } |
|
188 | - |
|
189 | - |
|
190 | - /** |
|
191 | - * This method returns the full digest string. |
|
192 | - * |
|
193 | - * It should be compatibile with mod_php format and other webservers. |
|
194 | - * |
|
195 | - * If the header could not be found, null will be returned |
|
196 | - * |
|
197 | - * @return mixed |
|
198 | - */ |
|
199 | - public function getDigest() { |
|
32 | + /** |
|
33 | + * These constants are used in setQOP(); |
|
34 | + */ |
|
35 | + const QOP_AUTH = 1; |
|
36 | + const QOP_AUTHINT = 2; |
|
37 | + |
|
38 | + protected $nonce; |
|
39 | + protected $opaque; |
|
40 | + protected $digestParts; |
|
41 | + protected $A1; |
|
42 | + protected $qop = self::QOP_AUTH; |
|
43 | + |
|
44 | + /** |
|
45 | + * Initializes the object |
|
46 | + */ |
|
47 | + public function __construct($realm = 'SabreTooth', RequestInterface $request, ResponseInterface $response) { |
|
48 | + |
|
49 | + $this->nonce = uniqid(); |
|
50 | + $this->opaque = md5($realm); |
|
51 | + parent::__construct($realm, $request, $response); |
|
52 | + |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * Gathers all information from the headers |
|
57 | + * |
|
58 | + * This method needs to be called prior to anything else. |
|
59 | + * |
|
60 | + * @return void |
|
61 | + */ |
|
62 | + public function init() { |
|
63 | + |
|
64 | + $digest = $this->getDigest(); |
|
65 | + $this->digestParts = $this->parseDigest($digest); |
|
66 | + |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Sets the quality of protection value. |
|
71 | + * |
|
72 | + * Possible values are: |
|
73 | + * Sabre\HTTP\DigestAuth::QOP_AUTH |
|
74 | + * Sabre\HTTP\DigestAuth::QOP_AUTHINT |
|
75 | + * |
|
76 | + * Multiple values can be specified using logical OR. |
|
77 | + * |
|
78 | + * QOP_AUTHINT ensures integrity of the request body, but this is not |
|
79 | + * supported by most HTTP clients. QOP_AUTHINT also requires the entire |
|
80 | + * request body to be md5'ed, which can put strains on CPU and memory. |
|
81 | + * |
|
82 | + * @param int $qop |
|
83 | + * @return void |
|
84 | + */ |
|
85 | + public function setQOP($qop) { |
|
86 | + |
|
87 | + $this->qop = $qop; |
|
88 | + |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Validates the user. |
|
93 | + * |
|
94 | + * The A1 parameter should be md5($username . ':' . $realm . ':' . $password); |
|
95 | + * |
|
96 | + * @param string $A1 |
|
97 | + * @return bool |
|
98 | + */ |
|
99 | + public function validateA1($A1) { |
|
100 | + |
|
101 | + $this->A1 = $A1; |
|
102 | + return $this->validate(); |
|
103 | + |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Validates authentication through a password. The actual password must be provided here. |
|
108 | + * It is strongly recommended not store the password in plain-text and use validateA1 instead. |
|
109 | + * |
|
110 | + * @param string $password |
|
111 | + * @return bool |
|
112 | + */ |
|
113 | + public function validatePassword($password) { |
|
114 | + |
|
115 | + $this->A1 = md5($this->digestParts['username'] . ':' . $this->realm . ':' . $password); |
|
116 | + return $this->validate(); |
|
117 | + |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Returns the username for the request |
|
122 | + * |
|
123 | + * @return string |
|
124 | + */ |
|
125 | + public function getUsername() { |
|
126 | + |
|
127 | + return $this->digestParts['username']; |
|
128 | + |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Validates the digest challenge |
|
133 | + * |
|
134 | + * @return bool |
|
135 | + */ |
|
136 | + protected function validate() { |
|
137 | + |
|
138 | + $A2 = $this->request->getMethod() . ':' . $this->digestParts['uri']; |
|
139 | + |
|
140 | + if ($this->digestParts['qop'] == 'auth-int') { |
|
141 | + // Making sure we support this qop value |
|
142 | + if (!($this->qop & self::QOP_AUTHINT)) return false; |
|
143 | + // We need to add an md5 of the entire request body to the A2 part of the hash |
|
144 | + $body = $this->request->getBody($asString = true); |
|
145 | + $this->request->setBody($body); |
|
146 | + $A2 .= ':' . md5($body); |
|
147 | + } else { |
|
148 | + |
|
149 | + // We need to make sure we support this qop value |
|
150 | + if (!($this->qop & self::QOP_AUTH)) return false; |
|
151 | + } |
|
152 | + |
|
153 | + $A2 = md5($A2); |
|
154 | + |
|
155 | + $validResponse = md5("{$this->A1}:{$this->digestParts['nonce']}:{$this->digestParts['nc']}:{$this->digestParts['cnonce']}:{$this->digestParts['qop']}:{$A2}"); |
|
156 | + |
|
157 | + return $this->digestParts['response'] == $validResponse; |
|
158 | + |
|
159 | + |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Returns an HTTP 401 header, forcing login |
|
164 | + * |
|
165 | + * This should be called when username and password are incorrect, or not supplied at all |
|
166 | + * |
|
167 | + * @return void |
|
168 | + */ |
|
169 | + public function requireLogin() { |
|
170 | + |
|
171 | + $qop = ''; |
|
172 | + switch ($this->qop) { |
|
173 | + case self::QOP_AUTH : |
|
174 | + $qop = 'auth'; |
|
175 | + break; |
|
176 | + case self::QOP_AUTHINT : |
|
177 | + $qop = 'auth-int'; |
|
178 | + break; |
|
179 | + case self::QOP_AUTH | self::QOP_AUTHINT : |
|
180 | + $qop = 'auth,auth-int'; |
|
181 | + break; |
|
182 | + } |
|
183 | + |
|
184 | + $this->response->addHeader('WWW-Authenticate', 'Digest realm="' . $this->realm . '",qop="' . $qop . '",nonce="' . $this->nonce . '",opaque="' . $this->opaque . '"'); |
|
185 | + $this->response->setStatus(401); |
|
186 | + |
|
187 | + } |
|
188 | + |
|
189 | + |
|
190 | + /** |
|
191 | + * This method returns the full digest string. |
|
192 | + * |
|
193 | + * It should be compatibile with mod_php format and other webservers. |
|
194 | + * |
|
195 | + * If the header could not be found, null will be returned |
|
196 | + * |
|
197 | + * @return mixed |
|
198 | + */ |
|
199 | + public function getDigest() { |
|
200 | 200 | |
201 | - return $this->request->getHeader('Authorization'); |
|
202 | - |
|
203 | - } |
|
204 | - |
|
205 | - |
|
206 | - /** |
|
207 | - * Parses the different pieces of the digest string into an array. |
|
208 | - * |
|
209 | - * This method returns false if an incomplete digest was supplied |
|
210 | - * |
|
211 | - * @param string $digest |
|
212 | - * @return mixed |
|
213 | - */ |
|
214 | - protected function parseDigest($digest) { |
|
201 | + return $this->request->getHeader('Authorization'); |
|
202 | + |
|
203 | + } |
|
204 | + |
|
205 | + |
|
206 | + /** |
|
207 | + * Parses the different pieces of the digest string into an array. |
|
208 | + * |
|
209 | + * This method returns false if an incomplete digest was supplied |
|
210 | + * |
|
211 | + * @param string $digest |
|
212 | + * @return mixed |
|
213 | + */ |
|
214 | + protected function parseDigest($digest) { |
|
215 | 215 | |
216 | - // protect against missing data |
|
217 | - $needed_parts = ['nonce' => 1, 'nc' => 1, 'cnonce' => 1, 'qop' => 1, 'username' => 1, 'uri' => 1, 'response' => 1]; |
|
218 | - $data = []; |
|
216 | + // protect against missing data |
|
217 | + $needed_parts = ['nonce' => 1, 'nc' => 1, 'cnonce' => 1, 'qop' => 1, 'username' => 1, 'uri' => 1, 'response' => 1]; |
|
218 | + $data = []; |
|
219 | 219 | |
220 | - preg_match_all('@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@', $digest, $matches, PREG_SET_ORDER); |
|
221 | - |
|
222 | - foreach ($matches as $m) { |
|
223 | - $data[$m[1]] = $m[2] ? $m[2] : $m[3]; |
|
224 | - unset($needed_parts[$m[1]]); |
|
225 | - } |
|
220 | + preg_match_all('@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@', $digest, $matches, PREG_SET_ORDER); |
|
221 | + |
|
222 | + foreach ($matches as $m) { |
|
223 | + $data[$m[1]] = $m[2] ? $m[2] : $m[3]; |
|
224 | + unset($needed_parts[$m[1]]); |
|
225 | + } |
|
226 | 226 | |
227 | - return $needed_parts ? false : $data; |
|
227 | + return $needed_parts ? false : $data; |
|
228 | 228 | |
229 | - } |
|
229 | + } |
|
230 | 230 | |
231 | 231 | } |
@@ -15,220 +15,220 @@ |
||
15 | 15 | */ |
16 | 16 | class AWS extends AbstractAuth { |
17 | 17 | |
18 | - /** |
|
19 | - * The signature supplied by the HTTP client |
|
20 | - * |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - private $signature = null; |
|
24 | - |
|
25 | - /** |
|
26 | - * The accesskey supplied by the HTTP client |
|
27 | - * |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - private $accessKey = null; |
|
31 | - |
|
32 | - /** |
|
33 | - * An error code, if any |
|
34 | - * |
|
35 | - * This value will be filled with one of the ERR_* constants |
|
36 | - * |
|
37 | - * @var int |
|
38 | - */ |
|
39 | - public $errorCode = 0; |
|
40 | - |
|
41 | - const ERR_NOAWSHEADER = 1; |
|
42 | - const ERR_MD5CHECKSUMWRONG = 2; |
|
43 | - const ERR_INVALIDDATEFORMAT = 3; |
|
44 | - const ERR_REQUESTTIMESKEWED = 4; |
|
45 | - const ERR_INVALIDSIGNATURE = 5; |
|
46 | - |
|
47 | - /** |
|
48 | - * Gathers all information from the headers |
|
49 | - * |
|
50 | - * This method needs to be called prior to anything else. |
|
51 | - * |
|
52 | - * @return bool |
|
53 | - */ |
|
54 | - public function init() { |
|
55 | - |
|
56 | - $authHeader = $this->request->getHeader('Authorization'); |
|
57 | - $authHeader = explode(' ', $authHeader); |
|
58 | - |
|
59 | - if ($authHeader[0] != 'AWS' || !isset($authHeader[1])) { |
|
60 | - $this->errorCode = self::ERR_NOAWSHEADER; |
|
61 | - return false; |
|
62 | - } |
|
63 | - |
|
64 | - list($this->accessKey, $this->signature) = explode(':', $authHeader[1]); |
|
65 | - |
|
66 | - return true; |
|
67 | - |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Returns the username for the request |
|
72 | - * |
|
73 | - * @return string |
|
74 | - */ |
|
75 | - public function getAccessKey() { |
|
76 | - |
|
77 | - return $this->accessKey; |
|
78 | - |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Validates the signature based on the secretKey |
|
83 | - * |
|
84 | - * @param string $secretKey |
|
85 | - * @return bool |
|
86 | - */ |
|
87 | - public function validate($secretKey) { |
|
88 | - |
|
89 | - $contentMD5 = $this->request->getHeader('Content-MD5'); |
|
90 | - |
|
91 | - if ($contentMD5) { |
|
92 | - // We need to validate the integrity of the request |
|
93 | - $body = $this->request->getBody(); |
|
94 | - $this->request->setBody($body); |
|
95 | - |
|
96 | - if ($contentMD5 != base64_encode(md5($body, true))) { |
|
97 | - // content-md5 header did not match md5 signature of body |
|
98 | - $this->errorCode = self::ERR_MD5CHECKSUMWRONG; |
|
99 | - return false; |
|
100 | - } |
|
101 | - |
|
102 | - } |
|
103 | - |
|
104 | - if (!$requestDate = $this->request->getHeader('x-amz-date')) |
|
105 | - $requestDate = $this->request->getHeader('Date'); |
|
106 | - |
|
107 | - if (!$this->validateRFC2616Date($requestDate)) |
|
108 | - return false; |
|
109 | - |
|
110 | - $amzHeaders = $this->getAmzHeaders(); |
|
111 | - |
|
112 | - $signature = base64_encode( |
|
113 | - $this->hmacsha1($secretKey, |
|
114 | - $this->request->getMethod() . "\n" . |
|
115 | - $contentMD5 . "\n" . |
|
116 | - $this->request->getHeader('Content-type') . "\n" . |
|
117 | - $requestDate . "\n" . |
|
118 | - $amzHeaders . |
|
119 | - $this->request->getUrl() |
|
120 | - ) |
|
121 | - ); |
|
122 | - |
|
123 | - if ($this->signature != $signature) { |
|
124 | - |
|
125 | - $this->errorCode = self::ERR_INVALIDSIGNATURE; |
|
126 | - return false; |
|
127 | - |
|
128 | - } |
|
129 | - |
|
130 | - return true; |
|
131 | - |
|
132 | - } |
|
133 | - |
|
134 | - |
|
135 | - /** |
|
136 | - * Returns an HTTP 401 header, forcing login |
|
137 | - * |
|
138 | - * This should be called when username and password are incorrect, or not supplied at all |
|
139 | - * |
|
140 | - * @return void |
|
141 | - */ |
|
142 | - public function requireLogin() { |
|
143 | - |
|
144 | - $this->response->addHeader('WWW-Authenticate', 'AWS'); |
|
145 | - $this->response->setStatus(401); |
|
146 | - |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Makes sure the supplied value is a valid RFC2616 date. |
|
151 | - * |
|
152 | - * If we would just use strtotime to get a valid timestamp, we have no way of checking if a |
|
153 | - * user just supplied the word 'now' for the date header. |
|
154 | - * |
|
155 | - * This function also makes sure the Date header is within 15 minutes of the operating |
|
156 | - * system date, to prevent replay attacks. |
|
157 | - * |
|
158 | - * @param string $dateHeader |
|
159 | - * @return bool |
|
160 | - */ |
|
161 | - protected function validateRFC2616Date($dateHeader) { |
|
162 | - |
|
163 | - $date = Util::parseHTTPDate($dateHeader); |
|
164 | - |
|
165 | - // Unknown format |
|
166 | - if (!$date) { |
|
167 | - $this->errorCode = self::ERR_INVALIDDATEFORMAT; |
|
168 | - return false; |
|
169 | - } |
|
170 | - |
|
171 | - $min = new \DateTime('-15 minutes'); |
|
172 | - $max = new \DateTime('+15 minutes'); |
|
173 | - |
|
174 | - // We allow 15 minutes around the current date/time |
|
175 | - if ($date > $max || $date < $min) { |
|
176 | - $this->errorCode = self::ERR_REQUESTTIMESKEWED; |
|
177 | - return false; |
|
178 | - } |
|
179 | - |
|
180 | - return $date; |
|
181 | - |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * Returns a list of AMZ headers |
|
186 | - * |
|
187 | - * @return string |
|
188 | - */ |
|
189 | - protected function getAmzHeaders() { |
|
190 | - |
|
191 | - $amzHeaders = []; |
|
192 | - $headers = $this->request->getHeaders(); |
|
193 | - foreach ($headers as $headerName => $headerValue) { |
|
194 | - if (strpos(strtolower($headerName), 'x-amz-') === 0) { |
|
195 | - $amzHeaders[strtolower($headerName)] = str_replace(["\r\n"], [' '], $headerValue[0]) . "\n"; |
|
196 | - } |
|
197 | - } |
|
198 | - ksort($amzHeaders); |
|
199 | - |
|
200 | - $headerStr = ''; |
|
201 | - foreach ($amzHeaders as $h => $v) { |
|
202 | - $headerStr .= $h . ':' . $v; |
|
203 | - } |
|
204 | - |
|
205 | - return $headerStr; |
|
206 | - |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Generates an HMAC-SHA1 signature |
|
211 | - * |
|
212 | - * @param string $key |
|
213 | - * @param string $message |
|
214 | - * @return string |
|
215 | - */ |
|
216 | - private function hmacsha1($key, $message) { |
|
217 | - |
|
218 | - if (function_exists('hash_hmac')) { |
|
219 | - return hash_hmac('sha1', $message, $key, true); |
|
220 | - } |
|
221 | - |
|
222 | - $blocksize = 64; |
|
223 | - if (strlen($key) > $blocksize) { |
|
224 | - $key = pack('H*', sha1($key)); |
|
225 | - } |
|
226 | - $key = str_pad($key, $blocksize, chr(0x00)); |
|
227 | - $ipad = str_repeat(chr(0x36), $blocksize); |
|
228 | - $opad = str_repeat(chr(0x5c), $blocksize); |
|
229 | - $hmac = pack('H*', sha1(($key ^ $opad) . pack('H*', sha1(($key ^ $ipad) . $message)))); |
|
230 | - return $hmac; |
|
231 | - |
|
232 | - } |
|
18 | + /** |
|
19 | + * The signature supplied by the HTTP client |
|
20 | + * |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + private $signature = null; |
|
24 | + |
|
25 | + /** |
|
26 | + * The accesskey supplied by the HTTP client |
|
27 | + * |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + private $accessKey = null; |
|
31 | + |
|
32 | + /** |
|
33 | + * An error code, if any |
|
34 | + * |
|
35 | + * This value will be filled with one of the ERR_* constants |
|
36 | + * |
|
37 | + * @var int |
|
38 | + */ |
|
39 | + public $errorCode = 0; |
|
40 | + |
|
41 | + const ERR_NOAWSHEADER = 1; |
|
42 | + const ERR_MD5CHECKSUMWRONG = 2; |
|
43 | + const ERR_INVALIDDATEFORMAT = 3; |
|
44 | + const ERR_REQUESTTIMESKEWED = 4; |
|
45 | + const ERR_INVALIDSIGNATURE = 5; |
|
46 | + |
|
47 | + /** |
|
48 | + * Gathers all information from the headers |
|
49 | + * |
|
50 | + * This method needs to be called prior to anything else. |
|
51 | + * |
|
52 | + * @return bool |
|
53 | + */ |
|
54 | + public function init() { |
|
55 | + |
|
56 | + $authHeader = $this->request->getHeader('Authorization'); |
|
57 | + $authHeader = explode(' ', $authHeader); |
|
58 | + |
|
59 | + if ($authHeader[0] != 'AWS' || !isset($authHeader[1])) { |
|
60 | + $this->errorCode = self::ERR_NOAWSHEADER; |
|
61 | + return false; |
|
62 | + } |
|
63 | + |
|
64 | + list($this->accessKey, $this->signature) = explode(':', $authHeader[1]); |
|
65 | + |
|
66 | + return true; |
|
67 | + |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Returns the username for the request |
|
72 | + * |
|
73 | + * @return string |
|
74 | + */ |
|
75 | + public function getAccessKey() { |
|
76 | + |
|
77 | + return $this->accessKey; |
|
78 | + |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Validates the signature based on the secretKey |
|
83 | + * |
|
84 | + * @param string $secretKey |
|
85 | + * @return bool |
|
86 | + */ |
|
87 | + public function validate($secretKey) { |
|
88 | + |
|
89 | + $contentMD5 = $this->request->getHeader('Content-MD5'); |
|
90 | + |
|
91 | + if ($contentMD5) { |
|
92 | + // We need to validate the integrity of the request |
|
93 | + $body = $this->request->getBody(); |
|
94 | + $this->request->setBody($body); |
|
95 | + |
|
96 | + if ($contentMD5 != base64_encode(md5($body, true))) { |
|
97 | + // content-md5 header did not match md5 signature of body |
|
98 | + $this->errorCode = self::ERR_MD5CHECKSUMWRONG; |
|
99 | + return false; |
|
100 | + } |
|
101 | + |
|
102 | + } |
|
103 | + |
|
104 | + if (!$requestDate = $this->request->getHeader('x-amz-date')) |
|
105 | + $requestDate = $this->request->getHeader('Date'); |
|
106 | + |
|
107 | + if (!$this->validateRFC2616Date($requestDate)) |
|
108 | + return false; |
|
109 | + |
|
110 | + $amzHeaders = $this->getAmzHeaders(); |
|
111 | + |
|
112 | + $signature = base64_encode( |
|
113 | + $this->hmacsha1($secretKey, |
|
114 | + $this->request->getMethod() . "\n" . |
|
115 | + $contentMD5 . "\n" . |
|
116 | + $this->request->getHeader('Content-type') . "\n" . |
|
117 | + $requestDate . "\n" . |
|
118 | + $amzHeaders . |
|
119 | + $this->request->getUrl() |
|
120 | + ) |
|
121 | + ); |
|
122 | + |
|
123 | + if ($this->signature != $signature) { |
|
124 | + |
|
125 | + $this->errorCode = self::ERR_INVALIDSIGNATURE; |
|
126 | + return false; |
|
127 | + |
|
128 | + } |
|
129 | + |
|
130 | + return true; |
|
131 | + |
|
132 | + } |
|
133 | + |
|
134 | + |
|
135 | + /** |
|
136 | + * Returns an HTTP 401 header, forcing login |
|
137 | + * |
|
138 | + * This should be called when username and password are incorrect, or not supplied at all |
|
139 | + * |
|
140 | + * @return void |
|
141 | + */ |
|
142 | + public function requireLogin() { |
|
143 | + |
|
144 | + $this->response->addHeader('WWW-Authenticate', 'AWS'); |
|
145 | + $this->response->setStatus(401); |
|
146 | + |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Makes sure the supplied value is a valid RFC2616 date. |
|
151 | + * |
|
152 | + * If we would just use strtotime to get a valid timestamp, we have no way of checking if a |
|
153 | + * user just supplied the word 'now' for the date header. |
|
154 | + * |
|
155 | + * This function also makes sure the Date header is within 15 minutes of the operating |
|
156 | + * system date, to prevent replay attacks. |
|
157 | + * |
|
158 | + * @param string $dateHeader |
|
159 | + * @return bool |
|
160 | + */ |
|
161 | + protected function validateRFC2616Date($dateHeader) { |
|
162 | + |
|
163 | + $date = Util::parseHTTPDate($dateHeader); |
|
164 | + |
|
165 | + // Unknown format |
|
166 | + if (!$date) { |
|
167 | + $this->errorCode = self::ERR_INVALIDDATEFORMAT; |
|
168 | + return false; |
|
169 | + } |
|
170 | + |
|
171 | + $min = new \DateTime('-15 minutes'); |
|
172 | + $max = new \DateTime('+15 minutes'); |
|
173 | + |
|
174 | + // We allow 15 minutes around the current date/time |
|
175 | + if ($date > $max || $date < $min) { |
|
176 | + $this->errorCode = self::ERR_REQUESTTIMESKEWED; |
|
177 | + return false; |
|
178 | + } |
|
179 | + |
|
180 | + return $date; |
|
181 | + |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * Returns a list of AMZ headers |
|
186 | + * |
|
187 | + * @return string |
|
188 | + */ |
|
189 | + protected function getAmzHeaders() { |
|
190 | + |
|
191 | + $amzHeaders = []; |
|
192 | + $headers = $this->request->getHeaders(); |
|
193 | + foreach ($headers as $headerName => $headerValue) { |
|
194 | + if (strpos(strtolower($headerName), 'x-amz-') === 0) { |
|
195 | + $amzHeaders[strtolower($headerName)] = str_replace(["\r\n"], [' '], $headerValue[0]) . "\n"; |
|
196 | + } |
|
197 | + } |
|
198 | + ksort($amzHeaders); |
|
199 | + |
|
200 | + $headerStr = ''; |
|
201 | + foreach ($amzHeaders as $h => $v) { |
|
202 | + $headerStr .= $h . ':' . $v; |
|
203 | + } |
|
204 | + |
|
205 | + return $headerStr; |
|
206 | + |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Generates an HMAC-SHA1 signature |
|
211 | + * |
|
212 | + * @param string $key |
|
213 | + * @param string $message |
|
214 | + * @return string |
|
215 | + */ |
|
216 | + private function hmacsha1($key, $message) { |
|
217 | + |
|
218 | + if (function_exists('hash_hmac')) { |
|
219 | + return hash_hmac('sha1', $message, $key, true); |
|
220 | + } |
|
221 | + |
|
222 | + $blocksize = 64; |
|
223 | + if (strlen($key) > $blocksize) { |
|
224 | + $key = pack('H*', sha1($key)); |
|
225 | + } |
|
226 | + $key = str_pad($key, $blocksize, chr(0x00)); |
|
227 | + $ipad = str_repeat(chr(0x36), $blocksize); |
|
228 | + $opad = str_repeat(chr(0x5c), $blocksize); |
|
229 | + $hmac = pack('H*', sha1(($key ^ $opad) . pack('H*', sha1(($key ^ $ipad) . $message)))); |
|
230 | + return $hmac; |
|
231 | + |
|
232 | + } |
|
233 | 233 | |
234 | 234 | } |