
    8Cf76              
      D   d dl mZ d dlmZ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mZmZmZmZ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! d dl"m#Z#m$Z$ ddl%m&Z&m'Z'm(Z( 	 d	 	 	 	 	 ddZ)	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 d dZ*	 	 	 d!	 	 	 	 	 	 	 	 	 d"dZ+ddef	 	 	 	 	 	 	 	 	 d#dZ,	 	 d$	 	 	 	 	 	 	 d%dZ-	 	 	 	 	 	 d&dZ.ddddddedddf
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d'dZ/	 	 	 d(	 	 	 	 	 	 	 	 	 d)dZ0	 	 	 	 	 	 d*	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d+dZ1y),    )annotations)datetimetimezone)
formatdateparsedate_to_datetime)
guess_type)path)PurePath)time)AnyAnyStrCallableDictOptionalUnion)
quote_plus)Header
open_async
stat_async)DEFAULT_HTTP_CONTENT_TYPE)Default_default)logger)HTMLProtocolRange   )HTTPResponseJSONResponseResponseStreamNc                    t        d| |      S )a  Returns an empty response to the client.

    Args:
        status (int, optional): HTTP response code. Defaults to `204`.
        headers ([type], optional): Custom HTTP headers. Defaults to `None`.

    Returns:
        HTTPResponse: An empty response to the client.
        bodystatusheadersr   )r$   r%   s     Z/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/sanic/response/convenience.pyemptyr(      s     SAAr!      c                $    t        | f||||d|S )a  Returns response object with body in json format.

    Args:
        body (Any): Response data to be serialized.
        status (int, optional): HTTP response code. Defaults to `200`.
        headers (Dict[str, str], optional): Custom HTTP headers. Defaults to `None`.
        content_type (str, optional): The content type (string) of the response. Defaults to `"application/json"`.
        dumps (Callable[..., str], optional): A custom json dumps function. Defaults to `None`.
        **kwargs (Any): Remaining arguments that are passed to the json encoder.

    Returns:
        JSONResponse: A response object with body in json format.
    )r$   r%   content_typedumps)r   )r#   r$   r%   r+   r,   kwargss         r'   jsonr.   $   s/    * !  r!   c                    t        | t              s"t        dt        |       j                   d      t        | |||      S )a	  Returns response object with body in text format.

    Args:
        body (str): Response data.
        status (int, optional): HTTP response code. Defaults to `200`.
        headers (Dict[str, str], optional): Custom HTTP headers. Defaults to `None`.
        content_type (str, optional): The content type (string) of the response. Defaults to `"text/plain; charset=utf-8"`.

    Returns:
        HTTPResponse: A response object with body in text format.

    Raises:
        TypeError: If the body is not a string.
    z!Bad body type. Expected str, got )r$   r%   r+   )
isinstancestr	TypeErrortype__name__r   r#   r$   r%   r+   s       r'   textr8   C   sJ    ( dC /T
0C0C/DAF
 	
 VW< r!   c                     t        | |||      S )a  Returns response object without encoding the body.

    Args:
        body (Optional[AnyStr]): Response data.
        status (int, optional): HTTP response code. Defaults to `200`.
        headers (Dict[str, str], optional): Custom HTTP headers. Defaults to `None`.
        content_type (str, optional): The content type (string) of the response. Defaults to `"application/octet-stream"`.

    Returns:
        HTTPResponse: A response object without encoding the body.
    r7   r&   r7   s       r'   rawr:   a   s    " !	 r!   c                    t        | t        t        f      s9t        | d      r| j	                         } nt        | d      r| j                         } t        | ||d      S )a  Returns response object with body in html format.

    Body should be a `str` or `bytes` like object, or an object with `__html__` or `_repr_html_`.

    Args:
        body (Union[str, bytes, HTMLProtocol]): Response data.
        status (int, optional): HTTP response code. Defaults to `200`.
        headers (Dict[str, str], optional): Custom HTTP headers. Defaults to `None`.

    Returns:
        HTTPResponse: A response object with body in html format.
    __html___repr_html_text/html; charset=utf-8r1   )r2   r3   byteshasattrr<   r=   r   r"   s      r'   htmlrA   z   sX    " dS%L)4$==?DT=)##%D/	 r!   c                  K   	 | j                  d      }	 t        |      }t        |t              s>t        j                  t        |      t        j                        j                  d      }|j                         F|j                         6t        j                  d       |j                  t        j                         nU|j                         E|j                         5t        j                  d	       |j                  t        j                         |j                         |j                         k  rt!        d
      S y# t        $ r Y yw xY w# t        t        f$ r t        j                  d|       Y yw xY ww)a=  Validate file based on request headers.

    Args:
        request_headers (Header): The request headers.
        last_modified (Union[datetime, float, int]): The last modified date and time of the file.

    Returns:
        Optional[HTTPResponse]: A response object with status 304 if the file is not modified.
    zIf-Modified-SinceNz9Ignorning invalid If-Modified-Since header received: '%s')tzr   microsecondzpCannot compare tz-aware and tz-naive datetimes. To avoid this conflict Sanic is converting last_modified to UTC.)tzinfoztCannot compare tz-aware and tz-naive datetimes. To avoid this conflict Sanic is converting if_modified_since to UTC.i0  )r$   )getoneKeyErrorr   r4   
