1 # Copyright (c) 2022 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """URL decoding and parsing and URL encoding.
19 from base64 import urlsafe_b64encode, urlsafe_b64decode
20 from urllib.parse import urlencode, urlunparse, urlparse, parse_qs
21 from zlib import compress, decompress
22 from zlib import error as ZlibErr
23 from binascii import Error as BinasciiErr
26 def url_encode(params: dict) -> str:
29 url_params = params.get("params", None)
31 encoded_params = urlsafe_b64encode(
32 compress(urlencode(url_params).encode("utf-8"), level=9)
33 ).rstrip(b"=").decode("utf-8")
35 encoded_params = str()
38 params.get("scheme", "http"),
39 params.get("netloc", str()),
40 params.get("path", str()),
42 params.get("query", str()),
47 def url_decode(url: str) -> dict:
51 parsed_url = urlparse(url)
52 except ValueError as err:
53 logging.warning(f"\nThe url {url} is not valid, ignoring.\n{repr(err)}")
56 if parsed_url.fragment:
58 padding = b"=" * (4 - (len(parsed_url.fragment) % 4))
59 params = parse_qs(decompress(
61 (parsed_url.fragment.encode("utf-8") + padding)
64 except (BinasciiErr, UnicodeDecodeError, ZlibErr) as err:
66 f"\nNot possible to decode the parameters from url: {url}"
67 f"\nEncoded parameters: '{parsed_url.fragment}'"
75 "scheme": parsed_url.scheme,
76 "netloc": parsed_url.netloc,
77 "path": parsed_url.path,
78 "query": parsed_url.query,
79 "fragment": parsed_url.fragment,