o
    uvXjV  ã                   @   sV  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& 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ƒZ0G dd„ deƒZ1dd„ Z2dd„ Z3dd„ Z4dS ) é    N)ÚBytesIO)Úchain)ÚquoteÚ	urlencodeÚurljoinÚurlsplit)Úsettings)Úsigning)ÚDisallowedHostÚImproperlyConfiguredÚRequestDataTooBig)Úuploadhandler)ÚMultiPartParserÚMultiPartParserError)ÚCaseInsensitiveMappingÚImmutableListÚMultiValueDict)ÚRemovedInDjango30Warning)Ú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   úP/var/www/html/myproject/venv/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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d.d-„ ƒZd/d0„ Zed1d2„ ƒZ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   r0   r   r   r   Ú__repr__A   s   zHttpRequest.__repr__c                 C   s
   t | jƒS ©N)ÚHttpHeadersr(   r0   r   r   r   ÚheadersF   ó   
zHttpRequest.headersc                 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)r1   ÚhostÚserver_portr   r   r   Ú_get_raw_hostJ   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.)rD   r   ÚALLOWED_HOSTSÚDEBUGÚsplit_domain_portÚvalidate_hostr
   )r1   rB   Úallowed_hostsÚdomainÚportÚmsgr   r   r   Úget_host]   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)r1   rL   r   r   r   r@   q   s
   

ÿzHttpRequest.get_portFc                 C   ó   |   | j|¡S r6   )Ú_get_full_pathr*   ©r1   Úforce_append_slashr   r   r   r3   y   ó   zHttpRequest.get_full_pathc                 C   rS   r6   )rT   r+   rU   r   r   r   Úget_full_path_info|   rW   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(   Úgetr   )r1   r*   rV   r   r   r   rT      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)r1   ÚkeyÚdefaultr^   r_   Ú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})ÚschemerB   r*   )Úformatrj   rD   r3   r0   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//%srY   z/./z/../z//é   )	r3   r   rj   Únetlocr*   Ú
startswithÚ_current_scheme_hostr   r   )r1   ÚlocationÚbitsr   r   r   Úbuild_absolute_uriª   s   	

ÿzHttpRequest.build_absolute_uric                 C   s   d  | j|  ¡ ¡S )Nz{}://{})rk   rj   rN   r0   r   r   r   rp   Ë   s   z HttpRequest._current_scheme_hostc                 C   s   dS )zf
        Hook for subclasses like WSGIRequest to implement. Return 'http' by
        default.
        Úhttpr   r0   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.Úhttpsrt   )r   ÚSECURE_PROXY_SSL_HEADERÚ
ValueErrorr   r(   r]   ru   )r1   ÚheaderÚsecure_valueÚheader_valuer   r   r   rj   Ö   s   ÿÿzHttpRequest.schemec                 C   s
   | j dkS )Nrv   )rj   r0   r   r   r   rA   ä   s   
zHttpRequest.is_securec                 C   s   | j  d¡dkS )NÚHTTP_X_REQUESTED_WITHÚXMLHttpRequest)r(   r]   r0   r   r   r   Úis_ajaxç   s   zHttpRequest.is_ajaxc                 C   s   | j S r6   ©Ú	_encodingr0   r   r   r   Úencodingê   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‚   )r1   Úvalr   r   r   r�   î   s   

ÿc                    s   ‡ fdd„t jD ƒˆ _d S )Nc                    s   g | ]}t  |ˆ ¡‘qS r   )r   Úload_handler)Ú.0Úhandlerr0   r   r   Ú
<listcomp>ü   s    ÿz4HttpRequest._initialize_handlers.<locals>.<listcomp>)r   ÚFILE_UPLOAD_HANDLERSÚ_upload_handlersr0   r   r0   r   Ú_initialize_handlersû   s   
ÿz HttpRequest._initialize_handlersc                 C   s   | j s|  ¡  | j S r6   )rŠ   r‹   r0   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Š   )r1   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   r�   Úparse)r1   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”   ÚIOErrorr   Úargsr   Ú_stream)r1   Úer   r   r   Úbody  s   

