update
This commit is contained in:
@@ -38,4 +38,10 @@ class BadRequest(HTTPServerException):
|
||||
class MethodNotAllowed(HTTPServerException):
|
||||
""" Method is not allowed """
|
||||
def __init(self, allowed_methods):
|
||||
self.allowed_methods = allowed_methods
|
||||
self.allowed_methods = allowed_methods
|
||||
|
||||
class NotImplemented(HTTPServerException):
|
||||
""" Functionality not implemented """
|
||||
|
||||
class NotFound(HTTPServerException):
|
||||
""" Resource not found """
|
@@ -19,7 +19,7 @@ class HTTPSocket:
|
||||
self.conn = conn
|
||||
self.conn.settimeout(TIMEOUT)
|
||||
self.conn.setblocking(True)
|
||||
self.conn.settimeout(3.0)
|
||||
self.conn.settimeout(60)
|
||||
self.file = self.conn.makefile("rb")
|
||||
|
||||
def close(self):
|
||||
|
@@ -7,7 +7,7 @@ from httplib.httpsocket import HTTPSocket
|
||||
|
||||
|
||||
def _get_start_line(client: HTTPSocket):
|
||||
line = client.read_line()
|
||||
line = client.read_line().strip()
|
||||
split = list(filter(None, line.split(" ")))
|
||||
if len(split) < 3:
|
||||
raise InvalidStatusLine(line) # TODO fix exception
|
||||
@@ -23,6 +23,8 @@ def _is_valid_http_version(http_version: str):
|
||||
if name != "HTTP" or not re.match(r"1\.[0|1]", version):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def get_status_line(client: HTTPSocket):
|
||||
line, (http_version, status, reason) = _get_start_line(client)
|
||||
@@ -43,17 +45,22 @@ def get_status_line(client: HTTPSocket):
|
||||
def parse_request_line(client: HTTPSocket):
|
||||
line, (method, target, version) = _get_start_line(client)
|
||||
|
||||
logging.debug("Parsed request-line=%r, method=%r, target=%r, version=%r", line, method, target, version)
|
||||
|
||||
if method not in ("CONNECT", "DELETE", "GET", "HEAD", "OPTIONS", "POST", "PUT", "TRACE"):
|
||||
raise BadRequest()
|
||||
|
||||
if not _is_valid_http_version(version):
|
||||
logging.debug("[ABRT] request: invalid http-version=%r", version)
|
||||
raise BadRequest()
|
||||
|
||||
if len(target) == "":
|
||||
raise BadRequest()
|
||||
parsed_target = urlparse(target)
|
||||
if len(parsed_target.path) > 0 and parsed_target.path[0] != "/" and parsed_target.netloc != "":
|
||||
parsed_target = urlparse(f"//{target}")
|
||||
|
||||
return method, parsed_target, version
|
||||
return method, parsed_target, version.split("/")[1]
|
||||
|
||||
|
||||
def retrieve_headers(client: HTTPSocket):
|
||||
@@ -85,13 +92,14 @@ def retrieve_headers(client: HTTPSocket):
|
||||
continue
|
||||
|
||||
(header, value) = line.split(":", 1)
|
||||
result.append((header.lower(), value.lower()))
|
||||
result.append((header.lower(), value.strip().lower()))
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def parse_request_headers(client: HTTPSocket):
|
||||
raw_headers = retrieve_headers(client)
|
||||
logging.debug("Received headers: %r", raw_headers)
|
||||
headers = {}
|
||||
|
||||
key: str
|
||||
@@ -107,7 +115,7 @@ def parse_request_headers(client: HTTPSocket):
|
||||
logging.error("Invalid content-length value: %r", value)
|
||||
raise BadRequest()
|
||||
elif key == "host":
|
||||
if value != client.host or key in headers:
|
||||
if value != client.host and value != client.host.split(":")[0] or key in headers:
|
||||
raise BadRequest()
|
||||
|
||||
headers[key] = value
|
||||
|
Reference in New Issue
Block a user