# Path: h11/_abnf.py
# OWS = r"[ \t]*"
# HEXDIG = r"[0-9A-Fa-f]"
# 
# Path: h11/_util.py
# def bytesify(s: Union[bytes, bytearray, memoryview, int, str]) -> bytes:
#     # Fast-path:
#     if type(s) is bytes:
#         return s
#     if isinstance(s, str):
#         s = s.encode("ascii")
#     if isinstance(s, int):
#         raise TypeError("expected bytes-like object, not int")
#     return bytes(s)
# 
# class LocalProtocolError(ProtocolError):
#     def _reraise_as_remote_protocol_error(self) -> NoReturn:
#         # After catching a LocalProtocolError, use this method to re-raise it
#         # as a RemoteProtocolError. This method must be called from inside an
#         # except: block.
#         #
#         # An easy way to get an equivalent RemoteProtocolError is just to
#         # modify 'self' in place.
#         self.__class__ = RemoteProtocolError  # type: ignore
#         # But the re-raising is somewhat non-trivial -- you might think that
#         # now that we've modified the in-flight exception object, that just
#         # doing 'raise' to re-raise it would be enough. But it turns out that
#         # this doesn't work, because Python tracks the exception type
#         # (exc_info[0]) separately from the exception object (exc_info[1]),
#         # and we only modified the latter. So we really do need to re-raise
#         # the new type explicitly.
#         # On py3, the traceback is part of the exception object, so our
#         # in-place modification preserved it and we can just re-raise:
#         raise self
# 
# def validate(
#     regex: Pattern[bytes], data: bytes, msg: str = "malformed data", *format_args: Any
# ) -> Dict[str, bytes]:
#     match = regex.fullmatch(data)
#     if not match:
#         if format_args:
#             msg = msg.format(*format_args)
#         raise LocalProtocolError(msg)
#     return match.groupdict()


import re
from typing import AnyStr, cast, List, overload, Sequence, Tuple, TYPE_CHECKING, Union
from ._abnf import field_name, field_value
from ._util import bytesify, LocalProtocolError, validate
    from ._events import Request
    from typing import Literal
    from typing_extensions import Literal  # type: ignore

@overload
def normalize_and_validate(headers: Headers, _parsed: Literal[True]) -> Headers:
    ...


@overload
def normalize_and_validate(headers: HeaderTypes, _parsed: Literal[False]) -> Headers:
    ...


@overload
def normalize_and_validate(
    headers: Union[Headers, HeaderTypes], _parsed: bool = False
) -> Headers:
    ...


def normalize_and_validate(
    headers: Union[Headers, HeaderTypes], _parsed: bool = False
) -> Headers:
    new_headers = []
    seen_content_length = None
    saw_transfer_encoding = False
    for name, value in headers:
        # For headers coming out of the parser, we can safely skip some steps,
        # because it always returns bytes and has already run these regexes
        # over the data:
        if not _parsed:
            name = bytesify(name)

# Path: h11/_abnf.py
# OWS = r"[ \t]*"
# HEXDIG = r"[0-9A-Fa-f]"
# 
# Path: h11/_util.py
# def bytesify(s: Union[bytes, bytearray, memoryview, int, str]) -> bytes:
#     # Fast-path:
#     if type(s) is bytes:
#         return s
#     if isinstance(s, str):
#         s = s.encode("ascii")
#     if isinstance(s, int):
#         raise TypeError("expected bytes-like object, not int")
#     return bytes(s)
# 
# class LocalProtocolError(ProtocolError):
#     def _reraise_as_remote_protocol_error(self) -> NoReturn:
#         # After catching a LocalProtocolError, use this method to re-raise it
#         # as a RemoteProtocolError. This method must be called from inside an
#         # except: block.
#         #
#         # An easy way to get an equivalent RemoteProtocolError is just to
#         # modify 'self' in place.
#         self.__class__ = RemoteProtocolError  # type: ignore
#         # But the re-raising is somewhat non-trivial -- you might think that
#         # now that we've modified the in-flight exception object, that just
#         # doing 'raise' to re-raise it would be enough. But it turns out that
#         # this doesn't work, because Python tracks the exception type
#         # (exc_info[0]) separately from the exception object (exc_info[1]),
#         # and we only modified the latter. So we really do need to re-raise
#         # the new type explicitly.
#         # On py3, the traceback is part of the exception object, so our
#         # in-place modification preserved it and we can just re-raise:
#         raise self
# 
# def validate(
#     regex: Pattern[bytes], data: bytes, msg: str = "malformed data", *format_args: Any
# ) -> Dict[str, bytes]:
#     match = regex.fullmatch(data)
#     if not match:
#         if format_args:
#             msg = msg.format(*format_args)
#         raise LocalProtocolError(msg)
#     return match.groupdict()


# Path: h11/_headers.py
import re
from typing import AnyStr, cast, List, overload, Sequence, Tuple, TYPE_CHECKING, Union
from ._abnf import field_name, field_value
from ._util import bytesify, LocalProtocolError, validate
    from ._events import Request
    from typing import Literal
    from typing_extensions import Literal  # type: ignore


@overload
def normalize_and_validate(headers: Headers, _parsed: Literal[True]) -> Headers:
    ...


@overload
def normalize_and_validate(headers: HeaderTypes, _parsed: Literal[False]) -> Headers:
    ...


@overload
def normalize_and_validate(
    headers: Union[Headers, HeaderTypes], _parsed: bool = False
) -> Headers:
    ...


def normalize_and_validate(
    headers: Union[Headers, HeaderTypes], _parsed: bool = False
) -> Headers:
    new_headers = []
    seen_content_length = None
    saw_transfer_encoding = False
    for name, value in headers:
        # For headers coming out of the parser, we can safely skip some steps,
        # because it always returns bytes and has already run these regexes
        # over the data:
        if not _parsed:
            name = bytesify(name)

            value = bytesify(value)