diff --git a/cm/migrations/0003_access_profile.py b/cm/migrations/0003_access_profile.py index 178685e95eaf2c4be94ca58e62d16b849e6f9522..ee3ffea078a6ceb9808aeb9c1922f55280ad3d66 100644 --- a/cm/migrations/0003_access_profile.py +++ b/cm/migrations/0003_access_profile.py @@ -10,7 +10,7 @@ from django.db import models # NOC modules -from noc.lib.url import URL +from noc.core.url import URL from noc.core.script.scheme import TELNET, SSH from noc.core.migration.base import BaseMigration diff --git a/core/url.py b/core/url.py new file mode 100644 index 0000000000000000000000000000000000000000..33de70a1198fb1cde8ba766e9724d2007853dfc3 --- /dev/null +++ b/core/url.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +# --------------------------------------------------------------------- +# URL Processing functions +# --------------------------------------------------------------------- +# Copyright (C) 2007-2012 The NOC Project +# See LICENSE for details +# --------------------------------------------------------------------- + +# Python modules +import re + +# Third-party modules +from six.moves.urllib.parse import unquote as urllib_unquote + +rx_url = re.compile( + r"^(?P[^:]+)://(?:(?P[^:]+):(?P[^@]+)@)(?P[^/:]+)(?::(?P\d+))?(?P.*)$" +) + + +class InvalidURLException(Exception): + pass + + +class URL(object): + def __init__(self, url): + self.url = url + match = rx_url.match(self.url) + if not match: + raise InvalidURLException + self.scheme = match.group("scheme") + self.user = urllib_unquote(match.group("user")) + self.password = urllib_unquote(match.group("password")) + self.host = match.group("host") + if match.group("port"): + self.port = int(match.group("port")) + else: + self.port = None + self.path = match.group("path") diff --git a/tests/test_url.py b/tests/test_url.py new file mode 100644 index 0000000000000000000000000000000000000000..59037ef1e835b86be82afefa4f6a91439198299b --- /dev/null +++ b/tests/test_url.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +# ---------------------------------------------------------------------- +# noc.core.url unittests +# ---------------------------------------------------------------------- +# Copyright (C) 2007-2019 The NOC Project +# See LICENSE for details +# ---------------------------------------------------------------------- + +# Third-party modules +import pytest + +# NOC modules +from noc.core.url import URL + + +@pytest.mark.parametrize( + "config, expected", + [("https://user:password@www.www.ru/login", "https://user:password@www.www.ru/login")], +) +def test_url(config, expected): + assert URL(config).url == expected + + +@pytest.mark.parametrize( + "config, expected", + [("https:///user::password@www.www.ru/login", "https://user:password@www.www.ru/login")], +) +def test_url_error(config, expected): + with pytest.raises(Exception): + assert URL(config).url == expected