o
    :ZceW                     @   sR  d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	m
Z
mZmZ d dlmZ d dlmZ d dlmZmZmZ d dlmZ d d	lmZmZ d d
lmZmZmZ d dlmZm Z  d dl!m"Z" d dl#m$Z$m%Z% e& Z'e(dZ)G dd de*Z+G dd de,Z-G dd dZ.G dd deZ/G dd deZ0dd Z1dd Z2dd Z3dS )    N)BytesIO)chain)quote	urlencodeurljoinurlsplit)settings)signing)DisallowedHostImproperlyConfiguredRequestDataTooBig)uploadhandler)MultiPartParserMultiPartParserError)CaseInsensitiveMappingImmutableListMultiValueDict)escape_uri_path
iri_to_uri)cached_property)is_same_domainlimited_parse_qslz1^([a-z0-9.-]+|\[[a-f0-9]*:[a-f0-9\.:]+\])(:\d+)?$c                   @   s   e Zd ZdS )UnreadablePostErrorN)__name__
__module____qualname__ r   r   I/var/www/chikooza/env/lib/python3.10/site-packages/django/http/request.pyr      s    r   c                   @   s   e Zd ZdZdS )RawPostDataExceptionz
    You cannot access raw_post_data from a request that has
    multipart/* POST data if it has been accessed via POST,
    FILES, etc..
    N)r   r   r   __doc__r   r   r   r   r      s    r   c                   @   sJ  e Zd ZdZdZg Zdd Zdd Zedd Z	d	d
 Z
dd Zdd Zdd ZdCddZdCddZdd ZeddfddZdd ZdDddZedd  Zd!d" Zed#d$ Zd%d& Zd'd( Zed)d* Zejd+d* Zd,d- Zed.d/ Zejd0d/ Zd1d2 Zed3d4 Z d5d6 Z!d7d8 Z"d9d: Z#d;d< Z$d=d> Z%d?d@ Z&dAdB Z'dS )EHttpRequestzA basic HTTP request.Nc                 C   sT   t dd| _t dd| _i | _i | _t | _d| _d| _d | _	d | _
d | _d | _d S )NT)mutable )	QueryDictGETPOSTCOOKIESMETAr   FILESpath	path_infomethodresolver_matchcontent_typecontent_paramsselfr   r   r   __init__/   s   
zHttpRequest.__init__c                 C   s6   | j d u s	|  sd| jj S d| jj| j |  f S )Nz<%s>z<%s: %s %r>)r+   get_full_path	__class__r   r/   r   r   r   __repr__A   s   zHttpRequest.__repr__c                 C   s
   t | jS N)HttpHeadersr'   r/   r   r   r   headersF      
zHttpRequest.headersc                 C   sb   t |dd\| _| _d| jv r/z
t| jd  W n
 ty&   Y dS w | jd | _dS dS )z/Set content_type, content_params, and encoding.CONTENT_TYPEr"   charsetN)	cgiparse_headergetr-   r.   codecslookupLookupErrorencoding)r0   metar   r   r   _set_content_type_paramsJ   s   
z$HttpRequest._set_content_type_paramsc                 C   sl   t jrd| jv r| jd }|S d| jv r| jd }|S | jd }|  }||  r+dndkr4d||f }|S )z
        Return the HTTP host using the environment or request headers. Skip
        allowed hosts protection, so may return an insecure host.
        HTTP_X_FORWARDED_HOST	HTTP_HOSTSERVER_NAME44380z%s:%s)r   USE_X_FORWARDED_HOSTr'   get_port	is_secure)r0   hostserver_portr   r   r   _get_raw_hostU   s   
	


zHttpRequest._get_raw_hostc                 C   sn   |   }tj}tjr|sg d}t|\}}|rt||r|S d| }|r/|d| 7 }t||d7 }t|)z>Return the HTTP host using the environment or request headers.)	localhostz	127.0.0.1z[::1]zInvalid HTTP_HOST header: %r.z) You may need to add %r to ALLOWED_HOSTS.zB The domain name provided is not valid according to RFC 1034/1035.)rN   r   ALLOWED_HOSTSDEBUGsplit_domain_portvalidate_hostr
   )r0   rL   allowed_hostsdomainportmsgr   r   r   get_hosth   s   
zHttpRequest.get_hostc                 C   s4   t jrd| jv r| jd }t|S | jd }t|S )z3Return the port number for the request as a string.HTTP_X_FORWARDED_PORTSERVER_PORT)r   USE_X_FORWARDED_PORTr'   str)r0   rV   r   r   r   rJ   |   s
   

zHttpRequest.get_portFc                 C      |  | j|S r5   )_get_full_pathr)   r0   force_append_slashr   r   r   r2         zHttpRequest.get_full_pathc                 C   r]   r5   )r^   r*   r_   r   r   r   get_full_path_info   ra   zHttpRequest.get_full_path_infoc              	   C   sL   dt ||r|dsdnd| jddr"dt| jdd f S df S )Nz%s%s%s/r"   QUERY_STRING?)r   endswithr'   r=   r   )r0   r)   r`   r   r   r   r^      s   "zHttpRequest._get_full_pathr"   c                 C   st   z| j | }W n ty   |tur| Y S  w ztj|| dj||d}W |S  tjy9   |tur8| Y S  w )z
        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.
        )salt)max_age)r&   KeyErrorRAISE_ERRORr	   get_cookie_signerunsignBadSignature)r0   keydefaultrg   rh   cookie_valuevaluer   r   r   get_signed_cookie   s"   zHttpRequest.get_signed_cookiec                 C   s   dj | j|  |  dS )z
        Return an absolute URI from variables available in this request. Skip
        allowed hosts protection, so may return insecure URI.
        z{scheme}://{host}{path})schemerL   r)   )formatrs   rN   r2   r/   r   r   r   get_raw_uri   s
   zHttpRequest.get_raw_uric                 C   s   |du r
d|    }t|}|jr|jsG|jdr>|js>|js>d|jvr>d|jvr>|dr5|dd }| j| }t|S t| j| j |}t|S )a  
        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.
        Nz//%src   z/./z/../z//   )	r2   r   rs   netlocr)   
startswith_current_scheme_hostr   r   )r0   locationbitsr   r   r   build_absolute_uri   s   	

zHttpRequest.build_absolute_uric                 C   s   d | j|  S )Nz{}://{})rt   rs   rX   r/   r   r   r   ry      s   z HttpRequest._current_scheme_hostc                 C   s   dS )zf
        Hook for subclasses like WSGIRequest to implement. Return 'http' by
        default.
        httpr   r/   r   r   r   _get_scheme   s   zHttpRequest._get_schemec                 C   sX   t jr(zt j\}}W n ty   tdw | j|}|d ur(||kr&dS dS |  S )NzJThe SECURE_PROXY_SSL_HEADER setting must be a tuple containing two values.httpsr}   )r   SECURE_PROXY_SSL_HEADER
ValueErrorr   r'   r=   r~   )r0   headersecure_valueheader_valuer   r   r   rs      s   zHttpRequest.schemec                 C   s
   | j dkS )Nr   )rs   r/   r   r   r   rK      s   
zHttpRequest.is_securec                 C   s   | j ddkS )NHTTP_X_REQUESTED_WITHXMLHttpRequest)r'   r=   r/   r   r   r   is_ajax   s   zHttpRequest.is_ajaxc                 C   s   | j S r5   	_encodingr/   r   r   r   rA      s   zHttpRequest.encodingc                 C   s*   || _ t| dr
| `t| dr| `dS dS )z
        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).
        r$   _postN)r   hasattrr$   r   )r0   valr   r   r   rA      s   

c                    s    fddt jD  _d S )Nc                    s   g | ]}t | qS r   )r   load_handler).0handlerr/   r   r   
<listcomp>  s    z4HttpRequest._initialize_handlers.<locals>.<listcomp>)r   FILE_UPLOAD_HANDLERS_upload_handlersr/   r   r/   r   _initialize_handlers  s   
z HttpRequest._initialize_handlersc                 C   s   | j s|   | j S r5   )r   r   r/   r   r   r   upload_handlers
  s   zHttpRequest.upload_handlersc                 C   s   t | dr	td|| _d S )N_fileszGYou cannot set the upload handlers after the upload has been processed.)r   AttributeErrorr   )r0   r   r   r   r   r     s   

c                 C   s*   t | jdd| _t||| j| j}| S )z9Return a tuple of (POST QueryDict, FILES MultiValueDict).zEYou cannot alter upload handlers after the upload has been processed.)warning)r   r   r   rA   parse)r0   r'   	post_dataparserr   r   r   parse_file_upload  s   zHttpRequest.parse_file_uploadc              
   C   s   t | dsB| jrtdtjd ur"t| jdpdtjkr"tdz| 	 | _
W n ty; } zt|j |d }~ww t| j
| _| j
S )N_bodyz?You cannot access body after reading from request's data streamCONTENT_LENGTHr   z;Request body exceeded settings.DATA_UPLOAD_MAX_MEMORY_SIZE.)r   _read_startedr   r   DATA_UPLOAD_MAX_MEMORY_SIZEintr'   r=   r   readr   OSErrorr   argsr   _stream)r0   er   r   r   body   s   

zHttpRequest.bodyc                 C   s   t  | _t | _d S r5   )r#   r   r   r   r/   r   r   r   _mark_post_parse_error2  s   z"HttpRequest._mark_post_parse_errorc                 C   s   | j dkrt| jdt | _| _dS | jr!t| ds!|   dS | j	dkrNt| dr1t
| j}n| }z| | j|\| _| _W dS  tyM   |    w | j	dkrct| j| jdt | _| _dS t| jdt | _| _dS )zFPopulate self._post and self._files if the content-type is a form typer%   )rA   Nr   zmultipart/form-dataz!application/x-www-form-urlencoded)r+   r#   r   r   r   r   r   r   r   r-   r   r   r   r'   r   r   )r0   datar   r   r   _load_post_and_files6  s&   



 z HttpRequest._load_post_and_filesc                 C   s:   t | drtdd | j D D ]}|  qd S d S )Nr   c                 s   s    | ]}|d  V  qdS )   Nr   )r   list_r   r   r   	<genexpr>U  s    z$HttpRequest.close.<locals>.<genexpr>)r   r   from_iterabler   listsclose)r0   fr   r   r   r   S  s
   

zHttpRequest.closec              
   O   @   d| _ z
| jj|i |W S  ty } zt|j |d }~ww NT)r   r   r   r   r   r   r0   r   kwargsr   r   r   r   r   `     zHttpRequest.readc              
   O   r   r   )r   r   readliner   r   r   r   r   r   r   r   g  r   zHttpRequest.readlinec                 C   s   t | jdS )N    )iterr   r/   r   r   r   __iter__n     zHttpRequest.__iter__c                 C   s   t | S r5   )listr/   r   r   r   	readlinesq  s   zHttpRequest.readlines)Fr5   )(r   r   r   r   r   r   r1   r4   r   r7   rC   rN   rX   rJ   r2   rb   r^   rj   rr   ru   r|   ry   r~   propertyrs   rK   r   rA   setterr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    (   sX    


	
!





	
r    c                       s@   e Zd ZdZddhZ fddZ fddZedd	 Z  Z	S )
r6   HTTP_r9   r   c                    s<   i }|  D ]\}}| |}|r|||< qt | d S r5   )itemsparse_header_namesuperr1   )r0   environr7   r   rq   namer3   r   r   r1   z  s   
zHttpHeaders.__init__c                    s   t  |ddS )z:Allow header lookup using underscores in place of hyphens._-)r   __getitem__replacer0   rn   r   r   r   r     s   zHttpHeaders.__getitem__c                 C   s>   | | jr|t| jd  }n|| jvrd S |dd S )Nr   r   )rx   HTTP_PREFIXlenUNPREFIXED_HEADERSr   title)clsr   r   r   r   r     s
   
zHttpHeaders.parse_header_name)
r   r   r   r   r   r1   r   classmethodr   __classcell__r   r   r   r   r6   u  s    r6   c                       s   e Zd ZdZdZdZd) fdd	Zed*dd	Ze	d
d Z
e
jdd Z
dd Z fddZ fddZdd Zdd Z fddZd+ fdd	Z fddZ fddZ fdd Z fd!d"Zd+ fd#d$	Zd%d& Zd+d'd(Z  ZS ),r#   a  
    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.
    TNFc                    s   t    |p	tj| _|pd}dtj| jd}t|tr3z|| j}W n t	y2   |d}Y nw t
|fi |D ]
\}}| || q;|| _d S )Nr"   T)keep_blank_valuesfields_limitrA   z
iso-8859-1)r   r1   r   DEFAULT_CHARSETrA   DATA_UPLOAD_MAX_NUMBER_FIELDS
isinstancebytesdecodeUnicodeDecodeErrorr   
appendlist_mutable)r0   query_stringr!   rA   parse_qsl_kwargsrn   rq   r   r   r   r1     s    


zQueryDict.__init__r"   c                 C   s2   | dd|d}|D ]}| || q	|sd|_|S )zt
        Return a new QueryDict with keys (may be repeated) from an iterable and
        values from value.
        r"   Tr!   rA   F)r   r   )r   iterablerq   r!   rA   qrn   r   r   r   fromkeys  s   zQueryDict.fromkeysc                 C   s   | j d u r	tj| _ | j S r5   )r   r   r   r/   r   r   r   rA     s   
zQueryDict.encodingc                 C   s
   || _ d S r5   r   )r0   rq   r   r   r   rA     r8   c                 C   s   | j stdd S )Nz$This QueryDict instance is immutable)r   r   r/   r   r   r   _assert_mutable  s   zQueryDict._assert_mutablec                    2   |    t|| j}t|| j}t || d S r5   )r   bytes_to_textrA   r   __setitem__r0   rn   rq   r   r   r   r        zQueryDict.__setitem__c                    s   |    t | d S r5   )r   r   __delitem__r   r   r   r   r     s   zQueryDict.__delitem__c                 C   s4   | j dd| jd}|  D ]
\}}||| q|S Nr"   Tr   )r3   rA   r   setlist)r0   resultrn   rq   r   r   r   __copy__  s   zQueryDict.__copy__c                 C   sP   | j dd| jd}||t| < |  D ]\}}|t||t|| q|S r   )r3   rA   idr   r   copydeepcopy)r0   memor   rn   rq   r   r   r   __deepcopy__  s
   zQueryDict.__deepcopy__c                    s8       t| j} fdd|D }t || d S )Nc                    s   g | ]}t | jqS r   )r   rA   )r   eltr/   r   r   r     s    z%QueryDict.setlist.<locals>.<listcomp>)r   r   rA   r   r   )r0   rn   r   r   r/   r   r     s   zQueryDict.setlistc                    s   |    t ||S r5   )r   r   setlistdefault)r0   rn   default_listr   r   r   r        zQueryDict.setlistdefaultc                    r   r5   )r   r   rA   r   r   r   r   r   r   r     r   zQueryDict.appendlistc                    s   |    t j|g|R  S r5   )r   r   pop)r0   rn   r   r   r   r   r     s   zQueryDict.popc                    s   |    t  S r5   )r   r   popitemr/   r   r   r   r     s   
zQueryDict.popitemc                    s   |    t   d S r5   )r   r   clearr/   r   r   r   r     r   zQueryDict.clearc                    s.   |    t|| j}t|| j}t ||S r5   )r   r   rA   r   
setdefault)r0   rn   ro   r   r   r   r     s   zQueryDict.setdefaultc                 C   s
   |  i S )z%Return a mutable copy of this object.)r   r/   r   r   r   r     r8   zQueryDict.copyc                    sb   g }r jfdd ndd   D ]\}| fdd|D  qd|S )ah  
        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/'
        c                    s   dt |  t | f S )Nz%s=%s)r   kv)safer   r   encode   s   z#QueryDict.urlencode.<locals>.encodec                 S   s   t | |iS r5   )r   r   r   r   r   r   #  r   c                 3   s.    | ]}  jt| jV  qd S r5   )r   rA   r\   )r   r   )r   r   r0   r   r   r   &  s
    
z&QueryDict.urlencode.<locals>.<genexpr>&)r   rA   r   extendjoin)r0   r   outputr   r   )r   r   r   r0   r   r     s   

zQueryDict.urlencode)NFN)r"   FNr5   )r   r   r   r   r   r   r1   r   r   r   rA   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r#     s2    

r#   c                 C   s   t | trt| |dS | S )u   
    Convert bytes objects to strings, using the given encoding. Illegally
    encoded input characters are replaced with Unicode "unknown" codepoint
    (�).

    Return any non-bytes objects without change.
    r   )r   r   r\   )srA   r   r   r   r   0  s   
r   c                 C   sx   |   } t| sdS | d dkr| dfS | dd}t|dkr#|n|d df\}}|d	r6|d
d n|}||fS )z
    Return a (domain, port) tuple from a given host.

    Returned domain is lowercased. If the host is invalid, the domain will be
    empty.
    )r"   r"   ]r"   :r   rv   r   .N)lowerhost_validation_rematchrsplitr   rf   )rL   r{   rU   rV   r   r   r   rR   >  s   
 rR   c                    s   t  fdd|D S )a4  
    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.
    c                 3   s"    | ]}|d kpt  |V  qdS )*N)r   )r   patternrL   r   r   r   c  s     z validate_host.<locals>.<genexpr>)any)rL   rT   r   r  r   rS   T  s   rS   )4r;   r>   r   reior   	itertoolsr   urllib.parser   r   r   r   django.confr   django.corer	   django.core.exceptionsr
   r   r   django.core.filesr   django.http.multipartparserr   r   django.utils.datastructuresr   r   r   django.utils.encodingr   r   django.utils.functionalr   django.utils.httpr   r   objectrj   compiler	  r   r   	Exceptionr   r    r6   r#   r   rR   rS   r   r   r   r   <module>   s:    
	  O "