Improve documentation, cleanup duplicated code
This commit is contained in:
@@ -3,8 +3,6 @@ import os
|
||||
import sys
|
||||
from abc import ABC, abstractmethod
|
||||
from datetime import datetime
|
||||
from time import mktime
|
||||
from wsgiref.handlers import format_date_time
|
||||
|
||||
from httplib import parser
|
||||
from httplib.exceptions import NotFound, Forbidden, NotModified
|
||||
@@ -62,14 +60,6 @@ class AbstractCommand(ABC):
|
||||
def _conditional_headers(self):
|
||||
pass
|
||||
|
||||
def _get_date(self):
|
||||
"""
|
||||
Returns a string representation of the current date according to RFC 1123.
|
||||
"""
|
||||
now = datetime.now()
|
||||
stamp = mktime(now.timetuple())
|
||||
return format_date_time(stamp)
|
||||
|
||||
@abstractmethod
|
||||
def execute(self):
|
||||
pass
|
||||
@@ -81,7 +71,7 @@ class AbstractCommand(ABC):
|
||||
self._process_conditional_headers()
|
||||
|
||||
message = f"HTTP/1.1 {status} {status_message[status]}\r\n"
|
||||
message += f"Date: {self._get_date()}\r\n"
|
||||
message += f"Date: {parser.get_date()}\r\n"
|
||||
|
||||
content_length = len(body)
|
||||
message += f"Content-Length: {content_length}\r\n"
|
||||
|
@@ -1,12 +1,9 @@
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
from datetime import datetime
|
||||
from socket import socket
|
||||
from time import mktime
|
||||
from typing import Union
|
||||
from urllib.parse import ParseResultBytes, ParseResult
|
||||
from wsgiref.handlers import format_date_time
|
||||
|
||||
from httplib import parser
|
||||
from httplib.exceptions import MethodNotAllowed, BadRequest, UnsupportedEncoding, NotImplemented, NotFound, \
|
||||
@@ -21,8 +18,11 @@ METHODS = ("GET", "HEAD", "PUT", "POST")
|
||||
|
||||
|
||||
class RequestHandler:
|
||||
"""
|
||||
Processes incoming HTTP request messages.
|
||||
"""
|
||||
|
||||
conn: HTTPSocket
|
||||
root = os.path.join(os.path.dirname(sys.argv[0]), "public")
|
||||
|
||||
def __init__(self, conn: socket, host):
|
||||
self.conn = ServerSocket(conn, host)
|
||||
@@ -42,15 +42,20 @@ class RequestHandler:
|
||||
|
||||
def _handle_message(self, retriever, line):
|
||||
lines = retriever.retrieve()
|
||||
|
||||
# Parse the request-line and headers
|
||||
(method, target, version) = parser.parse_request_line(line)
|
||||
headers = parser.parse_headers(lines)
|
||||
|
||||
# Create the response message object
|
||||
message = Message(version, method, target, headers, retriever.buffer)
|
||||
|
||||
logging.debug("---request begin---\r\n%s---request end---", "".join(message.raw))
|
||||
|
||||
# validate if the request is valid
|
||||
self._validate_request(message)
|
||||
|
||||
# The body (if available) hasn't been retrieved up till now.
|
||||
body = b""
|
||||
if self._has_body(headers):
|
||||
try:
|
||||
@@ -64,14 +69,25 @@ class RequestHandler:
|
||||
|
||||
message.body = body
|
||||
|
||||
# completed message
|
||||
|
||||
# message completed
|
||||
cmd = command.create(message)
|
||||
msg = cmd.execute()
|
||||
|
||||
logging.debug("---response begin---\r\n%s\r\n---response end---", msg.split(b"\r\n\r\n", 1)[0].decode(FORMAT))
|
||||
# Send the response message
|
||||
self.conn.conn.sendall(msg)
|
||||
|
||||
def _check_request_line(self, method: str, target: Union[ParseResultBytes, ParseResult], version):
|
||||
"""
|
||||
Checks if the request-line is valid. Throws an appriopriate exception if not.
|
||||
@param method: HTTP request method
|
||||
@param target: The request target
|
||||
@param version: The HTTP version
|
||||
@raise MethodNotAllowed: if the method is not any of the allowed methods in `METHODS`
|
||||
@raise HTTPVersionNotSupported: If the HTTP version is not supported by this server
|
||||
@raise BadRequest: If the scheme of the target is not supported
|
||||
@raise NotFound: If the target is not found on this server
|
||||
"""
|
||||
|
||||
if method not in METHODS:
|
||||
raise MethodNotAllowed(METHODS)
|
||||
@@ -91,12 +107,25 @@ class RequestHandler:
|
||||
raise NotFound(str(target))
|
||||
|
||||
def _validate_request(self, msg):
|
||||
"""
|
||||
Validates the message request-line and headers. Throws an error if the message is invalid.
|
||||
|
||||
@see: _check_request_line for exceptions raised when validating the request-line.
|
||||
@param msg: the message to validate
|
||||
@raise BadRequest: if HTTP 1.1 and the Host header is missing
|
||||
"""
|
||||
|
||||
if msg.version == "1.1" and "host" not in msg.headers:
|
||||
raise BadRequest("Missing host header")
|
||||
|
||||
self._check_request_line(msg.method, msg.target, msg.version)
|
||||
|
||||
def _has_body(self, headers):
|
||||
"""
|
||||
Check if the headers notify the existing of a message body.
|
||||
@param headers: the headers to check
|
||||
@return: True if the message has a body. False otherwise.
|
||||
"""
|
||||
|
||||
if "transfer-encoding" in headers:
|
||||
return True
|
||||
@@ -106,16 +135,10 @@ class RequestHandler:
|
||||
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def _get_date():
|
||||
now = datetime.now()
|
||||
stamp = mktime(now.timetuple())
|
||||
return format_date_time(stamp)
|
||||
|
||||
@staticmethod
|
||||
def send_error(client: socket, code, message):
|
||||
message = f"HTTP/1.1 {code} {message}\r\n"
|
||||
message += RequestHandler._get_date() + "\r\n"
|
||||
message += parser.get_date() + "\r\n"
|
||||
message += "Content-Length: 0\r\n"
|
||||
message += "\r\n"
|
||||
|
||||
|
Reference in New Issue
Block a user