Fix small issues, improve error handling and documentation
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import logging
|
||||
import os
|
||||
import pathlib
|
||||
import re
|
||||
import urllib
|
||||
from typing import Dict
|
||||
@@ -85,6 +87,11 @@ 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.
|
||||
"""
|
||||
headers = []
|
||||
|
||||
try:
|
||||
@@ -127,17 +134,21 @@ def parse_headers(lines):
|
||||
def check_next_header(headers, next_header: str, next_value: str):
|
||||
if next_header == "content-length":
|
||||
if "content-length" in headers:
|
||||
logging.error("Multiple content-length headers specified")
|
||||
raise InvalidResponse()
|
||||
raise InvalidResponse("Multiple content-length headers specified")
|
||||
if not next_value.isnumeric() or int(next_value) <= 0:
|
||||
logging.error("Invalid content-length value: %r", next_value)
|
||||
raise InvalidResponse()
|
||||
raise InvalidResponse(f"Invalid content-length value: {next_value}")
|
||||
|
||||
|
||||
def parse_uri(uri: str):
|
||||
"""
|
||||
Parse the specified URI into the host, port and path.
|
||||
If the URI is invalid, this method will try to create one.
|
||||
@param uri: the URI to be parsed
|
||||
@return: A tuple with the host, port and path
|
||||
"""
|
||||
parsed = urlsplit(uri)
|
||||
|
||||
# If there is no netloc, the given string is not a valid URI, so split on /
|
||||
# If there is no hostname, the given string is not a valid URI, so split on /
|
||||
if parsed.hostname:
|
||||
host = parsed.hostname
|
||||
path = parsed.path
|
||||
@@ -180,6 +191,12 @@ def urljoin(base, url):
|
||||
|
||||
|
||||
def get_charset(headers: Dict[str, str]):
|
||||
"""
|
||||
Returns the charset of the content from the headers if found. Otherwise returns `FORMAT`
|
||||
|
||||
@param headers: the headers to retrieve the charset from
|
||||
@return: A charset
|
||||
"""
|
||||
if "content-type" in headers:
|
||||
content_type = headers["content-type"]
|
||||
match = re.search(r"charset\s*=\s*([a-z\-0-9]*)", content_type, re.I)
|
||||
@@ -187,3 +204,17 @@ def get_charset(headers: Dict[str, str]):
|
||||
return match.group(1)
|
||||
|
||||
return FORMAT
|
||||
|
||||
|
||||
def get_relative_save_path(path: str):
|
||||
"""
|
||||
Returns the specified path relative to the working directory.
|
||||
|
||||
@param path: the path to compute
|
||||
@return: the relative path
|
||||
"""
|
||||
|
||||
path_obj = pathlib.PurePath(path)
|
||||
root = pathlib.PurePath(os.getcwd())
|
||||
rel = path_obj.relative_to(root)
|
||||
return str(rel)
|
||||
|
Reference in New Issue
Block a user