Skip to content

Commit

Permalink
Support HTTP proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
werrolf committed Oct 26, 2018
1 parent cc2b75a commit cea222d
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/Component/Http/CurlTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@

class CurlTransport implements TransportInterface
{
/** @var ProxySettings|null */
protected $proxySettings;

/**
* @param ProxySettings|array|null $proxySettings
*/
public function __construct($proxySettings = null)
{
if ($proxySettings && is_array($proxySettings)) {
$proxySettings = ProxySettings::fromArray($proxySettings);
}
$this->proxySettings = $proxySettings ?: null;
}

public function getUrl($url, RequestOptions $options = null)
{
$options = $options ?: RequestOptions::makeDefaults();
Expand Down Expand Up @@ -56,6 +70,19 @@ protected function initCurl(RequestOptions $options, $url, $method = 'GET')
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_FAILONERROR => 0,
);
if ($this->proxySettings) {
$curlOpts += array(
CURLOPT_PROXY => $this->proxySettings->host,
CURLOPT_PROXYPORT => $this->proxySettings->port,
);
if ($this->proxySettings->user) {
$curlOpts += array(CURLOPT_PROXYUSERPWD, implode(':', array(
curl_escape($ch, $this->proxySettings->user),
curl_escape($ch, $this->proxySettings->password),
)));
}
}

curl_setopt_array($ch, $curlOpts);
return $ch;
}
Expand Down
66 changes: 66 additions & 0 deletions lib/Component/Http/ProxySettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php


namespace Wheregroup\SymfonyExt\TransportBundle\Component\Http;


/**
* POD object to encapsulate http / https proxy settings
*
* @todo: support proxy user / password
*/
class ProxySettings
{
public $host;
public $port;
public $user;
public $password;

public static $defaultValues = array(
'port' => 8080,
'user' => null,
'password' => null,
);

public function __construct($host, $port, $user = null, $password = null)
{
if ((!$user) xor (!$password)) {
throw new \InvalidArgumentException("User and password must be either both empty or both non-empty");
}

$this->host = $host;
$this->port = intval($port);
$this->user = $user ?: null;
$this->password = $password ?: null;
}

/**
* Construct instance from array. Expects array keys 'host' (mandatory), 'port' (optional; default 8080),
* 'user' (optional), 'password' (required if user is set, required empty if user is not set).
*
* @param array $values
* @return static
*/
public static function fromArray($values)
{
$validKeys = array(
'host',
'port',
'user',
'password',
);
$invalidKeys = array_diff(array_keys($values), $validKeys);
if ($invalidKeys) {
throw new \InvalidArgumentException("Unsupported keys " . implode(', ', $invalidKeys));
}
if (!$values || !array_key_exists('host', $values)) {
throw new \InvalidArgumentException("Missing / empty mandatory value for host");
}
if (empty($values['port'])) {
$values['port'] = static::$defaultValues['port'];
}
// add user + password null defaults
$values += static::$defaultValues;
return new static($values['host'], $values['port'], $values['user'], $values['password']);
}
}

0 comments on commit cea222d

Please sign in to comment.