From af2c56f96d8be2f783332f5e4c502e8d15835b32 Mon Sep 17 00:00:00 2001 From: Trygve Aaberge Date: Sat, 6 Jan 2024 16:32:14 +0100 Subject: URL encode cookies if not encoded The cookies need to be encoded for the Slack API to accept them, so this makes it work if the config is set with cookies that are not encoded. --- slack/util.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'slack/util.py') diff --git a/slack/util.py b/slack/util.py index c988f1f..14d18dc 100644 --- a/slack/util.py +++ b/slack/util.py @@ -12,6 +12,7 @@ from typing import ( TypeVar, Union, ) +from urllib.parse import quote, unquote import weechat @@ -45,6 +46,22 @@ def unhtmlescape(text: str) -> str: return text.replace("<", "<").replace(">", ">").replace("&", "&") +def url_encode_if_not_encoded(value: str) -> str: + is_encoded = value != unquote(value) + if is_encoded: + return value + else: + return quote(value) + + +def get_cookies(cookie_config: str) -> str: + cookie_pairs = [cookie.split("=", 1) for cookie in cookie_config.split(";")] + for cookie_pair in cookie_pairs: + cookie_pair[0] = cookie_pair[0].strip() + cookie_pair[1] = url_encode_if_not_encoded(cookie_pair[1].strip()) + return "; ".join("=".join(cookie_pair) for cookie_pair in cookie_pairs) + + # From https://github.com/more-itertools/more-itertools/blob/v10.1.0/more_itertools/recipes.py#L93-L106 def take(n: int, iterable: Iterable[T]) -> List[T]: """Return first *n* items of the iterable as a list. -- cgit