Skip to content

Commit

Permalink
Improve handling of curl proxification. (#15)
Browse files Browse the repository at this point in the history
- Set all curl proxy options in setCurlProxyOptions.
- Remove setting CURLOPT_FOLLOWLOCATION based on whether SOCKS 5 proxy is enabled (can still be set with curloptions in defaults as necessary).
- Add support for SOCK5_HOSTNAME proxy.
  • Loading branch information
EreMaijala committed Jul 1, 2020
1 parent a29cd54 commit 1813d6d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
20 changes: 13 additions & 7 deletions src/VuFindHttp/HttpService.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ protected function setCurlProxyOptions($adapter)
$adapter
->setCurlOption(CURLOPT_PROXYPORT, $this->proxyConfig['proxy_port']);
}
// HTTP is default, so handle only the SOCKS 5 proxy types
switch ($this->proxyConfig['proxy_type'] ?? '') {
case 'socks5':
$adapter->setCurlOption(CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
break;
case 'socks5_hostname':
$adapter->setCurlOption(CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
break;
}
}

/**
Expand All @@ -132,7 +141,7 @@ protected function hasCurlAdapterAsDefault()
* Returns the client given as argument with appropriate proxy setup.
*
* @param \Laminas\Http\Client $client HTTP client
* @param array $options ZF2 ProxyAdapter options
* @param array $options Laminas ProxyAdapter options
*
* @return \Laminas\Http\Client
*/
Expand All @@ -141,15 +150,12 @@ public function proxify(\Laminas\Http\Client $client, array $options = [])
if ($this->proxyConfig) {
$host = $client->getUri()->getHost();
if (!$this->isLocal($host)) {
$proxyType = $this->proxyConfig['proxy_type'] ?? 'default';
$proxyType = $this->proxyConfig['proxy_type'] ?? 'default';

if ($proxyType == 'socks5') {
if (in_array($proxyType, ['socks5', 'socks5_hostname'])) {
$adapter = new \Laminas\Http\Client\Adapter\Curl();
// Apply standard proxy options for Curl adapter:
// Apply proxy options for Curl adapter:
$this->setCurlProxyOptions($adapter);
// Add SOCKS5 settings:
$adapter->setCurlOption(CURLOPT_FOLLOWLOCATION, true);
$adapter->setCurlOption(CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
$client->setAdapter($adapter);
} elseif ($proxyType == 'default') {
// If the user has manually configured a Curl adapter,
Expand Down
48 changes: 46 additions & 2 deletions tests/unit-tests/src/VuFindTest/HttpServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function testGetAppendsHeaders()
),
$this->equalTo('1.1'),
$this->equalTo(
['Host' => 'example.tld', 'Connection' => 'close', 'Accept-Encoding' => 'gzip, deflate','User-Agent' => 'Laminas\Http\Client', 'Content-Type' => 'application/json', 'Accept' => 'application/json']
['Host' => 'example.tld', 'Connection' => 'close', 'Accept-Encoding' => 'gzip, deflate','User-Agent' => 'Laminas_Http_Client', 'Content-Type' => 'application/json', 'Accept' => 'application/json']
)
);
$service->setDefaultAdapter($adapter);
Expand All @@ -185,7 +185,7 @@ public function testPostAppendsHeaders()
),
$this->equalTo('1.1'),
$this->equalTo(
['Host' => 'example.tld', 'Connection' => 'close', 'Accept-Encoding' => 'gzip, deflate','User-Agent' => 'Laminas\Http\Client', 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Content-Length' => '5']
['Host' => 'example.tld', 'Connection' => 'close', 'Accept-Encoding' => 'gzip, deflate','User-Agent' => 'Laminas_Http_Client', 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Content-Length' => '5']
)
);
$service->setDefaultAdapter($adapter);
Expand Down Expand Up @@ -305,6 +305,7 @@ public function testProxifySocks5()
$this->assertEquals(
CURLPROXY_SOCKS5, $config['curloptions'][CURLOPT_PROXYTYPE]
);
$this->assertNotContains(CURLOPT_FOLLOWLOCATION, $config['curloptions']);
}

/**
Expand Down Expand Up @@ -444,6 +445,49 @@ public function testTimeout()
$this->assertEquals($clientConfig['timeout'], 67);
}

/**
* Test defaults with Curl adapter.
*
* @return void
*/
public function testCurlAdapterDefaults()
{
$service = new Service(
[],
['adapter' => '\Laminas\Http\Client\Adapter\Curl']
);
$client = $service->createClient('http://example.tld:8080');
$adapter = $client->getAdapter();
$this->assertInstanceOf('Laminas\Http\Client\Adapter\Curl', $adapter);
$config = $adapter->getConfig();
$this->assertTrue(empty($config['curloptions']));
}

/**
* Test defaults with Curl adapter.
*
* @return void
*/
public function testCurlAdapterFollowLocation()
{
$service = new Service(
[],
[
'adapter' => '\Laminas\Http\Client\Adapter\Curl',
'curloptions' => [
52 => '1'
]
]
);
$client = $service->createClient('http://example.tld:8080');
$adapter = $client->getAdapter();
$this->assertInstanceOf('Laminas\Http\Client\Adapter\Curl', $adapter);
$config = $adapter->getConfig();
$this->assertEquals(
'1', $config['curloptions'][CURLOPT_FOLLOWLOCATION] ?? null
);
}

/**
* Return protected or private property.
*
Expand Down

0 comments on commit 1813d6d

Please sign in to comment.