From 74f74c38d5cd32a69f12318c9e40efcd257a08da Mon Sep 17 00:00:00 2001 From: Colin O'Dell Date: Tue, 2 Oct 2018 09:52:23 -0400 Subject: [PATCH 1/3] Throw a \RuntimeException whenever a curl request fails (#90) --- lib/Client.php | 8 ++++++++ test/unit/ClientTest.php | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/Client.php b/lib/Client.php index ec732b9..73031fe 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -415,6 +415,10 @@ private function parseResponse($channel, $content) $headerSize = curl_getinfo($channel, CURLINFO_HEADER_SIZE); $statusCode = curl_getinfo($channel, CURLINFO_HTTP_CODE); + if ($statusCode === 0) { + throw new \RuntimeException(curl_error($channel)); + } + $responseBody = substr($content, $headerSize); $responseHeaders = substr($content, 0, $headerSize); @@ -453,6 +457,8 @@ private function retryRequest(array $responseHeaders, $method, $url, $body, $hea * @param bool $retryOnLimit should retry if rate limit is reach? * * @return Response object + * + * @throws \RuntimeException */ public function makeRequest($method, $url, $body = null, $headers = null, $retryOnLimit = false) { @@ -481,6 +487,8 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry * @param array $requests * * @return Response[] + * + * @throws \RuntimeException */ public function makeAllRequests(array $requests = []) { diff --git a/test/unit/ClientTest.php b/test/unit/ClientTest.php index 538b1b0..fb379a9 100644 --- a/test/unit/ClientTest.php +++ b/test/unit/ClientTest.php @@ -195,6 +195,16 @@ public function testCreateCurlOptionsWithBodyAndHeaders() ], $result); } + /** + * @expectedException \RuntimeException + * @expectedExceptionMessageRegExp /SSL certificate problem/i + */ + public function testMakeRequestWithUntrustedRootCert() + { + $client = new Client('https://untrusted-root.badssl.com/'); + $client->makeRequest('GET', 'https://untrusted-root.badssl.com/'); + } + /** * @param object $obj * @param string $name From 5513444bc5999174036cfbe18a3c898a3489f9b5 Mon Sep 17 00:00:00 2001 From: Colin O'Dell Date: Tue, 2 Oct 2018 10:10:22 -0400 Subject: [PATCH 2/3] Revert "Throw a \RuntimeException whenever a curl request fails (#90)" This reverts commit 74f74c38d5cd32a69f12318c9e40efcd257a08da. --- lib/Client.php | 8 -------- test/unit/ClientTest.php | 10 ---------- 2 files changed, 18 deletions(-) diff --git a/lib/Client.php b/lib/Client.php index 73031fe..ec732b9 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -415,10 +415,6 @@ private function parseResponse($channel, $content) $headerSize = curl_getinfo($channel, CURLINFO_HEADER_SIZE); $statusCode = curl_getinfo($channel, CURLINFO_HTTP_CODE); - if ($statusCode === 0) { - throw new \RuntimeException(curl_error($channel)); - } - $responseBody = substr($content, $headerSize); $responseHeaders = substr($content, 0, $headerSize); @@ -457,8 +453,6 @@ private function retryRequest(array $responseHeaders, $method, $url, $body, $hea * @param bool $retryOnLimit should retry if rate limit is reach? * * @return Response object - * - * @throws \RuntimeException */ public function makeRequest($method, $url, $body = null, $headers = null, $retryOnLimit = false) { @@ -487,8 +481,6 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry * @param array $requests * * @return Response[] - * - * @throws \RuntimeException */ public function makeAllRequests(array $requests = []) { diff --git a/test/unit/ClientTest.php b/test/unit/ClientTest.php index fb379a9..538b1b0 100644 --- a/test/unit/ClientTest.php +++ b/test/unit/ClientTest.php @@ -195,16 +195,6 @@ public function testCreateCurlOptionsWithBodyAndHeaders() ], $result); } - /** - * @expectedException \RuntimeException - * @expectedExceptionMessageRegExp /SSL certificate problem/i - */ - public function testMakeRequestWithUntrustedRootCert() - { - $client = new Client('https://untrusted-root.badssl.com/'); - $client->makeRequest('GET', 'https://untrusted-root.badssl.com/'); - } - /** * @param object $obj * @param string $name From a39209d7adfd2915346cc70955fce93eab2ee0a4 Mon Sep 17 00:00:00 2001 From: Colin O'Dell Date: Tue, 2 Oct 2018 10:11:14 -0400 Subject: [PATCH 3/3] Auto-locate the system's CA bundle or fall back to Mozilla's (#90) --- composer.json | 3 +++ lib/Client.php | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/composer.json b/composer.json index 5c0a3af..1b3101d 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,9 @@ "phpunit/phpunit": "~4.4", "squizlabs/php_codesniffer": "~2.0" }, + "suggest": { + "composer/ca-bundle": "Including this library will ensure that a valid CA bundle is available for secure connections" + }, "autoload": { "psr-4": { "SendGrid\\": "lib/" diff --git a/lib/Client.php b/lib/Client.php index ec732b9..4480d92 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -367,6 +367,15 @@ private function createCurlOptions($method, $body = null, $headers = null) } $options[CURLOPT_HTTPHEADER] = $headers; + if (class_exists('\\Composer\\CaBundle\\CaBundle') && method_exists('\\Composer\\CaBundle\\CaBundle', 'getSystemCaRootBundlePath')) { + $caPathOrFile = \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath(); + if (is_dir($caPathOrFile) || (is_link($caPathOrFile) && is_dir(readlink($caPathOrFile)))) { + $options[CURLOPT_CAPATH] = $caPathOrFile; + } else { + $options[CURLOPT_CAINFO] = $caPathOrFile; + } + } + return $options; }