Created
February 10, 2012 16:59
-
-
Save kiall/1790885 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/classes/kohana/request.php b/classes/kohana/request.php | |
index 7eb5716..c34393f 100644 | |
--- a/classes/kohana/request.php | |
+++ b/classes/kohana/request.php | |
@@ -45,15 +45,15 @@ class Kohana_Request implements HTTP_Request { | |
* If $cache parameter is set, the response for the request will attempt to | |
* be retrieved from the cache. | |
* | |
- * @param string $uri URI of the request | |
- * @param Cache $cache | |
- * @param array $injected_routes an array of routes to use, for testing | |
+ * @param string $uri URI of the request | |
+ * @param array $client_params An array of params to pass to the request client | |
+ * @param array $injected_routes An array of routes to use, for testing | |
* @return void | |
* @throws Request_Exception | |
* @uses Route::all | |
* @uses Route::matches | |
*/ | |
- public static function factory($uri = TRUE, HTTP_Cache $cache = NULL, $injected_routes = array()) | |
+ public static function factory($uri = TRUE, $client_params = array(), $injected_routes = array()) | |
{ | |
// If this is the initial request | |
if ( ! Request::$initial) | |
@@ -156,7 +156,7 @@ class Kohana_Request implements HTTP_Request { | |
} | |
// Create the instance singleton | |
- Request::$initial = $request = new Request($uri, $cache, $injected_routes); | |
+ Request::$initial = $request = new Request($uri, $client_params, $injected_routes); | |
// Store global GET and POST data in the initial request only | |
$request->protocol($protocol) | |
@@ -200,7 +200,7 @@ class Kohana_Request implements HTTP_Request { | |
} | |
else | |
{ | |
- $request = new Request($uri, $cache, $injected_routes); | |
+ $request = new Request($uri, $client_params, $injected_routes); | |
} | |
return $request; | |
@@ -641,15 +641,15 @@ class Kohana_Request implements HTTP_Request { | |
* If $cache parameter is set, the response for the request will attempt to | |
* be retrieved from the cache. | |
* | |
- * @param string $uri URI of the request | |
- * @param HTTP_Cache $cache | |
- * @param array $injected_routes an array of routes to use, for testing | |
+ * @param string $uri URI of the request | |
+ * @param array $client_params Array of params to pass to the request client | |
+ * @param array $injected_routes An array of routes to use, for testing | |
* @return void | |
* @throws Request_Exception | |
* @uses Route::all | |
* @uses Route::matches | |
*/ | |
- public function __construct($uri, HTTP_Cache $cache = NULL, $injected_routes = array()) | |
+ public function __construct($uri, $client_params = array(), $injected_routes = array()) | |
{ | |
// Initialise the header | |
$this->_header = new HTTP_Header(array()); | |
@@ -726,7 +726,7 @@ class Kohana_Request implements HTTP_Request { | |
$this->_params = $params; | |
// Apply the client | |
- $this->_client = new Request_Client_Internal(array('cache' => $cache)); | |
+ $this->_client = new Request_Client_Internal($client_params); | |
} | |
else | |
{ | |
@@ -746,7 +746,7 @@ class Kohana_Request implements HTTP_Request { | |
$this->_external = TRUE; | |
// Setup the client | |
- $this->_client = Request_Client_External::factory(array('cache' => $cache)); | |
+ $this->_client = Request_Client_External::factory($client_params); | |
} | |
} | |
diff --git a/classes/kohana/request/client.php b/classes/kohana/request/client.php | |
index 9e73b1c..b38c4f8 100644 | |
--- a/classes/kohana/request/client.php | |
+++ b/classes/kohana/request/client.php | |
@@ -19,6 +19,16 @@ abstract class Kohana_Request_Client { | |
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'); | |
+ | |
+ /** | |
* Creates a new `Request_Client` object, | |
* allows for dependency injection. | |
* | |
@@ -64,7 +74,37 @@ abstract class Kohana_Request_Client { | |
if ($this->_cache instanceof HTTP_Cache) | |
return $this->_cache->execute($this, $request, $response); | |
- return $this->execute_request($request, $response); | |
+ $response = $this->execute_request($request, $response); | |
+ | |
+ // Do we need to follow a Location header ? | |
+ if ($this->_follow AND in_array($response->status(), array(201, 301, 302, 303, 307)) | |
+ AND $response->headers('Location')) | |
+ { | |
+ // Figure out which method to use for the follow request | |
+ switch ($response->status()) | |
+ { | |
+ default: | |
+ case 301: | |
+ case 302: | |
+ case 307: | |
+ $follow_method = $request->method(); | |
+ break; | |
+ case 201: | |
+ case 303: | |
+ $follow_method = Request::GET; | |
+ break; | |
+ } | |
+ | |
+ // Prepare the additional request | |
+ $follow_request = Request::factory($response->headers('Location')) | |
+ ->method($follow_method) | |
+ ->headers(Arr::extract($request->headers(), $this->_follow_headers)); | |
+ | |
+ // Execute the additional request | |
+ $response = $follow_request->execute(); | |
+ } | |
+ | |
+ return $response; | |
} | |
/** | |
@@ -96,4 +136,22 @@ abstract class Kohana_Request_Client { | |
$this->_cache = $cache; | |
return $this; | |
} | |
+ | |
+ /** | |
+ * Getter and setter for the follow redirects | |
+ * settubg. | |
+ * | |
+ * @param bool $follow Boolean indicating if redirects should be followed | |
+ * @return bool | |
+ * @return Request_Client | |
+ */ | |
+ public function follow($follow = FALSE) | |
+ {echo "WTF"; die; | |
+ if ($follow === NULL) | |
+ return $this->_follow; | |
+ | |
+ $this->_follow = $follow; | |
+ | |
+ return $this; | |
+ } | |
} | |
\ No newline at end of file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment