From: Roland Häder Date: Tue, 20 Jun 2023 12:40:08 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=11ff5183010d3171e39ba2f4ab586f032b8f0337;p=fba.git Continued: - added new helper for cookies --- diff --git a/fba/csrf.py b/fba/csrf.py index a7f942d..537314e 100644 --- a/fba/csrf.py +++ b/fba/csrf.py @@ -20,6 +20,8 @@ import reqto from fba import config from fba import network +from fba.helpers import cookies + def determine(domain: str, headers: dict) -> dict: # DEBUG: print(f"DEBUG: domain='{domain}',headers()={len(headers)} - CALLED!") if not isinstance(domain, str): @@ -44,6 +46,10 @@ def determine(domain: str, headers: dict) -> dict: # DEBUG: print(f"DEBUG: response.ok='{response.ok}',response.status_code={response.status_code},response.text()={len(response.text)}") if response.ok and len(response.text) > 0: + # Save cookies + cookies.store(domain, response.cookies.get_dict()) + + # Parse text meta = bs4.BeautifulSoup( response.text, "html.parser" diff --git a/fba/helpers/__init__.py b/fba/helpers/__init__.py index b6b121a..91fed03 100644 --- a/fba/helpers/__init__.py +++ b/fba/helpers/__init__.py @@ -15,6 +15,7 @@ __all__ = [ 'cache', + 'cookies', 'dicts', 'locking', 'tidyup', diff --git a/fba/helpers/cookies.py b/fba/helpers/cookies.py new file mode 100644 index 0000000..70f45cc --- /dev/null +++ b/fba/helpers/cookies.py @@ -0,0 +1,43 @@ +# Fedi API Block - An aggregator for fetching blocking data from fediverse nodes +# Copyright (C) 2023 Free Software Foundation +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +# Cookies stor +_cookies = {} + +def store (domain: str, cookies: dict): + # DEBUG: print(f"DEBUG: domain='{domain}',cookies()={len(cookies)} - CALLED!") + if not isinstance(domain, str): + raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'") + elif domain == "": + raise ValueError("Parameter 'domain' is empty") + elif not isinstance(cookies, dict): + raise ValueError(f"Parameter cookies[]='{type(cookies)}' is not 'dict'") + + _cookies[domain] = cookies + + # DEBUG: print(f"DEBUG: EXIT!") + +def get_all(domain: str) -> dict: + # DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!") + if not isinstance(domain, str): + raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'") + elif domain == "": + raise ValueError("Parameter 'domain' is empty") + elif not domain in _cookies: + raise Exception(f"domain='{domain}' has no cookies stored, maybe invoke store() first?") + + # DEBUG: print(f"DEBUG: _cookies[{domain}]()={len(_cookies[domain])} - EXIT!") + return _cookies[domain] diff --git a/fba/network.py b/fba/network.py index 3ebba9d..60486b9 100644 --- a/fba/network.py +++ b/fba/network.py @@ -23,6 +23,8 @@ import validators from fba import config from fba import fba +from fba.helpers import cookies + from fba.models import instances # HTTP headers for non-API requests @@ -79,7 +81,8 @@ def post_json_api(domain: str, path: str, data: str = "", headers: dict = {}) -> f"https://{domain}{path}", data=data, headers={**api_headers, **headers}, - timeout=(config.get("connection_timeout"), config.get("read_timeout")) + timeout=(config.get("connection_timeout"), config.get("read_timeout")), + cookies=cookies.get_all(domain) ) json_reply["json"] = json_from_response(response) @@ -167,7 +170,8 @@ def get_json_api(domain: str, path: str, headers: dict, timeout: tuple) -> dict: response = reqto.get( f"https://{domain}{path}", headers={**api_headers, **headers}, - timeout=timeout + timeout=timeout, + cookies=cookies.get_all(domain) ) except exceptions as exception: @@ -264,7 +268,8 @@ def fetch_response(domain: str, path: str, headers: dict, timeout: tuple) -> req response = reqto.get( f"https://{domain}{path}", headers=headers, - timeout=timeout + timeout=timeout, + cookies=cookies.get_all(domain) ) except exceptions as exception: