from collections.abc import Iterable, Mapping
from io import BytesIO
from re import Pattern
from typing import Any, BinaryIO, overload
from typing_extensions import Self

from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.models import AnonymousUser
from django.contrib.sessions.backends.base import SessionBase
from django.contrib.sites.models import Site
from django.core.files import uploadedfile, uploadhandler
from django.urls import ResolverMatch
from django.utils.datastructures import (
    CaseInsensitiveMapping,
    ImmutableList,
    MultiValueDict,
)

RAISE_ERROR: object = ...
host_validation_re: Pattern[str] = ...

class UnreadablePostError(IOError): ...
class RawPostDataException(Exception): ...

UploadHandlerList = (
    list[uploadhandler.FileUploadHandler]
    | ImmutableList[uploadhandler.FileUploadHandler]
)

class HttpHeaders(CaseInsensitiveMapping[str, str]):
    HTTP_PREFIX: str = ...
    UNPREFIXED_HEADERS: set[str] = ...
    def __init__(self, environ: Mapping[str, Any]) -> None: ...
    @classmethod
    def parse_header_name(cls, header: str) -> str | None: ...

class HttpRequest(BytesIO):
    GET: QueryDict = ...
    POST: QueryDict = ...
    COOKIES: dict[str, str] = ...
    META: dict[str, Any] = ...
    FILES: MultiValueDict[str, uploadedfile.UploadedFile] = ...
    path: str = ...
    path_info: str = ...
    method: str | None = ...
    resolver_match: ResolverMatch = ...
    content_type: str | None = ...
    content_params: dict[str, str] | None = ...
    user: AbstractBaseUser | AnonymousUser
    site: Site
    session: SessionBase
    encoding: str | None = ...
    upload_handlers: UploadHandlerList = ...
    def __init__(self) -> None: ...
    def get_host(self) -> str: ...
    def get_port(self) -> str: ...
    def get_full_path(self, force_append_slash: bool = ...) -> str: ...
    def get_full_path_info(self, force_append_slash: bool = ...) -> str: ...
    def get_signed_cookie(
        self,
        key: str,
        default: Any = ...,
        salt: str = ...,
        max_age: int | None = ...,
    ) -> str | None: ...
    def get_raw_uri(self) -> str: ...
    def build_absolute_uri(self, location: str | None = ...) -> str: ...
    @property
    def scheme(self) -> str | None: ...
    def is_secure(self) -> bool: ...
    def is_ajax(self) -> bool: ...
    def parse_file_upload(
        self, META: Mapping[str, Any], post_data: BinaryIO
    ) -> tuple[QueryDict, MultiValueDict[str, uploadedfile.UploadedFile]]: ...
    @property
    def headers(self) -> HttpHeaders: ...
    @property
    def body(self) -> bytes: ...
    def _load_post_and_files(self) -> None: ...

class QueryDict(MultiValueDict[str, str]):
    encoding: str = ...
    _mutable: bool = ...
    def __init__(
        self,
        query_string: str | bytes | None = ...,
        mutable: bool = ...,
        encoding: str | None = ...,
    ) -> None: ...
    def setlist(self, key: str, list_: list[str]) -> None: ...
    def setlistdefault(
        self, key: str, default_list: list[str] | None = ...
    ) -> list[str]: ...
    def appendlist(self, key: str, value: str) -> None: ...
    def urlencode(self, safe: str | None = ...) -> str: ...
    @classmethod
    def fromkeys(
        cls,
        iterable: Iterable[bytes | str],
        value: Any = ...,
        mutable: bool = ...,
        encoding: str | None = ...,
    ) -> Self: ...

@overload
def bytes_to_text(s: bytes, encoding: str) -> str: ...
@overload
def bytes_to_text(s: str, encoding: str) -> str: ...
@overload
def bytes_to_text(s: None, encoding: str) -> None: ...
def split_domain_port(host: str) -> tuple[str, str]: ...
def validate_host(host: str, allowed_hosts: Iterable[str]) -> bool: ...
