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

Fixes new proxy code #60

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/Buzz/Client/AbstractClient.php
Expand Up @@ -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)
{
Expand Down Expand Up @@ -50,4 +52,14 @@ public function getVerifyPeer()
{
return $this->verifyPeer;
}

public function setProxy(Util\Url $proxy)
{
$this->proxy = $proxy;
}

public function getProxy()
{
return $this->proxy;
}
}
40 changes: 39 additions & 1 deletion lib/Buzz/Client/AbstractStream.php
Expand Up @@ -15,7 +15,7 @@ abstract class AbstractStream extends AbstractClient
*/
public function getStreamContextArray(Message\Request $request)
{
return array(
$options = array(
'http' => array(
// values from the request
'method' => $request->getMethod(),
Expand All @@ -32,5 +32,43 @@ public function getStreamContextArray(Message\Request $request)
'verify_peer' => $this->getVerifyPeer(),
),
);

if ($proxyOption = $this->getProxyOption()) {
$options['http']['proxy'] = $proxyOption;
$options['http']['request_fulluri'] = true;
}

return $options;
}

protected function getProxyOption()
{
if (!$this->getProxy()) {
return null;
}

if ($user = $this->getProxy()->getUser()) {
if ($password = $this->getProxy()->getPassword()) {
$proxyAuth = $user.':'.$password.'@';
} else {
$proxyAuth = $user.'@';
}
} else {
$proxyAuth = '';
}

// We actually need the port otherwise stream croaks. Had initially
// tried proxy->getHost() and it did not work since it removed the
// port it matched the scheme default.
$proxy = $this->proxy->getHostname() . ':' . $this->proxy->getPort();

if ('htttps' === $this->proxy->getScheme()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo here =)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice. :)

if (!extension_loaded('openssl')) {
throw new \RuntimeException('You must enable the openssl extension to use a proxy over https');
}
return 'ssl://'.$proxyAuth.$proxy;
}

return 'tcp://'.$proxyAuth.$proxy;
}
}
15 changes: 15 additions & 0 deletions lib/Buzz/Client/Curl.php
Expand Up @@ -140,6 +140,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()) {
// We actually need the port otherwise curl croaks. Had initially
// tried proxy->getHost() and it did not work since it removed the
// port it matched the scheme default.
curl_setopt($curl, CURLOPT_PROXY, $proxy->getHost().':'.$proxy->getPort());
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you tried: curl_setopt($curl, CURLOPT_PROXYPORT, $proxy->getPort(); ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, yeah, that would make more sense. Also, I should have done getHostname() instead of getHost() anyway. Thanks.

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_PROXYUSERPWD, null);
}
}

public function __destruct()
Expand Down
11 changes: 11 additions & 0 deletions lib/Buzz/Util/Url.php
Expand Up @@ -29,6 +29,17 @@ public function __construct($url)
$components['path'] = '/'.$path;
}

if (isset($components['scheme']) && !isset($components['port'])) {
switch($components['scheme']) {
case 'http':
$components['port'] = 80;
break;
case 'https':
$components['port'] = 443;
break;
}
}

$this->url = $url;
$this->components = $components;
}
Expand Down