aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/net.py
diff options
context:
space:
mode:
Diffstat (limited to 'webmentions_ssg/net.py')
-rw-r--r--webmentions_ssg/net.py16
1 files changed, 15 insertions, 1 deletions
diff --git a/webmentions_ssg/net.py b/webmentions_ssg/net.py
index 7c29edc..02c7f2b 100644
--- a/webmentions_ssg/net.py
+++ b/webmentions_ssg/net.py
@@ -7,7 +7,7 @@
import ipaddress
import socket
import time
-from collections.abc import Iterable
+from collections.abc import Iterable, Mapping
from contextlib import ExitStack
from http import HTTPStatus
from urllib.parse import urlsplit
@@ -252,6 +252,20 @@ def public_addresses(
return tuple(str(address) for address in addresses)
+def hostname_of(url: str) -> str | None:
+ """Return a normalized URL hostname, or None for malformed URLs."""
+ try:
+ hostname = urlsplit(url).hostname
+ except (TypeError, ValueError):
+ return None
+ return hostname.rstrip(".") if hostname is not None else None
+
+
+def content_type_of(headers: Mapping[str, str]) -> str:
+ """Return a normalized media type without Content-Type parameters."""
+ return headers.get("Content-Type", "").partition(";")[0].strip().lower()
+
+
def is_http_url(url: str) -> bool:
"""Check whether a URL is an absolute HTTP or HTTPS URL.