/
Client.php
300 lines (265 loc) · 8.07 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php defined('SYSPATH') OR die('No direct script access.');
/**
* Request Client. Processes a [Request] and handles [HTTP_Caching] if
* available. Will usually return a [Response] object as a result of the
* request unless an unexpected error occurs.
*
* @package Kohana
* @category Base
* @author Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
* @since 3.1.0
*/
abstract class Kohana_Request_Client {
/**
* @var Cache Caching library for request caching
*/
protected $_cache;
/**
* @var bool Should redirects be followed?
*/
protected $_follow = FALSE;
/**
* @var array Headers to preserve when following a redirect
*/
protected $_follow_headers = array('Authorization');
/**
* @var bool Follow 302 redirect with original request method?
*/
protected $_strict_redirect = TRUE;
/**
* @var array Callbacks to use when response contains given headers
*/
protected $_header_callbacks = array(
'Location' => 'Request_Client::on_header_location'
);
/**
* Creates a new `Request_Client` object,
* allows for dependency injection.
*
* @param array $params Params
*/
public function __construct(array $params = array())
{
foreach ($params as $key => $value)
{
if (method_exists($this, $key))
{
$this->$key($value);
}
}
}
/**
* Processes the request, executing the controller action that handles this
* request, determined by the [Route].
*
* 1. Before the controller action is called, the [Controller::before] method
* will be called.
* 2. Next the controller action will be called.
* 3. After the controller action is called, the [Controller::after] method
* will be called.
*
* By default, the output from the controller is captured and returned, and
* no headers are sent.
*
* $request->execute();
*
* @param Request $request
* @param Response $response
* @return Response
* @throws Kohana_Exception
* @uses [Kohana::$profiling]
* @uses [Profiler]
*/
public function execute(Request $request)
{
$orig_response = $response = Response::factory();
if (($cache = $this->cache()) instanceof HTTP_Cache)
return $cache->execute($this, $request, $response);
$response = $this->execute_request($request, $response);
// Execute response callbacks
foreach ($this->header_callbacks() as $header => $callback)
{
if ($response->headers($header))
{
$cb_result = call_user_func($callback, $request, $response, $this);
if ($cb_result instanceof Request)
{
// If the callback returns a request, automatically assign client params
$client = $cb_result->client();
$client->cache($this->cache());
$client->follow($this->follow());
$client->follow_headers($this->follow_headers());
$client->header_callbacks($this->header_callbacks());
// Execute the request
$response = $cb_result->execute();
}
elseif ($cb_result instanceof Response)
{
// Assign the returned response
$response = $cb_result;
}
// If the callback has created a new response, do not process any further
if ($response !== $orig_response)
break;
}
}
return $response;
}
/**
* Processes the request passed to it and returns the response from
* the URI resource identified.
*
* This method must be implemented by all clients.
*
* @param Request $request request to execute by client
* @param Response $response
* @return Response
* @since 3.2.0
*/
abstract public function execute_request(Request $request, Response $response);
/**
* Getter and setter for the internal caching engine,
* used to cache responses if available and valid.
*
* @param HTTP_Cache $cache engine to use for caching
* @return HTTP_Cache
* @return Request_Client
*/
public function cache(HTTP_Cache $cache = NULL)
{
if ($cache === NULL)
return $this->_cache;
$this->_cache = $cache;
return $this;
}
/**
* Getter and setter for the follow redirects
* setting.
*
* @param bool $follow Boolean indicating if redirects should be followed
* @return bool
* @return Request_Client
*/
public function follow($follow = NULL)
{
if ($follow === NULL)
return $this->_follow;
$this->_follow = $follow;
return $this;
}
/**
* Getter and setter for the follow redirects
* headers array.
*
* @param array $follow_headers Array of headers to be re-used when following a Location header
* @return array
* @return Request_Client
*/
public function follow_headers($follow_headers = NULL)
{
if ($follow_headers === NULL)
return $this->_follow_headers;
$this->_follow_headers = $follow_headers;
return $this;
}
/**
* Getter and setter for the strict redirects setting
*
* [!!] HTTP/1.1 specifies that a 302 redirect should be followed using the
* original request method. However, the vast majority of clients and servers
* get this wrong, with 302 widely used for 'POST - 302 redirect - GET' patterns.
* By default, Kohana's client is fully compliant with the HTTP spec. Some
* non-compliant third party sites may require that strict_redirect is set
* FALSE to force the client to switch to GET following a 302 response.
*
* @param bool $strict_redirect Boolean indicating if 302 redirects should be followed with the original method
* @return Request_Client
*/
public function strict_redirect($strict_redirect = NULL)
{
if ($strict_redirect === NULL)
return $this->_strict_redirect;
$this->_strict_redirect = $strict_redirect;
return $this;
}
/**
* Getter and setter for the header callbacks array.
*
* Accepts an array with HTTP response headers as keys and a PHP callback
* function as values. These callbacks will be triggered if a response contains
* the given header and can either issue a subsequent request or manipulate
* the response as required.
*
* By default, the [Request_Client::on_header_location] callback is assigned
* to the Location header to support automatic redirect following.
*
* $client->header_callbacks(array(
* 'Location' => 'Request_Client::on_header_location',
* 'WWW-Authenticate' => function($request, $response, $client) {return $new_response;},
* );
*
* @param array $header_callbacks Array of callbacks to trigger on presence of given headers
* @return Request_Client
*/
public function header_callbacks($header_callbacks = NULL)
{
if ($header_callbacks === NULL)
return $this->_header_callbacks;
$this->_header_callbacks = $header_callbacks;
return $this;
}
/**
* The default handler for following redirects, triggered by the presence of
* a Location header in the response.
*
* The client's follow property must be set TRUE and the HTTP response status
* one of 201, 301, 302, 303 or 307 for the redirect to be followed.
*
* @param Request $request
* @param Response $response
* @param Request_Client $client
*/
public static function on_header_location(Request $request, Response $response, Request_Client $client)
{
// Do we need to follow a Location header ?
if ($client->follow() AND in_array($response->status(), array(201, 301, 302, 303, 307)))
{
// Figure out which method to use for the follow request
switch ($response->status())
{
default:
case 301:
case 307:
$follow_method = $request->method();
break;
case 201:
case 303:
$follow_method = Request::GET;
break;
case 302:
// Cater for sites with broken HTTP redirect implementations
if ($client->strict_redirect())
{
$follow_method = $request->method();
}
else
{
$follow_method = Request::GET;
}
break;
}
// Prepare the additional request
$follow_request = Request::factory($response->headers('Location'))
->method($follow_method)
->headers(Arr::extract($request->headers(), $client->follow_headers()));
if ($follow_method !== Request::GET)
{
$follow_request->body($request->body());
}
return $follow_request;
}
return NULL;
}
}