From 8c6e8833834195c9856d8a34837aea30eee94c2d Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 28 Jun 2023 09:29:26 +0900 Subject: [PATCH 1/2] feat: add request option `proxy` --- system/HTTP/CURLRequest.php | 6 ++++++ .../HTTP/CURLRequestDoNotShareOptionsTest.php | 14 ++++++++++++++ tests/system/HTTP/CURLRequestTest.php | 14 ++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/system/HTTP/CURLRequest.php b/system/HTTP/CURLRequest.php index 377f51e46006..0c8e8c455ffd 100644 --- a/system/HTTP/CURLRequest.php +++ b/system/HTTP/CURLRequest.php @@ -550,6 +550,12 @@ protected function setCURLOptions(array $curlOptions = [], array $config = []) } } + // Proxy + if (isset($config['proxy'])) { + $curlOptions[CURLOPT_HTTPPROXYTUNNEL] = true; + $curlOptions[CURLOPT_PROXY] = $config['proxy']; + } + // Debug if ($config['debug']) { $curlOptions[CURLOPT_VERBOSE] = 1; diff --git a/tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php b/tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php index 7dcc47f40082..20e5435d7cad 100644 --- a/tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php +++ b/tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php @@ -559,6 +559,20 @@ public function testSSLWithBadKey() ]); } + public function testProxyuOption() + { + $this->request->request('get', 'http://example.com', [ + 'proxy' => 'http://localhost:3128', + ]); + + $options = $this->request->curl_options; + + $this->assertArrayHasKey(CURLOPT_PROXY, $options); + $this->assertSame('http://localhost:3128', $options[CURLOPT_PROXY]); + $this->assertArrayHasKey(CURLOPT_HTTPPROXYTUNNEL, $options); + $this->assertTrue($options[CURLOPT_HTTPPROXYTUNNEL]); + } + public function testDebugOptionTrue() { $this->request->request('get', 'http://example.com', [ diff --git a/tests/system/HTTP/CURLRequestTest.php b/tests/system/HTTP/CURLRequestTest.php index 92c43cc58461..6f99779e7c71 100644 --- a/tests/system/HTTP/CURLRequestTest.php +++ b/tests/system/HTTP/CURLRequestTest.php @@ -542,6 +542,20 @@ public function testSSLWithBadKey() ]); } + public function testProxyuOption() + { + $this->request->request('get', 'http://example.com', [ + 'proxy' => 'http://localhost:3128', + ]); + + $options = $this->request->curl_options; + + $this->assertArrayHasKey(CURLOPT_PROXY, $options); + $this->assertSame('http://localhost:3128', $options[CURLOPT_PROXY]); + $this->assertArrayHasKey(CURLOPT_HTTPPROXYTUNNEL, $options); + $this->assertTrue($options[CURLOPT_HTTPPROXYTUNNEL]); + } + public function testDebugOptionTrue() { $this->request->request('get', 'http://example.com', [ From 35d7d2941eaa9b1db1f23c6fd8191f39fc5f0dee Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 28 Jun 2023 09:30:05 +0900 Subject: [PATCH 2/2] docs: update docs --- user_guide_src/source/changelogs/v4.4.0.rst | 3 ++- user_guide_src/source/libraries/curlrequest.rst | 11 +++++++++++ user_guide_src/source/libraries/curlrequest/035.php | 7 +++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 user_guide_src/source/libraries/curlrequest/035.php diff --git a/user_guide_src/source/changelogs/v4.4.0.rst b/user_guide_src/source/changelogs/v4.4.0.rst index e340d33cc667..1668a181cef8 100644 --- a/user_guide_src/source/changelogs/v4.4.0.rst +++ b/user_guide_src/source/changelogs/v4.4.0.rst @@ -95,9 +95,10 @@ Libraries - **Validation:** Added ``Validation::getValidated()`` method that gets the actual validated data. See :ref:`validation-getting-validated-data` for details. - **Images:** The option ``$quality`` can now be used to compress WebP images. - - **Uploaded Files:** Added ``UploadedFiles::getClientPath()`` method that returns the value of the `full_path` index of the file if it was uploaded via directory upload. +- **CURLRequest:** Added a request option ``proxy``. See + :ref:`CURLRequest Class `. Helpers and Functions ===================== diff --git a/user_guide_src/source/libraries/curlrequest.rst b/user_guide_src/source/libraries/curlrequest.rst index 18afd51f771c..b2ecbcdf5f0c 100644 --- a/user_guide_src/source/libraries/curlrequest.rst +++ b/user_guide_src/source/libraries/curlrequest.rst @@ -296,6 +296,17 @@ has been disabled. Any files that you want to send must be passed as instances o ``form_params`` for ``application/x-www-form-urlencoded`` requests, and ``multipart`` for ``multipart/form-data`` requests. +.. _curlrequest-request-options-proxy: + +proxy +===== + +.. versionadded:: 4.4.0 + +You can set a proxy by passing an associative array as the ``proxy`` option: + +.. literalinclude:: curlrequest/035.php + query ===== diff --git a/user_guide_src/source/libraries/curlrequest/035.php b/user_guide_src/source/libraries/curlrequest/035.php new file mode 100644 index 000000000000..a729f9f3be22 --- /dev/null +++ b/user_guide_src/source/libraries/curlrequest/035.php @@ -0,0 +1,7 @@ +request( + 'GET', + 'http://example.com', + ['proxy' => 'http://localhost:3128'] +);