you cannot access rawpostdata from a request that has multipart post data if it has been accessed via post files etc a basic http request the encoding used in getpost dicts none means use default setting encoding none uploadhandlers def initself warning the wsgirequest subclass doesn t call super any variable assignment made here should also happen in wsgirequest init self get querydictmutabletrue self post querydictmutabletrue self cookies self meta self files multivaluedict self path self pathinfo self method none self resolvermatch none self contenttype none self contentparams none def reprself if self method is none or not self getfullpath return s self class name return s s r self class name self method self getfullpath cachedproperty def headersself return httpheadersself meta cachedproperty def acceptedtypesself set contenttype contentparams and encoding self contenttype self contentparams parseheaderparameters meta getcontenttype if charset in self contentparams try codecs lookupself contentparamscharset except lookuperror pass else self encoding self contentparamscharset def getrawhostself we try three options in order of decreasing preference if settings usexforwardedhost and httpxforwardedhost in self meta host self metahttpxforwardedhost elif httphost in self meta host self metahttphost else reconstruct the host using the algorithm from pep 333 host self metaservername serverport self getport if serverport 443 if self issecure else 80 host s s host serverport return host def gethostself allow variants of localhost if allowedhosts is empty and debugtrue return the port number for the request as a string if settings usexforwardedport and httpxforwardedport in self meta port self metahttpxforwardedport else port self metaserverport return strport def getfullpathself forceappendslashfalse return self getfullpathself path forceappendslash def getfullpathinfoself forceappendslashfalse return self getfullpathself pathinfo forceappendslash def getfullpathself path forceappendslash rfc 3986 requires query string arguments to be in the ascii range rather than crash if this doesn t happen we encode defensively return sss escapeuripathpath if forceappendslash and not path endswith else iritouriself meta getquerystring if self meta getquerystring else def getsignedcookieself key defaultraiseerror salt maxagenone try cookievalue self cookieskey except keyerror if default is not raiseerror return default else raise try value signing getcookiesignersaltkey salt unsign cookievalue maxagemaxage except signing badsignature if default is not raiseerror return default else raise return value def buildabsoluteuriself locationnone if location is none make it an absolute url but schemeless and domainless for the edge case that the path starts with location s self getfullpath else coerce lazy locations location strlocation bits urlsplitlocation if not bits scheme and bits netloc handle the simple most common case if the location is absolute and a scheme or host netloc isn t provided skip an expensive urljoin as long as no path segments are or if bits path startswith and not bits scheme and not bits netloc and not in bits path and not in bits path if location starts with but has no netloc reuse the schema and netloc from the current request strip the double slashes and continue as if it wasn t specified location self currentschemehost location removeprefix else join the constructed url with the provided location which allows the provided location to apply query strings to the base path location urljoinself currentschemehost self path location return iritourilocation cachedproperty def currentschemehostself return formatself scheme self gethost def getschemeself return http property def schemeself if settings secureproxysslheader try header securevalue settings secureproxysslheader except valueerror raise improperlyconfigured the secureproxysslheader setting must be a tuple containing two values headervalue self meta getheader if headervalue is not none headervalue headervalue split 1 return https if headervalue strip securevalue else http return self getscheme def issecureself return self scheme https property def encodingself return self encoding encoding setter def encodingself val self encoding val if hasattrself get del self get if hasattrself post del self post def initializehandlersself self uploadhandlers uploadhandler loadhandlerhandler self for handler in settings fileuploadhandlers property def uploadhandlersself if not self uploadhandlers if there are no upload handlers defined initialize them from settings self initializehandlers return self uploadhandlers uploadhandlers setter def uploadhandlersself uploadhandlers if hasattrself files raise attributeerror you cannot set the upload handlers after the upload has been processed self uploadhandlers uploadhandlers def parsefileuploadself meta postdata limit the maximum request data size that will be handled inmemory populate self post and self files if the contenttype is a form type if self method post self post self files querydictencodingself encoding multivaluedict return if self readstarted and not hasattrself body self markpostparseerror return if self contenttype multipartformdata if hasattrself body use already read data data bytesioself body else data self try self post self files self parsefileuploadself meta data except multipartparsererror toomanyfilessent an error occurred while parsing post data since when formatting the error the request handler might access self post set self post and self file to prevent attempts to parse post data again self markpostparseerror raise elif self contenttype applicationxwwwformurlencoded according to rfc 1866 the applicationxwwwformurlencoded content type does not have a charset and should be always treated as utf8 if self encoding is not none and self encoding lower utf8 raise badrequest http requests with the applicationxwwwformurlencoded content type must be utf8 encoded self post querydictself body encodingutf8 self files multivaluedict else self post self files querydictencodingself encoding multivaluedict def closeself if hasattrself files for f in chain fromiterablelist1 for list in self files lists f close filelike and iterator interface expects self stream to be set to an appropriate source of bytes by a corresponding request subclass e g wsgirequest also when request data has already been read by request post or request body self stream points to a bytesio instance containing that data def readself args kwargs self readstarted true try return self stream readargs kwargs except oserror as e raise unreadableposterrore args from e def readlineself args kwargs self readstarted true try return self stream readlineargs kwargs except oserror as e raise unreadableposterrore args from e def iterself return iterself readline b def readlinesself return listself class httpheaderscaseinsensitivemapping httpprefix http pep 333 gives two headers which aren t prepended with http unprefixedheaders contenttype contentlength def initself environ headers for header value in environ items name self parseheadernameheader if name headersname value super initheaders def getitemself key a specialized multivaluedict which represents a query string a querydict can be used to represent get or post data it subclasses multivaluedict since keys in such data can be repeated for instance in the data from a form with a select multiple field by default querydicts are immutable though the copy method will always return a mutable copy both keys and values set on this class are converted from the given encoding defaultcharset by default to str these are both reset in init but is specified here at the class level so that unpickling will have valid values querystring normally contains urlencoded data a subset of ascii but some user agents are misbehaving valueerror can also be raised if the strictparsing argument to parseqsl is true as that is not used by django assume that the exception was raised by exceeding the value of maxnumfields instead of fragile checks of exception message strings return a new querydict with keys may be repeated from an iterable and values from value return a mutable copy of this object return self deepcopy def urlencodeself safenone output if safe safe safe encodeself encoding def encodek v return ss quotek safe quotev safe else def encodek v return urlencodek v for k list in self lists output extend encodek encodeself encoding strv encodeself encoding for v in list return joinoutput class mediatype def initself mediatyperawline fulltype self params parseheaderparameters mediatyperawline if mediatyperawline else self maintype self subtype fulltype partition def strself paramsstr join ss k v for k v in self params items return sss self maintype s self subtype if self subtype else paramsstr def reprself return s s self class qualname self property def isalltypesself return self maintype and self subtype def matchself other if self isalltypes return true other mediatypeother if self maintype other maintype and self subtype in other subtype return true return false it s neither necessary nor appropriate to use django utils encoding forcestr for parsing urls and form inputs thus this slightly more restricted function used by querydict def bytestotexts encoding if isinstances bytes return strs encoding replace else return s def splitdomainporthost if match hostvalidationre fullmatchhost lower domain port match groupsdefault remove a trailing dot if present from the domain return domain removesuffix port return def validatehosthost allowedhosts return any pattern or issamedomainhost pattern for pattern in allowedhosts def parseacceptheaderheader return mediatypetoken for token in header split if token strip you cannot access raw_post_data from a request that has multipart post data if it has been accessed via post files etc a basic http request the encoding used in get post dicts none means use default setting warning the wsgirequest subclass doesn t call super any variable assignment made here should also happen in wsgirequest __init__ return a list of mediatype instances set content_type content_params and encoding return the http host using the environment or request headers skip allowed hosts protection so may return an insecure host we try three options in order of decreasing preference reconstruct the host using the algorithm from pep 333 return the http host using the environment or request headers allow variants of localhost if allowed_hosts is empty and debug true return the port number for the request as a string rfc 3986 requires query string arguments to be in the ascii range rather than crash if this doesn t happen we encode defensively attempt to return a signed cookie if the signature fails or the cookie has expired raise an exception unless the default argument is provided in which case return that value build an absolute uri from the location and the variables available in this request if no location is specified build the absolute uri using request get_full_path if the location is absolute convert it to an rfc 3987 compliant uri and return it if location is relative or is scheme relative i e example com urljoin it to a base url constructed from the request variables make it an absolute url but schemeless and domainless for the edge case that the path starts with coerce lazy locations handle the simple most common case if the location is absolute and a scheme or host netloc isn t provided skip an expensive urljoin as long as no path segments are or if location starts with but has no netloc reuse the schema and netloc from the current request strip the double slashes and continue as if it wasn t specified join the constructed url with the provided location which allows the provided location to apply query strings to the base path hook for subclasses like wsgirequest to implement return http by default set the encoding used for get post accesses if the get or post dictionary has already been created remove and recreate it on the next access so that it is decoded correctly if there are no upload handlers defined initialize them from settings return a tuple of post querydict files multivaluedict limit the maximum request data size that will be handled in memory populate self _post and self _files if the content type is a form type use already read data an error occurred while parsing post data since when formatting the error the request handler might access self post set self _post and self _file to prevent attempts to parse post data again according to rfc 1866 the application x www form urlencoded content type does not have a charset and should be always treated as utf 8 file like and iterator interface expects self _stream to be set to an appropriate source of bytes by a corresponding request subclass e g wsgirequest also when request data has already been read by request post or request body self _stream points to a bytesio instance containing that data pep 333 gives two headers which aren t prepended with http_ allow header lookup using underscores in place of hyphens a specialized multivaluedict which represents a query string a querydict can be used to represent get or post data it subclasses multivaluedict since keys in such data can be repeated for instance in the data from a form with a select multiple field by default querydicts are immutable though the copy method will always return a mutable copy both keys and values set on this class are converted from the given encoding default_charset by default to str these are both reset in __init__ but is specified here at the class level so that unpickling will have valid values query_string normally contains url encoded data a subset of ascii but some user agents are misbehaving valueerror can also be raised if the strict_parsing argument to parse_qsl is true as that is not used by django assume that the exception was raised by exceeding the value of max_num_fields instead of fragile checks of exception message strings return a new querydict with keys may be repeated from an iterable and values from value return a mutable copy of this object return an encoded string of all query string arguments safe specifies characters which don t require quoting for example q querydict mutable true q next a b q urlencode next 2fa 26b 2f q urlencode safe next a 26b it s neither necessary nor appropriate to use django utils encoding force_str for parsing urls and form inputs thus this slightly more restricted function used by querydict convert bytes objects to strings using the given encoding illegally encoded input characters are replaced with unicode unknown codepoint ufffd return any non bytes objects without change return a domain port tuple from a given host returned domain is lowercased if the host is invalid the domain will be empty remove a trailing dot if present from the domain validate the given host for this site check that the host looks valid and matches a host or host pattern in the given list of allowed_hosts any pattern beginning with a period matches a domain and all its subdomains e g example com matches example com and any subdomain matches anything and anything else must match exactly note this function assumes that the given host is lowercased and has already had the port if any stripped off return true for a valid host false otherwise
import codecs
import copy
from io import BytesIO
from itertools import chain
from urllib.parse import parse_qsl, quote, urlencode, urljoin, urlsplit

