Skip to content

Commit

Permalink
Merge pull request #635 from rollbar/fixed/480-curl-ip-resolve-for-ipv6
Browse files Browse the repository at this point in the history
Fixed #480 IP could not resolve IPv6
  • Loading branch information
danielmorell authored Mar 1, 2024
2 parents 9befe9c + 8e03ea4 commit 2f9eccb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Senders/CurlSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class CurlSender implements SenderInterface
private $maxBatchRequests = 75;
private $batchRequests = array();
private $inflightRequests = array();
private $ipResolve = CURL_IPRESOLVE_V4;

public function __construct($opts)
{
Expand Down Expand Up @@ -54,6 +55,10 @@ public function __construct($opts)
if (array_key_exists('ca_cert_path', $opts)) {
$this->caCertPath = $opts['ca_cert_path'];
}
if (array_key_exists('ip_resolve', $opts)
&& in_array($opts['ip_resolve'], [CURL_IPRESOLVE_V4, CURL_IPRESOLVE_V6, CURL_IPRESOLVE_WHATEVER])) {
$this->ipResolve = $opts['ip_resolve'];
}
}

public function getEndpoint()
Expand Down Expand Up @@ -151,7 +156,7 @@ public function setCurlOptions(CurlHandle $handle, EncodedPayload $payload, stri
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($handle, CURLOPT_HTTPHEADER, array('X-Rollbar-Access-Token: ' . $accessToken));
curl_setopt($handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($handle, CURLOPT_IPRESOLVE, $this->ipResolve);

if (!is_null($this->caCertPath)) {
curl_setopt($handle, CURLOPT_CAINFO, $this->caCertPath);
Expand Down
43 changes: 43 additions & 0 deletions tests/CurlSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Rollbar;

use ReflectionClass;
use ReflectionException;
use Rollbar\Payload\Level;
use Rollbar\Senders\CurlSender;

class CurlSenderTest extends BaseRollbarTest
{
Expand All @@ -26,4 +29,44 @@ public function testCurlError(): void
)
);
}

/**
* This test will fail if the {@see CurlSender::$ipResolve} property is renamed or removed.
*/
public function testIPResolve(): void
{
$sender = new CurlSender([
'ip_resolve' => CURL_IPRESOLVE_V4,
]);
self::assertSame(CURL_IPRESOLVE_V4, self::getPrivateProperty($sender, 'ipResolve'));

$sender = new CurlSender([]);
self::assertSame(CURL_IPRESOLVE_V4, self::getPrivateProperty($sender, 'ipResolve'));

$sender = new CurlSender([
'ip_resolve' => CURL_IPRESOLVE_V6,
]);
self::assertSame(CURL_IPRESOLVE_V6, self::getPrivateProperty($sender, 'ipResolve'));

$sender = new CurlSender([
'ip_resolve' => CURL_IPRESOLVE_WHATEVER,
]);
self::assertSame(CURL_IPRESOLVE_WHATEVER, self::getPrivateProperty($sender, 'ipResolve'));
}

/**
* Returns the value of a private property of an object.
*
* @param object $object The object from which to get the private property.
* @param string $property The name of the private property to get.
* @return mixed
* @throws ReflectionException If the object or property does not exist.
*/
private static function getPrivateProperty(object $object, string $property): mixed
{
$reflection = new ReflectionClass($object);
$property = $reflection->getProperty($property);
$property->setAccessible(true);
return $property->getValue($object);
}
}

0 comments on commit 2f9eccb

Please sign in to comment.