Permalink
Browse files

added proxy support

  • Loading branch information...
1 parent 0804524 commit 414def624a630b62ec92c811f17151bcb62f7185 @kriswallsmith committed Mar 26, 2012
@@ -3,13 +3,15 @@
namespace Buzz\Client;
use Buzz\Message;
+use Buzz\Util;
abstract class AbstractClient
{
protected $ignoreErrors = true;
protected $maxRedirects = 5;
protected $timeout = 5;
protected $verifyPeer = true;
+ protected $proxy;
public function setIgnoreErrors($ignoreErrors)
{
@@ -50,4 +52,14 @@ public function getVerifyPeer()
{
return $this->verifyPeer;
}
+
+ public function setProxy($proxy)
+ {
+ $this->proxy = $proxy instanceof Util\Url ? $proxy : new Util\Url($proxy);
+ }
+
+ public function getProxy()
+ {
+ return $this->proxy;
+ }
}
@@ -3,6 +3,7 @@
namespace Buzz\Client;
use Buzz\Message;
+use Buzz\Util;
abstract class AbstractStream extends AbstractClient
{
@@ -15,7 +16,7 @@
*/
public function getStreamContextArray(Message\Request $request)
{
- return array(
+ $options = array(
'http' => array(
// values from the request
'method' => $request->getMethod(),
@@ -32,5 +33,41 @@ public function getStreamContextArray(Message\Request $request)
'verify_peer' => $this->getVerifyPeer(),
),
);
+
+ if ($proxy = $this->getProxy()) {
+ $options['http']['proxy'] = self::getProxyOption($proxy);
+ $options['http']['request_fulluri'] = true;
+ }
+
+ return $options;
+ }
+
+ /**
+ * Converts an URL to a proxy string option.
+ *
+ * @param Url $url The proxy URL
+ *
+ * @return string The proxy option
+ */
+ static private function getProxyOption(Util\Url $url)
+ {
+ static $schemeMap = array(
+ 'http' => 'tcp',
+ 'https' => 'ssl',
+ );
+
+ $scheme = $url->getScheme();
+ $proxy = isset($schemeMap[$scheme]) ? $schemeMap[$scheme] : $scheme;
+ $proxy .= '://';
+
+ if ($user = $url->getUser()) {
+ $proxy .= $user.':'.$url->getPassword().'@';
+ }
+
+ $proxy .= $url->getHostname();
+ $proxy .= ':';
+ $proxy .= $url->getPort();
+
+ return $proxy;
}
}
View
@@ -141,6 +141,21 @@ protected function prepare(Message\Request $request, Message\Response $response,
curl_setopt($curl, CURLOPT_MAXREDIRS, $this->maxRedirects);
curl_setopt($curl, CURLOPT_FAILONERROR, !$this->ignoreErrors);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->verifyPeer);
+
+ if ($proxy = $this->getProxy()) {
+ curl_setopt($curl, CURLOPT_PROXY, $proxy->getHostname());
+ curl_setopt($curl, CURLOPT_PROXYPORT, $proxy->getPort());
+
+ if ($user = $proxy->getUser()) {
+ curl_setopt($curl, CURLOPT_PROXYUSERPWD, $user.':'.$proxy->getPassword());
+ } else {
+ curl_setopt($curl, CURLOPT_PROXYUSERPWD, null);
+ }
+ } else {
+ curl_setopt($curl, CURLOPT_PROXY, null);
+ curl_setopt($curl, CURLOPT_PROXYPORT, null);
+ curl_setopt($curl, CURLOPT_PROXYUSERPWD, null);
+ }
}
public function __destruct()
View
@@ -4,6 +4,11 @@
class Url
{
+ static private $defaultPorts = array(
+ 'http' => 80,
+ 'https' => 443,
+ );
+
private $url;
private $components;
@@ -35,6 +40,16 @@ public function __construct($url)
}
}
+ // default scheme
+ if (!isset($components['scheme'])) {
+ $components['scheme'] = 'http';
+ }
+
+ // default port
+ if (!isset($components['port']) && isset(self::$defaultPorts[$components['scheme']])) {
+ $components['port'] = self::$defaultPorts[$components['scheme']];
+ }
+
$this->url = $url;
$this->components = $components;
}
@@ -86,19 +101,13 @@ public function getFragment()
*/
public function getHost()
{
- // default ports
- static $map = array(
- 'http' => 80,
- 'https' => 443,
- );
-
if ($hostname = $this->parseUrl('host')) {
$host = $scheme = $this->parseUrl('scheme', 'http');
$host .= '://';
$host .= $hostname;
$port = $this->parseUrl('port');
- if ($port && (!isset($map[$scheme]) || $map[$scheme] != $port)) {
+ if ($port && (!isset(self::$defaultPorts[$scheme]) || self::$defaultPorts[$scheme] != $port)) {
$host .= ':'.$port;
}
View
@@ -9,6 +9,7 @@
<php>
<!-- <server name="TEST_SERVER" value="http://localhost/buzz/test/server.php" /> -->
+ <!-- <server name="TEST_PROXY" value="127.0.0.1:8080" /> -->
</php>
<filter>
@@ -137,10 +137,22 @@ public function testPlus($client)
public function provideClient()
{
- return array(
+ $clients = array(
array(new Curl()),
array(new FileGetContents()),
);
+
+ if (isset($_SERVER['TEST_PROXY'])) {
+ $client = new Curl();
+ $client->setProxy($_SERVER['TEST_PROXY']);
+ $clients[] = array($client);
+
+ $client = new FileGetContents();
+ $client->setProxy($_SERVER['TEST_PROXY']);
+ $clients[] = array($client);
+ }
+
+ return $clients;
}
public function provideClientAndMethod()

0 comments on commit 414def6

Please sign in to comment.