Skip to content

Commit

Permalink
Handle invalid RDAP response
Browse files Browse the repository at this point in the history
  • Loading branch information
mbardelmeijer committed Oct 30, 2023
1 parent 4eb6212 commit b8034d9
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,14 @@ $domain = Rdap::domain('google.com'); // returns an instance of `Spatie\Rdap\Res

If you pass a non-existing domain, then the `domain()` function will return `null`.

## Handling timeouts
## Handling errors

Sometimes RDAP is slow in responding. If a response isn't returned in a timely manner, a `Spatie\Rdap\Exceptions\RdapRequestTimedOut` exception will be thrown.

Sometimes RDAP servers return with an invalid response. If that happens, a `Spatie\Rdap\Exceptions\InvalidRdapResponse` exception will be thrown.

Both exceptions implement `Spatie\Rdap\Exceptions\RdapException`. You can catch that exception to handle both cases.

## Working with RDAP DNS

For each TLD a specific server is used to respond to domain queries. Such a server is called a "DNS server". The official list of all RDAP DNS server is available as JSON [here](https://data.iana.org/rdap/dns.json).
Expand Down
16 changes: 16 additions & 0 deletions src/Exceptions/InvalidRdapResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Spatie\Rdap\Exceptions;

use Exception;
use Illuminate\Http\Client\ConnectionException;

class InvalidRdapResponse extends Exception implements RdapException
{
public static function make(string $domain): self
{
return new static(
"The request to RDAP to get domain data for `{$domain}` returned a invalid response.",
);
}
}
8 changes: 8 additions & 0 deletions src/Exceptions/RdapException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Spatie\Rdap\Exceptions;

interface RdapException
{

}
4 changes: 2 additions & 2 deletions src/Exceptions/RdapRequestTimedOut.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use Exception;
use Illuminate\Http\Client\ConnectionException;

class RdapRequestTimedOut extends Exception
class RdapRequestTimedOut extends Exception implements RdapException
{
public static function make(string $domain, ConnectionException $exception)
public static function make(string $domain, ConnectionException $exception): self
{
return new static(
"The request to RDAP to get domain data for `{$domain}` timed out.",
Expand Down
7 changes: 7 additions & 0 deletions src/Rdap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;
use Spatie\Rdap\Exceptions\InvalidRdapResponse;
use Spatie\Rdap\Exceptions\RdapRequestTimedOut;
use Spatie\Rdap\Responses\DomainResponse;

Expand Down Expand Up @@ -47,6 +48,12 @@ public function domain(
throw RdapRequestTimedOut::make($domain, $exception);
}

if (empty($response)) {
// Some misconfigured RDAP servers might return (invalid) HTML responses.
// The JSON conversion will return an empty array in that case.
throw InvalidRdapResponse::make($domain);
}

return new DomainResponse($response);
}

Expand Down
17 changes: 17 additions & 0 deletions tests/RdapTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use Illuminate\Support\Facades\Http;
use Spatie\Rdap\CouldNotFindRdapServer;
use Spatie\Rdap\Exceptions\InvalidRdapResponse;
use Spatie\Rdap\Exceptions\RdapRequestTimedOut;
use Spatie\Rdap\Rdap;
use Spatie\Rdap\Responses\DomainResponse;
Expand Down Expand Up @@ -52,3 +54,18 @@
expect($timedOut)->toBeInstanceOf(RdapRequestTimedOut::class);
}
});

it('throws a invalid response exception if rdap servers returns invalid response', function () {
Http::fake([
'rdap.nic.io/*' => Http::response('invalid response'),
]);

try {
$result = $this->rdap->domain('invalid-domain-response.com');

// sometimes it returns null
expect($result)->toBeNull();
} catch (InvalidRdapResponse $invalidResponse) {
expect($invalidResponse)->toBeInstanceOf(InvalidRdapResponse::class);
}
});

0 comments on commit b8034d9

Please sign in to comment.