Update
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import logging
|
||||
import re
|
||||
from urllib.parse import urlparse
|
||||
from urllib.parse import urlparse, urlsplit
|
||||
|
||||
from httplib.exceptions import InvalidStatusLine, InvalidResponse, BadRequest
|
||||
from httplib.httpsocket import HTTPSocket
|
||||
@@ -42,6 +42,26 @@ def get_status_line(client: HTTPSocket):
|
||||
return version, status, reason
|
||||
|
||||
|
||||
def parse_status_line(line: str):
|
||||
split = list(filter(None, line.strip().split(" ", 2)))
|
||||
if len(split) < 3:
|
||||
raise InvalidStatusLine(line) # TODO fix exception
|
||||
|
||||
(http_version, status, reason) = split
|
||||
|
||||
if not _is_valid_http_version(http_version):
|
||||
raise InvalidStatusLine(line)
|
||||
version = http_version[:4]
|
||||
|
||||
if not re.match(r"\d{3}", status):
|
||||
raise InvalidStatusLine(line)
|
||||
status = int(status)
|
||||
if status < 100 or status > 999:
|
||||
raise InvalidStatusLine(line)
|
||||
|
||||
return version, status, reason
|
||||
|
||||
|
||||
def parse_request_line(client: HTTPSocket):
|
||||
line, (method, target, version) = _get_start_line(client)
|
||||
|
||||
@@ -119,7 +139,7 @@ def parse_request_headers(client: HTTPSocket):
|
||||
raise BadRequest()
|
||||
|
||||
headers[key] = value
|
||||
|
||||
|
||||
return headers
|
||||
|
||||
|
||||
@@ -157,6 +177,38 @@ def get_headers(client: HTTPSocket):
|
||||
|
||||
return result
|
||||
|
||||
def parse_headers(lines):
|
||||
headers = []
|
||||
# first header after the status-line may not contain a space
|
||||
for line in lines:
|
||||
line = next(lines)
|
||||
if line[0].isspace():
|
||||
continue
|
||||
else:
|
||||
break
|
||||
|
||||
for line in lines:
|
||||
if line in ("\r\n", "\n", " "):
|
||||
break
|
||||
|
||||
if line[0].isspace():
|
||||
headers[-1] = headers[-1].rstrip("\r\n")
|
||||
|
||||
headers.append(line.lstrip())
|
||||
|
||||
result = {}
|
||||
header_str = "".join(headers)
|
||||
for line in header_str.splitlines():
|
||||
pos = line.find(":")
|
||||
|
||||
if pos <= 0 or pos >= len(line) - 1:
|
||||
continue
|
||||
|
||||
(header, value) = map(str.strip, line.split(":", 1))
|
||||
check_next_header(result, header, value)
|
||||
result[header.lower()] = value.lower()
|
||||
|
||||
return result
|
||||
|
||||
def check_next_header(headers, next_header: str, next_value: str):
|
||||
if next_header == "content-length":
|
||||
@@ -166,3 +218,25 @@ def check_next_header(headers, next_header: str, next_value: str):
|
||||
if not next_value.isnumeric() or int(next_value) <= 0:
|
||||
logging.error("Invalid content-length value: %r", next_value)
|
||||
raise InvalidResponse()
|
||||
|
||||
|
||||
def parse_uri(uri: str):
|
||||
parsed = urlsplit(uri)
|
||||
|
||||
# If there is no netloc, the given string is not a valid URI, so split on /
|
||||
if parsed.hostname:
|
||||
host = parsed.hostname
|
||||
path = parsed.path
|
||||
if parsed.query != '':
|
||||
path = f"{path}?{parsed.query}"
|
||||
else:
|
||||
(host, path) = uri.split("/", 1)
|
||||
|
||||
if ":" in host:
|
||||
host, port = host.split(":", 1)
|
||||
elif parsed.scheme == "https":
|
||||
port = 443
|
||||
else:
|
||||
port = 80
|
||||
|
||||
return host, port, path
|
||||
|
Reference in New Issue
Block a user