
    hN                     n   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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   e dejB                        Z" G d de#      Z$ G d d      Z% G d de%      Z& G d de%      Z' G d de'      Z( G d de&      Z) G d de)      Z* G d de)      Z+ G d d e&      Z, G d! d"e&      Z- G d# d$e&      Z. G d% d&e&      Z/ G d' d(e&      Z0 G d) d*e&      Z1 G d+ d,e&      Z2 G d- d.e3      Z4 G d/ d0e&      Z5y)1    N)Header)	responses)quoteurlparse)settings)signalssigning)DisallowedRedirect)DjangoJSONEncoder)SimpleCookie)timezone)
iri_to_uri)	http_date)_lazy_re_compilez ;\s*charset=(?P<charset>[^\s;]+)c                       e Zd Zy)BadHeaderErrorN__name__
__module____qualname__     W/var/www/html/ranktracker/api/venv/lib/python3.12/site-packages/django/http/response.pyr   r          r   r   c                   (   e Zd ZdZdZddZed        Zej                  d        Zed        Z	e	j                  d        Z	d	 Z
e
Zed
        Zd dZd Zd Zd Zd ZeZd Zd!dZ	 	 d"dZd Zd#dZd$dZd Zd Zd Zd Zd Zd Zd Zd Z d Z!y)%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                 \   i | _         g | _        d | _        t               | _        d| _        |=	 t        |      | _        d| j                  cxk  rdk  st        d       t        d      || _
        || _        |d| j                  z  }|| d<   y # t        t        f$ r t        d      w xY w)NFz$HTTP status code must be an integer.d   iW  z4HTTP status code must be an integer from 100 to 599.ztext/html; charset=%sContent-Type)_headers_resource_closers_handler_classr   cookiesclosedintstatus_code
ValueError	TypeError_reason_phrase_charsetcharset)selfcontent_typestatusreasonr,   s        r   __init__zHttpResponseBase.__init__'   s     !# ##~H#&v;  $**1c1 !WXX 2 !WXX$2T\\AL+^ 	* H FGGHs   B B+c                 r    | j                   | j                   S t        j                  | j                  d      S )NzUnknown Status Code)r*   r   getr'   r-   s    r   reason_phrasezHttpResponseBase.reason_phrase@   s4    *&&& }}T--/DEEr   c                     || _         y N)r*   r-   values     r   r5   zHttpResponseBase.reason_phraseH   s
    #r   c                     | j                   | j                   S | j                  dd      }t        j                  |      }|r|d   j	                  dd      S t
        j                  S )Nr     r,   ")r+   r3   _charset_from_content_type_researchreplacer   DEFAULT_CHARSET)r-   r.   matcheds      r   r,   zHttpResponseBase.charsetL   s]    ==$== xx3/66|D9%--c266'''r   c                     || _         y r7   )r+   r8   s     r   r,   zHttpResponseBase.charsetW   s	    r   c           	          d }| j                   j                         D cg c]  \  }} ||d      dz    ||d      z    }}}dj                  |      S c c}}w )zHTTP headers as a bytestring.c                 H    t        | t              r| S | j                  |      S r7   )
isinstancebytesencode)valencodings     r   to_bytesz4HttpResponseBase.serialize_headers.<locals>.to_bytes]   s    $S%03Jcjj6JJr   asciis   : latin-1s   
)r!   valuesjoin)r-   rJ   keyr9   headerss        r   serialize_headersz"HttpResponseBase.serialize_headers[   sf    	K
 #mm224
U c7#e+hui.HH
 
 ||G$$	
s    Ac                     d| v rd| d   z  S dS )Nr    z, "%s"r;   r   r4   s    r   _content_type_for_reprz'HttpResponseBase._content_type_for_reprh   s    2@D2Hx$~..PbPr   c                    t        |t        t        f      st        |      }t        |t              rd|v sd|v st        |t              rd|v sd|v rt        d|z        	 t        |t              r|j	                  |       |S |j                  |      }	 |S # t        $ rQ}|r+t        |dt        j                        j	                         }n|xj                  d|z  z  c_
         Y d	}~|S d	}~ww xY 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 %r)zutf-8)
