From 8a632fb732e3773963892758fe471329315bca2b Mon Sep 17 00:00:00 2001 From: Jenifer Tabita Ciuciu-Kiss Date: Mon, 21 Oct 2024 02:33:15 +0200 Subject: [PATCH] In case of HEAD request do not return body --- ontologytimemachine/custom_proxy.py | 6 ++--- ontologytimemachine/utils/proxy_logic.py | 31 ++++++++++++++++-------- tests/test_proxy_logic.py | 22 ----------------- 3 files changed, 23 insertions(+), 36 deletions(-) delete mode 100644 tests/test_proxy_logic.py diff --git a/ontologytimemachine/custom_proxy.py b/ontologytimemachine/custom_proxy.py index d37f3dd..37d8f98 100644 --- a/ontologytimemachine/custom_proxy.py +++ b/ontologytimemachine/custom_proxy.py @@ -37,7 +37,7 @@ def __init__(self, *args, **kwargs): logger.info(f"Init - Object ID: {id(self)}") super().__init__(*args, **kwargs) self.config = config - logger.info(self.config) + logger.info(f"Config: {self.config}") def before_upstream_connection(self, request: HttpParser) -> HttpParser | None: # self.client.config = None @@ -67,7 +67,7 @@ def before_upstream_connection(self, request: HttpParser) -> HttpParser | None: and self.config.clientConfigViaProxyAuth == ClientConfigViaProxyAuth.OPTIONAL ): - logger.info("Auth configuration is optional, not procided.") + logger.info("Auth configuration is optional, not provided.") if config_from_auth and not hasattr(self.client, "config"): self.client.config = config_from_auth logger.info(f"New config: {config_from_auth}") @@ -96,8 +96,6 @@ def before_upstream_connection(self, request: HttpParser) -> HttpParser | None: logger.info("CONNECT request was blocked due to the configuration") return None - # # If only ontology mode, return None in all other cases - logger.info(f"Config: {config}") response = get_response_from_request(wrapped_request, config) if response: self.queue_response(response) diff --git a/ontologytimemachine/utils/proxy_logic.py b/ontologytimemachine/utils/proxy_logic.py index 62bebd5..b51bfae 100644 --- a/ontologytimemachine/utils/proxy_logic.py +++ b/ontologytimemachine/utils/proxy_logic.py @@ -145,12 +145,19 @@ def is_archivo_ontology_request(wrapped_request): return False -def request_ontology(url, headers, disableRemovingRedirects=False, timeout=5): +def request_ontology( + wrapped_request, url, headers, disableRemovingRedirects=False, timeout=5 +): allow_redirects = not disableRemovingRedirects try: - response = requests.get( - url=url, headers=headers, allow_redirects=allow_redirects, timeout=5 - ) + if wrapped_request.is_head_request(): + response = requests.head( + url=url, headers=headers, allow_redirects=allow_redirects, timeout=5 + ) + else: + response = requests.get( + url=url, headers=headers, allow_redirects=allow_redirects, timeout=5 + ) logger.info("Successfully fetched ontology") return response except Exception as e: @@ -175,7 +182,7 @@ def proxy_logic(wrapped_request, config): if config.ontoVersion == OntoVersion.ORIGINAL: ontology, _, _ = wrapped_request.get_request_url_host_path() - response = fetch_original(ontology, headers, config) + response = fetch_original(wrapped_request, ontology, headers, config) elif config.ontoVersion == OntoVersion.ORIGINAL_FAILOVER_LIVE_LATEST: response = fetch_failover( wrapped_request, headers, config.disableRemovingRedirects @@ -192,16 +199,20 @@ def proxy_logic(wrapped_request, config): # Fetch from the original source, no matter what -def fetch_original(ontology, headers, disableRemovingRedirects): +def fetch_original(wrapped_request, ontology, headers, disableRemovingRedirects): logger.info(f"Fetching original ontology from URL: {ontology}") - return request_ontology(ontology, headers, disableRemovingRedirects) + return request_ontology( + wrapped_request, ontology, headers, disableRemovingRedirects + ) # Failover mode def fetch_failover(wrapped_request, headers, disableRemovingRedirects): ontology, _, _ = wrapped_request.get_request_url_host_path() logger.info(f"Fetching original ontology with failover from URL: {ontology}") - original_response = request_ontology(ontology, headers, disableRemovingRedirects) + original_response = request_ontology( + wrapped_request, ontology, headers, disableRemovingRedirects + ) if original_response.status_code in passthrough_status_codes: requested_mimetypes_with_priority = parse_accept_header_with_priority( headers["Accept"] @@ -236,7 +247,7 @@ def fetch_latest_archived(wrapped_request, headers): ontology, _, _ = wrapped_request.get_request_url_host_path() dbpedia_url = f"{archivo_api}?o={ontology}&f={format}" logger.info(f"Fetching from DBpedia Archivo API: {dbpedia_url}") - return request_ontology(dbpedia_url, headers) + return request_ontology(wrapped_request, dbpedia_url, headers) def fetch_timestamp_archived(wrapped_request, headers, config): @@ -250,7 +261,7 @@ def fetch_timestamp_archived(wrapped_request, headers, config): ontology, _, _ = wrapped_request.get_request_url_host_path() dbpedia_url = f"{archivo_api}?o={ontology}&f={format}&v={config.timestamp}" logger.info(f"Fetching from DBpedia Archivo API: {dbpedia_url}") - return request_ontology(dbpedia_url, headers) + return request_ontology(wrapped_request, dbpedia_url, headers) def fetch_dependency_manifest(ontology, headers, manifest): diff --git a/tests/test_proxy_logic.py b/tests/test_proxy_logic.py deleted file mode 100644 index 69cb1ae..0000000 --- a/tests/test_proxy_logic.py +++ /dev/null @@ -1,22 +0,0 @@ -import unittest -from ontologytimemachine.utils.proxy_logic import ( - do_block_CONNECT_request, - do_deny_request_due_non_archivo_ontology_uri, - load_archivo_urls, - is_archivo_ontology_request, - proxy_logic, - fetch_original, -) - - -class TestProxyLogic(unittest.TestCase): - - def test_fetch_original(self): - url = "https://example.com" - headers = {"Accept": "text/html"} - response = fetch_original(url, headers, False) - self.assertEqual(response.status_code, 200) - - -if __name__ == "__main__": - unittest.main()