-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
107 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# coding: utf-8 | ||
# ============================================================================= | ||
# Ural URL Fingerprinting Unit Tests | ||
# ============================================================================= | ||
from __future__ import unicode_literals | ||
|
||
from ural import canonicalize_url | ||
|
||
TESTS = [ | ||
(" http://lemonde.fr/test.html ", "http://lemonde.fr/test.html"), | ||
("http://lemonde.fr/test\x00.html", "http://lemonde.fr/test.html"), | ||
("lemonde.fr", "https://lemonde.fr"), | ||
("http://LEMONDE.FR/TEST", "http://lemonde.fr/TEST"), | ||
("http://lemonde.fr:80/test", "http://lemonde.fr/test"), | ||
("http://xn--tlrama-bvab.fr", "http://télérama.fr"), | ||
( | ||
"http://mozilla.org?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B", | ||
"http://mozilla.org?x=шеллы", | ||
), | ||
("http://mozilla.org?x=шеллы", "http://mozilla.org?x=шеллы"), | ||
] | ||
|
||
|
||
class TestFingerprintUrl(object): | ||
def test_canonicalize_url(self): | ||
for url, result in TESTS: | ||
assert canonicalize_url(url) == result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from ural.utils import ( | ||
urlsplit, | ||
urlunsplit, | ||
SplitResult, | ||
split_netloc, | ||
unsplit_netloc, | ||
space_aware_unquote, | ||
decode_punycode_hostname, | ||
) | ||
from ural.ensure_protocol import ensure_protocol | ||
from ural.patterns import CONTROL_CHARS_RE | ||
|
||
|
||
def canonicalize_url(url, default_protocol="https", unsplit=True): | ||
# Cleaning | ||
url = url.strip() | ||
url = CONTROL_CHARS_RE.sub("", url) | ||
|
||
# Ensuring a protocol | ||
url = ensure_protocol(url, default_protocol) | ||
|
||
# Parsing | ||
scheme, netloc, path, query, fragment = urlsplit(url) | ||
auth, hostname, port = split_netloc(netloc) | ||
|
||
# Decoding and normalizing hostname | ||
hostname = decode_punycode_hostname(hostname) | ||
hostname = hostname.lower() | ||
|
||
# Dropping HTTP/HTTPS ports | ||
if port == "80" or port == "443": | ||
port = "" | ||
|
||
netloc = unsplit_netloc(auth, hostname, port) | ||
|
||
# Unquoting | ||
path = space_aware_unquote(path) | ||
query = space_aware_unquote(query) | ||
fragment = space_aware_unquote(fragment) | ||
|
||
result = SplitResult(scheme, netloc, path, query, fragment) | ||
|
||
if not unsplit: | ||
return result | ||
|
||
# Serializing | ||
return urlunsplit(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
def canonicalize_url( | ||
url: str, default_protocol: str = ..., unsplit: bool = ... | ||
) -> str: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters