Skip to content

Commit 82d8e94

Browse files
ANKUR DWIVEDIANKUR DWIVEDI
authored andcommitted
improve test case
1 parent d05c7f6 commit 82d8e94

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

imagekitio/url.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ def get_signature_timestamp(expiry_seconds: int = None) -> int:
130130

131131
@staticmethod
132132
def get_signature(private_key, url, url_endpoint, expiry_timestamp: int) -> str:
133-
url = Url.encode_string_if_required(url)
134133
""" "
135134
create signature(hashed hex key) from
136135
private_key, url, url_endpoint and expiry_timestamp
@@ -143,7 +142,7 @@ def get_signature(private_key, url, url_endpoint, expiry_timestamp: int) -> str:
143142
expiry_timestamp = Default.DEFAULT_TIMESTAMP.value
144143

145144
replaced_url = url.replace(url_endpoint, "") + str(expiry_timestamp)
146-
145+
replaced_url = Url.encode_string_if_required(replaced_url)
147146
signature = hmac.new(
148147
key=private_key.encode(), msg=replaced_url.encode(), digestmod=hashlib.sha1
149148
)
@@ -214,12 +213,14 @@ def transformation_to_str(transformation):
214213
return Default.CHAIN_TRANSFORM_DELIMITER.value.join(parsed_transforms)
215214

216215
@staticmethod
217-
def custom_encodeURIComponent(url_str):
218-
parsed_url = urlparse(url_str)
219-
encoded_url = f"{parsed_url.scheme}://{parsed_url.netloc}"
220-
encoded_url +=quote(parsed_url.path, safe='~@#$&()*!+=:;,?/\'')
221-
if(parsed_url.query):
222-
encoded_url = encoded_url+"?"+quote(unquote(parsed_url.query), safe='~@#$&()*!+=:;?/\'')
216+
def encodeURI(url_str):
217+
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI
218+
if "?" in url_str:
219+
# here, we have applied the quote(unquote(url)) function to handle the query string.
220+
# This function ensures that characters in the query are properly encoded. While some characters are already encoded, others require encoding for correct processing.
221+
encoded_url = quote(url_str.split('?')[0], safe='~@#$&()*!+=:;,?/\'')+"?"+quote(unquote(url_str.split('?')[1]), safe='~@#$&()*!+=:;?/\'')
222+
else:
223+
encoded_url = quote(url_str.split('?')[0], safe='~@#$&()*!+=:;,?/\'')
223224
return encoded_url
224225

225226
@staticmethod
@@ -228,4 +229,4 @@ def has_more_than_ascii(s):
228229

229230
@staticmethod
230231
def encode_string_if_required(s):
231-
return Url.custom_encodeURIComponent(s) if Url.has_more_than_ascii(s) else s
232+
return Url.encodeURI(s) if Url.has_more_than_ascii(s) else s

0 commit comments

Comments
 (0)