Fix small issues, improve error handling and documentation
This commit is contained in:
@@ -5,7 +5,7 @@ from urllib.parse import urlparse
|
||||
|
||||
from client.httpclient import HTTPClient
|
||||
from httplib import parser
|
||||
from httplib.exceptions import InvalidResponse, InvalidStatusLine, UnsupportedEncoding
|
||||
from httplib.exceptions import InvalidResponse, InvalidStatusLine, UnsupportedEncoding, UnsupportedProtocol
|
||||
from httplib.httpsocket import FORMAT
|
||||
from httplib.message import ResponseMessage as Message
|
||||
from httplib.retriever import PreambleRetriever
|
||||
@@ -42,12 +42,10 @@ class AbstractCommand(ABC):
|
||||
_host: str
|
||||
_path: str
|
||||
_port: int
|
||||
sub_request: bool
|
||||
|
||||
def __init__(self, uri: str, port):
|
||||
self.uri = uri
|
||||
self._port = int(port)
|
||||
self.sub_request = False
|
||||
|
||||
@property
|
||||
def uri(self):
|
||||
@@ -81,23 +79,24 @@ class AbstractCommand(ABC):
|
||||
|
||||
@param sub_request: If this execution is in function of a prior command.
|
||||
"""
|
||||
self.uri = ""
|
||||
self.sub_request = sub_request
|
||||
(host, path) = self.parse_uri()
|
||||
|
||||
client = sockets.get(host)
|
||||
client = sockets.get(self.host)
|
||||
|
||||
if client and client.is_closed():
|
||||
sockets.pop(self.host)
|
||||
client = None
|
||||
|
||||
if not client:
|
||||
client = HTTPClient(host)
|
||||
client.conn.connect((host, self.port))
|
||||
sockets[host] = client
|
||||
logging.info("Connecting to %s", self.host)
|
||||
client = HTTPClient(self.host)
|
||||
client.conn.connect((self.host, self.port))
|
||||
logging.info("Connected.")
|
||||
sockets[self.host] = client
|
||||
else:
|
||||
logging.info("Reusing socket for %s", self.host)
|
||||
|
||||
message = f"{self.method} {path} HTTP/1.1\r\n"
|
||||
message += f"Host: {host}:{self.port}\r\n"
|
||||
message = f"{self.method} {self.path} HTTP/1.1\r\n"
|
||||
message += f"Host: {self.host}:{self.port}\r\n"
|
||||
message += "Accept: */*\r\n"
|
||||
message += "Accept-Encoding: identity\r\n"
|
||||
encoded_msg = self._build_message(message)
|
||||
@@ -111,13 +110,17 @@ class AbstractCommand(ABC):
|
||||
try:
|
||||
self._await_response(client)
|
||||
except InvalidResponse as e:
|
||||
logging.debug("Internal error: Response could not be parsed", exc_info=e)
|
||||
return
|
||||
logging.error("Response could not be parsed")
|
||||
logging.debug("", exc_info=e)
|
||||
except InvalidStatusLine as e:
|
||||
logging.debug("Internal error: Invalid status-line in response", exc_info=e)
|
||||
return
|
||||
logging.error("Invalid status-line in response")
|
||||
logging.debug("", exc_info=e)
|
||||
except UnsupportedEncoding as e:
|
||||
logging.debug("Internal error: Unsupported encoding in response", exc_info=e)
|
||||
logging.error("Unsupported encoding in response")
|
||||
logging.debug("", exc_info=e)
|
||||
except UnsupportedProtocol as e:
|
||||
logging.error("Unsupported protocol: %s", e.protocol)
|
||||
logging.debug("", exc_info=e)
|
||||
finally:
|
||||
if not sub_request:
|
||||
client.close()
|
||||
|
Reference in New Issue
Block a user