o
    {,hce                     @   s6  d dl Z d dlZd dlZd dlZd dlZ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 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 d d
lmZ d dlmZ d dlm Z  d dl!m"Z" d dl#m$Z$ d dl%m&Z&m'Z' d dl(m)Z) e)dej*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/Z1G dd de1Z2G dd de0Z3G d d! d!e3Z4G d"d# d#e3Z5G d$d% d%e0Z6G d&d' d'e0Z7G d(d) d)e0Z8G d*d+ d+e0Z9G d,d- d-e0Z:G d.d/ d/e0Z;G d0d1 d1e0Z<G d2d3 d3e=Z>G d4d5 d5e0Z?dS )6    N)Header)	responses)urlsplit)async_to_syncsync_to_async)settings)signalssigning)DisallowedRedirect)DjangoJSONEncoder)SimpleCookie)timezone)CaseInsensitiveMapping)
iri_to_uri)cached_property)content_disposition_header	http_date)_lazy_re_compilez ;\s*charset=(?P<charset>[^\s;]+)c                   @   s@   e Zd Zdd ZdddZdd Zdd	 ZdddZdd Zd
S )ResponseHeadersc                 C   s.   i | _ |r| |D ]
\}}|| |< q
dS dS )zm
        Populate the initial data using __setitem__ to ensure values are
        correctly encoded.
        N)_store_unpack_items)selfdataheadervalue r   J/var/www/html/rh/venv/lib/python3.10/site-packages/django/http/response.py__init__"   s   
zResponseHeaders.__init__Fc              
   C   s   z2t |tr|| nt |tr||}n	t|}|| d|v s(d|v r0td|dW |S  ty } zAt |trGd|v sTd|v sTt |tr]d|v sTd|v r]td|d||rjt|dtj	d }n
| j
d	| 7  _
 W Y d
}~|S d
}~ww )z
        Convert headers key/value to ascii/latin-1 native strings.
        `charset` must be 'ascii' or 'latin-1'. If `mime_encode` is True and
        `value` can't be represented in the given charset, apply MIME-encoding.
        
z*Header values can't contain newlines (got )   
   utf-8)
maxlinelenz,, HTTP response headers must be in %s formatN)
isinstancestrencodebytesdecodeBadHeaderErrorUnicodeErrorr   sysmaxsizereason)r   r   charsetmime_encodeer   r   r   _convert_to_charset,   s>   





z#ResponseHeaders._convert_to_charsetc                 C      |  | d S N)pop)r   keyr   r   r   __delitem__S      zResponseHeaders.__delitem__c                 C   s2   |  |d}| j |ddd}||f| j| < d S )Nasciilatin-1T)r0   )r2   r   lowerr   r6   r   r   r   r   __setitem__V   s   zResponseHeaders.__setitem__Nc                 C   s   | j | |S r4   )r   r5   r;   )r   r6   defaultr   r   r   r5   [   s   zResponseHeaders.popc                 C   s   || vr
|| |< d S d S r4   r   r<   r   r   r   
setdefault^      zResponseHeaders.setdefaultFr4   )	__name__
__module____qualname__r   r2   r7   r=   r5   r?   r   r   r   r   r   !   s    

'
r   c                   @      e Zd ZdS )r*   NrB   rC   rD   r   r   r   r   r*   c       r*   c                   @   s   e Zd ZdZdZ	d9ddZedd Zejdd Zed	d
 Z	e	jdd
 Z	dd Z
e
Zedd Zdd Zdd Zdd Zdd ZeZdd Zd:ddZ								d;dd Zd!d" Zd<d#d$Zd=d%d&Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Z dS )>HttpResponseBasez
    An HTTP response base class with dictionary-accessed headers.

    This class doesn't handle content. It should not be used directly.
    Use the HttpResponse and StreamingHttpResponse subclasses instead.
       Nc              	   C   s   t || _|| _d| jvr|d u rd| j }|| jd< n|r#tdg | _d | _t | _d| _	|d ur\zt
|| _W n ttfyH   tdw d| j  krWdks\td td|| _d S )	NContent-Typeztext/html; charset=zX'headers' must not contain 'Content-Type' when the 'content_type' parameter is provided.Fz$HTTP status code must be an integer.d   iW  z4HTTP status code must be an integer from 100 to 599.)r   headers_charsetr/   
ValueError_resource_closers_handler_classr   cookiesclosedintstatus_code	TypeError_reason_phrase)r   content_typestatusr.   r/   rL   r   r   r   r   q   s2   


