aboutsummaryrefslogtreecommitdiff
path: root/src/hydrilla/proxy/policies/payload_resource.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/hydrilla/proxy/policies/payload_resource.py')
-rw-r--r--src/hydrilla/proxy/policies/payload_resource.py61
1 files changed, 28 insertions, 33 deletions
diff --git a/src/hydrilla/proxy/policies/payload_resource.py b/src/hydrilla/proxy/policies/payload_resource.py
index ae6a490..10a43e6 100644
--- a/src/hydrilla/proxy/policies/payload_resource.py
+++ b/src/hydrilla/proxy/policies/payload_resource.py
@@ -219,9 +219,9 @@ def merge_response_headers(
)
-ProducedAny = t.Union[
- http_messages.ProducedResponse,
- http_messages.ProducedRequest
+MessageInfo = t.Union[
+ http_messages.ResponseInfo,
+ http_messages.RequestInfo
]
@dc.dataclass(frozen=True)
@@ -251,31 +251,30 @@ class PayloadResourcePolicy(PayloadAwarePolicy):
== ('api', 'unrestricted_http')
def _make_file_resource_response(self, path: tuple[str, ...]) \
- -> http_messages.ProducedResponse:
- """...."""
+ -> http_messages.ResponseInfo:
try:
file_data = self.payload_data.ref.get_file_data(path)
except state.MissingItemError:
return resource_blocked_response
if file_data is None:
- return http_messages.ProducedResponse(
- 404,
- [(b'Content-Type', b'text/plain; charset=utf-8')],
- _('api.file_not_found').encode()
+ return http_messages.ResponseInfo.make(
+ status_code = 404,
+ headers = [('Content-Type', 'text/plain; charset=utf-8')],
+ body =_('api.file_not_found').encode()
)
- return http_messages.ProducedResponse(
- 200,
- ((b'Content-Type', file_data.mime_type.encode()),),
- file_data.contents
+ return http_messages.ResponseInfo.make(
+ status_code = 200,
+ headers = [('Content-Type', file_data.mime_type)],
+ body = file_data.contents
)
def _make_api_response(
self,
path: tuple[str, ...],
request_info: http_messages.RequestInfo
- ) -> ProducedAny:
+ ) -> MessageInfo:
if path[0] == 'page_init_script.js':
with jinja_lock:
template = jinja_env.get_template('page_init_script.js.jinja')
@@ -288,10 +287,10 @@ class PayloadResourcePolicy(PayloadAwarePolicy):
haketilo_version = encode_string_for_js(ver_str)
)
- return http_messages.ProducedResponse(
- 200,
- ((b'Content-Type', b'application/javascript'),),
- js.encode()
+ return http_messages.ResponseInfo.make(
+ status_code = 200,
+ headers = [('Content-Type', 'application/javascript')],
+ body = js.encode()
)
if path[0] == 'unrestricted_http':
@@ -315,13 +314,10 @@ class PayloadResourcePolicy(PayloadAwarePolicy):
extra_headers = extra_headers
)
- result_headers_bytes = \
- [(h.encode(), v.encode()) for h, v in result_headers]
-
- return http_messages.ProducedRequest(
+ return http_messages.RequestInfo.make(
url = target_url,
method = request_info.method,
- headers = result_headers_bytes,
+ headers = result_headers,
body = request_info.body
)
except:
@@ -330,7 +326,7 @@ class PayloadResourcePolicy(PayloadAwarePolicy):
return resource_blocked_response
def consume_request(self, request_info: http_messages.RequestInfo) \
- -> ProducedAny:
+ -> MessageInfo:
resource_path = self.extract_resource_path(request_info.url)
if resource_path == ():
@@ -346,7 +342,7 @@ class PayloadResourcePolicy(PayloadAwarePolicy):
self,
request_info: http_messages.RequestInfo,
response_info: http_messages.ResponseInfo
- ) -> http_messages.ProducedResponse:
+ ) -> http_messages.ResponseInfo:
"""
This method shall only be called for responses to unrestricted HTTP API
requests. Its purpose is to sanitize response headers and smuggle their
@@ -375,17 +371,17 @@ class PayloadResourcePolicy(PayloadAwarePolicy):
extra_headers = extra_headers
)
- return http_messages.ProducedResponse(
+ return http_messages.ResponseInfo.make(
status_code = response_info.status_code,
- headers = [(h.encode(), v.encode()) for h, v in merged_headers],
+ headers = merged_headers,
body = response_info.body,
)
-resource_blocked_response = http_messages.ProducedResponse(
- 403,
- [(b'Content-Type', b'text/plain; charset=utf-8')],
- _('api.resource_not_enabled_for_access').encode()
+resource_blocked_response = http_messages.ResponseInfo.make(
+ status_code = 403,
+ headers = [('Content-Type', 'text/plain; charset=utf-8')],
+ body = _('api.resource_not_enabled_for_access').encode()
)
@dc.dataclass(frozen=True)
@@ -396,8 +392,7 @@ class BlockedResponsePolicy(base.Policy):
priority: t.ClassVar[base.PolicyPriority] = base.PolicyPriority._THREE
def consume_request(self, request_info: http_messages.RequestInfo) \
- -> http_messages.ProducedResponse:
- """...."""
+ -> http_messages.ResponseInfo:
return resource_blocked_response