from django.conf import settings
from django.core import signing
from django.core.exceptions import (
    BadRequest,
    DisallowedHost,
    ImproperlyConfigured,
    RequestDataTooBig,
    TooManyFieldsSent,
)
from django.core.files import uploadhandler
from django.http.multipartparser import (
    MultiPartParser,
    MultiPartParserError,
    TooManyFilesSent,
)
from django.utils.datastructures import (
    CaseInsensitiveMapping,
    ImmutableList,
    MultiValueDict,
)
from django.utils.encoding import escape_uri_path, iri_to_uri
from django.utils.functional import cached_property
from django.utils.http import is_same_domain, parse_header_parameters
from django.utils.regex_helper import _lazy_re_compile

RAISE_ERROR = object()
host_validation_re = _lazy_re_compile(
    r"^([a-z0-9.-]+|\[[a-f0-9]*:[a-f0-9.:]+\])(?::([0-9]+))?$"
)


class UnreadablePostError(OSError):
    pass


class RawPostDataException(Exception):
    

    pass


class HttpRequest:
    

    
    _encoding = None
    _upload_handlers = []

    def __init__(self):
        
        
        

        self.GET = QueryDict(mutable=True)
        self.POST = QueryDict(mutable=True)
        self.COOKIES = {}
        self.META = {}
        self.FILES = MultiValueDict()

        self.path = ""
        self.path_info = ""
        self.method = None
        self.resolver_match = None
        self.content_type = None
        self.content_params = None

    def __repr__(self):
        if self.method is None or not self.get_full_path():
            return "<%s>" % self.__class__.__name__
        return "<%s: %s %r>" % (
            self.__class__.__name__,
            self.method,
            self.get_full_path(),
        )

    @cached_property
    def headers(self):
        return HttpHeaders(self.META)

    @cached_property
    def accepted_types(self):
        
        return parse_accept_header(self.headers.get("Accept", "*/*"))

    def accepts(self, media_type):
        return any(
            accepted_type.match(media_type) for accepted_type in self.accepted_types
        )

    def _set_content_type_params(self, meta):
        
        self.content_type, self.content_params = parse_header_parameters(
            meta.get("CONTENT_TYPE", "")
        )
        if "charset" in self.content_params:
            try:
                codecs.lookup(self.content_params["charset"])
            except LookupError:
                pass
            else:
                self.encoding = self.content_params["charset"]

    def _get_raw_host(self):
        
        
        if settings.USE_X_FORWARDED_HOST and ("HTTP_X_FORWARDED_HOST" in self.META):
            host = self.META["HTTP_X_FORWARDED_HOST"]
        elif "HTTP_HOST" in self.META:
            host = self.META["HTTP_HOST"]
        else:
            
            host = self.META["SERVER_NAME"]
            server_port = self.get_port()
            if server_port != ("443" if self.is_secure() else "80"):
                host = "%s:%s" % (host, server_port)
        return host

    def get_host(self):
        
        host = self._get_raw_host()

        
        allowed_hosts = settings.ALLOWED_HOSTS
        if settings.DEBUG and not allowed_hosts:
            allowed_hosts = [".localhost", "127.0.0.1", "[::1]"]

        domain, port = split_domain_port(host)
        if domain and validate_host(domain, allowed_hosts):
            return host
        else:
            msg = "Invalid HTTP_HOST header: %r." % host
            if domain:
                msg += " You may need to add %r to ALLOWED_HOSTS." % domain
            else:
                msg += (
                    " The domain name provided is not valid according to RFC 1034/1035."
                )
            raise DisallowedHost(msg)

    def get_port(self):
        
        if settings.USE_X_FORWARDED_PORT and "HTTP_X_FORWARDED_PORT" in self.META:
            port = self.META["HTTP_X_FORWARDED_PORT"]
        else:
            port = self.META["SERVER_PORT"]
        return str(port)

    def get_full_path(self, force_append_slash=False):
        return self._get_full_path(self.path, force_append_slash)

    def get_full_path_info(self, force_append_slash=False):
        return self._get_full_path(self.path_info, force_append_slash)

    def _get_full_path(self, path, force_append_slash):
        
        
        return "%s%s%s" % (
            escape_uri_path(path),
            "/" if force_append_slash and not path.endswith("/") else "",
            ("?" + iri_to_uri(self.META.get("QUERY_STRING", "")))
            if self.META.get("QUERY_STRING", "")
            else "",
        )

    def get_signed_cookie(self, key, default=RAISE_ERROR, salt="", max_age=None):
        
        try:
            cookie_value = self.COOKIES[key]
        except KeyError:
            if default is not RAISE_ERROR:
                return default
            else:
                raise
        try:
            value = signing.get_cookie_signer(salt=key + salt).unsign(
                cookie_value, max_age=max_age
            )
        except signing.BadSignature:
            if default is not RAISE_ERROR:
                return default
            else:
                raise
        return value

    def build_absolute_uri(self, location=None):
        
        if location is None:
            
            
            location = "//%s" % self.get_full_path()
        else:
            
            location = str(location)
        bits = urlsplit(location)
        if not (bits.scheme and bits.netloc):
            
            
            
            if (
                bits.path.startswith("/")
                and not bits.scheme
                and not bits.netloc
                and "/./" not in bits.path
                and "/../" not in bits.path
            ):
                
                
                
                location = self._current_scheme_host + location.removeprefix("//")
            else:
                
                
                
                location = urljoin(self._current_scheme_host + self.path, location)
        return iri_to_uri(location)

    @cached_property
    def _current_scheme_host(self):
        return "{}://{}".format(self.scheme, self.get_host())

    def _get_scheme(self):
        
        return "http"

    @property
    def scheme(self):
        if settings.SECURE_PROXY_SSL_HEADER:
            try:
                header, secure_value = settings.SECURE_PROXY_SSL_HEADER
            except ValueError:
                raise ImproperlyConfigured(
                    "The SECURE_PROXY_SSL_HEADER setting must be a tuple containing "
                    "two values."
                )
            header_value = self.META.get(header)
            if header_value is not None:
                header_value, *_ = header_value.split(",", 1)
                return "https" if header_value.strip() == secure_value else "http"
        return self._get_scheme()

    def is_secure(self):
        return self.scheme == "https"

    @property
    def encoding(self):
        return self._encoding

    @encoding.setter
    def encoding(self, val):
        
        self._encoding = val
        if hasattr(self, "GET"):
            del self.GET
        if hasattr(self, "_post"):
            del self._post

    def _initialize_handlers(self):
        self._upload_handlers = [
            uploadhandler.load_handler(handler, self)
            for handler in settings.FILE_UPLOAD_HANDLERS
        ]

    @property
    def upload_handlers(self):
        if not self._upload_handlers:
            
            self._initialize_handlers()
        return self._upload_handlers

    @upload_handlers.setter
    def upload_handlers(self, upload_handlers):
        if hasattr(self, "_files"):
            raise AttributeError(
                "You cannot set the upload handlers after the upload has been "
                "processed."
            )
        self._upload_handlers = upload_handlers

    def parse_file_upload(self, META, post_data):
        
        self.upload_handlers = ImmutableList(
            self.upload_handlers,
            warning=(
                "You cannot alter upload handlers after the upload has been "
                "processed."
            ),
        )
        parser = MultiPartParser(META, post_data, self.upload_handlers, self.encoding)
        return parser.parse()

    @property
    def body(self):
        if not hasattr(self, "_body"):
            if self._read_started:
                raise RawPostDataException(
                    "You cannot access body after reading from request's data stream"
                )

            
            if (
                settings.DATA_UPLOAD_MAX_MEMORY_SIZE is not None
                and int(self.META.get("CONTENT_LENGTH") or 0)
                > settings.DATA_UPLOAD_MAX_MEMORY_SIZE
            ):
                raise RequestDataTooBig(
                    "Request body exceeded settings.DATA_UPLOAD_MAX_MEMORY_SIZE."
                )

            try:
                self._body = self.read()
            except OSError as e:
                raise UnreadablePostError(*e.args) from e
            finally:
                self._stream.close()
            self._stream = BytesIO(self._body)
        return self._body

    def _mark_post_parse_error(self):
        self._post = QueryDict()
        self._files = MultiValueDict()

    def _load_post_and_files(self):
        
        if self.method != "POST":
            self._post, self._files = (
                QueryDict(encoding=self._encoding),
                MultiValueDict(),
            )
            return
        if self._read_started and not hasattr(self, "_body"):
            self._mark_post_parse_error()
            return

        if self.content_type == "multipart/form-data":
            if hasattr(self, "_body"):
                
                data = BytesIO(self._body)
            else:
                data = self
            try:
                self._post, self._files = self.parse_file_upload(self.META, data)
            except (MultiPartParserError, TooManyFilesSent):
                
                
                
                
                self._mark_post_parse_error()
                raise
        elif self.content_type == "application/x-www-form-urlencoded":
            
            
            
            if self._encoding is not None and self._encoding.lower() != "utf-8":
                raise BadRequest(
                    "HTTP requests with the 'application/x-www-form-urlencoded' "
                    "content type must be UTF-8 encoded."
                )
            self._post = QueryDict(self.body, encoding="utf-8")
            self._files = MultiValueDict()
        else:
            self._post, self._files = (
                QueryDict(encoding=self._encoding),
                MultiValueDict(),
            )

    def close(self):
        if hasattr(self, "_files"):
            for f in chain.from_iterable(list_[1] for list_ in self._files.lists()):
                f.close()

    
    
    
    
    
    
    

    def read(self, *args, **kwargs):
        self._read_started = True
        try:
            return self._stream.read(*args, **kwargs)
        except OSError as e:
            raise UnreadablePostError(*e.args) from e

    def readline(self, *args, **kwargs):
        self._read_started = True
        try:
            return self._stream.readline(*args, **kwargs)
        except OSError as e:
            raise UnreadablePostError(*e.args) from e

    def __iter__(self):
        return iter(self.readline, b"")

    def readlines(self):
        return list(self)