zHttpResponseBase.__init__c                 C   s   | j d ur| j S t| jdS )NzUnknown Status Code)rV   r   getrT   r   r   r   r   reason_phrase   s   
zHttpResponseBase.reason_phrasec                 C   
   || _ d S r4   )rV   r   r   r   r   r   r[         
c                 C   sD   | j d ur| j S | jd }rt| }r|d ddS tjS )NrJ   r/   " )rM   rL   rY   _charset_from_content_type_researchreplacer   DEFAULT_CHARSET)r   rW   matchedr   r   r   r/      s   
zHttpResponseBase.charsetc                 C   r\   r4   )rM   r]   r   r   r   r/      r^   c                 C   s   d dd | j D S )zHTTP headers as a bytestring.s   
c                 S   s(   g | ]\}}| d d | d qS )r9   s   : r:   )r'   ).0r6   r   r   r   r   
<listcomp>   s    z6HttpResponseBase.serialize_headers.<locals>.<listcomp>)joinrL   itemsrZ   r   r   r   serialize_headers   s
   z"HttpResponseBase.serialize_headersc                 C   s   d| j v rd| j d  S dS )NrJ   z, "%s"r`   rL   rZ   r   r   r   _content_type_for_repr   s
   
z'HttpResponseBase._content_type_for_reprc                 C   s   || j |< d S r4   rk   )r   r   r   r   r   r   r=      r8   zHttpResponseBase.__setitem__c                 C   s   | j |= d S r4   rk   r   r   r   r   r   r7         zHttpResponseBase.__delitem__c                 C   s
   | j | S r4   rk   rm   r   r   r   __getitem__      
zHttpResponseBase.__getitem__c                 C   s
   || j v S )z$Case-insensitive check for a header.rk   rm   r   r   r   
has_header   r^   zHttpResponseBase.has_headerc                 C   s
   | j  S r4   )rL   ri   rZ   r   r   r   ri      rp   zHttpResponseBase.itemsc                 C   s   | j ||S r4   )rL   rY   )r   r   	alternater   r   r   rY      r8   zHttpResponseBase.getr`   /Fc
                 C   sr  || j |< |durMt|tjrEt|rt|tjj}|tjjtjjd }
|
tjdd7 }
d}|dur9t	dt
d|
jd |
j }n|| j | d< nd	| j | d< |durzt|tjrb| }t|| j | d
< |sztt | | j | d< |dur|| j | d< |dur|| j | d< |rd| j | d< |rd| j | d< |	r|	 dvrt	d|	| j | d< dS dS )a  
        Set a cookie.

        ``expires`` can be:
        - a string in the correct format,
        - a naive ``datetime.datetime`` object in UTC,
        - an aware ``datetime.datetime`` object in any time zone.
        If it is a ``datetime.datetime`` object then calculate ``max_age``.

        ``max_age`` can be:
        - int/float specifying seconds,
        - ``datetime.timedelta`` object.
        N)tz   )secondsz/'expires' and 'max_age' can't be used together.r   iQ expiresr`   zmax-agepathdomainTsecurehttponly)laxnonestrictz,samesite must be "lax", "none", or "strict".samesite)rQ   r%   datetimer   is_naive
make_awareutcnow	timedeltarN   maxdaysrv   total_secondsrS   r   timer;   )r   r6   r   max_agerw   rx   ry   rz   r{   r   deltar   r   r   
set_cookie   s@   

zHttpResponseBase.set_cookiec                 C   s   | j || dS )z,Set a header unless it has already been set.N)rL   r?   r<   r   r   r   r?        zHttpResponseBase.setdefaultc                 K   s*   t j|| d|}| j||fi |S )N)salt)r	   get_cookie_signersignr   )r   r6   r   r   kwargsr   r   r   set_signed_cookie  s   z"HttpResponseBase.set_signed_cookiec              	   C   s6   | dp|o| dk}| j|d|||d|d d S )N)z	__Secure-z__Host-r}   r   zThu, 01 Jan 1970 00:00:00 GMT)r   rx   ry   rz   rw   r   )
startswithr;   r   )r   r6   rx   ry   r   rz   r   r   r   delete_cookie  s   

zHttpResponseBase.delete_cookiec                 C   s@   t |ttfrt|S t |trt|| jS t|| jS )z=Turn a value into a bytestring encoded in the output charset.)r%   r(   
memoryviewr&   r'   r/   r]   r   r   r   
make_bytes2  s
   	
zHttpResponseBase.make_bytesc              	   C   sL   | j D ]}z|  W q ty   Y qw | j   d| _tjj| jd d S )NT)sender)rO   	ExceptionclearrR   r   request_finishedsendrP   )r   closerr   r   r   closeG  s   