ValueErrorr   warningr2   r   fromtimestampfloatr   utcreplace	utcoffset	timestampr   )request_headerslast_modifiedif_modified_sinces      r'   validate_filerT      s`    +223FG12CD mX. ..- X\\

'a'
  	
 	!)'')5F	
 	X\\2!-'')1J	
 	!!!6 $5$?$?$AA3''I   z" J	
 sE   FE E D'F	EFEF%E?<F>E??FTc                \  K   t        |t              r!|j                  d      j                         }n/t        |t              rt        |        d{   }|j                  }|r||rt        ||       d{   }|r|S |xs i }|r|j                  dt        |d             |r|j                  dd| d	       |	rd
}n2|r.d| }|j                  dt        t               |z   d             nd}|j                  d|       |xs t        j                  |       d   }t        | d       d{   4 d{   }|
ru|j                  |
j                         d{    |j!                  |
j"                         d{   }d|
j                   d|
j$                   d|
j&                   |d<   d}n|j!                          d{   }ddd      d{    |xs t)        |      d   xs d}t+        |||      S 7 7 7 7 7 7 7 G7 9# 1 d{  7  sw Y   IxY ww)as  Return a response object with file data.

    Args:
        location (Union[str, PurePath]): Location of file on system.
        status (int, optional): HTTP response code. Won't enforce the passed in status if only a part of the content will be sent (206) or file is being validated (304). Defaults to 200.
        request_headers (Optional[Header], optional): The request headers.
        validate_when_requested (bool, optional): If `True`, will validate the file when requested. Defaults to True.
        mime_type (Optional[str], optional): Specific mime_type.
        headers (Optional[Dict[str, str]], optional): Custom Headers.
        filename (Optional[str], optional): Override filename.
        last_modified (Optional[Union[datetime, float, int, Default]], optional): The last modified date and time of the file.
        max_age (Optional[Union[float, int]], optional): Max age for cache control.
        no_store (Optional[bool], optional): Any cache should not store this response. Defaults to None.
        _range (Optional[Range], optional):

    Returns:
        HTTPResponse: The response object with the file data.
    r   rD   NzLast-ModifiedT)usegmtContent-Dispositionattachment; filename=""zno-storezpublic, max-age=expireszno-cachezcache-controlrbmodebytes -/Content-Range   
text/plainr7   )r2   r   rN   rP   r   r   st_mtimerT   
setdefaultr   r   r	   splitr   seekstartreadsizeendtotalr   r   )locationr$   rQ   validate_when_requested	mime_typer%   filenamerR   max_ageno_store_rangestatresponsecache_controlf
out_streams                   r'   filerz      s2    @ -*%--!-<FFH	M7	+)) 	 '&FFOmGZdC	
 !%;H:Q#G	
 "	*7)4 	
 #634::h/3Ht44 	( 	(&&&&& vvfkk22J a

