# Path: via/exceptions.py
# class BadURL(RequestBasedException):
#     """An invalid URL was discovered."""
# 
#     def __init__(self, message, requests_err=None, url=None):
#         super().__init__(message, requests_err)
#         self.url = url
# 
#     status_int = 400


from urllib.parse import unquote, urlparse
from h_vialib import Configuration
from pyramid.httpexceptions import HTTPBadRequest
from via.exceptions import BadURL

    @classmethod
    def _normalise_url(cls, url):
        """Return a normalized URL from user input.

        Take a URL from user input (eg. a URL input field or path parameter) and
        convert it to a normalized form.
        """

        return Configuration.strip_from_url(cls._parse_url(url).geturl())

    @classmethod
    def _parse_url(cls, url):
        parsed = cls._repeated_decode(url)

        if not parsed.scheme:
            if not parsed.netloc:
                # Without a scheme urlparse often assumes the domain is the
                # path. To prevent this add a fake scheme and try again
                parsed = cls._parse_url("https://" + url.lstrip("."))
            else:
                parsed = parsed._replace(scheme="https")

        return parsed

    @classmethod
    def _repeated_decode(cls, url):
        try:
            parsed = urlparse(url)
        except ValueError as exc:

# Path: via/exceptions.py
# class BadURL(RequestBasedException):
#     """An invalid URL was discovered."""
# 
#     def __init__(self, message, requests_err=None, url=None):
#         super().__init__(message, requests_err)
#         self.url = url
# 
#     status_int = 400


# Path: via/resources.py
from urllib.parse import unquote, urlparse
from h_vialib import Configuration
from pyramid.httpexceptions import HTTPBadRequest
from via.exceptions import BadURL


    @classmethod
    def _normalise_url(cls, url):
        """Return a normalized URL from user input.

        Take a URL from user input (eg. a URL input field or path parameter) and
        convert it to a normalized form.
        """

        return Configuration.strip_from_url(cls._parse_url(url).geturl())

    @classmethod
    def _parse_url(cls, url):
        parsed = cls._repeated_decode(url)

        if not parsed.scheme:
            if not parsed.netloc:
                # Without a scheme urlparse often assumes the domain is the
                # path. To prevent this add a fake scheme and try again
                parsed = cls._parse_url("https://" + url.lstrip("."))
            else:
                parsed = parsed._replace(scheme="https")

        return parsed

    @classmethod
    def _repeated_decode(cls, url):
        try:
            parsed = urlparse(url)
        except ValueError as exc:

            raise BadURL(str(exc), url=url) from exc