-
Notifications
You must be signed in to change notification settings - Fork 498
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
Changes from 3 commits
189e5e2
2695b1a
d561c61
d2cb625
1de4657
82651ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
||
|
@@ -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](); | ||
} | ||
|
||
/**#@+ | ||
|
@@ -277,6 +282,10 @@ public static function patch($url, $headers, $data = array(), $options = array() | |
* transport object. Defaults to the first working transport from | ||
* {@see getTransport()} | ||
* (string|Requests_Transport, default: {@see getTransport()}) | ||
* - `needs_ssl`: whether the chosen transport will need to be able to perform HTTPS | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need to be an option, or can we just autodetect? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't sure there would not be a case scenario where someone would want to force that... Couldn't think of one myself, but I'm just the plain regular HTTP query guy, so I made that an option. What do you think? Just autodetect? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Let's go with just autodetection for now; if someone needs access to this, they're probably doing it wrong. Worst case scenario, we can always add APIs, but we can't remove them as easily. :) |
||
* requests. If unset, this option automatically sets to True when the requested | ||
* URL starts with 'https://' | ||
* (boolean, default: false if $url is HTTP, true if $url is HTTPS) | ||
* - `hooks`: Hooks handler. | ||
* (Requests_Hooker, default: new Requests_Hooks()) | ||
* - `verify`: Should we verify SSL certificates? Allows passing in a custom | ||
|
@@ -314,7 +323,8 @@ public static function request($url, $headers = array(), $data = array(), $type | |
} | ||
} | ||
else { | ||
$transport = self::get_transport(); | ||
$capabilities = array('ssl' => $options['needs_ssl']); | ||
$transport = self::get_transport($capabilities); | ||
} | ||
$response = $transport->request($url, $headers, $data, $options); | ||
|
||
|
@@ -479,8 +489,12 @@ 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); | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to kill the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you misunderstood the test: if no preg_match, throw, else: populate options['need_ssl'] as needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, but we don't need the else, since there's two options: preg_match doesn't match, so we throw (and exit from the function), or we continue the function. There's no need for the else, since the rest of the function is basically an else. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, that. I thought it would make it clearer where that $matches comes from. Removing it :) |
||
if (empty($options['needs_ssl'])) { | ||
$options['needs_ssl'] = ($matches[0] == 'https://'); | ||
} | ||
} | ||
|
||
if (empty($options['hooks'])) { | ||
|
There was a problem hiding this comment.
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
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely. Nice catch.