maxlinelenz,, HTTP response headers must be in %s formatN)rE   rF   strr   rG   decodeUnicodeErrorr   sysmaxsizer0   )r-   r9   r,   mime_encodees        r   _convert_to_charsetz$HttpResponseBase._convert_to_charsetl   s     %%.JEu%5E>Ue^5#&DEMTU] !PSX!XYY	%%W%  W-   	ug#++FMMOJWTT  	s   !!B B 	C2!AC--C2c                     | j                  |d      }| j                  |dd      }||f| j                  |j                         <   y )NrK   rL   T)r_   )ra   r!   lower)r-   headerr9   s      r   __setitem__zHttpResponseBase.__setitem__   sC    ))&':((	t(L)/flln%r   c                 X    | j                   j                  |j                         d       y NF)r!   poprc   r-   rd   s     r   __delitem__zHttpResponseBase.__delitem__   s    &,,.%0r   c                 B    | j                   |j                            d   S N   )r!   rc   ri   s     r   __getitem__zHttpResponseBase.__getitem__   s    }}V\\^,Q//r   c                 :    |j                         | j                  v S )z$Case-insensitive check for a header.)rc   r!   ri   s     r   
has_headerzHttpResponseBase.has_header   s    ||~..r   c                 6    | j                   j                         S r7   )r!   rM   r4   s    r   itemszHttpResponseBase.items   s    }}##%%r   c                 `    | j                   j                  |j                         d |f      d   S rl   )r!   r3   rc   )r-   rd   	alternates      r   r3   zHttpResponseBase.get   s)    }}  $	1BCAFFr   c
                 P   || j                   |<   |t        |t        j                        rt        j                  |      r$t        j
                  |t        j                        }||j                         z
  }
|
t        j                  d      z   }
d}t        d|
j                  dz  |
j                  z         }n%|| j                   |   d<   nd| j                   |   d<   |D|| j                   |   d<   |s0t        t        j                         |z         | j                   |   d<   ||| j                   |   d	<   ||| j                   |   d
<   |rd| j                   |   d<   |rd| j                   |   d<   |	r0|	j                         dvrt        d      |	| j                   |   d<   yy)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``.
        Nrm   )secondsr   iQ expiresr;   zmax-agepathdomainTsecurehttponly)laxnonestrictz,samesite must be "lax", "none", or "strict".samesite)r$   rE   datetimer   is_aware
make_naiveutcutcnow	timedeltamaxdaysrv   r   timerc   r(   )r-   rO   r9   max_agerw   rx   ry   rz   r{   r   deltas              r   
set_cookiezHttpResponseBase.set_cookie   s    "S'8#4#45$$W-&11'8<<HG'.."22  2 21 ==ae!3emm!CD/6S!),+-DLLi(+2DLLi(/8w9N/OS!),(,DLLf%*0DLLh'*.DLLh',0DLLj)~~'@@ !OPP,4DLLj) r   c                     || vr|| |<   yy)z,Set a header unless it has already been set.Nr   )r-   rO   r9   s      r   
setdefaultzHttpResponseBase.setdefault   s    d?DI r   c                 z    t        j                  ||z         j                  |      } | j                  ||fi |S )N)salt)r	   get_cookie_signersignr   )r-   rO   r9   r   kwargss        r   set_signed_cookiez"HttpResponseBase.set_signed_cookie   s9    ))sTz:??FtsE4V44r   c           	          |j                  d      xs |xr |j                         dk(  }| j                  |d|||d|       y )N)z	__Secure-z__Host-r}   r   zThu, 01 Jan 1970 00:00:00 GMT)r   rx   ry   rz   rw   r   )
startswithrc   r   )r-   rO   rx   ry   r   rz   s         r   delete_cookiezHttpResponseBase.delete_cookie   sR     NN34 64(..*f4 	 	fV3h 	 	
r   c                     t        |t        t        f      rt        |      S t        |t              r$t        |j	                  | j
                              S t        |      j	                  | j
                        S )z=Turn a value into a bytestring encoded in the output charset.)rE   rF   