|1V\\NC  F vvxJ	( 	( DZ1!4DI	 k * GB 5 	(&2 (	( 	( 	( 	(s   AH,H%H,8H9B:H,3H4H,<H=H, !H!H""HHAHHHH,H*H,H,H,H,HHHH,H)H H)%H,c                N    |xs i }t        | d      }||d<   t        |||      S )a;  Cause a HTTP redirect (302 by default) by setting a Location header.

    Args:
        to (str): path or fully qualified URL to redirect to
        headers (Optional[Dict[str, str]], optional): optional dict of headers to include in the new request. Defaults to None.
        status (int, optional): status code (int) of the new request, defaults to 302. Defaults to 302.
        content_type (str, optional): the content type (string) of the response. Defaults to "text/html; charset=utf-8".

    Returns:
        HTTPResponse: A response object with the redirect.
    z:/%#?&=@[]!$&'()*+,;)safeLocationr1   )r   r   )tor%   r$   r+   safe_tos        r'   redirectr   .  s<    " mG "89G "GJw\ r!   c                H   K   |xs i }|r|j                  dd| d       |xs t        j                         d   }|xs t        |      d   xs d}r4j                  }j
                  }j                  }	d| d| d	|	 |d
<   d} fd}
t        |
|||      S w)a  Return a streaming response object with file data.

    :param location: Location of file on system.
    :param chunk_size: The size of each chunk in the stream (in bytes)
    :param mime_type: Specific mime_type.
    :param headers: Custom Headers.
    :param filename: Override filename.
    :param _range:

    Args:
        location (Union[str, PurePath]): Location of file on system.
        status (int, optional): HTTP response code. Won't enforce the passed in status if only a part of the content will be sent (206) or file is being validated (304). Defaults to `200`.
        chunk_size (int, optional): The size of each chunk in the stream (in bytes). Defaults to `4096`.
        mime_type (Optional[str], optional): Specific mime_type.
        headers (Optional[Dict[str, str]], optional): Custom HTTP headers.
        filename (Optional[str], optional): Override filename.
        _range (Optional[Range], optional): The range of bytes to send.
    rW   rX   rY   r[   r   rd   r_   r`   ra   rb   rc   c                ~  K   t        d       d {   4 d {   }r|j                  j                         d {    j                  }|dkD  r|j	                  t        j                  f             d {   }t        |      dk  rnq|t        |      z  }| j                  |       d {    |dkD  rjnC	 |j	                         d {   }t        |      dk  rn| j                  |       d {    Bd d d       d {    y 7 7 7 7 7 h7 I7 #7 # 1 d {  7  sw Y   y xY ww)Nr\   r]   r   r   )r   rh   ri   rk   rj   minlenwrite)rv   rx   to_sendcontentrt   
chunk_sizern   s       r'   _streaming_fnz"file_stream.<locals>._streaming_fnv  s'    #H488 	2 	2AffV\\*** ++k$%FF3Z/H+I$JJG7|a's7|+G"..111 k $%FF:$66G7|a'"..111	 	2 	2 	28 	2* K 2 7 2	2 	2 	2 	2s   D=DD=DD=!D(D>D(D5D(7D 8	D(D(D"'D( D$D(D=D&D=D=D(D( D("D($D(&D=(D:.D1/D:6D=)streaming_fnr$   r%   r+   )rf   r	   rg   r   ri   rl   rm   r   )rn   r$   r   rp   r%   rq   rt   ri   rl   rm   r   s   ` `   `    r'   file_streamr   L  s     6 mG!%;H:Q#G	
 34::h/3HDZ1!4DIjj%+E7!C5%#A 2$ "	 s   BB")   N)r$   intr%   Optional[Dict[str, str]]returnr   )r)   Nzapplication/jsonN)r#   r   r$   r   r%   r   r+   r3   r,   zOptional[Callable[..., str]]r-   r   r   r   )r)   Nztext/plain; charset=utf-8)
r#   r3   r$   r   r%   r   r+   r3   r   r   )
r#   zOptional[AnyStr]r$   r   r%   r   r+   r3   r   r   )r)   N)r#   zUnion[str, bytes, HTMLProtocol]r$   r   r%   r   r   r   )rQ   r   rR   zUnion[datetime, float, int]r   zOptional[HTTPResponse])rn   Union[str, PurePath]r$   r   rQ   zOptional[Header]ro   boolrp   Optional[str]r%   r   rq   r   rR   z.Optional[Union[datetime, float, int, Default]]rr   zOptional[Union[float, int]]rs   zOptional[bool]rt   Optional[Range]r   r   )Ni.  r>   )
r~   r3   r%   r   r$   r   r+   r3   r   r   )r)   i   NNNN)rn   r   r$   r   r   r   rp   r   r%   r   rq   r   rt   r   r   r   )2
__future__r   r   r   email.utilsr   r   	mimetypesr   osr	   pathlibr
   r   typingr   r   r   r   r   r   urllib.parser   sanic.compatr   r   r   sanic.constantsr   sanic.helpersr   r   	sanic.logr   sanic.models.protocol_typesr   r   typesr   r   r   r(   r.   r8   r:   rA   rT   rz   r   r    r!   r'   <module>r      s   " ' 9      ? ? # 7 7 5 +  ; = = <@BB 8BB" (,**.
 & 	
 (  B (,3	
 & 	
 @ (,1	
 & 	
 6 (,
) & 	>22,G22n (,$(#(,"DL+/#"]"]] &] "	]
 ] &] ] B] )] ] ] ]D )-2	%  	
 @ #(,""A"AA A 	A
 &A A A Ar!   