dns-over-https/lib/Nameserver.php

61 lines
1.5 KiB
PHP
Raw Permalink Normal View History

2019-06-10 19:32:28 +02:00
<?php
namespace Amp\DoH;
2019-06-11 16:40:22 +02:00
use Amp\Dns\ConfigException;
2019-06-10 19:32:28 +02:00
final class Nameserver
{
const RFC8484_GET = 0;
const RFC8484_POST = 1;
const GOOGLE_JSON = 2;
private $type;
private $uri;
2019-06-10 21:04:10 +02:00
private $host;
2019-06-10 19:32:28 +02:00
private $headers = [];
public function __construct(string $uri, int $type = self::RFC8484_POST, array $headers = [])
{
2019-06-11 20:07:46 +02:00
if (\parse_url($uri, PHP_URL_SCHEME) !== 'https') {
2019-06-11 16:40:22 +02:00
throw new ConfigException('Did not provide a valid HTTPS url!');
}
2019-06-11 20:07:46 +02:00
if (!\in_array($type, [self::RFC8484_GET, self::RFC8484_POST, self::GOOGLE_JSON])) {
2019-06-11 16:40:22 +02:00
throw new ConfigException('Invalid nameserver type provided!');
}
2019-06-10 19:32:28 +02:00
$this->uri = $uri;
$this->type = $type;
$this->headers = $headers;
2019-06-11 20:07:46 +02:00
$this->host = \parse_url($uri, PHP_URL_HOST);
2019-06-10 19:32:28 +02:00
}
public function getUri(): string
{
return $this->uri;
}
2019-06-10 21:04:10 +02:00
public function getHost(): string
{
return $this->host;
}
2019-06-10 19:32:28 +02:00
public function getHeaders(): array
{
return $this->headers;
}
public function getType(): int
{
return $this->type;
}
public function __toString(): string
{
2019-06-10 21:04:10 +02:00
return $this->uri;
2019-06-11 19:07:51 +02:00
/*
2019-06-10 19:32:28 +02:00
switch ($this->type) {
case self::RFC8484_GET:
return "{$this->uri} RFC 8484 GET";
case self::RFC8484_POST:
return "{$this->uri} RFC 8484 POST";
case self::GOOGLE_JSON:
return "{$this->uri} google JSON";
2019-06-11 19:07:51 +02:00
}*/
2019-06-10 19:32:28 +02:00
}
}