Skip to content

Commit 4c2390e

Browse files
authored
Merge pull request #249 from ynput/enhancement/validate-url-enhancements
Utils: Validate url enhancements
2 parents c06f3cc + b750d21 commit 4c2390e

File tree

1 file changed

+48
-11
lines changed

1 file changed

+48
-11
lines changed

ayon_api/utils.py

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import uuid
55
import string
66
import platform
7+
import traceback
78
import collections
89
from urllib.parse import urlparse, urlencode
910
import typing
@@ -322,18 +323,38 @@ def _try_parse_url(url: str) -> Optional[str]:
322323

323324

324325
def _try_connect_to_server(
325-
url: str, timeout: Optional[float] = None
326-
) -> bool:
326+
url: str,
327+
timeout: Optional[float],
328+
verify: Optional["Union[str, bool]"],
329+
cert: Optional[str],
330+
) -> Optional[str]:
327331
if timeout is None:
328332
timeout = get_default_timeout()
333+
334+
if verify is None:
335+
verify = os.environ.get("AYON_CA_FILE") or True
336+
337+
if cert is None:
338+
cert = os.environ.get("AYON_CERT_FILE") or None
339+
329340
try:
330341
# TODO add validation if the url lead to AYON server
331342
# - this won't validate if the url lead to 'google.com'
332-
requests.get(url, timeout=timeout)
343+
response = requests.get(
344+
url,
345+
timeout=timeout,
346+
verify=verify,
347+
cert=cert,
348+
)
349+
if response.history:
350+
return response.history[-1].headers["location"].rstrip("/")
351+
return url
333352

334-
except BaseException:
335-
return False
336-
return True
353+
except Exception:
354+
print(f"Failed to connect to '{url}'")
355+
traceback.print_exc()
356+
357+
return None
337358

338359

339360
def login_to_server(
@@ -463,7 +484,12 @@ def is_token_valid(
463484
return False
464485

465486

466-
def validate_url(url: str, timeout: Optional[int] = None) -> str:
487+
def validate_url(
488+
url: str,
489+
timeout: Optional[int] = None,
490+
verify: Optional["Union[str, bool]"] = None,
491+
cert: Optional[str] = None,
492+
) -> str:
467493
"""Validate url if is valid and server is available.
468494
469495
Validation checks if can be parsed as url and contains scheme.
@@ -520,12 +546,23 @@ def validate_url(url: str, timeout: Optional[int] = None) -> str:
520546
# Try add 'https://' scheme if is missing
521547
# - this will trigger UrlError if both will crash
522548
if not parsed_url.scheme:
523-
new_url = "https://" + modified_url
524-
if _try_connect_to_server(new_url, timeout=timeout):
549+
new_url = _try_connect_to_server(
550+
"http://" + modified_url,
551+
timeout=timeout,
552+
verify=verify,
553+
cert=cert,
554+
)
555+
if new_url:
525556
return new_url
526557

527-
if _try_connect_to_server(modified_url, timeout=timeout):
528-
return modified_url
558+
new_url = _try_connect_to_server(
559+
modified_url,
560+
timeout=timeout,
561+
verify=verify,
562+
cert=cert,
563+
)
564+
if new_url:
565+
return new_url
529566

530567
hints = []
531568
if "/" in parsed_url.path or not parsed_url.scheme:

0 commit comments

Comments
 (0)