
    AHj,                        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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 d
dlmZ 	 d dlmZ  ee      5  d dlmZ  d dl!Z"ddd        ejF                  d      Z$ e%       Z&dejN                  fdZ( G d d      Z) G d d      Z*y# e$ r	 d dlmZ Y fw xY w# 1 sw Y   XxY w)    )suppressN)Future)AnyOptionalUnion)	AwaitableCallable)	timedelta   )Curl)CurlCffiWarning   )Cookies)	HTTPErrorRequestException)Headers)loads)markdownifyzcharset=([\w-]+)qc                     | j                   5  | j                  j                          | j                  j	                          d| _        d d d        y # 1 sw Y   y xY w)Nr   )mutexqueueclearall_tasks_done
notify_allunfinished_tasks)r   s    Q/root/tools/cai/cai_env/lib/python3.12/site-packages/curl_cffi/requests/models.pyclear_queuer      sE    	
 		##%  s   <AAc            
       2    e Zd ZdZ	 ddedededee   fdZy)	RequestzRepresenting a sent request.

    Attributes:
        url: request url.
        headers: request headers.
        method: request http method.
        body: request body as bytes, or None if not provided.
    Nurlheadersmethodbodyc                 <    || _         || _        || _        || _        y N)r!   r"   r#   r$   )selfr!   r"   r#   r$   s        r   __init__zRequest.__init__/   s      	    r&   )	__name__
__module____qualname____doc__strr   r   bytesr(    r)   r   r    r    %   s;     !%

 
 	

 uo
r)   r    c                   :   e Zd ZdZddee   dee   fdZede	fd       Z
ede	fd       Zej                  d	e	ddfd
       Zedee	   fd       Zede	fd       Zde	fdZdede	fdZd ZddZddZd Zd ZddZddZddZde	fdZdefdZd Zde	fdZy) Responsea  Contains information the server sends.

    Attributes:
        url: url used in the request.
        content: response body in bytes.
        text: response body in str.
        status_code: http status code.
        reason: http response reason, such as OK, Not Found.
        ok: is status_code in [200, 400)?
        headers: response headers.
        cookies: response cookies.
        elapsed: timedelta of the request duration.
        encoding: http body encoding.
        charset: alias for encoding.
        primary_ip: primary ip of the server.
        primary_port: primary port of the server.
        local_ip: local ip used in this connection.
        local_port: local port used in this connection.
        charset_encoding: encoding specified by the Content-Type header.
        default_encoding: encoding for decoding response content if charset is not found
            in headers. Defaults to "utf-8". Can be set to a callable for automatic
            detection.
        redirect_count: how many redirects happened.
        redirect_url: the final redirected url.
        http_version: http version used.
        history: history redirections, only headers are available.
        download_size: total downloaded bytes (body).
        upload_size: total uploaded bytes (body).
        header_size: total header size.
        request_size: request size.
        response_size: download_size + header_size
    Ncurlrequestc                    || _         || _        d| _        d| _        d| _        d| _        d| _        t               | _        t               | _
        t               | _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        g | _        i | _        d | _        d | _        d | _        d | _        d| _        d| _        d| _        d| _        d| _        d| _         y )	N r)      OKTutf-8r   F)!r3   r4   r!   contentstatus_codereasonokr   r"   r   cookiesr
   elapseddefault_encodingredirect_countredirect_urlhttp_version
primary_ipprimary_portlocal_ip
local_porthistoryinfosr   stream_taskastream_taskquit_now_stream_closeddownload_sizeupload_sizeheader_sizerequest_sizeresponse_size)r'   r3   r4   s      r   r(   zResponse.__init__^   s    	yy"++DK!!" -/%'
,0
-115#"# ! !!""#r)   returnc                     | j                   S )zAlias for encoding.)encodingr'   s    r   charsetzResponse.charset~   s     }}r)   c                    t        | d      sp| j                  }|Wt        | j                  t              r| j                  }n0t        | j                        r| j                  | j                        }|xs d| _        | j                  S )a  
        Determines the encoding to decode byte content into text.

        The method follows a specific priority to decide the encoding:
        1. If ``.encoding`` has been explicitly set, it is used.
        2. The encoding specified by the ``charset`` parameter in the ``Content-Type``
            header.
        3. The encoding specified by the ``default_encoding`` attribute. This can either
            be a string (e.g., "utf-8") or a callable for charset autodetection.
        	_encodingr9   )hasattrcharset_encoding
isinstancer@   r.   callabler:   rY   )r'   rU   s     r   rU   zResponse.encoding   sr     t[),,Hd33S9#44Hd334#44T\\BH%0DN~~r)   valuec                 @    t        | d      rt        d      || _        y )N_textz0Cannot set encoding after text has been accessed)rZ   
ValueErrorrY   )r'   r^   s     r   rU   zResponse.encoding   s    4!OPPr)   c                     | j                   j                  d      }|r*t        j                  |      }|r|j	                  d      S dS y)z=Return the encoding, as specified by the Content-Type header.zContent-Typer   N)r"   get
CHARSET_REsearchgroup)r'   content_typecharset_matchs      r   r[   zResponse.charset_encoding   sF     ||''7&--l;M-:=&&q)DDr)   c                     t        | d      s?| j                  sd| _        | j                  S | j                  | j                        | _        | j                  S )Nr`   r6   )rZ   r:   r`   _decoderV   s    r   textzResponse.text   sF    tW%<<
 zz "\\$,,7
