Fediverse: Add support for missing host-meta document

This commit is contained in:
Valentin Lorentz 2022-12-28 14:54:48 +01:00
parent 21a2ace7a1
commit 501770e544
2 changed files with 17 additions and 7 deletions

View File

@ -61,10 +61,6 @@ class ProtocolError(ActivityPubError):
pass
class HostmetaError(ProtocolError):
pass
class ActivityPubProtocolError(ActivityPubError):
pass
@ -118,7 +114,7 @@ def convert_exceptions(to_class, msg="", from_none=False):
@sandbox
def _get_webfinger_url(hostname):
with convert_exceptions(HostmetaError):
try:
doc = ET.fromstring(
web.getUrlContent("https://%s/.well-known/host-meta" % hostname)
)
@ -126,8 +122,9 @@ def _get_webfinger_url(hostname):
for link in doc.iter(XRD_URI + "Link"):
if link.attrib["rel"] == "lrdd":
return link.attrib["template"]
return "https://%s/.well-known/webfinger?resource={uri}"
except web.Error:
# Fall back to the default Webfinger URL
return "https://%s/.well-known/webfinger?resource={uri}" % hostname
def has_webfinger_support(hostname):

View File

@ -255,6 +255,19 @@ class NetworklessFediverseTestCase(BaseFediverseTestCase):
"\x02someuser\x02 (@someuser@example.org): My Biography",
)
def testProfileNoHostmeta(self):
expected_requests = [
(HOSTMETA_URL, utils.web.Error("blah")),
(WEBFINGER_URL, WEBFINGER_DATA),
(ACTOR_URL, ACTOR_DATA),
]
with self.mockRequests(expected_requests):
self.assertResponse(
"profile @someuser@example.org",
"\x02someuser\x02 (@someuser@example.org): My Biography",
)
def testProfileSnarfer(self):
with self.mockWebfingerSupport("not called"), self.mockRequests([]):
self.assertSnarfNoResponse("aaa @nonexistinguser@example.org bbb")