Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contextually check for a valid transport #101

Merged
merged 6 commits into from
Feb 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this serialize to the same thing regardless of array order? Haven't tested, but I suspect no, so might need a sort.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely. Nice catch.


// 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;
}
}