zzr)   c                     t        j                  | j                        }|j                         }|j	                  d      }t        d| d| d      }|S )NT)html_partialz<h1>z</h1><main>z</main>)rdDocumentr:   titlesummarymd)r'   docrp   rq   
body_as_mds        r   markdownzResponse.markdown   sN    kk$,,'		++4+0$ug[	AB
r)   r:   c                     	 |j                  | j                  d      S # t        t        f$ r |j                  d      cY S w xY w)Nreplace)errorsz	utf-8-sig)decoderU   UnicodeDecodeErrorLookupError)r'   r:   s     r   rj   zResponse._decode   sA    	/>>$--	>BB"K0 	/>>+..	/s     AAc                 j    | j                   s't        d| j                   d| j                   d|       y)z2Raise an error if status code is not in [200, 400)zHTTP Error z: r   N)r=   r   r;   r<   rV   s    r   raise_for_statuszResponse.raise_for_status   s6    wwk$*:*:);2dkk]KQPTUU r)   c              #     K   d}| j                  ||      D ]_  }|||z   }|r|j                  |      n|j                         }|r%|d   r |r|d   d   |d   k(  r|j                         nd}|E d{    a || yy7 wz
        iterate streaming content line by line, separated by ``\n``.

        Copied from: https://requests.readthedocs.io/en/latest/_modules/requests/models/
        which is under the License: Apache 2.0
        N)
chunk_sizedecode_unicode)iter_contentsplit
splitlinespop)r'   r   r   	delimiterpendingchunkliness          r   
iter_lineszResponse.iter_lines   s      &&!. ' 
 	E "%.7EKK	*U=M=M=OE U2Y5U2Yr]eBi5O 		  	 M  s   A1B3B4Bc              #   T  K   |rt        j                  dt        d       |r
t               | j                  r| j
                  sJ d       	 | j                  j                         }t        |t              r| j                          ||t        u rn| J| j                          ywzD
        iterate streaming content chunk by chunk in bytes.
        z9chunk_size is ignored, there is no way to tell curl that.r   )
stacklevelzstream mode is not enabled.N)warningswarnr   NotImplementedErrorr   r3   rc   r\   r   _finalize_stream
STREAM_ENDr'   r   r   r   s       r   r   zResponse.iter_content   s      MMK
 %''zzdiiF)FF'JJNN$E %!12%%' 
"K  	s   B&B(c                 .    t        | j                  fi |S )z+return a parsed json object of the content.)r   r:   )r'   kws     r   jsonzResponse.json   s    T\\(R((r)   c                 $    | j                          yz:Close the streaming connection, only valid in stream mode.N)r   rV   s    r   closezResponse.close   s    r)   c                 \   | j                   ry | j                  | j                  | j                  y d| _         | j                  r| j                  j	                          | j                  r| j                  j                          | j                  r| j                  j                          y y )NT)rM   r   rJ   rL   setresultr3   r   rV   s    r   r   zResponse._finalize_stream  s    ::$"2"2":t}}?T"==MM##%99IIOO r)   c                  K   d}| j                  ||      2 3 d{   }|||z   }|r|j                  |      n|j                         }|r%|d   r |r|d   d   |d   k(  r|j                         nd}|D ]  }| 	 i7 d6 || yywr   )aiter_contentr   r   r   )r'   r   r   r   r   r   r   lines           r   aiter_lineszResponse.aiter_lines  s      --!. . 
 	 	% "%.7EKK	*U=M=M=OE U2Y5U2Yr]eBi5O 		   
	 
 M s&   BBBBA!BB
Bc                  K   |rt        j                  dt        d       |r
t               | j                  r| j
                  sJ d       	 | j                  j                          d{   }t        |t              r| j                          d{    ||t        u r| j                          d{    y| s7 U7 /7 wr   )r   r   r   r   r   r3   rc   r\   r   acloser   r   s       r   r   zResponse.aiter_content+  s      MMK
 %''zzdiiF)FF'**..**E %!12kkm## 
"kkm##K * $
 $s6   A(C*C +'CC!C4C5CCCc                 \   K   | j                  | j                          d{         S 7 w)z*
        Return a decoded string.
        N)rj   acontentrV   s    r   atextzResponse.atextI  s#      ||$--/1221s   ,*	,c                    K   g }| j                         2 3 d{   }|j                  |       7 6 dj                  |      S w)z8wait and read the streaming content in one bytes object.Nr)   )r   appendjoin)r'   chunksr   s      r   r   zResponse.acontentO  sG     --/ 	! 	!%MM% 	!/xxs   A202A2Ac                 R   K   | j                   r| j                    d{    yy7 wr   )rK   rV   s    r   r   zResponse.acloseV  s(      #### #s   '%'c                 "    d| j                    dS )Nz<Response [z]>)r;   rV   s    r   __repr__zResponse.__repr__]  s    T--.b11r)   )NN)NFN)NF)rS   N)r*   r+   r,   r-   r   r   r    r(   propertyr.   rW   rU   setterr[   rk   ru   r/   rj   r}   r   r   r   r   r   r   r   r   r   r   r   r0   r)   r   r2   r2   <   s,   B$Xd^ $Xg=N $@    #  * __c d  
 (3-   c  # /u / /V
4 <) 6<3S 3   $2# 2r)   r2   )+
contextlibr   r   rer   concurrent.futuresr   typingr   r   r   collections.abcr   r	   datetimer
   r3   r   utilsr   r>   r   
exceptionsr   r   r"   r   orjsonr   ImportErrorr   r   rr   readabilityrn   compilerd   objectr   Queuer   r    r2   r0   r)   r   <module>r      s      	  % ' ' /   #  3  k - RZZ+,
X
5;;  .b2 b2S   s   B1  C1B?>B?C