zHttpResponseBase.closec                 C      t d| jj Nz This %s instance is not writableOSError	__class__rB   r   contentr   r   r   writeR     zHttpResponseBase.writec                 C   s   d S r4   r   rZ   r   r   r   flushU     zHttpResponseBase.flushc                 C   r   )Nz)This %s instance cannot tell its positionr   rZ   r   r   r   tellX  s   
zHttpResponseBase.tellc                 C      dS NFr   rZ   r   r   r   readable`  r   zHttpResponseBase.readablec                 C   r   r   r   rZ   r   r   r   seekablec  r   zHttpResponseBase.seekablec                 C   r   r   r   rZ   r   r   r   writablef  r   zHttpResponseBase.writablec                 C   r   r   r   )r   linesr   r   r   
writelinesi  r   zHttpResponseBase.writelines)NNNNNr4   )r`   NNrs   NFFN)r`   )rs   NN)!rB   rC   rD   __doc__rT   r   propertyr[   setterr/   rj   	__bytes__rl   r=   r7   ro   rq   __contains__ri   rY   r   r?   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rH   g   sZ    




	


@

rH   c                       s   e Zd ZdZdZd fdd	Zdd Zdd	 ZeZe	d
d Z
e
jdd Z
edd Zdd Zdd Zdd Zdd Zdd Zdd Z  ZS )HttpResponsezs
    An HTTP response class with a string as content.

    This content can be read, appended to, or replaced.
    F    c                       t  j|i | || _d S r4   )superr   r   )r   r   argsr   r   r   r   r   v  s   
zHttpResponse.__init__c                 C      d| j j| j| jd S Nz5<%(cls)s status_code=%(status_code)d%(content_type)s>)clsrT   rW   r   rB   rT   rl   rZ   r   r   r   __repr__{  
   zHttpResponse.__repr__c                 C   s   |   d | j S )z6Full HTTP message, including headers, as a bytestring.s   

)rj   r   rZ   r   r   r   	serialize  r   zHttpResponse.serializec                 C      d | jS Nr   )rh   
_containerrZ   r   r   r   r     s   zHttpResponse.contentc                    s   t |dr/t|tttfs/d fdd|D }t |dr.z|  W n ty-   Y nw n |}|g _	 j
dd  d S )N__iter__r   c                 3   s    | ]}  |V  qd S r4   r   )rf   chunkrZ   r   r   	<genexpr>  s    z'HttpResponse.content.<locals>.<genexpr>r   text)hasattrr%   r(   r   r&   rh   r   r   r   r   __dict__r5   )r   r   r   r   rZ   r   r     s   


c                 C   s   | j | jpdS )Nr#   )r   r)   r/   rZ   r   r   r   r     r   zHttpResponse.textc                 C   
   t | jS r4   )iterr   rZ   r   r   r   r     rp   zHttpResponse.__iter__c                 C   s   | j | | d S r4   )r   appendr   r   r   r   r   r     s   zHttpResponse.writec                 C   r   r4   )lenr   rZ   r   r   r   r     rp   zHttpResponse.tellc                 C   s   | j S r4   )r   rZ   r   r   r   getvalue  s   zHttpResponse.getvaluec                 C   r   )NTr   rZ   r   r   r   r     r   zHttpResponse.writablec                 C   s   |D ]}|  | qd S r4   )r   )r   r   liner   r   r   r     r@   zHttpResponse.writelines)r   )rB   rC   rD   r   	streamingr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   r   r   r   m  s&    


r   c                       s   e Zd ZdZdZd fdd	Zdd Zedd	 Zed
d Z	edd Z
e
jdd Z
dd Zdd Zdd Zdd Z  ZS )StreamingHttpResponsea$  
    A streaming HTTP response class with an iterator as content.

    This should only be iterated once, when the response is streamed to the
    client. However, it can be appended to or replaced with a new iterator
    that wraps the original content (or yields entirely new content).
    Tr   c                    r   r4   )r   r   streaming_content)r   r   r   r   r   r   r   r     s   
zStreamingHttpResponse.__init__c                 C   r   r   )r   rD   rT   rl   rZ   r   r   r   r     r   zStreamingHttpResponse.__repr__c                 C   r   )NzMThis %s instance has no `content` attribute. Use `streaming_content` instead.AttributeErrorr   rB   rZ   r   r   r   r     s
   zStreamingHttpResponse.contentc                 C   r   )Nz)This %s instance has no `text` attribute.r   rZ   r   r   r   r     s   
zStreamingHttpResponse.textc                    s.   j rj  fdd}| S tjjS )Nc                    s&    2 z3 d H W }  | V  q6 d S r4   r   )part	_iteratorr   r   r   awrapper  s   z9StreamingHttpResponse.streaming_content.<locals>.awrapper)is_asyncr   mapr   )r   r   r   r   r   r     s
   z'StreamingHttpResponse.streaming_contentc                 C   r3   r4   )_set_streaming_contentr]   r   r   r   r     s   c                 C   sX   z
t || _d| _W n ty   t|| _d| _Y nw t|dr*| j|j d S d S )NFTr   )	r   r   r   rU   aiterr   rO   r   r   r]   r   r   r   r     s   




z,StreamingHttpResponse._set_streaming_contentc                 C   sR   zt | jW S  ty(   tjdtdd dd }t| jt t|| j	 Y S w )NzStreamingHttpResponse must consume asynchronous iterators in order to serve them synchronously. Use a synchronous iterator instead.   
stacklevelc                    s(   g }| 2 z3 d H W }| | q6 |S r4   )r   )r   as_listr   r   r   r   to_list  s   z/StreamingHttpResponse.__iter__.<locals>.to_list)
r   r   rU   warningswarnWarningr   r   r   r   )r   r   r   r   r   r     s   zStreamingHttpResponse.__iter__c                 C  sj   z| j 2 z	3 d H W }|V  q6 W d S  ty4   tjdtdd tt| j I d H D ]}|V  q+Y d S w )NzStreamingHttpResponse must consume synchronous iterators in order to serve them asynchronously. Use an asynchronous iterator instead.r   r   )r   rU   r   r   r   r   list)r   r   r   r   r   	__aiter__  s   zStreamingHttpResponse.__aiter__c                 C   r   r   )rh   r   rZ   r   r   r   r     rn   zStreamingHttpResponse.getvalue)r   )rB   rC   rD   r   r   r   r   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Zddd fdd
Z fdd	Zd
d Z  ZS )FileResponsez>
    A streaming HTTP response class optimized for files.
    i   Fr`   )as_attachmentfilenamec                   s8   || _ || _d|vp|d d u | _t j|i | d S )NrW   )r   r   _no_explicit_content_typer   r   )r   r   r   r   r   r   r   r   r   (  s
   zFileResponse.__init__c                    sl   t |dsd _t |S | _ t  drj j t fddd}  t | d S )Nreadr   c                      s     jS r4   )r   
