Browse Source

Add support for custom curl options.

Add ability for users to send arbitrary curl options. The provided curl
options will overwrite any options set by the adapter.
Mark Story 7 years ago
parent
commit
5e08596b72
2 changed files with 42 additions and 3 deletions
  1. 12 3
      src/Http/Client/Adapter/Curl.php
  2. 30 0
      tests/TestCase/Http/Client/Adapter/CurlTest.php

+ 12 - 3
src/Http/Client/Adapter/Curl.php

@@ -19,8 +19,12 @@ use Cake\Http\Client\Response;
 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
 {
@@ -96,7 +100,6 @@ class Curl implements AdapterInterface
             '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,
@@ -109,6 +112,12 @@ class Curl implements AdapterInterface
         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;
     }

+ 30 - 0
tests/TestCase/Http/Client/Adapter/CurlTest.php

@@ -272,4 +272,34 @@ class CurlTest extends TestCase
         ];
         $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);
+    }
 }