Skip to content

Commit

Permalink
Revert "Refactored the remote class"
Browse files Browse the repository at this point in the history
This reverts commit 3deb9d5.

Conflicts:

	classes/kohana/remote.php
  • Loading branch information
zombor committed Nov 6, 2010
1 parent 5afe7c0 commit c77d29f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
78 changes: 47 additions & 31 deletions classes/kohana/remote.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,45 @@ class Kohana_Remote {
CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; Kohana v3.0 +http://kohanaframework.org/)',
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 5,
CURLOPT_HEADER => FALSE
CURLOPT_HEADERFUNCTION => array('Remote', '_parse_headers'),
CURLOPT_HEADER => FALSE,
);

/**
* Returns the output of a remote URL. Any [curl option](http://php.net/curl_setopt)
* may be used.
* @var array Headers from the request
*/
protected static $_headers = array();

/**
* Parses the returned headers from the remote
* request
*
* @param resource the curl resource
* @param string the full header string
* @return int
*/
protected static function _parse_headers($remote, $header)
{
$headers = array();

if (preg_match_all('/(\w[^\s:]*):[ ]*([^\r\n]*(?:\r\n[ \t][^\r\n]*)*)/', $header, $matches))
{
foreach ($matches[0] as $key => $value)
$headers[$matches[1][$key]] = $matches[2][$key];
}

// If there are headers to apply
if ($headers)
{
Remote::$_headers += $headers;
}

return strlen($header);
}

/**
* Returns the output of a remote URL.
* Any [curl option](http://php.net/curl_setopt) may be used.
*
* // Do a simple GET request
* $data = Remote::get($url);
Expand All @@ -35,12 +68,12 @@ class Kohana_Remote {
* @param string remote URL
* @param array curl options
* @return [Kohana_Response]
* @throws [Kohana_Remote_Exception]
* @throws [Kohana_Exception]
*/
public static function get($url, array $options = NULL)
{
if ( ! extension_loaded('curl'))
throw new Kohana_Remote_Exception('This method requires the CURL php extension to be loaded, check CURL documentation: :url', array(':url' => 'http://uk3.php.net/manual/en/book.curl.php'));
// Reset the headers
Remote::$_headers = array();

if ($options === NULL)
{
Expand All @@ -53,38 +86,17 @@ public static function get($url, array $options = NULL)
$options = $options + Remote::$default_options;
}

// The transfer must always be returned
$options[CURLOPT_RETURNTRANSFER] = TRUE;
$options[CURLOPT_USERAGENT] = 'Mozilla/5.0 (compatible; Kohana v'.Kohana::VERSION.' +http://kohanaphp.com/)';

$headers = array();

// Parse the headers
$options[CURLOPT_HEADERFUNCTION] = function ($remote, $header) use ( & $headers) {
$parsed_headers = array();

if (preg_match_all('/(\w[^\s:]*):[ ]*([^\r\n]*(?:\r\n[ \t][^\r\n]*)*)/', $header, $matches))
{
foreach ($matches[0] as $key => $value)
$parsed_headers[$matches[1][$key]] = $matches[2][$key];
}

// If there are headers to apply
if ($parsed_headers)
{
$headers += $parsed_headers;
}

return strlen($header);
};

// Open a new remote connection
$remote = curl_init($url);

// Set connection options
if ( ! curl_setopt_array($remote, $options))
{
curl_close($remote);
throw new Kohana_Remote_Exception('Failed to set CURL options, check CURL documentation: :url',
throw new Kohana_Exception('Failed to set CURL options, check CURL documentation: :url',
array(':url' => 'http://php.net/curl_setopt_array'));
}

Expand All @@ -95,9 +107,13 @@ public static function get($url, array $options = NULL)
$code = curl_getinfo($remote, CURLINFO_HTTP_CODE);

if ($code AND $code < 200 OR $code > 299)
{
$error = $response;
}
elseif ($response === FALSE)
{
$error = curl_error($remote);
}

// Close the connection
curl_close($remote);
Expand All @@ -110,7 +126,7 @@ public static function get($url, array $options = NULL)

return new Response(array(
'status' => $code,
'headers' => $headers,
'headers' => Remote::$_headers,
'body' => $response,
));
}
Expand Down Expand Up @@ -179,4 +195,4 @@ public static function status($url, array $http_headers = array())
return $response;
}

} // End remote
} // End remote
3 changes: 0 additions & 3 deletions classes/kohana/remote/exception.php

This file was deleted.

0 comments on commit c77d29f

Please sign in to comment.