From 174b78800c02f2684b20ebb2f13e520e2c1f8640 Mon Sep 17 00:00:00 2001 From: Deon George Date: Wed, 15 Dec 2010 12:26:53 +1100 Subject: [PATCH] Upstream patch: Remote updates to support sessions --- .../kohana/system/classes/kohana/remote.php | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/includes/kohana/system/classes/kohana/remote.php b/includes/kohana/system/classes/kohana/remote.php index 0a2c604b..c1d5ce41 100644 --- a/includes/kohana/system/classes/kohana/remote.php +++ b/includes/kohana/system/classes/kohana/remote.php @@ -9,6 +9,8 @@ * @license http://kohanaphp.com/license */ class Kohana_Remote { + // Store our curl session resource + static $remote = NULL; // Default curl options public static $default_options = array @@ -36,7 +38,7 @@ class Kohana_Remote { * @return string * @throws Kohana_Exception */ - public static function get($url, array $options = NULL) + public static function get($url, array $options = NULL, $session = FALSE) { if ($options === NULL) { @@ -52,8 +54,20 @@ class Kohana_Remote { // The transfer must always be returned $options[CURLOPT_RETURNTRANSFER] = TRUE; + // If we have an old session, lets auto close it. + if (! $session AND Remote::$remote) + Remote::clear(); + // Open a new remote connection - $remote = curl_init($url); + if (! $session OR is_null(Remote::$remote)) + { + $remote = Remote::$remote = curl_init($url); + } + else + { + $remote = Remote::$remote; + curl_setopt($remote,CURLOPT_URL,$url); + } // Set connection options if ( ! curl_setopt_array($remote, $options)) @@ -77,9 +91,6 @@ class Kohana_Remote { $error = curl_error($remote); } - // Close the connection - curl_close($remote); - if (isset($error)) { throw new Kohana_Exception('Error fetching remote :url [ status :code ] :error', @@ -89,6 +100,17 @@ class Kohana_Remote { return $response; } + /** + * Clear an existing curl session + */ + public static function clear() + { + if (is_resource(Remote::$remote)) + curl_close(Remote::$remote); + + Remote::$remote = NULL; + } + /** * Returns the status code (200, 500, etc) for a URL. *