block_sizer   fileliker   r   r   <lambda>8  s    z5FileResponse._set_streaming_content.<locals>.<lambda>r   )	r   file_to_streamr   r   rO   r   r   r   set_headersr]   r   r   r   r   0  s   



z#FileResponse._set_streaming_contentc                    s  t  dd}t|tr|nd}t dot d p  }t dre|r?  } dtj   | j	d<  | nDt drQ 
 j   j	d< n2tj|rdtj|   j	d< n|rtt fd	d
dj	d<  tj	d  tj tjjp|}jr|rt|\}}dddddd||}|pdj	d< ndj	d< tj| }r|j	d< dS dS )z
        Set some common response headers (Content-Length, Content-Type, and
        Content-Disposition) based on the `filelike` response content.
        namer`   seekr   r   r   zContent-Length	getbufferc                      s   t  jS r4   )r   r   r   r   r   r   r   r   V  s    z*FileResponse.set_headers.<locals>.<lambda>zapplication/x-brotlizapplication/x-bzipzapplication/x-compresszapplication/gzipzapplication/x-xz)brbzip2compressgzipxzzapplication/octet-streamrJ   zContent-DispositionN)getattrr%   r&   r   r   r   r   ioSEEK_ENDrL   r   nbytesosrx   existsgetsizesumr   rS   basenamer   r   	mimetypes
guess_typerY   r   r   )r   r   r   r   initial_positionrW   encodingcontent_dispositionr   r   r   r   <  sV   






zFileResponse.set_headers)	rB   rC   rD   r   r   r   r   r   r   r   r   r   r   r   !  s    r   c                       s:   e Zd Zg dZd	 fdd	Zedd Zdd Z  ZS )
HttpResponseRedirectBase)httphttpsftpFc                    s^   t  j|i | t|| d< tt|}|r| j| _|jr+|j| jvr-t	d|j d S d S )NLocationz)Unsafe redirect to URL with protocol '%s')
r   r   r   r   r&   status_code_preserve_requestrT   schemeallowed_schemesr
   )r   redirect_topreserve_requestr   r   parsedr   r   r   r   v  s   z!HttpResponseRedirectBase.__init__c                 C   s   | d S )Nr  r   rZ   r   r   r   r     s    z!HttpResponseRedirectBase.<lambda>c                 C   s   d| j j| j| j| jd S )NzD<%(cls)s status_code=%(status_code)d%(content_type)s, url="%(url)s">)r   rT   rW   url)r   rB   rT   rl   r  rZ   r   r   r   r     s   z!HttpResponseRedirectBase.__repr__rA   )	rB   rC   rD   r  r   r   r  r   r   r   r   r   r   r  s  s
    r  c                   @      e Zd ZdZdZdS )HttpResponseRedirecti.  i3  NrB   rC   rD   rT   r  r   r   r   r   r        r  c                   @   r  )HttpResponsePermanentRedirecti-  i4  Nr  r   r   r   r   r    r  r  c                       s0   e Zd ZdZ fddZejjdd Z  ZS )HttpResponseNotModifiedi0  c                    s   t  j|i | | d= d S )Nzcontent-type)r   r   )r   r   r   r   r   r   r     s   
z HttpResponseNotModified.__init__c                 C   s   |rt dg | _d S )Nz7You cannot set content to a 304 (Not Modified) response)r   r   r]   r   r   r   r     s
   
zHttpResponseNotModified.content)	rB   rC   rD   rT   r   r   r   r   r   r   r   r   r   r    s
    r  c                   @      e Zd ZdZdS )HttpResponseBadRequesti  NrB   rC   rD   rT   r   r   r   r   r        r  c                   @   r  )HttpResponseNotFoundi  Nr   r   r   r   r   r"    r!  r"  c                   @   r  )HttpResponseForbiddeni  Nr   r   r   r   r   r#    r!  r#  c                       s(   e Zd ZdZ fddZdd Z  ZS )HttpResponseNotAllowedi  c                    s$   t  j|i | d|| d< d S )Nz, Allow)r   r   rh   )r   permitted_methodsr   r   r   r   r   r     s   zHttpResponseNotAllowed.__init__c                 C   s   d| j j| j| j| d d S )NzC<%(cls)s [%(methods)s] status_code=%(status_code)d%(content_type)s>r%  )r   rT   rW   methodsr   rZ   r   r   r   r     s   zHttpResponseNotAllowed.__repr__)rB   rC   rD   rT   r   r   r   r   r   r   r   r$    s    r$  c                   @   r  )HttpResponseGonei  Nr   r   r   r   r   r(    r!  r(  c                   @   r  )HttpResponseServerErrori  Nr   r   r   r   r   r)    r!  r)  c                   @   rE   )Http404NrF   r   r   r   r   r*    rG   r*  c                       s(   e Zd ZdZeddf fdd	Z  ZS )JsonResponseaH  
    An HTTP response class that consumes data to be serialized to JSON.

    :param data: Data to be dumped into json. By default only ``dict`` objects
      are allowed to be passed due to a security flaw before ECMAScript 5. See
      the ``safe`` parameter for more information.
    :param encoder: Should be a json encoder class. Defaults to
      ``django.core.serializers.json.DjangoJSONEncoder``.
    :param safe: Controls if only ``dict`` objects may be serialized. Defaults
      to ``True``.
    :param json_dumps_params: A dictionary of kwargs passed to json.dumps().
    TNc                    s^   |rt |tstd|d u ri }|dd tj|fd|i|}t jdd|i| d S )NzTIn order to allow non-dict objects to be serialized set the safe parameter to False.rW   zapplication/jsonr   r   r   )r%   dictrU   r?   jsondumpsr   r   )r   r   encodersafejson_dumps_paramsr   r   r   r   r     s   zJsonResponse.__init__)rB   rC   rD   r   r   r   r   r   r   r   r   r+    s    r+  )@r   r   r-  r  r  rer,   r   r   email.headerr   http.clientr   urllib.parser   asgiref.syncr   r   django.confr   django.corer   r	   django.core.exceptionsr
   django.core.serializers.jsonr   django.http.cookier   django.utilsr   django.utils.datastructuresr   django.utils.encodingr   django.utils.functionalr   django.utils.httpr   r   django.utils.regex_helperr   Ira   r   rN   r*   rH   r   r   r   r  r  r  r  r  r"  r#  r$  r(  r)  r   r*  r+  r   r   r   r   <module>   s^    B  IkR