blob: 51c037a2e64ee1b611a2acbe7aea51a08c14b244 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
import ipaddress
from urllib.parse import urlsplit
import dns.exception
import dns.resolver
class AddressResolutionError(Exception):
pass
def is_public_url(url: str) -> bool:
hostname = urlsplit(url).hostname
if hostname is None:
raise ValueError("No hostname was specified")
try:
answers = dns.resolver.resolve_name(hostname)
except dns.exception.DNSException as exc:
raise AddressResolutionError(
f"Could not resolve hostname {hostname!r}"
) from exc
else:
for address in answers.addresses():
if not ipaddress.ip_address(address).is_global:
return False
return True
def is_http_url(url: str) -> bool:
"""Return whether a URL is an absolute HTTP or HTTPS URL."""
try:
parsed = urlsplit(url)
except ValueError:
return False
return parsed.scheme.lower() in {"http", "https"} and parsed.hostname is not None
|