diff --git a/kiss_headers/api.py b/kiss_headers/api.py index 1e4f18e..a521fe2 100644 --- a/kiss_headers/api.py +++ b/kiss_headers/api.py @@ -18,6 +18,7 @@ is_content_json_object, is_legal_header_name, normalize_str, + transform_possible_encoded, ) T = TypeVar("T", bound=CustomHeader, covariant=True) @@ -31,7 +32,7 @@ def parse_it(raw_headers: Any) -> Headers: TypeError: If passed argument cannot be parsed to extract headers from it. """ - headers: Optional[Iterable[Tuple[str, Any]]] = None + headers: Optional[Iterable[Tuple[Union[str, bytes], Union[str, bytes]]]] = None if isinstance(raw_headers, str): if raw_headers.startswith("{") and raw_headers.endswith("}"): @@ -71,7 +72,9 @@ def parse_it(raw_headers: Any) -> Headers: ) ) - revised_headers: List[Tuple[str, str]] = decode_partials(headers) + revised_headers: List[Tuple[str, str]] = decode_partials( + transform_possible_encoded(headers) + ) # Sometime raw content does not begin with headers. If that is the case, search for the next line. if ( diff --git a/kiss_headers/models.py b/kiss_headers/models.py index a909f52..266b160 100644 --- a/kiss_headers/models.py +++ b/kiss_headers/models.py @@ -28,7 +28,7 @@ class Header(object): """ # Most common attribute that are associated with value in headers. - # Used for type hint, auto completion purpose + # Used for type hint, auto-completion purpose charset: str format: str boundary: str @@ -39,6 +39,11 @@ class Header(object): samesite: str domain: str filename: str + to: str + report_to: str + endpoints: str + max_age: str + group: str def __init__(self, name: str, content: str): """ @@ -619,6 +624,8 @@ class Headers(object): alt_svc: Union[Header, List[Header]] + location: Union[Header, List[Header]] + age: Union[Header, List[Header]] cache_control: Union[Header, List[Header]] clear_site_data: Union[Header, List[Header]] @@ -650,8 +657,9 @@ class Headers(object): set_cookie: Union[Header, List[Header]] content_disposition: Union[Header, List[Header]] - content_type: Union[Header, List[Header]] + content_range: Union[Header, List[Header]] + content_encoding: Union[Header, List[Header]] host: Union[Header, List[Header]] referer: Union[Header, List[Header]] diff --git a/kiss_headers/utils.py b/kiss_headers/utils.py index 5647270..31b2037 100644 --- a/kiss_headers/utils.py +++ b/kiss_headers/utils.py @@ -1,6 +1,6 @@ from email.header import decode_header from re import findall, search, sub -from typing import Any, Iterable, List, Optional, Set, Tuple, Type +from typing import Any, Iterable, List, Optional, Set, Tuple, Type, Union RESERVED_KEYWORD: Set[str] = { "and_", @@ -461,3 +461,18 @@ def is_content_json_object(content: str) -> bool: return (content.startswith("{") and content.endswith("}")) or ( content.startswith("[") and content.endswith("]") ) + + +def transform_possible_encoded( + headers: Iterable[Tuple[Union[str, bytes], Union[str, bytes]]] +) -> Iterable[Tuple[str, str]]: + decoded = [] + + for k, v in headers: + if isinstance(k, bytes): + k = k.decode("utf_8") + if isinstance(v, bytes): + v = v.decode("utf_8") + decoded.append((k, v)) + + return decoded diff --git a/kiss_headers/version.py b/kiss_headers/version.py index 06f9e53..a005319 100644 --- a/kiss_headers/version.py +++ b/kiss_headers/version.py @@ -2,5 +2,5 @@ Expose version """ -__version__ = "2.4.0" +__version__ = "2.4.1" VERSION = __version__.split(".")