€ÿzHttpRequest.bodyc                 C   s   t ƒ | _tƒ | _d S r6   )r$   r‚   r   r�   r0   r   r   r   Ú_mark_post_parse_error'  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&   )r�   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ž   )r1   Údatar   r   r   Ú_load_post_and_files+  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†   Úlr   r   r   Ú	<genexpr>J  s   € z$HttpRequest.close.<locals>.<genexpr>)rƒ   r   Úfrom_iterabler�   ÚlistsÚclose)r1   Úfr   r   r   r§   H  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›   ©r1   r›   Úkwargsr�   r   r   r   r™   U  ó   €ÿzHttpRequest.readc              
   O   r©   rª   )r–   rœ   Úreadlinerš   r   r›   r«   r   r   r   r®   \  r­   zHttpRequest.readlinec                 C   s   t | jdƒS )Nó    )Úiterr®   r0   r   r   r   Ú__iter__c  ó   zHttpRequest.__iter__c                 c   s    � t jdtdd� | E d H  d S )NzIHttpRequest.xreadlines() is deprecated in favor of iterating the request.rm   )Ú
stacklevel)ÚwarningsÚwarnr   r0   r   r   r   Ú
xreadlinesf  s   €þzHttpRequest.xreadlinesc                 C   s   t | ƒS r6   )Úlistr0   r   r   r   Ú	readlinesm  s   zHttpRequest.readlines)Fr6   )(r   r   r   r    r€   rŠ   r2   r5   r   r8   rD   rN   r@   r3   rX   rT   ra   ri   rl   rs   rp   ru   Úpropertyrj   rA   r~   r�   Úsetterr‹   rŒ   r“   rž   rŸ   r¡   r§   r™   r®   r±   r¶   r¸   r   r   r   r   r!   (   sX    


	
!





	
r!   c                       s4   e Zd ZdZddhZ‡ fdd„Zedd„ ƒZ‡  ZS )r7   ÚHTTP_ÚCONTENT_TYPEr•   c                    s<   i }|  ¡ D ]\}}|  |¡}|r|||< qtƒ  |¡ d S r6   )ÚitemsÚparse_header_nameÚsuperr2   )r1   Úenvironr8   ry   rh   Úname©r4   r   r   r2   v  s   
€zHttpHeaders.__init__c                 C   s>   |  | j¡r|t| jƒd … }n|| jvrd S | dd¡ ¡ S )NÚ_ú-)ro   ÚHTTP_PREFIXÚlenÚUNPREFIXED_HEADERSÚreplaceÚtitle)Úclsry   r   r   r   r¾   ~  s
   
zHttpHeaders.parse_header_name)	r   r   r   rÅ   rÇ   r2   Úclassmethodr¾   Ú__classcell__r   r   rÂ   r   r7   q  s    r7   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_limitr�   z
iso-8859-1)r¿   r2   r   ÚDEFAULT_CHARSETr�   ÚDATA_UPLOAD_MAX_NUMBER_FIELDSÚ
isinstanceÚbytesÚdecodeÚUnicodeDecodeErrorr   Ú
appendlistÚ_mutable)r1   Úquery_stringr"   r�   Úparse_qsl_kwargsre   rh   rÂ   r   r   r2   ›  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"   r�   F)rÕ   rÖ   )rÊ   Úiterablerh   r"   r�   Úqre   r   r   r   Úfromkeys¯  s   zQueryDict.fromkeysc                 C   s   | j d u r	tj| _ | j S r6   )r€   r   rÏ   r0   r   r   r   r�   ¼  s   
zQueryDict.encodingc                 C   s
   || _ d S r6   r   )r1   rh   r   r   r   r�   Â  r9   c                 C   s   | j stdƒ‚d S )Nz$This QueryDict instance is immutable)rÖ   rŽ   r0   r   r   r   Ú_assert_mutableÆ  s   ÿzQueryDict._assert_mutablec                    ó2   |   ¡  t|| jƒ}t|| jƒ}tƒ  ||¡ d S r6   )rÝ   Úbytes_to_textr�   r¿   Ú__setitem__©r1   re   rh   rÂ   r   r   rà   Ê  ó   zQueryDict.__setitem__c                    s   |   ¡  tƒ  |¡ d S r6   )rÝ   r¿   Ú__delitem__)r1   re   rÂ   r   r   rã   Ð  s   zQueryDict.__delitem__c                 C   s4   | j dd| jd�}|  ¡ D ]
