From e315389a6799b2f5282192ea24a2aceb5d0bf89d Mon Sep 17 00:00:00 2001 From: Martin Ivancik Date: Fri, 7 Feb 2020 10:51:16 +0100 Subject: [PATCH 1/2] Add param for specifying, whether url query params should be sorted. --- url_normalize/url_normalize.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/url_normalize/url_normalize.py b/url_normalize/url_normalize.py index 0c2d96d..bf5e9a5 100644 --- a/url_normalize/url_normalize.py +++ b/url_normalize/url_normalize.py @@ -183,7 +183,7 @@ def normalize_fragment(fragment): return quote(unquote(fragment), "~") -def normalize_query(query): +def normalize_query(query, sort_query_params): """Normalize query part of the url. Params: @@ -193,20 +193,16 @@ def normalize_query(query): string : normalized query data. """ - query = "&".join( - sorted( - [ - "=".join( - [quote(unquote(t), "~:/?#[]@!$'()*+,;=") for t in q.split("=", 1)] - ) - for q in query.split("&") - ] - ) - ) + param_arr = ["=".join( + [quote(unquote(t), "~:/?#[]@!$'()*+,;=") for t in q.split("=", 1)]) for q in query.split("&") + ] + if sort_query_params: + param_arr = sorted(param_arr) + query = "&".join(param_arr) return query -def url_normalize(url, charset=DEFAULT_CHARSET, default_scheme=DEFAULT_SCHEME): +def url_normalize(url, charset=DEFAULT_CHARSET, default_scheme=DEFAULT_SCHEME, sort_query_params=True): """URI normalization routine. Sometimes you get an URL by a user that just isn't a real @@ -234,7 +230,7 @@ def url_normalize(url, charset=DEFAULT_CHARSET, default_scheme=DEFAULT_SCHEME): scheme=normalize_scheme(url_elements.scheme), userinfo=normalize_userinfo(url_elements.userinfo), host=normalize_host(url_elements.host, charset), - query=normalize_query(url_elements.query), + query=normalize_query(url_elements.query, sort_query_params), fragment=normalize_fragment(url_elements.fragment), ) url_elements = url_elements._replace( From 7b65b40d95f4a1142180bd0b87764fb060ce4951 Mon Sep 17 00:00:00 2001 From: Martin Ivancik Date: Fri, 7 Feb 2020 11:06:31 +0100 Subject: [PATCH 2/2] Add default value. --- url_normalize/url_normalize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/url_normalize/url_normalize.py b/url_normalize/url_normalize.py index bf5e9a5..b025908 100644 --- a/url_normalize/url_normalize.py +++ b/url_normalize/url_normalize.py @@ -183,7 +183,7 @@ def normalize_fragment(fragment): return quote(unquote(fragment), "~") -def normalize_query(query, sort_query_params): +def normalize_query(query, sort_query_params=True): """Normalize query part of the url. Params: