Set User-Agent and Accept for outbound requests (#293)

This commit is contained in:
Michael Manfre 2022-12-27 18:50:39 -05:00 committed by GitHub
parent b523d34c53
commit ca40a0d150
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 24 deletions

View file

@ -52,7 +52,11 @@ async def get_remote_file(
""" """
Download a URL and return the File and content-type. Download a URL and return the File and content-type.
""" """
async with httpx.AsyncClient() as client: headers = {
"User-Agent": settings.TAKAHE_USER_AGENT,
}
async with httpx.AsyncClient(headers=headers) as client:
async with client.stream("GET", url, timeout=timeout) as stream: async with client.stream("GET", url, timeout=timeout) as stream:
allow_download = max_size is None allow_download = max_size is None
if max_size: if max_size:

View file

@ -561,15 +561,17 @@ class Identity(StatorModel):
(actor uri, canonical handle) or None, None if it does not resolve. (actor uri, canonical handle) or None, None if it does not resolve.
""" """
domain = handle.split("@")[1].lower() domain = handle.split("@")[1].lower()
webfinger_url = "https://{domain}/.well-known/webfinger?resource={uri}" webfinger_url = f"https://{domain}/.well-known/webfinger?resource={{uri}}"
try:
async with httpx.AsyncClient( async with httpx.AsyncClient(
timeout=settings.SETUP.REMOTE_TIMEOUT timeout=settings.SETUP.REMOTE_TIMEOUT,
headers={"User-Agent": settings.TAKAHE_USER_AGENT},
) as client: ) as client:
try:
response = await client.get( response = await client.get(
f"https://{domain}/.well-known/host-meta", f"https://{domain}/.well-known/host-meta",
follow_redirects=True, follow_redirects=True,
headers={"Accept": "application/xml"},
) )
# In the case of anything other than a success, we'll still try # In the case of anything other than a success, we'll still try
@ -586,25 +588,25 @@ class Identity(StatorModel):
pass pass
try: try:
async with httpx.AsyncClient(
timeout=settings.SETUP.REMOTE_TIMEOUT
) as client:
response = await client.get( response = await client.get(
webfinger_url.format(domain=domain, uri=f"acct:{handle}"), webfinger_url.format(uri=f"acct:{handle}"),
follow_redirects=True, follow_redirects=True,
headers={"Accept": "application/json"},
) )
except httpx.RequestError: response.raise_for_status()
return None, None except httpx.RequestError as ex:
response = getattr(ex, "response", None)
if response.status_code in [404, 410]: if (
return None, None response
if response.status_code >= 500: and response.status_code < 500
return None, None and response.status_code not in [404, 410]
if response.status_code >= 400: ):
raise ValueError( raise ValueError(
f"Client error fetching webfinger: {response.status_code}", f"Client error fetching webfinger: {response.status_code}",
response.content, response.content,
) )
return None, None
try: try:
data = response.json() data = response.json()
except ValueError: except ValueError: