Improve logging, fix small issues
This commit is contained in:
@@ -125,17 +125,34 @@ class AbstractCommand(ABC):
|
||||
if not sub_request:
|
||||
client.close()
|
||||
|
||||
|
||||
|
||||
def _get_preamble(self, client):
|
||||
"""
|
||||
Returns the preamble (start-line and headers) of the response of this command.
|
||||
@param client: the client object to retrieve from
|
||||
@return: A Message object containing the HTTP-version, status code, status message, headers and buffer
|
||||
"""
|
||||
retriever = PreambleRetriever(client)
|
||||
lines = retriever.retrieve()
|
||||
(version, status, msg) = parser.parse_status_line(next(lines))
|
||||
headers = parser.parse_headers(lines)
|
||||
|
||||
buffer = retriever.buffer
|
||||
logging.debug("---response begin---\r\n%s---response end---", "".join(buffer))
|
||||
|
||||
return Message(version, status, msg, headers, buffer)
|
||||
|
||||
def _await_response(self, client):
|
||||
"""
|
||||
Simple response method.
|
||||
|
||||
Receives the response and prints to stdout.
|
||||
"""
|
||||
while True:
|
||||
line = client.read_line()
|
||||
print(line, end="")
|
||||
if line in ("\r\n", "\n", ""):
|
||||
break
|
||||
|
||||
msg = self._get_preamble(client)
|
||||
|
||||
print("".join(msg.raw))
|
||||
|
||||
def _build_message(self, message: str) -> bytes:
|
||||
return (message + "\r\n").encode(FORMAT)
|
||||
@@ -163,25 +180,6 @@ class AbstractCommand(ABC):
|
||||
return host, path
|
||||
|
||||
|
||||
class AbstractWithBodyCommand(AbstractCommand, ABC):
|
||||
"""
|
||||
The building block for creating an HTTP message for an HTTP method with a body (POST and PUT).
|
||||
"""
|
||||
|
||||
def _build_message(self, message: str) -> bytes:
|
||||
body = input(f"Enter {self.method} data: ").encode(FORMAT)
|
||||
print()
|
||||
|
||||
message += "Content-Type: text/plain\r\n"
|
||||
message += f"Content-Length: {len(body)}\r\n"
|
||||
message += "\r\n"
|
||||
message = message.encode(FORMAT)
|
||||
message += body
|
||||
message += b"\r\n"
|
||||
|
||||
return message
|
||||
|
||||
|
||||
class HeadCommand(AbstractCommand):
|
||||
"""
|
||||
A Command for sending a `HEAD` request.
|
||||
@@ -207,22 +205,6 @@ class GetCommand(AbstractCommand):
|
||||
def method(self):
|
||||
return "GET"
|
||||
|
||||
def _get_preamble(self, client):
|
||||
"""
|
||||
Returns the preamble (start-line and headers) of the response of this command.
|
||||
@param client: the client object to retrieve from
|
||||
@return: A Message object containing the HTTP-version, status code, status message, headers and buffer
|
||||
"""
|
||||
retriever = PreambleRetriever(client)
|
||||
lines = retriever.retrieve()
|
||||
(version, status, msg) = parser.parse_status_line(next(lines))
|
||||
headers = parser.parse_headers(lines)
|
||||
|
||||
buffer = retriever.buffer
|
||||
logging.debug("---response begin---\r\n%s---response end---", "".join(buffer))
|
||||
|
||||
return Message(version, status, msg, headers, buffer)
|
||||
|
||||
def _await_response(self, client):
|
||||
"""
|
||||
Handles the response of this command.
|
||||
@@ -233,6 +215,27 @@ class GetCommand(AbstractCommand):
|
||||
self.filename = responsehandler.handle(client, msg, self, self.dir)
|
||||
|
||||
|
||||
class AbstractWithBodyCommand(AbstractCommand, ABC):
|
||||
"""
|
||||
The building block for creating an HTTP message for an HTTP method with a body (POST and PUT).
|
||||
"""
|
||||
|
||||
def _build_message(self, message: str) -> bytes:
|
||||
input_line = input(f"Enter {self.method} data: ")
|
||||
input_line += "\r\n"
|
||||
body = input_line.encode(FORMAT)
|
||||
print()
|
||||
|
||||
message += "Content-Type: text/plain\r\n"
|
||||
message += f"Content-Length: {len(body)}\r\n"
|
||||
message += "\r\n"
|
||||
message = message.encode(FORMAT)
|
||||
message += body
|
||||
message += b"\r\n"
|
||||
|
||||
return message
|
||||
|
||||
|
||||
class PostCommand(AbstractWithBodyCommand):
|
||||
"""
|
||||
A command for sending a `POST` request.
|
||||
|
@@ -1,6 +0,0 @@
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
|
||||
class HTMLParser:
|
||||
def __init__(self, soup: BeautifulSoup):
|
||||
pass
|
@@ -65,7 +65,7 @@ class ResponseHandler(ABC):
|
||||
|
||||
class BasicResponseHandler(ResponseHandler):
|
||||
"""
|
||||
Response handler which skips the body of the message and only shows the headers.
|
||||
Response handler which will handle redirects and other HTTP status codes.
|
||||
In case of a redirect, it will process it and pass it to the appropriate response handler.
|
||||
"""
|
||||
|
||||
|
Reference in New Issue
Block a user