memoryviewrZ   rG   r,   r8   s     r   
make_byteszHttpResponseBase.make_bytes   sX     eeZ01<eS!dll3445z  ..r   c                     | j                   D ]
  }	  |         | j                   j                          d| _        t        j
                  j                  | j                         y # t        $ r Y dw xY w)NT)sender)r"   	Exceptionclearr%   r   request_finishedsendr#   )r-   closers     r   closezHttpResponseBase.close   sm    ,, 	F	 	$$&  %%T-@-@%A  s   A&&	A21A2c                 F    t        d| j                  j                  z        Nz This %s instance is not writableOSError	__class__r   r-   contents     r   writezHttpResponseBase.write      84>>;R;RRSSr   c                      y r7   r   r4   s    r   flushzHttpResponseBase.flush  s    r   c                 F    t        d| j                  j                  z        )Nz)This %s instance cannot tell its positionr   r4   s    r   tellzHttpResponseBase.tell  s    ADNND[D[[\\r   c                      yrg   r   r4   s    r   readablezHttpResponseBase.readable      r   c                      yrg   r   r4   s    r   seekablezHttpResponseBase.seekable  r   r   c                      yrg   r   r4   s    r   writablezHttpResponseBase.writable  r   r   c                 F    t        d| j                  j                  z        r   r   )r-   liness     r   
writelineszHttpResponseBase.writelines  r   r   )NNNN)Fr7   )r;   NN/NFFN)r;   )r   NN)"r   r   r   __doc__r'   r1   propertyr5   setterr,   rQ   	__bytes__rS   ra   re   rj   rn   rp   __contains__rr   r3   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s
    K,2 F F $ $ ( ( ^^ 	% "IQ Q68
10/ L&G JMGK,5\
5
 /*	BT]Tr   r   c                        e Zd ZdZdZd fd	Zd Zd ZeZe	d        Z
e
j                  d        Z
d Zd	 Zd
 Zd Zd Zd Z xZS )HttpResponsezx
    An HTTP response class with a string as content.

    This content that can be read, appended to, or replaced.
    Fc                 2    t        |   |i | || _        y r7   )superr1   r   )r-   r   argsr   r   s       r   r1   zHttpResponse.__init__'  s    $)&)r   c                 d    d| j                   j                  | j                  | j                  dz  S )Nz5<%(cls)s status_code=%(status_code)d%(content_type)s>)clsr'   r.   r   r   r'   rS   r4   s    r   __repr__zHttpResponse.__repr__,  s4    F>>**++ 77J
 
 	
r   c                 B    | j                         dz   | j                  z   S )z6Full HTTP message, including headers, as a bytestring.s   

)rQ   r   r4   s    r   	serializezHttpResponse.serialize3  s    %%'+5DDr   c                 8    dj                  | j                        S Nr   )rN   
_containerr4   s    r   r   zHttpResponse.content9  s    xx((r   c                 
    t        |d      rNt        |t        t        f      s8dj	                   fd|D              }t        |d      r#	 |j                          n j                  |      }|g _        y # t        $ r Y w xY w)N__iter__r   c              3   @   K   | ]  }j                  |        y wr7   )r   ).0chunkr-   s     r   	<genexpr>z'HttpResponse.content.<locals>.<genexpr>A  s     I%tu5Is   r   )	hasattrrE   rF   rZ   rN   r   r   r   r   )r-   r9   r   s   `  r   r   zHttpResponse.content=  su     5*%j.MhhI5IIGug&KKM ooe,G") ! s   A6 6	BBc                 ,    t        | j                        S r7   )iterr   r4   s    r   r   zHttpResponse.__iter__L  s    DOO$$r   c                 X    | j                   j                  | j                  |             y r7   )r   appendr   r   s     r   r   zHttpResponse.writeO  s    tw78r   c                 ,    t        | j                        S r7   )lenr   r4   s    r   r   zHttpResponse.tellR  s    4<<  r   c                     | j                   S r7   )r   r4   s    r   getvaluezHttpResponse.getvalueU  s    ||r   c                      y)NTr   r4   s    r   r   zHttpResponse.writableX  s    r   c                 4    |D ]  }| j                  |        y r7   )r   )r-   r   lines      r   r   zHttpResponse.writelines[  s     	DJJt	r   )r   )r   r   r   r   	streamingr1   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   s   @r   r   r     sn     I

E I) ) ^^$ $%9!r   r   c                   ~     e Zd ZdZdZd
 fd	Zed        Zed        Zej                  d        Zd Z
d Zd	 Z x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).
    Tc                 2    t        |   |i | || _        y r7   )r   r1   streaming_content)r-   r   r   r   r   s       r   r1   zStreamingHttpResponse.__init__k  s    $)&) "3r   c                 F    t        d| j                  j                  z        )NzMThis %s instance has no `content` attribute. Use `streaming_content` instead.)AttributeErrorr   r   r4   s    r   r   zStreamingHttpResponse.contentq  s'    +-1^^-D-DE
 	
r   c                 B    t        | j                  | j                        S r7   )mapr   	_iteratorr4   s    r   r   z'StreamingHttpResponse.streaming_contentx  s    4??DNN33r   c                 &    | j                  |       y r7   )_set_streaming_contentr8   s     r   r   z'StreamingHttpResponse.streaming_content|  s    ##E*r   c                     t        |      | _        t        |d      r&| j                  j	                  |j
                         y y )Nr   )r   r   r   r"   r   r   r8   s     r   r   z,StreamingHttpResponse._set_streaming_content  s4    e5'"""))%++6 #r   c                     | j                   S r7   )r   r4   s    r   r   zStreamingHttpResponse.__iter__  s    %%%r   c                 8    dj                  | j                        S r   )rN   r   r4   s    r   r   zStreamingHttpResponse.getvalue  s    xx..//r   )r   )r   r   r   r   r   r1   r   r   r   r   r   r   r   r   r   s   @r   r   r   `  se     I3 
 
 4 4 + +7&0r   r   c                   >     e Zd ZdZdZddd fd
Z fdZd Z xZS )	FileResponsez>
    A streaming HTTP response class optimized for files.
    i   Fr;   )as_attachmentfilenamec                @    || _         || _        t        |   |i | y r7   )r   r   r   r1   )r-   r   r   r   r   r   s        r   r1   zFileResponse.__init__  s$    * $)&)r   c                 "    t        |d      sd  _        t           |      S |x _        t        d      r% j                  j                  j                         t         fdd      } j                         t           |       y )Nreadr   c                  :     j                  j                        S r7   )r   
block_size)fileliker-   s   r   <lambda>z5FileResponse._set_streaming_content.<locals>.<lambda>  s    X]]4??; r   r   )	r   file_to_streamr   r   r"   r   r   r   set_headers)r-   r9   r   r   s   ` @r   r   z#FileResponse._set_streaming_content  s|    uf%"&D71%88)..h8W%""))(..9;SA"&u-r   c                 r   dddd}t        |dd      }t        |t              r|r|n| j                  }t        j
                  j                  |      r-t        j
                  j                  |j                        | d<   n)t        |d      r|j                         j                  | d<   | j                  d	d
      j                  d      r;|r4t        j                  |      \  }}|j                  ||      }|xs d| d	<   nd| d	<   | j                  xs t        j
                  j!                  |      }|rI| j"                  rdnd}	 |j%                  d       dj'                  |      }dj'                  ||      | d<   y| j"                  rd| d<   yy# t(        $ r dj'                  t+        |            }Y Nw xY w)z
        Set some common response headers (Content-Length, Content-Type, and
        Content-Disposition) based on the `filelike` response content.
        zapplication/x-bzipzapplication/gzipzapplication/x-xz)bzip2gzipxznameNzContent-Length	getbufferr    r;   z	text/htmlzapplication/octet-stream
attachmentinlinerK   zfilename="{}"zfilename*=utf-8''{}z{}; {}zContent-Disposition)getattrrE   rZ   r   osrx   isabsgetsizer  r   r  nbytesr3   r   	mimetypes
guess_typebasenamer   rG   formatUnicodeEncodeErrorr   )r-   r   encoding_mapr   r.   rI   disposition	file_exprs           r   r   zFileResponse.set_headers  s    *&$

 8VT2 *8S 9h8T]]77=="%'WW__X]]%CD!"X{+%-%7%7%9%@%@D!"88NB'22;?)2)=)=h)G&h  ,//,G'3'Q7Q^$'A^$==>BGG$4$4X$>*.*<*<,(KJ(+228<	 +3//+y*QD&'*6D&'   & J188xI	Js   "F #F65F6)	r   r   r   r   r   r1   r   r   r   r   s   @r   r   r     s#     J,1B *

.%7r   r   c                   >     e Zd Zg dZ fdZ ed       Zd Z xZS )HttpResponseRedirectBase)httphttpsftpc                     t        |   |i | t        |      | d<   t        t	        |            }|j
                  r1|j
                  | j                  vrt        d|j
                  z        y y )NLocationz)Unsafe redirect to URL with protocol '%s')r   r1   r   r   rZ   schemeallowed_schemesr
   )r-   redirect_tor   r   parsedr   s        r   r1   z!HttpResponseRedirectBase.__init__  sk    $)&)%k2Z#k*+==V]]$2F2FF$%PSYS`S`%`aa G=r   c                     | d   S )Nr  r   r4   s    r   r   z!HttpResponseRedirectBase.<lambda>  s    Z 0 r   c                 z    d| j                   j                  | j                  | j                  | j                  dz  S )NzD<%(cls)s status_code=%(status_code)d%(content_type)s, url="%(url)s">)r   r'   r.   url)r   r   r'   rS   r  r4   s    r   r   z!HttpResponseRedirectBase.__repr__  s;    U>>**++ 7788	Y
 
 	
r   )	r   r   r   r  r1   r   r  r   r   r   s   @r   r  r    s     .Ob 0
1C
r   r  c                       e Zd ZdZy)HttpResponseRedirecti.  Nr   r   r   r'   r   r   r   r!  r!        Kr   r!  c                       e Zd ZdZy)HttpResponsePermanentRedirecti-  Nr"  r   r   r   r%  r%    r#  r   r%  c                   Z     e Zd ZdZ fdZej                  j                  d        Z xZS )HttpResponseNotModifiedi0  c                 *    t        |   |i | | d= y )Nzcontent-type)r   r1   )r-   r   r   r   s      r   r1   z HttpResponseNotModified.__init__  s    $)&) r   c                 ,    |rt        d      g | _        y )Nz7You cannot set content to a 304 (Not Modified) response)r   r   r8   s     r   r   zHttpResponseNotModified.content  s     !Z[[r   )	r   r   r   r'   r1   r   r   r   r   r   s   @r   r'  r'    s-    K!    !r   r'  c                       e Zd ZdZy)HttpResponseBadRequesti  Nr"  r   r   r   r+  r+    r#  r   r+  c                       e Zd ZdZy)HttpResponseNotFoundi  Nr"  r   r   r   r-  r-    r#  r   r-  c                       e Zd ZdZy)HttpResponseForbiddeni  Nr"  r   r   r   r/  r/    r#  r   r/  c                   (     e Zd ZdZ fdZd Z xZS )HttpResponseNotAllowedi  c                 L    t        |   |i | dj                  |      | d<   y )Nz, Allow)r   r1   rN   )r-   permitted_methodsr   r   r   s       r   r1   zHttpResponseNotAllowed.__init__  s(    $)&)		"34Wr   c                 l    d| j                   j                  | j                  | j                  | d   dz  S )NzC<%(cls)s [%(methods)s] status_code=%(status_code)d%(content_type)s>r3  )r   r'   r.   methodsr   r4   s    r   r   zHttpResponseNotAllowed.__repr__
  s;    T>>**++ 77G}	X
 
 	
r   )r   r   r   r'   r1   r   r   r   s   @r   r1  r1    s    K5
r   r1  c                       e Zd ZdZy)HttpResponseGonei  Nr"  r   r   r   r8  r8    r#  r   r8  c                       e Zd ZdZy)HttpResponseServerErrori  Nr"  r   r   r   r:  r:    r#  r   r:  c                       e Zd Zy)Http404Nr   r   r   r   r<  r<    r   r   r<  c                   *     e Zd ZdZeddf fd	Z x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                     |rt        |t              st        d      |i }|j                  dd       t	        j
                  |fd|i|}t        |   dd|i| y )NzTIn order to allow non-dict objects to be serialized set the safe parameter to False.r.   zapplication/jsonr   r   r   )rE   dictr)   r   jsondumpsr   r1   )r-   dataencodersafejson_dumps_paramsr   r   s         r   r1   zJsonResponse.__init__-  sn    
4.+  $ ".*<=zz$AGA/@A000r   )r   r   r   r   r   r1   r   r   s   @r   r>  r>    s     &7T#'1 1r   r>  )6r   rA  r
  r  rer]   r   email.headerr   http.clientr   urllib.parser   r   django.confr   django.corer   r	   django.core.exceptionsr
   django.core.serializers.jsonr   django.http.cookier   django.utilsr   django.utils.encodingr   django.utils.httpr   django.utils.regex_helperr   Ir=   r(   r   r   r   r   r   r  r!  r%  r'  r+  r-  r/  r1  r8  r:  r   r<  r>  r   r   r   <module>rU     s*      	 	 
   ! (   ( 5 : + ! , ' 6 01TVXVZVZ [ 	Z 	~T ~TB?# ?D*0, *0Z<7( <7~
| 
*3 $< l \ < L 
\ 
 | l 	i 	1< 1r   