Skip to content

Commit aa9987c

Browse files
committed
Throw a \RuntimeException whenever a curl request fails (sendgrid#90)
1 parent e9a04d9 commit aa9987c

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
"phpunit/phpunit": "~4.4",
2424
"squizlabs/php_codesniffer": "~2.0"
2525
},
26+
"suggest": {
27+
"composer/ca-bundle": "Including this library will ensure that a valid CA bundle is available for secure connections"
28+
},
2629
"autoload": {
2730
"psr-4": {
2831
"SendGrid\\": "lib/"

lib/Client.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,15 @@ private function createCurlOptions($method, $body = null, $headers = null)
367367
}
368368
$options[CURLOPT_HTTPHEADER] = $headers;
369369

370+
if (class_exists('\\Composer\\CaBundle\\CaBundle') && method_exists('\\Composer\\CaBundle\\CaBundle', 'getSystemCaRootBundlePath')) {
371+
$caPathOrFile = \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath();
372+
if (is_dir($caPathOrFile) || (is_link($caPathOrFile) && is_dir(readlink($caPathOrFile)))) {
373+
$options[CURLOPT_CAPATH] = $caPathOrFile;
374+
} else {
375+
$options[CURLOPT_CAINFO] = $caPathOrFile;
376+
}
377+
}
378+
370379
return $options;
371380
}
372381

@@ -415,6 +424,10 @@ private function parseResponse($channel, $content)
415424
$headerSize = curl_getinfo($channel, CURLINFO_HEADER_SIZE);
416425
$statusCode = curl_getinfo($channel, CURLINFO_HTTP_CODE);
417426

427+
if ($statusCode === 0) {
428+
throw new \RuntimeException(curl_error($channel));
429+
}
430+
418431
$responseBody = substr($content, $headerSize);
419432

420433
$responseHeaders = substr($content, 0, $headerSize);

test/unit/ClientTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ public function testCreateCurlOptionsWithBodyAndHeaders()
195195
], $result);
196196
}
197197

198+
/**
199+
* @expectedException \RuntimeException
200+
* @expectedExceptionMessageRegExp /SSL certificate problem/
201+
*/
202+
public function testMakeRequestWithUntrustedRootCert()
203+
{
204+
$client = new Client('https://untrusted-root.badssl.com/');
205+
$client->makeRequest('GET', 'https://untrusted-root.badssl.com/');
206+
}
207+
198208
/**
199209
* @param object $obj
200210
* @param string $name

0 commit comments

Comments
 (0)