
    wg&                       d dl m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mZ dd	lmZ dd
lmZ ddlmZ ej0                  rddlmZmZ  ej8                  e      Ze	 G d d             Z G d de      Z y)    )annotationsN)contextmanager)	dataclass)HTTPException)BytesIOIOBase   )InvalidHeaderTimeoutError)BaseHTTPResponse)Retry   )EmscriptenRequest)BaseHTTPConnectionBaseHTTPSConnectionc                  6    e Zd ZU ded<   ded<   ded<   ded<   y	)
EmscriptenResponseintstatus_codezdict[str, str]headerszIOBase | bytesbodyr   requestN)__name__
__module____qualname____annotations__     h/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/urllib3/contrib/emscripten/response.pyr   r      s    
r   r   c                  J    e Zd Z	 	 d	 	 	 	 	 d fdZedd       Zej                  dd       Zedd       Zedd       Zej                  dd       Z	 d	 	 	 	 	 ddZ	ddZ
	 	 	 d	 	 	 	 	 	 	 dd	Z	 	 d	 	 	 	 	 dd
ZddZddZedd       ZddZddZed d       Z xZS )!EmscriptenHttpResponseWrapperc           	     $   d | _         d | _        || _        || _        || _        d| _        t        |   |j                  |j                  |dddd       | j                  | j                  j                  j                        | _        d| _        y )NFr   zHTTP/? T)r   statusrequest_urlversionversion_stringreasondecode_content)_pool_body	_response_url_connection_closedsuper__init__r   r   _init_lengthr   methodlength_remaininglength_is_certain)selfinternal_responseurl
connection	__class__s       r   r1   z&EmscriptenHttpResponseWrapper.__init__   s     

*	%%--$00# 	 	
 !% 1 1$..2H2H2O2O P!&r   c                    | j                   S Nr-   r6   s    r   r8   z!EmscriptenHttpResponseWrapper.url7   s    yyr   c                    || _         y r<   r=   )r6   r8   s     r   r8   z!EmscriptenHttpResponseWrapper.url;   s	    	r   c                    | j                   S r<   )r.   r>   s    r   r9   z(EmscriptenHttpResponseWrapper.connection?   s    r   c                    | j                   S r<   _retriesr>   s    r   retriesz%EmscriptenHttpResponseWrapper.retriesC   s    }}r   c                    || _         y r<   rB   )r6   rD   s     r   rD   z%EmscriptenHttpResponseWrapper.retriesG   s      r   c              #  D   K   	 | j                  ||      }|r| nyw)a_  
        A generator wrapper for the read() method. A call will block until
        ``amt`` bytes have been read from the connection or until the
        connection is closed.

        :param amt:
            How much of the content to read. The generator will return up to
            much data per iteration, but may return less. This is particularly
            likely when using compressed data. However, the empty string will
            never be returned.

        :param decode_content:
            If True, will attempt to decode the body based on the
            'content-encoding' header.
        )amtr)   Nread)r6   rG   r)   datas       r   streamz$EmscriptenHttpResponseWrapper.streamL   s-     $ 99^9DD
 s    c                   | j                   j                  d      }|\	 |j                  d      D ch c]  }t        |       }}t	        |      dkD  rt        d|z        |j                         }|dk  rd }nd }| j                  dv sd| j                  cxk  rdk  sn |d	k(  rd}|S c c}w # t        $ r d }Y @w xY w)
Nzcontent-length,r   z8Content-Length contained multiple unmatching values (%s)r   )   i0  d      HEAD)	r   getsplitr   lenr
   pop
