Skip to content

Commit

Permalink
Add support for custom curl options.
Browse files Browse the repository at this point in the history
Add ability for users to send arbitrary curl options. The provided curl
options will overwrite any options set by the adapter.
  • Loading branch information
markstory committed Jun 11, 2018
1 parent 66dc497 commit 5e08596
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/Http/Client/Adapter/Curl.php
Expand Up @@ -19,8 +19,12 @@
use Cake\Http\Exception\HttpException;

/**
* Implements sending Cake\Http\Client\Request
* via ext/curl.
* Implements sending Cake\Http\Client\Request via ext/curl.
*
* In addition to the standard options documented in Cake\Http\Client,
* this adapter supports all available curl options. Additional curl options
* can be set via the `curl` option key when making requests or configuring
* a client.
*/
class Curl implements AdapterInterface
{
Expand Down Expand Up @@ -96,7 +100,6 @@ public function buildOptions(Request $request, array $options)
'timeout' => CURLOPT_TIMEOUT,
'ssl_verify_peer' => CURLOPT_SSL_VERIFYPEER,
'ssl_verify_host' => CURLOPT_SSL_VERIFYHOST,
'ssl_verify_status' => CURLOPT_SSL_VERIFYSTATUS,
'ssl_cafile' => CURLOPT_CAINFO,
'ssl_local_cert' => CURLOPT_SSLCERT,
'ssl_passphrase' => CURLOPT_SSLCERTPASSWD,
Expand All @@ -109,6 +112,12 @@ public function buildOptions(Request $request, array $options)
if (isset($options['proxy']['proxy'])) {
$out[CURLOPT_PROXY] = $options['proxy']['proxy'];
}
if (isset($options['curl']) && is_array($options['curl'])) {
// Can't use array_merge() because keys will be re-ordered.
foreach ($options['curl'] as $key => $value) {
$out[$key] = $value;
}
}

return $out;
}
Expand Down
30 changes: 30 additions & 0 deletions tests/TestCase/Http/Client/Adapter/CurlTest.php
Expand Up @@ -272,4 +272,34 @@ public function testBuildOptionsProxy()
];
$this->assertSame($expected, $result);
}

/**
* Test converting client options into curl ones.
*
* @return void
*/
public function testBuildOptionsCurlOptions()
{
$options = [
'curl' => [
CURLOPT_USERAGENT => 'Super-secret'
]
];
$request = new Request('http://localhost/things', 'GET');
$result = $this->curl->buildOptions($request, $options);
$expected = [
CURLOPT_URL => 'http://localhost/things',
CURLOPT_HTTP_VERSION => '1.1',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => [
'Connection: close',
'User-Agent: CakePHP',
],
CURLOPT_HTTPGET => true,
CURLOPT_CAINFO => $this->caFile,
CURLOPT_USERAGENT => 'Super-secret'
];
$this->assertSame($expected, $result);
}
}

0 comments on commit 5e08596

Please sign in to comment.