a
    }yd_                     @   s  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 d dl m!Z!m"Z" d dl#m$Z$ d dl%m&Z&m'Z' d dl(m)Z) ddl*m+Z+ e, Z-e)dZ.G dd de/Z0G dd de1Z2G dd dZ3G dd deZ4G dd deZ5G dd dZ6dd  Z7d!d" Z8d#d$ Z9d%d& Z:dS )'    N)BytesIO)chain)quote	urlencodeurljoinurlsplit)settings)signing)DisallowedHostImproperlyConfiguredRequestDataTooBig)uploadhandler)MultiPartParserMultiPartParserError)CaseInsensitiveMappingImmutableListMultiValueDict)RemovedInDjango40Warning)escape_uri_path
iri_to_uri)cached_property)is_same_domainlimited_parse_qsl)_lazy_re_compile   )parse_headerz1^([a-z0-9.-]+|\[[a-f0-9]*:[a-f0-9\.:]+\])(:\d+)?$c                   @   s   e Zd ZdS )UnreadablePostErrorN)__name__
__module____qualname__ r    r    K/var/www/html/Ranjet/env/lib/python3.9/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                   @   s^  e Zd ZdZdZg Zdd Zdd Zedd Z	ed	d
 Z
dd Zdd Zdd Zdd Zdd ZdGddZdHddZdd ZeddfddZdd  ZdId!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d0d1 Zed2d3 Z e jd4d3 Z d5d6 Z!ed7d8 Z"d9d: Z#d;d< Z$d=d> Z%d?d@ Z&dAdB Z'dCdD Z(dEdF Z)dS )J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   ZFILESpath	path_infomethodZresolver_matchcontent_typecontent_paramsselfr    r    r!   __init__3   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   r1   r    r    r!   __repr__E   s    zHttpRequest.__repr__c                 C   s
   t | jS N)HttpHeadersr+   r1   r    r    r!   headersJ   s    zHttpRequest.headersc                 C   s   t | jddS )z%Return a list of MediaType instances.ZAcceptz*/*)parse_accept_headerr9   getr1   r    r    r!   accepted_typesN   s    zHttpRequest.accepted_typesc                    s   t  fdd| jD S )Nc                 3   s   | ]}|  V  qd S r7   )match).0Zaccepted_type
media_typer    r!   	<genexpr>T   s   z&HttpRequest.accepts.<locals>.<genexpr>)anyr<   )r2   r@   r    r?   r!   acceptsS   s    zHttpRequest.acceptsc                 C   s\   t |dd\| _| _d| jv rXzt| jd  W n tyJ   Y n0 | jd | _dS )z/Set content_type, content_params, and encoding.CONTENT_TYPEr&   charsetN)	cgir   r;   r/   r0   codecslookupLookupErrorencoding)r2   metar    r    r!   _set_content_type_paramsY   s    
z$HttpRequest._set_content_type_paramsc                 C   sh   t jrd| jv r| jd }nHd| jv r2| jd }n2| jd }|  }||  rRdndkrdd||f }|S )z
        Return the HTTP host using the environment or request headers. Skip
        allowed hosts protection, so may return an insecure host.
        ZHTTP_X_FORWARDED_HOSTZ	HTTP_HOSTZSERVER_NAMEZ443Z80z%s:%s)r   USE_X_FORWARDED_HOSTr+   get_port	is_secure)r2   hostZserver_portr    r    r!   _get_raw_hostd   s    

zHttpRequest._get_raw_hostc                 C   sl   |   }tj}tjr |s g d}t|\}}|r>t||r>|S d| }|rX|d| 7 }n|d7 }t|dS )z>Return the HTTP host using the environment or request headers.)z
.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.N)rQ   r   ALLOWED_HOSTSDEBUGsplit_domain_portvalidate_hostr
   )r2   rP   allowed_hostsdomainportmsgr    r    r!   get_hostw   s    
zHttpRequest.get_hostc                 C   s.   t jrd| jv r| jd }n
| jd }t|S )z3Return the port number for the request as a string.ZHTTP_X_FORWARDED_PORTZSERVER_PORT)r   USE_X_FORWARDED_PORTr+   str)r2   rX   r    r    r!   rN      s    
zHttpRequest.get_portFc                 C   s   |  | j|S r7   )_get_full_pathr,   r2   force_append_slashr    r    r!   r4      s    zHttpRequest.get_full_pathc                 C   s   |  | j|S r7   )r]   r-   r^   r    r    r!   get_full_path_info   s    zHttpRequest.get_full_path_infoc              	   C   sH   dt ||r|dsdnd| jddr@dt| jdd ndf S )N%s%s%s/r&   ZQUERY_STRING?)r   endswithr+   r;   r   )r2   r,   r_   r    r    r!   r]      s
    &zHttpRequest._get_full_pathr&   c                 C   s~   z| j | }W n$ ty2   |tur,| Y S  Y n0 ztj|| dj||d}W n& tjyx   |turr| Y S  Y n0 |S )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	   Zget_cookie_signerZunsignZBadSignature)r2   keydefaultre   rf   Z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})schemerP   r,   )formatrm   rQ   r4   r1   r    r    r!   get_raw_uri   s
    zHttpRequest.get_raw_uric                 C   s   |du rd|    }nt|}t|}|jr2|js|jdr|js|jsd|jvrd|jvr|drt|dd }| j| }n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//%srb   z/./z/../z//   )
r4   r\   r   rm   netlocr,   
startswith_current_scheme_hostr   r   )r2   locationbitsr    r    r!   build_absolute_uri   s    	
zHttpRequest.build_absolute_uric                 C   s   d | j|  S )Nz{}://{})rn   rm   rZ   r1   r    r    r!   rs      s    z HttpRequest._current_scheme_hostc                 C   s   dS )zf
        Hook for subclasses like WSGIRequest to implement. Return 'http' by
        default.
        httpr    r1   r    r    r!   _get_scheme   s    zHttpRequest._get_schemec                 C   s\   t jrTzt j\}}W n ty.   tdY n0 | j|}|d urT||krPdS dS |  S )NzJThe SECURE_PROXY_SSL_HEADER setting must be a tuple containing two values.httpsrw   )r   SECURE_PROXY_SSL_HEADER
ValueErrorr   r+   r;   rx   )r2   headerZsecure_valueZheader_valuer    r    r!   rm      s    
zHttpRequest.schemec                 C   s
   | j dkS )Nry   )rm   r1   r    r    r!   rO     s    zHttpRequest.is_securec                 C   s    t jdtdd | jddkS )Nzfrequest.is_ajax() is deprecated. See Django 3.1 release notes for more details about this deprecation.rp   )
stacklevelZHTTP_X_REQUESTED_WITHZXMLHttpRequest)warningswarnr   r+   r;   r1   r    r    r!   is_ajax  s    zHttpRequest.is_ajaxc                 C   s   | j S r7   	_encodingr1   r    r    r!   rJ     s    zHttpRequest.encodingc                 C   s&   || _ t| dr| `t| dr"| `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   )r2   valr    r    r!   rJ     s
    

c                    s    fddt jD  _d S )Nc                    s   g | ]}t | qS r    )r   Zload_handler)r>   handlerr1   r    r!   
<listcomp>  s   z4HttpRequest._initialize_handlers.<locals>.<listcomp>)r   FILE_UPLOAD_HANDLERS_upload_handlersr1   r    r1   r!   _initialize_handlers  s    
z HttpRequest._initialize_handlersc                 C   s   | j s|   | j S r7   )r   r   r1   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   )r2   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   rJ   parse)r2   r+   Z	post_dataparserr    r    r!   parse_file_upload/  s    zHttpRequest.parse_file_uploadc              
   C   s   t | ds| jrtdtjd urDt| jdp2dtjkrDtdz| 	 | _
W n0 ty } zt|j |W Y d }~n
d }~0 0 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)r2   er    r    r!   body8  s    

"zHttpRequest.bodyc                 C   s   t  | _t | _d S r7   )r'   r   r   r   r1   r    r    r!   _mark_post_parse_errorJ  s    z"HttpRequest._mark_post_parse_errorc                 C   s   | j dkr&t| jdt  | _| _dS | jrBt| dsB|   dS | j	dkrt| drbt
| j}n| }z| | j|\| _| _W q ty   |    Y q0 n@| j	dkrt| j| jdt  | _| _nt| jdt  | _| _dS )zFPopulate self._post and self._files if the content-type is a form typer)   )rJ   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   )r2   datar    r    r!   _load_post_and_filesN  s$    




z HttpRequest._load_post_and_filesc                 C   s6   t | dr2tdd | j D D ]}|  q$d S )Nr   c                 s   s   | ]}|d  V  qdS )r   Nr    )r>   list_r    r    r!   rA   m      z$HttpRequest.close.<locals>.<genexpr>)r   r   from_iterabler   listsclose)r2   fr    r    r!   r   k  s    
zHttpRequest.closec              
   O   sP   d| _ z| jj|i |W S  tyJ } zt|j |W Y d }~n
d }~0 0 d S NT)r   r   r   r   r   r   r2   r   kwargsr   r    r    r!   r   x  s
    zHttpRequest.readc              
   O   sP   d| _ z| jj|i |W S  tyJ } zt|j |W Y d }~n
d }~0 0 d S r   )r   r   readliner   r   r   r   r    r    r!   r     s
    zHttpRequest.readlinec                 C   s   t | jdS )Nr   )iterr   r1   r    r    r!   __iter__  s    zHttpRequest.__iter__c                 C   s   t | S r7   )listr1   r    r    r!   	readlines  s    zHttpRequest.readlines)F)F)N)*r   r   r   r#   r   r   r3   r6   r   r9   r<   rC   rL   rQ   rZ   rN   r4   r`   r]   rh   rl   ro   rv   rs   rx   propertyrm   rO   r   rJ   setterr   r   r   r   r   r   r   r   r   r   r   r    r    r    r!   r$   ,   s\   



	
$

	



	
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 )
r8   ZHTTP_rD   r   c                    s<   i }|  D ]\}}| |}|r|||< qt | d S r7   )itemsparse_header_namesuperr3   )r2   environr9   r|   rk   namer5   r    r!   r3     s    

zHttpHeaders.__init__c                    s   t  |ddS )z:Allow header lookup using underscores in place of hyphens._-)r   __getitem__replacer2   ri   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   )rr   HTTP_PREFIXlenUNPREFIXED_HEADERSr   title)clsr|   r    r    r!   r     s
    
zHttpHeaders.parse_header_name)
r   r   r   r   r   r3   r   classmethodr   __classcell__r    r    r   r!   r8     s   r8   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rfz|| j}W n t	yd   |d}Y n0 t
|fi |D ]\}}| || qv|| _d S )Nr&   T)keep_blank_valuesZfields_limitrJ   z
iso-8859-1)r   r3   r   DEFAULT_CHARSETrJ   DATA_UPLOAD_MAX_NUMBER_FIELDS
isinstancebytesdecodeUnicodeDecodeErrorr   
appendlist_mutable)r2   Zquery_stringr%   rJ   Zparse_qsl_kwargsri   rk   r   r    r!   r3     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%   rJ   F)r   r   )r   iterablerk   r%   rJ   qri   r    r    r!   fromkeys  s    zQueryDict.fromkeysc                 C   s   | j d u rtj| _ | j S r7   )r   r   r   r1   r    r    r!   rJ     s    
zQueryDict.encodingc                 C   s
   || _ d S r7   r   )r2   rk   r    r    r!   rJ     s    c                 C   s   | j stdd S )Nz$This QueryDict instance is immutable)r   r   r1   r    r    r!   _assert_mutable  s    zQueryDict._assert_mutablec                    s2   |    t|| j}t|| j}t || d S r7   )r   bytes_to_textrJ   r   __setitem__r2   ri   rk   r   r    r!   r     s    zQueryDict.__setitem__c                    s   |    t | d S r7   )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   )r5   rJ   r   setlist)r2   resultri   rk   r    r    r!   __copy__  s    zQueryDict.__copy__c                 C   sP   | j dd| jd}||t| < |  D ]$\}}|t||t|| q&|S r   )r5   rJ   idr   r   copydeepcopy)r2   memor   ri   rk   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   rJ   )r>   eltr1   r    r!   r     r   z%QueryDict.setlist.<locals>.<listcomp>)r   r   rJ   r   r   )r2   ri   r   r   r1   r!   r     s    zQueryDict.setlistc                    s   |    t ||S r7   )r   r   setlistdefault)r2   ri   Zdefault_listr   r    r!   r     s    zQueryDict.setlistdefaultc                    s2   |    t|| j}t|| j}t || d S r7   )r   r   rJ   r   r   r   r   r    r!   r     s    zQueryDict.appendlistc                    s   |    t j|g|R  S r7   )r   r   pop)r2   ri   r   r   r    r!   r     s    zQueryDict.popc                    s   |    t  S r7   )r   r   popitemr1   r   r    r!   r     s    zQueryDict.popitemc                    s   |    t   d S r7   )r   r   clearr1   r   r    r!   r     s    zQueryDict.clearc                    s.   |    t|| j}t|| j}t ||S r7   )r   r   rJ   r   
setdefault)r2   ri   rj   r   r    r!   r     s    zQueryDict.setdefaultc                 C   s
   |  i S )z%Return a mutable copy of this object.)r   r1   r    r    r!   r   #  s    zQueryDict.copyc                    sb   g }r" jfdd ndd   D ]$\}| fdd|D  q2d|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!   encode8  s    z#QueryDict.urlencode.<locals>.encodec                 S   s   t | |iS r7   )r   r   r    r    r!   r   ;  s    c                 3   s,   | ]$}  jt| jV  qd S r7   )r   rJ   r\   )r>   r   )r   r   r2   r    r!   rA   >  s   z&QueryDict.urlencode.<locals>.<genexpr>&)r   rJ   r   extendjoin)r2   r   outputr   r    )r   r   r   r2   r!   r   '  s    
zQueryDict.urlencode)NFN)r&   FN)N)N)N)r   r   r   r#   r   r   r3   r   r   r   rJ   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r    r   r!   r'     s0   

r'   c                   @   s8   e Zd Zdd Zdd Zdd Zedd Zd	d
 ZdS )	MediaTypec                 C   s4   t |r|dnd\}| _|d\| _}| _d S )Nasciir   rb   )r   r   params	partition	main_typesub_type)r2   Zmedia_type_raw_lineZ	full_typer   r    r    r!   r3   F  s    
zMediaType.__init__c                 C   s:   d dd | j D }d| j| jr0d| j nd|f S )Nr&   c                 s   s$   | ]\}}d || df V  qdS )z; %s=%sr   N)r   )r>   r   r   r    r    r!   rA   M  s   z$MediaType.__str__.<locals>.<genexpr>ra   z/%s)r   r   r   r   r   )r2   Z
params_strr    r    r!   __str__L  s    
zMediaType.__str__c                 C   s   d| j j| f S )Nz<%s: %s>)r5   r   r1   r    r    r!   r6   W  s    zMediaType.__repr__c                 C   s   | j dko| jdkS )N*)r   r   r1   r    r    r!   is_all_typesZ  s    zMediaType.is_all_typesc                 C   s6   | j r
dS t|}| j|jkr2| jd|jhv r2dS dS )NTr   F)r   r   r   r   )r2   otherr    r    r!   r=   ^  s    zMediaType.matchN)	r   r   r   r3   r   r6   r   r   r=   r    r    r    r!   r   E  s   
r   c                 C   s   t | trt| |dS | S d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   N)r   r   r\   )srJ   r    r    r!   r   j  s    
r   c                 C   sx   |   } t| sdS | d dkr*| dfS | dd}t|dkrF|n
|d df\}}|d	rl|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   rp   r   .N)lowerhost_validation_rer=   rsplitr   rd   )rP   ru   rW   rX   r    r    r!   rT   x  s    
 rT   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 )r   N)r   )r>   patternrP   r    r!   rA     r   z validate_host.<locals>.<genexpr>)rB   )rP   rV   r    r   r!   rU     s    rU   c                 C   s   dd |  dD S )Nc                 S   s   g | ]}|  rt|qS r    )stripr   )r>   tokenr    r    r!   r     r   z'parse_accept_header.<locals>.<listcomp>,)split)r|   r    r    r!   r:     s    r:   );rF   rG   r   r~   ior   	itertoolsr   urllib.parser   r   r   r   django.confr   django.corer	   django.core.exceptionsr
   r   r   Zdjango.core.filesr   Zdjango.http.multipartparserr   r   Zdjango.utils.datastructuresr   r   r   django.utils.deprecationr   django.utils.encodingr   r   django.utils.functionalr   Zdjango.utils.httpr   r   django.utils.regex_helperr   Zmultipartparserr   objectrh   r   r   r   	Exceptionr"   r$   r8   r'   r   r   rT   rU   r:   r    r    r    r!   <module>   sB   	  c %