ValueErrorr$   )r6   request_methodcontent_lengthvallengthslengths         r   r2   z*EmscriptenHttpResponseWrapper._init_lengthf   s    %)\\%5%56F%G%" 0>/C/CC/HI3s8IIw<!#'13AB  ! A:!F F KK:%dkk'C''F1 J  s"   B/ B*.B/ *B/ /B=<B=c                2   | j                   sP| j                  Dt        | j                  j                  t              r!| j                  j                  j
                  ry| j                         5  t        | j                  j                  t              sYt        | j                  j                        | _        d| _	        t        | j                  j                        | j                  _        ||dk\  rd}| j                  j                  j                  |      }| j                  't        | j                  t        |      z
  d      | _        | j                  r| j                  dk(  st        |      |k  r$| j                  j                  j                          t        j                  t         |      cd d d        S | j                  j                  j                         }|r|| _        | j                  't        | j                  t        |      z
  d      | _        t        |      dk(  s| j                  r3| j                  dk(  r$| j                  j                  j                          t        j                  t         |      cd d d        S # 1 sw Y   y xY w)Nr   Tr   F)r/   r,   
isinstancer   r   closed_error_catcherrT   r4   r5   r   rI   maxclosetypingcastbytesr+   )r6   rG   r)   cache_contentrJ   s        r   rI   z"EmscriptenHttpResponseWrapper.read   s    LL~~%4>>..7DNN<O<O<V<V  " 	0dnn116:(+DNN,?,?(@%)-&&-dnn.A.A&B#3!8 %~~**//4((4,/0E0ED	0QST,UD)**t/D/D/IcOO NN''--/{{5$/%	0 	0( ~~**//1 !%DJ((4,/0E0ED	0QST,UD)t9>**t/D/D/I NN''--/{{5$/=	0 	0 	0s   .EJ=CJJc              #  @   K   	 | j                  ||      }|sy | wr<   rH   )r6   rG   r)   rd   s       r   read_chunkedz*EmscriptenHttpResponseWrapper.read_chunked   s+      IIc>2EK	 s   c                    | j                   r| j                  sy | j                   j                  | j                         d | _        y r<   )r*   r.   	_put_connr>   s    r   release_connz*EmscriptenHttpResponseWrapper.release_conn   s4    zz!1!1

T--.r   c                $    | j                          y r<   )ra   r>   s    r   
drain_connz(EmscriptenHttpResponseWrapper.drain_conn   s    

r   c                V    | j                   r| j                   S | j                  d      S )NT)re   )r+   rI   r>   s    r   rJ   z"EmscriptenHttpResponseWrapper.data   s$    ::::994900r   c                b    | j                   j                  d      }t        j                  |      S )a  
        Deserializes the body of the HTTP response as a Python object.

        The body of the HTTP response must be encoded using UTF-8, as per
        `RFC 8529 Section 8.1 <https://www.rfc-editor.org/rfc/rfc8259#section-8.1>`_.

        To use a custom JSON decoder pass the result of :attr:`HTTPResponse.data` to
        your custom decoder instead.

        If the body of the HTTP response is not decodable to UTF-8, a
        `UnicodeDecodeError` will be raised. If the body of the HTTP response is not a
        valid JSON document, a `json.JSONDecodeError` will be raised.

        Read more :ref:`here <json_content>`.

        :returns: The body of the HTTP response as a Python object.
        zutf-8)rJ   decode_jsonloads)r6   rJ   s     r   jsonz"EmscriptenHttpResponseWrapper.json   s'    $ yy({{4  r   c                   | j                   s}t        | j                  j                  t              r$| j                  j                  j                          | j                  r!| j                  j                          d | _        d| _         y y )NT)r/   r]   r,   r   r   ra   r.   r>   s    r   ra   z#EmscriptenHttpResponseWrapper.close   sb    ||$..--v6##))+  &&(#' DL r   c              #    K   ddl m}m} d}	 d d}	 |syt        | j                  j                  t              rD| j                  j                  j                  s$| j                  j                  j                          | j                          yt        | j                  j                  t              r2| j                  j                  j                  r| j                          yyy# |$ r}t        t	        |            d}~w|$ r}t        t	        |            d}~ww xY w# |syt        | j                  j                  t              rD| j                  j                  j                  s$| j                  j                  j                          | j                          w t        | j                  j                  t              r2| j                  j                  j                  r| j                          w w w xY ww)z
        Catch Emscripten specific exceptions thrown by fetch.py,
        instead re-raising urllib3 variants, so that low-level exceptions
        are not leaked in the high-level api.

        On exit, release the connection back to the pool.
        r   )_RequestError_TimeoutErrorFNT)fetchru   rv   r   strr   r]   r,   r   r   r^   ra   rj   )r6   ru   rv   
clean_exites        r   r_   z,EmscriptenHttpResponseWrapper._error_catcher   sv     	8
	( J  t~~22F; NN//66NN''--/!!#
 t~~22F;++22%%' 3 <+  	's1v&& 	(A''	(
  t~~22F; NN//66NN''--/!!#
 t~~22F;++22%%' 3 <sA   G:C' CG:'D!,D  D!DD!!D$ $CG77G:)NN)r7   r   r8   
str | Noner9   /BaseHTTPConnection | BaseHTTPSConnection | None)returnr{   )r8   r{   r}   None)r}   r|   )r}   Retry | None)rD   r   r}   r~   )i   N)rG   
int | Noner)   bool | Noner}   ztyping.Generator[bytes])rW   r{   r}   r   )NNF)rG   r   r)   r   re   boolr}   rd   )r}   r~   )r}   rd   )r}   z
typing.Any)r}   ztyping.Generator[None])r   r   r   r1   propertyr8   setterr9   rD   rK   r2   rI   rg   rj   rl   rJ   rr   ra   r   r_   __classcell__)r:   s   @r   r!   r!      s_    FJ	'-' ' D	'0   	ZZ        ^^   
 FJ7B	 4#N &*#	+0+0 $+0 	+0
 
+0^ &*

 $
 
!	
  1 1!*  )( )(r   r!   )!
__future__r   rr   rp   loggingrb   
contextlibr   dataclassesr   http.clientr   ior   r   
exceptionsr
   r   responser   
util.retryr   r   r   TYPE_CHECKING_base_connectionr   r   	getLoggerr   logr   r!   r   r   r   <module>r      so    "    % ! 6  5 (  &	Kg!   ($4 (r   