\}}| ||¡ q|S ©Nr#   TrÙ   )r4   r�   r¦   Úsetlist)r1   Úresultre   rh   r   r   r   Ú__copy__Ô  s   zQueryDict.__copy__c                 C   sP   | j dd| jd�}||t| ƒ< |  ¡ D ]\}}| t ||¡t ||¡¡ q|S rä   )r4   r�   Úidr¦   rå   ÚcopyÚdeepcopy)r1   Úmemoræ   re   rh   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ß   r�   )r†   Úeltr0   r   r   rˆ   ä  s    z%QueryDict.setlist.<locals>.<listcomp>)rÝ   rß   r�   r¿   rå   )r1   re   Úlist_rÂ   r0   r   rå   á  s   zQueryDict.setlistc                    s   |   ¡  tƒ  ||¡S r6   )rÝ   r¿   Úsetlistdefault)r1   re   Údefault_listrÂ   r   r   rï   ç  ó   zQueryDict.setlistdefaultc                    rÞ   r6   )rÝ   rß   r�   r¿   rÕ   rá   rÂ   r   r   rÕ   ë  râ   zQueryDict.appendlistc                    s   |   ¡  tƒ j|g|¢R Ž S r6   )rÝ   r¿   Úpop)r1   re   r›   rÂ   r   r   rò   ñ  s   zQueryDict.popc                    s   |   ¡  tƒ  ¡ S r6   )rÝ   r¿   Úpopitemr0   rÂ   r   r   ró   õ  s   
zQueryDict.popitemc                    s   |   ¡  tƒ  ¡  d S r6   )rÝ   r¿   Úclearr0   rÂ   r   r   rô   ù  rñ   zQueryDict.clearc                    s.   |   ¡  t|| jƒ}t|| jƒ}tƒ  ||¡S r6   )rÝ   rß   r�   r¿   Ú
setdefault)r1   re   rf   rÂ   r   r   rõ   ý  s   zQueryDict.setdefaultc                 C   s
   |   i ¡S )z%Return a mutable copy of this object.)rì   r0   r   r   r   ré     r9   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 r6   )r   rö   r   r   r   rú     r²   c                 3   s.   � | ]}ˆ ˆ  ˆj¡t|ƒ  ˆj¡ƒV  qd S r6   )rú   r�   rR   )r†   rø   )rú   r÷   r1   r   r   r¤     s
   € ÿ
ÿz&QueryDict.urlencode.<locals>.<genexpr>ú&)rú   r�   r¦   ÚextendÚjoin)r1   rù   Úoutputrî   r   )rú   r÷   rù   r1   r   r     s   
þ
zQueryDict.urlencode)NFN)r#   FNr6   )r   r   r   r    rÖ   r€   r2   rË   rÜ   r¹   r�   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Ò   rR   )Úsr�   r   r   r   rß   (  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¢   rm   r   Ú.N)ÚlowerÚhost_validation_reÚmatchÚrsplitrÆ   r\   )rB   rr   rK   rL   r   r   r   rH   6  s   
 rH   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©rB   r   r   r¤   [  s   €  z validate_host.<locals>.<genexpr>)Úany)rB   rJ   r   r
  r   rI   L  s   rI   )5ré   Úrer´   Ú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.deprecationr   Údjango.utils.encodingr   r   Údjango.utils.functionalr   Údjango.utils.httpr   r   Úobjectra   Úcompiler  rš   r   Ú	Exceptionr   r!   r7   r$   rß   rH   rI   r   r   r   r   Ú<module>   s:    
	  K "