class HttpHeaders(CaseInsensitiveMapping):
    HTTP_PREFIX = "HTTP_"
    
    UNPREFIXED_HEADERS = {"CONTENT_TYPE", "CONTENT_LENGTH"}

    def __init__(self, environ):
        headers = {}
        for header, value in environ.items():
            name = self.parse_header_name(header)
            if name:
                headers[name] = value
        super().__init__(headers)

    def __getitem__(self, key):
        
        return super().__getitem__(key.replace("_", "-"))

    @classmethod
    def parse_header_name(cls, header):
        if header.startswith(cls.HTTP_PREFIX):
            header = header.removeprefix(cls.HTTP_PREFIX)
        elif header not in cls.UNPREFIXED_HEADERS:
            return None
        return header.replace("_", "-").title()

    @classmethod
    def to_wsgi_name(cls, header):
        header = header.replace("-", "_").upper()
        if header in cls.UNPREFIXED_HEADERS:
            return header
        return f"{cls.HTTP_PREFIX}{header}"

    @classmethod
    def to_asgi_name(cls, header):
        return header.replace("-", "_").upper()

    @classmethod
    def to_wsgi_names(cls, headers):
        return {
            cls.to_wsgi_name(header_name): value
            for header_name, value in headers.items()
        }

    @classmethod
    def to_asgi_names(cls, headers):
        return {
            cls.to_asgi_name(header_name): value
            for header_name, value in headers.items()
        }


class QueryDict(MultiValueDict):
    

    
    
    _mutable = True
    _encoding = None

    def __init__(self, query_string=None, mutable=False, encoding=None):
        super().__init__()
        self.encoding = encoding or settings.DEFAULT_CHARSET
        query_string = query_string or ""
        parse_qsl_kwargs = {
            "keep_blank_values": True,
            "encoding": self.encoding,
            "max_num_fields": settings.DATA_UPLOAD_MAX_NUMBER_FIELDS,
        }
        if isinstance(query_string, bytes):
            
            try:
                query_string = query_string.decode(self.encoding)
            except UnicodeDecodeError:
                
                query_string = query_string.decode("iso-8859-1")
        try:
            for key, value in parse_qsl(query_string, **parse_qsl_kwargs):
                self.appendlist(key, value)
        except ValueError as e:
            
            
            
            
            raise TooManyFieldsSent(
                "The number of GET/POST parameters exceeded "
                "settings.DATA_UPLOAD_MAX_NUMBER_FIELDS."
            ) from e
        self._mutable = mutable

    @classmethod
    def fromkeys(cls, iterable, value="", mutable=False, encoding=None):
        
        q = cls("", mutable=True, encoding=encoding)
        for key in iterable:
            q.appendlist(key, value)
        if not mutable:
            q._mutable = False
        return q

    @property
    def encoding(self):
        if self._encoding is None:
            self._encoding = settings.DEFAULT_CHARSET
        return self._encoding

    @encoding.setter
    def encoding(self, value):
        self._encoding = value

    def _assert_mutable(self):
        if not self._mutable:
            raise AttributeError("This QueryDict instance is immutable")

    def __setitem__(self, key, value):
        self._assert_mutable()
        key = bytes_to_text(key, self.encoding)
        value = bytes_to_text(value, self.encoding)
        super().__setitem__(key, value)

    def __delitem__(self, key):
        self._assert_mutable()
        super().__delitem__(key)

    def __copy__(self):
        result = self.__class__("", mutable=True, encoding=self.encoding)
        for key, value in self.lists():
            result.setlist(key, value)
        return result

    def __deepcopy__(self, memo):
        result = self.__class__("", mutable=True, encoding=self.encoding)
        memo[id(self)] = result
        for key, value in self.lists():
            result.setlist(copy.deepcopy(key, memo), copy.deepcopy(value, memo))
        return result

    def setlist(self, key, list_):
        self._assert_mutable()
        key = bytes_to_text(key, self.encoding)
        list_ = [bytes_to_text(elt, self.encoding) for elt in list_]
        super().setlist(key, list_)

    def setlistdefault(self, key, default_list=None):
        self._assert_mutable()
        return super().setlistdefault(key, default_list)

    def appendlist(self, key, value):
        self._assert_mutable()
        key = bytes_to_text(key, self.encoding)
        value = bytes_to_text(value, self.encoding)
        super().appendlist(key, value)

    def pop(self, key, *args):
        self._assert_mutable()
        return super().pop(key, *args)

    def popitem(self):
        self._assert_mutable()
        return super().popitem()

    def clear(self):
        self._assert_mutable()
        super().clear()

    def setdefault(self, key, default=None):
        self._assert_mutable()
        key = bytes_to_text(key, self.encoding)
        default = bytes_to_text(default, self.encoding)
        return super().setdefault(key, default)

    def copy(self):
        
        return self.__deepcopy__({})

    def urlencode(self, safe=None):
        
        output = []
        if safe:
            safe = safe.encode(self.encoding)

            def encode(k, v):
                return "%s=%s" % ((quote(k, safe), quote(v, safe)))

        else:

            def encode(k, v):
                return urlencode({k: v})

        for k, list_ in self.lists():
            output.extend(
                encode(k.encode(self.encoding), str(v).encode(self.encoding))
                for v in list_
            )
        return "&".join(output)


