Skip to content

Commit

Permalink
Merge pull request #101 from ozh/transport_with_context
Browse files Browse the repository at this point in the history
Contextually check for a valid transport
  • Loading branch information
rmccue committed Feb 11, 2014
2 parents 5052e11 + 82651ea commit a689479
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
34 changes: 20 additions & 14 deletions library/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ class Requests {
*
* Use {@see get_transport()} instead
*
* @var string|null
* @var array
*/
public static $transport = null;
public static $transport = array();

/**
* This is a static class, do not instantiate it
Expand Down Expand Up @@ -147,11 +147,16 @@ public static function add_transport($transport) {
* @throws Requests_Exception If no valid transport is found (`notransport`)
* @return Requests_Transport
*/
protected static function get_transport() {
protected static function get_transport($capabilities = array()) {
// Caching code, don't bother testing coverage
// @codeCoverageIgnoreStart
if (self::$transport !== null) {
return new self::$transport();
// array of capabilities as a string to be used as an array key
ksort($capabilities);
$cap_string = serialize($capabilities);

// Don't search for a transport if it's already been done for these $capabilities
if (isset(self::$transport[$cap_string]) && self::$transport[$cap_string] !== null) {
return new self::$transport[$cap_string]();
}
// @codeCoverageIgnoreEnd

Expand All @@ -167,17 +172,17 @@ protected static function get_transport() {
if (!class_exists($class))
continue;

$result = call_user_func(array($class, 'test'));
$result = call_user_func(array($class, 'test'), $capabilities);
if ($result) {
self::$transport = $class;
self::$transport[$cap_string] = $class;
break;
}
}
if (self::$transport === null) {
if (self::$transport[$cap_string] === null) {
throw new Requests_Exception('No working transports found', 'notransport', self::$transports);
}

return new self::$transport();
return new self::$transport[$cap_string]();
}

/**#@+
Expand Down Expand Up @@ -312,9 +317,10 @@ public static function request($url, $headers = array(), $data = array(), $type
if (is_string($options['transport'])) {
$transport = new $transport();
}
}
else {
$transport = self::get_transport();
} else {
$need_ssl = (0 === stripos($url, 'https://'));
$capabilities = array('ssl' => $need_ssl);
$transport = self::get_transport($capabilities);
}
$response = $transport->request($url, $headers, $data, $options);

Expand Down Expand Up @@ -479,7 +485,7 @@ protected static function get_default_options($multirequest = false) {
* @return array $options
*/
protected static function set_defaults(&$url, &$headers, &$data, &$type, &$options) {
if (!preg_match('/^http(s)?:\/\//i', $url)) {
if (!preg_match('/^http(s)?:\/\//i', $url, $matches)) {
throw new Requests_Exception('Only HTTP requests are handled.', 'nonhttp', $url);
}

Expand Down
14 changes: 12 additions & 2 deletions library/Requests/Transport/cURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,17 @@ protected static function format_get($url, $data) {
* @codeCoverageIgnore
* @return boolean True if the transport is valid, false otherwise.
*/
public static function test() {
return (function_exists('curl_init') && function_exists('curl_exec'));
public static function test($capabilities = array()) {
if (!function_exists('curl_init') && !function_exists('curl_exec'))
return false;

// If needed, check that our installed curl version supports SSL
if (isset( $capabilities['ssl'] ) && $capabilities['ssl']) {
$curl_version = curl_version();
if (!(CURL_VERSION_SSL & $curl_version['features']))
return false;
}

return true;
}
}
13 changes: 11 additions & 2 deletions library/Requests/Transport/fsockopen.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,16 @@ public function verify_certificate_from_context($host, $context) {
* @codeCoverageIgnore
* @return boolean True if the transport is valid, false otherwise.
*/
public static function test() {
return function_exists('fsockopen');
public static function test($capabilities = array()) {
if (!function_exists('fsockopen'))
return false;

// If needed, check that streams support SSL
if (isset( $capabilities['ssl'] ) && $capabilities['ssl']) {
if (!extension_loaded('openssl') || !function_exists('openssl_x509_parse'))
return false;
}

return true;
}
}

0 comments on commit a689479

Please sign in to comment.