e74eb41689c9777e5973cee234d35515b2c35319
[csit.git] / resources / tools / dash / app / pal / data / url_processing.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """URL decoding and parsing and URL encoding.
15 """
16
17 import logging
18
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
24
25
26 def url_encode(params: dict) -> str:
27     """
28     """
29     url_params = params.get("params", None)
30     if url_params:
31         encoded_params = urlsafe_b64encode(
32             compress(urlencode(url_params).encode("utf-8"))
33         ).rstrip(b"=").decode("utf-8")
34     else:
35         encoded_params = str()
36
37     return urlunparse((
38         params.get("scheme", "http"),
39         params.get("netloc", str()),
40         params.get("path", str()),
41         str(),  # params
42         params.get("query", str()),
43         encoded_params
44     ))
45
46
47 def url_decode(url: str) -> dict:
48     """
49     """
50     try:
51         parsed_url = urlparse(url)
52     except ValueError as err:
53         logging.warning(f"\nThe url {url} is not valid, ignoring.\n{repr(err)}")
54         return None
55
56     if parsed_url.fragment:
57         try:
58             padding = b"=" * (4 - (len(parsed_url.fragment) % 4))
59             params = parse_qs(decompress(
60                 urlsafe_b64decode(
61                     (parsed_url.fragment.encode("utf-8") + padding)
62                 )).decode("utf-8")
63             )
64         except (BinasciiErr, UnicodeDecodeError, ZlibErr) as err:
65             logging.warning(
66                 f"\nNot possible to decode the parameters from url: {url}"
67                 f"\nEncoded parameters: '{parsed_url.fragment}'"
68                 f"\n{repr(err)}"
69             )
70             return None
71     else:
72         params = None
73
74     return {
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,
80         "params": params
81     }