class MediaType:
    def __init__(self, media_type_raw_line):
        full_type, self.params = parse_header_parameters(
            media_type_raw_line if media_type_raw_line else ""
        )
        self.main_type, _, self.sub_type = full_type.partition("/")

    def __str__(self):
        params_str = "".join("; %s=%s" % (k, v) for k, v in self.params.items())
        return "%s%s%s" % (
            self.main_type,
            ("/%s" % self.sub_type) if self.sub_type else "",
            params_str,
        )

    def __repr__(self):
        return "<%s: %s>" % (self.__class__.__qualname__, self)

    @property
    def is_all_types(self):
        return self.main_type == "*" and self.sub_type == "*"

    def match(self, other):
        if self.is_all_types:
            return True
        other = MediaType(other)
        if self.main_type == other.main_type and self.sub_type in {"*", other.sub_type}:
            return True
        return False





def bytes_to_text(s, encoding):
    
    if isinstance(s, bytes):
        return str(s, encoding, "replace")
    else:
        return s


def split_domain_port(host):
    
    if match := host_validation_re.fullmatch(host.lower()):
        domain, port = match.groups(default="")
        
        return domain.removesuffix("."), port
    return "", ""


def validate_host(host, allowed_hosts):
    
    return any(
        pattern == "*" or is_same_domain(host, pattern) for pattern in allowed_hosts
    )


def parse_accept_header(header):
    return [MediaType(token) for token in header.split(",") if token.strip()]