Improve documentation, cleanup duplicated code
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
class HTTPException(Exception):
|
||||
""" Base class for HTTP exceptions """
|
||||
"""
|
||||
Base class for HTTP exceptions
|
||||
"""
|
||||
|
||||
|
||||
class UnhandledHTTPCode(Exception):
|
||||
"""
|
||||
Exception thrown if HTTP codes are not further processed.
|
||||
"""
|
||||
status_code: str
|
||||
headers: str
|
||||
cause: str
|
||||
@@ -40,10 +45,12 @@ class UnsupportedEncoding(HTTPException):
|
||||
self.enc_type = enc_type
|
||||
self.encoding = encoding
|
||||
|
||||
|
||||
class UnsupportedProtocol(HTTPException):
|
||||
"""
|
||||
Protocol is not supported
|
||||
"""
|
||||
|
||||
def __init__(self, protocol):
|
||||
self.protocol = protocol
|
||||
|
||||
@@ -54,7 +61,9 @@ class IncompleteResponse(HTTPException):
|
||||
|
||||
|
||||
class HTTPServerException(HTTPException):
|
||||
""" Base class for HTTP Server exceptions """
|
||||
"""
|
||||
Base class for HTTP Server exceptions
|
||||
"""
|
||||
status_code: str
|
||||
message: str
|
||||
body: str
|
||||
@@ -66,29 +75,39 @@ class HTTPServerException(HTTPException):
|
||||
|
||||
|
||||
class HTTPServerCloseException(HTTPServerException):
|
||||
""" When thrown, the connection should be closed """
|
||||
"""
|
||||
When raised, the connection should be closed
|
||||
"""
|
||||
|
||||
|
||||
class BadRequest(HTTPServerCloseException):
|
||||
""" Malformed HTTP request"""
|
||||
"""
|
||||
Malformed HTTP request
|
||||
"""
|
||||
status_code = 400
|
||||
message = "Bad Request"
|
||||
|
||||
|
||||
class Forbidden(HTTPServerException):
|
||||
""" Request not allowed """
|
||||
"""
|
||||
Request not allowed
|
||||
"""
|
||||
status_code = 403
|
||||
message = "Forbidden"
|
||||
|
||||
|
||||
class NotFound(HTTPServerException):
|
||||
""" Resource not found """
|
||||
"""
|
||||
Resource not found
|
||||
"""
|
||||
status_code = 404
|
||||
message = "Not Found"
|
||||
|
||||
|
||||
class MethodNotAllowed(HTTPServerException):
|
||||
""" Method is not allowed """
|
||||
"""
|
||||
Method is not allowed
|
||||
"""
|
||||
status_code = 405
|
||||
message = "Method Not Allowed"
|
||||
|
||||
@@ -97,37 +116,49 @@ class MethodNotAllowed(HTTPServerException):
|
||||
|
||||
|
||||
class InternalServerError(HTTPServerCloseException):
|
||||
""" Internal Server Error """
|
||||
"""
|
||||
Internal Server Error
|
||||
"""
|
||||
status_code = 500
|
||||
message = "Internal Server Error"
|
||||
|
||||
|
||||
class NotImplemented(HTTPServerException):
|
||||
""" Functionality not implemented """
|
||||
"""
|
||||
Functionality not implemented
|
||||
"""
|
||||
status_code = 501
|
||||
message = "Not Implemented"
|
||||
|
||||
|
||||
class HTTPVersionNotSupported(HTTPServerCloseException):
|
||||
""" The server does not support the major version HTTP used in the request message """
|
||||
"""
|
||||
The server does not support the major version HTTP used in the request message
|
||||
"""
|
||||
status_code = 505
|
||||
message = "HTTP Version Not Supported"
|
||||
|
||||
|
||||
class Conflict(HTTPServerException):
|
||||
""" Conflict in the current state of the target resource """
|
||||
"""
|
||||
Conflict in the current state of the target resource
|
||||
"""
|
||||
status_code = 409
|
||||
message = "Conflict"
|
||||
|
||||
|
||||
class NotModified(HTTPServerException):
|
||||
""" Requested resource was not modified """
|
||||
"""
|
||||
Requested resource was not modified
|
||||
"""
|
||||
status_code = 304
|
||||
message = "Not Modified"
|
||||
|
||||
|
||||
class InvalidRequestLine(BadRequest):
|
||||
""" Request start-line is invalid """
|
||||
"""
|
||||
Request start-line is invalid
|
||||
"""
|
||||
|
||||
def __init__(self, line):
|
||||
self.request_line = line
|
||||
|
@@ -3,8 +3,11 @@ import os
|
||||
import pathlib
|
||||
import re
|
||||
import urllib
|
||||
from datetime import datetime
|
||||
from time import mktime
|
||||
from typing import Dict
|
||||
from urllib.parse import urlparse, urlsplit
|
||||
from wsgiref.handlers import format_date_time
|
||||
|
||||
from httplib.exceptions import InvalidStatusLine, InvalidResponse, BadRequest, InvalidRequestLine
|
||||
from httplib.httpsocket import FORMAT
|
||||
@@ -58,7 +61,7 @@ def parse_status_line(line: str):
|
||||
|
||||
def parse_request_line(line: str):
|
||||
"""
|
||||
Parses the specified line as and HTTP request-line.
|
||||
Parses the specified line as an HTTP request-line.
|
||||
Returns the method, target as ParseResult and HTTP version from the request-line.
|
||||
|
||||
@param line: the request-line to be parsed
|
||||
@@ -89,6 +92,7 @@ def parse_request_line(line: str):
|
||||
def parse_headers(lines):
|
||||
"""
|
||||
Parses the lines from the `lines` iterator as headers.
|
||||
|
||||
@param lines: iterator to retrieve the lines from.
|
||||
@return: A dictionary with header as key and value as value.
|
||||
"""
|
||||
@@ -218,3 +222,12 @@ def get_relative_save_path(path: str):
|
||||
root = pathlib.PurePath(os.getcwd())
|
||||
rel = path_obj.relative_to(root)
|
||||
return str(rel)
|
||||
|
||||
|
||||
def get_date():
|
||||
"""
|
||||
Returns a string representation of the current date according to RFC 1123.
|
||||
"""
|
||||
now = datetime.now()
|
||||
stamp = mktime(now.timetuple())
|
||||
return format_date_time(stamp)
|
||||
|
Reference in New Issue
Block a user