diff options
author | Wojtek Kosior <koszko@koszko.org> | 2022-08-11 15:47:13 +0200 |
---|---|---|
committer | Wojtek Kosior <koszko@koszko.org> | 2022-08-11 15:47:13 +0200 |
commit | a38d19576c387e505cc468b20ca5b8bcf2fa4759 (patch) | |
tree | a578f4056dfea683d3bb2714ae1620eac576da0e /src/hydrilla/proxy/policies | |
parent | ad97639bbf982b5b3b2757e75c3f91556e3a8eac (diff) | |
download | haketilo-hydrilla-a38d19576c387e505cc468b20ca5b8bcf2fa4759.tar.gz haketilo-hydrilla-a38d19576c387e505cc468b20ca5b8bcf2fa4759.zip |
move classes/protocols for representing HTTP requests and responses data into a separate file
Diffstat (limited to 'src/hydrilla/proxy/policies')
-rw-r--r-- | src/hydrilla/proxy/policies/base.py | 87 | ||||
-rw-r--r-- | src/hydrilla/proxy/policies/payload.py | 25 | ||||
-rw-r--r-- | src/hydrilla/proxy/policies/payload_resource.py | 17 | ||||
-rw-r--r-- | src/hydrilla/proxy/policies/rule.py | 7 |
4 files changed, 36 insertions, 100 deletions
diff --git a/src/hydrilla/proxy/policies/base.py b/src/hydrilla/proxy/policies/base.py index 3bde6f2..bb95d29 100644 --- a/src/hydrilla/proxy/policies/base.py +++ b/src/hydrilla/proxy/policies/base.py @@ -31,13 +31,6 @@ # Enable using with Python 3.7. from __future__ import annotations -import sys - -if sys.version_info >= (3, 8): - from typing import Protocol -else: - from typing_extensions import Protocol - import dataclasses as dc import typing as t import enum @@ -46,8 +39,8 @@ from abc import ABC, abstractmethod from immutables import Map -from ...url_patterns import ParsedUrl from .. import state +from .. import http_messages class PolicyPriority(int, enum.Enum): @@ -56,72 +49,10 @@ class PolicyPriority(int, enum.Enum): _TWO = 2 _THREE = 3 -DefaultGetValue = t.TypeVar('DefaultGetValue', object, None) - -class IHeaders(Protocol): - """....""" - def __getitem__(self, key: str) -> str: ... - - def get_all(self, key: str) -> t.Iterable[str]: ... - - def get(self, key: str, default: DefaultGetValue = None) \ - -> t.Union[str, DefaultGetValue]: ... - - def items(self) -> t.Iterable[tuple[str, str]]: ... - -def encode_headers_items(headers: t.Iterable[tuple[str, str]]) \ - -> t.Iterable[tuple[bytes, bytes]]: - """....""" - for name, value in headers: - yield name.encode(), value.encode() - -@dc.dataclass(frozen=True) -class ProducedRequest: - """....""" - url: str - method: str - headers: t.Iterable[tuple[bytes, bytes]] - body: bytes - -@dc.dataclass(frozen=True) -class RequestInfo: - """....""" - url: ParsedUrl - method: str - headers: IHeaders - body: bytes - - def make_produced_request(self) -> ProducedRequest: - """....""" - return ProducedRequest( - url = self.url.orig_url, - method = self.method, - headers = encode_headers_items(self.headers.items()), - body = self.body - ) - -@dc.dataclass(frozen=True) -class ProducedResponse: - """....""" - status_code: int - headers: t.Iterable[tuple[bytes, bytes]] - body: bytes - -@dc.dataclass(frozen=True) -class ResponseInfo: - """....""" - url: ParsedUrl - status_code: int - headers: IHeaders - body: bytes - - def make_produced_response(self) -> ProducedResponse: - """....""" - return ProducedResponse( - status_code = self.status_code, - headers = encode_headers_items(self.headers.items()), - body = self.body - ) +ProducedMessage = t.Union[ + http_messages.ProducedRequest, + http_messages.ProducedResponse +] class Policy(ABC): """....""" @@ -134,13 +65,13 @@ class Policy(ABC): def anticache(self) -> bool: return self.process_request or self.process_response - def consume_request(self, request_info: RequestInfo) \ - -> t.Optional[t.Union[ProducedRequest, ProducedResponse]]: + def consume_request(self, request_info: http_messages.RequestInfo) \ + -> t.Optional[ProducedMessage]: """....""" return None - def consume_response(self, response_info: ResponseInfo) \ - -> t.Optional[ProducedResponse]: + def consume_response(self, response_info: http_messages.ResponseInfo) \ + -> t.Optional[http_messages.ProducedResponse]: """....""" return None diff --git a/src/hydrilla/proxy/policies/payload.py b/src/hydrilla/proxy/policies/payload.py index 1a88ea1..ad0fa05 100644 --- a/src/hydrilla/proxy/policies/payload.py +++ b/src/hydrilla/proxy/policies/payload.py @@ -38,8 +38,9 @@ import re import bs4 # type: ignore from ...url_patterns import ParsedUrl -from .. import state from .. import csp +from .. import state +from .. import http_messages from . import base @dc.dataclass(frozen=True) # type: ignore[misc] @@ -96,7 +97,7 @@ charset= # no whitespace allowed in parameter as per RFC $ # forbid possible dangling characters after closing '"' ''', re.VERBOSE | re.IGNORECASE) -def deduce_content_type(headers: base.IHeaders) \ +def deduce_content_type(headers: http_messages.IHeaders) \ -> tuple[t.Optional[str], t.Optional[str]]: """....""" content_type = headers.get('content-type') @@ -157,7 +158,7 @@ class PayloadInjectPolicy(PayloadAwarePolicy): "script-src-attr 'none'" )) - def _modify_headers(self, response_info: base.ResponseInfo) \ + def _modify_headers(self, response_info: http_messages.ResponseInfo) \ -> t.Iterable[tuple[bytes, bytes]]: """....""" for header_name, header_value in response_info.headers.items(): @@ -211,8 +212,10 @@ class PayloadInjectPolicy(PayloadAwarePolicy): # https://www.w3.org/International/tests/repository/html5/the-input-byte-stream/results-basics#precedence return UTF8_BOM + soup.encode() - def _consume_response_unsafe(self, response_info: base.ResponseInfo) \ - -> base.ProducedResponse: + def _consume_response_unsafe( + self, + response_info: http_messages.ResponseInfo + ) -> http_messages.ProducedResponse: """....""" new_response = response_info.make_produced_response() @@ -237,8 +240,8 @@ class PayloadInjectPolicy(PayloadAwarePolicy): return dc.replace(new_response, body=new_data) - def consume_response(self, response_info: base.ResponseInfo) \ - -> base.ProducedResponse: + def consume_response(self, response_info: http_messages.ResponseInfo) \ + -> http_messages.ProducedResponse: """....""" try: return self._consume_response_unsafe(response_info) @@ -252,7 +255,7 @@ class PayloadInjectPolicy(PayloadAwarePolicy): e.__traceback__ ) - return base.ProducedResponse( + return http_messages.ProducedResponse( 500, ((b'Content-Type', b'text/plain; charset=utf-8'),), '\n'.join(error_info_list).encode() @@ -282,11 +285,11 @@ class PayloadSuggestPolicy(PayloadAwarePolicy): """....""" priority: t.ClassVar[base.PolicyPriority] = base.PolicyPriority._ONE - def make_response(self, request_info: base.RequestInfo) \ - -> base.ProducedResponse: + def make_response(self, request_info: http_messages.RequestInfo) \ + -> http_messages.ProducedResponse: """....""" # TODO: implement - return base.ProducedResponse(200, ((b'a', b'b'),), b'') + return http_messages.ProducedResponse(200, ((b'a', b'b'),), b'') @dc.dataclass(frozen=True, unsafe_hash=True) # type: ignore[misc] diff --git a/src/hydrilla/proxy/policies/payload_resource.py b/src/hydrilla/proxy/policies/payload_resource.py index b255d4e..3e1b31a 100644 --- a/src/hydrilla/proxy/policies/payload_resource.py +++ b/src/hydrilla/proxy/policies/payload_resource.py @@ -61,6 +61,7 @@ import typing as t from ...translations import smart_gettext as _ from .. import state +from .. import http_messages from . import base from .payload import PayloadAwarePolicy, PayloadAwarePolicyFactory @@ -73,7 +74,7 @@ class PayloadResourcePolicy(PayloadAwarePolicy): priority: t.ClassVar[base.PolicyPriority] = base.PolicyPriority._THREE def _make_file_resource_response(self, path: tuple[str, ...]) \ - -> base.ProducedResponse: + -> http_messages.ProducedResponse: """....""" try: file_data = self.payload_data.payload_ref.get_file_data( @@ -84,20 +85,20 @@ class PayloadResourcePolicy(PayloadAwarePolicy): return resource_blocked_response if file_data is None: - return base.ProducedResponse( + return http_messages.ProducedResponse( 404, [(b'Content-Type', b'text/plain; charset=utf-8')], _('api.file_not_found').encode() ) - return base.ProducedResponse( + return http_messages.ProducedResponse( 200, ((b'Content-Type', file_data.type.encode()),), file_data.contents ) - def consume_request(self, request_info: base.RequestInfo) \ - -> base.ProducedResponse: + def consume_request(self, request_info: http_messages.RequestInfo) \ + -> http_messages.ProducedResponse: """....""" # Payload resource pattern has path of the form: # "/some/arbitrary/segments/<per-session_token>/***" @@ -120,7 +121,7 @@ class PayloadResourcePolicy(PayloadAwarePolicy): return resource_blocked_response -resource_blocked_response = base.ProducedResponse( +resource_blocked_response = http_messages.ProducedResponse( 403, [(b'Content-Type', b'text/plain; charset=utf-8')], _('api.resource_not_enabled_for_access').encode() @@ -133,8 +134,8 @@ class BlockedResponsePolicy(base.Policy): priority: t.ClassVar[base.PolicyPriority] = base.PolicyPriority._THREE - def consume_request(self, request_info: base.RequestInfo) \ - -> base.ProducedResponse: + def consume_request(self, request_info: http_messages.RequestInfo) \ + -> http_messages.ProducedResponse: """....""" return resource_blocked_response diff --git a/src/hydrilla/proxy/policies/rule.py b/src/hydrilla/proxy/policies/rule.py index eb70147..bcb110e 100644 --- a/src/hydrilla/proxy/policies/rule.py +++ b/src/hydrilla/proxy/policies/rule.py @@ -37,6 +37,7 @@ import typing as t from ...url_patterns import ParsedPattern from .. import csp from .. import state +from ..import http_messages from . import base @@ -50,7 +51,7 @@ class BlockPolicy(base.Policy): priority: t.ClassVar[base.PolicyPriority] = base.PolicyPriority._TWO - def _modify_headers(self, response_info: base.ResponseInfo) \ + def _modify_headers(self, response_info: http_messages.ResponseInfo) \ -> t.Iterable[tuple[bytes, bytes]]: """....""" csp_policies = csp.extract(response_info.headers) @@ -80,8 +81,8 @@ class BlockPolicy(base.Policy): yield b'Content-Security-Policy', extra_csp.encode() - def consume_response(self, response_info: base.ResponseInfo) \ - -> base.ProducedResponse: + def consume_response(self, response_info: http_messages.ResponseInfo) \ + -> http_messages.ProducedResponse: """....""" new_response = response_info.make_produced_response() |