From 1699d0ee816f015a6edc22f1f0121a4beea08bbb Mon Sep 17 00:00:00 2001 From: Christoph Gerneth Date: Mon, 22 Nov 2021 21:04:41 +0100 Subject: [PATCH] added header support --- restler/engine/transport_layer/messaging.py | 22 +++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/restler/engine/transport_layer/messaging.py b/restler/engine/transport_layer/messaging.py index 2612a81d..fe71c8d5 100644 --- a/restler/engine/transport_layer/messaging.py +++ b/restler/engine/transport_layer/messaging.py @@ -1,7 +1,7 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. -""" Transport layer fuctionality using python sockets. """ +""" Transport layer functionality using python sockets. """ from __future__ import print_function from abc import ABCMeta, abstractmethod import ssl @@ -67,8 +67,7 @@ def __init__(self, connection_settings: Dict) -> None: self.connection_settings = connection_settings self.ignore_decoding_failures = Settings().ignore_decoding_failures - - + @abstractmethod def __del__(self): pass @@ -77,7 +76,6 @@ def __del__(self): def sendRecv(self, message: str, req_timeout_sec: int) -> Tuple[bool, str]: pass - def _get_method_from_message(self, message): end_of_method_idx = message.find(" ") method_name = message[0:end_of_method_idx] @@ -94,8 +92,18 @@ def _get_uri_segment_from_message(self, message): segment_end_index = message[segment_start_index+1:].find(' ') + segment_start_index+1 segment = message[segment_start_index+1:segment_end_index] return segment + + def _get_headers_from_message(self, message) -> Dict: + # FIXME: ugly + header_index = message.find('\r\n') + payload_index = message.find(DELIM) - + headers = message[header_index+2:payload_index] + h = dict() + for line in headers.split('\r\n'): + k, v = line.split(':') + h[k.strip()] = v.strip() + return h class Http2Sock(BaseSocket): @@ -117,7 +125,6 @@ def __init__(self, connection_settings: Dict) -> None: secure=self.connection_settings.use_ssl ) - def sendRecv(self, message: str, req_timeout_sec: int) -> Tuple[bool, str]: super().sendRecv(message, req_timeout_sec) method = self._get_method_from_message(message) @@ -130,7 +137,7 @@ def sendRecv(self, message: str, req_timeout_sec: int) -> Tuple[bool, str]: method, url=uri_segment, body=bytes(message_body, UTF8), #TODO: allow other encodings - #headers=self._get_header_from_message(message) # TODO: implement header support + headers=self._get_headers_from_message(message) ) response = self.client.get_response() @@ -139,7 +146,6 @@ def sendRecv(self, message: str, req_timeout_sec: int) -> Tuple[bool, str]: return (True, res) - def __del__(self): pass