This commit is contained in:
2021-03-26 18:25:03 +01:00
parent 7476870acc
commit fdbd865889
11 changed files with 297 additions and 136 deletions

View File

@@ -3,7 +3,7 @@ import os.path
import re
from urllib.parse import urlparse, urlsplit
from httplib.exceptions import InvalidStatusLine, InvalidResponse, BadRequest
from httplib.exceptions import InvalidStatusLine, InvalidResponse, BadRequest, InvalidRequestLine
from httplib.httpsocket import HTTPSocket
@@ -48,7 +48,7 @@ def parse_status_line(line: str):
if len(split) < 3:
raise InvalidStatusLine(line) # TODO fix exception
(http_version, status, reason) = split
http_version, status, reason = split
if not _is_valid_http_version(http_version):
raise InvalidStatusLine(line)
@@ -63,11 +63,12 @@ def parse_status_line(line: str):
return version, status, reason
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)
def parse_request_line(line: str):
split = list(filter(None, line.rstrip().split(" ", 2)))
if len(split) < 3:
raise InvalidRequestLine(line)
method, target, version = split
if method not in ("CONNECT", "DELETE", "GET", "HEAD", "OPTIONS", "POST", "PUT", "TRACE"):
raise BadRequest()
@@ -146,7 +147,7 @@ def parse_request_headers(client: HTTPSocket):
def get_headers(client: HTTPSocket):
headers = []
# first header after the status-line may not contain a space
# first header after the status-line may not start with a space
while True:
line = client.read_line()
if line[0].isspace():
@@ -181,21 +182,28 @@ def get_headers(client: HTTPSocket):
def parse_headers(lines):
headers = []
# first header after the status-line may not contain a space
for line in lines:
if line[0].isspace():
continue
else:
break
for line in lines:
if line in ("\r\n", "\n", " "):
break
try:
# first header after the start-line may not start with a space
line = next(lines)
while True:
if line[0].isspace():
continue
else:
break
if line[0].isspace():
headers[-1] = headers[-1].rstrip("\r\n")
while True:
if line in ("\r\n", "\n", ""):
break
headers.append(line.lstrip())
if line[0].isspace():
headers[-1] = headers[-1].rstrip("\r\n")
headers.append(line.lstrip())
line = next(lines)
except StopIteration:
# No more lines to be parsed
pass
result = {}
header_str = "".join(headers)