
    ,hn                        d Z ddgZddlZddlZddlZ	 ddlZej                  ej                  ej                  ej                  dZ
ddlmZmZ ddlmZ ddlmZmZmZmZmZ d	d
lmZmZmZmZmZmZ d	dlmZm Z m!Z!m"Z" ddl#m$Z$m%Z% de&de'f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.y# e$ r dZY w xY w)zFModule implementing low-level socket communication with MySQL servers.MySQLTcpSocketMySQLUnixSocket    N)TLSv1TLSv1.1TLSv1.2TLSv1.3)ABCabstractmethod)deque)AnyDequeListOptionalTuple   )InterfaceErrorNotSupportedErrorOperationalErrorProgrammingErrorReadTimeoutErrorWriteTimeoutError)COMPRESSED_PACKET_HEADER_LENGTHMAX_PAYLOAD_LENGTHMIN_COMPRESS_LENGTHPACKET_HEADER_LENGTH   )StreamWriteropen_connectionerrreturnc                 f    | j                   st        |       S | j                    d| j                   S )z`Reformat the IOError error message.

    This function reformats the IOError error message.
     )errnostrstrerror)r   s    ]/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/mysql/connector/aio/network.py_strioerrorr'   I   s,    
 993s8GSYYKq*GG    c                       e Zd ZdZe	 	 	 ddedededee	   dee	   dee	   d	dfd
       Z
e	 ddej                  dedee	   d	efd       Zy)NetworkBrokeraP  Broker class interface.

    The network object is a broker used as a delegate by a socket object. Whenever the
    socket wants to deliver or get packets to or from the MySQL server it needs to rely
    on its network broker (netbroker).

    The netbroker sends `payloads` and receives `packets`.

    A packet is a bytes sequence, it has a header and body (referred to as payload).
    The first `PACKET_HEADER_LENGTH` or `COMPRESSED_PACKET_HEADER_LENGTH`
    (as appropriate) bytes correspond to the `header`, the remaining ones represent the
    `payload`.

    The maximum payload length allowed to be sent per packet to the server is
    `MAX_PAYLOAD_LENGTH`. When  `send` is called with a payload whose length is greater
    than `MAX_PAYLOAD_LENGTH` the netbroker breaks it down into packets, so the caller
    of `send` can provide payloads of arbitrary length.

    Finally, data received by the netbroker comes directly from the server, expect to
    get a packet for each call to `recv`. The received packet contains a header and
    payload, the latter respecting `MAX_PAYLOAD_LENGTH`.
    Nwriteraddresspayloadpacket_numbercompressed_packet_numberwrite_timeoutr    c                    K   yw)a  Send `payload` to the MySQL server.

        If provided a payload whose length is greater than `MAX_PAYLOAD_LENGTH`, it is
        broken down into packets.

        Args:
            sock: Object holding the socket connection.
            address: Socket's location.
            payload: Packet's body to send.
            packet_number: Sequence id (packet ID) to attach to the header when sending
                           plain packets.
            compressed_packet_number: Same as `packet_number` but used when sending
                                      compressed packets.
            write_timeout: Timeout in seconds before which sending a packet to the server
                           should finish else WriteTimeoutError is raised.


        Raises:
            :class:`OperationalError`: If something goes wrong while sending packets to
                                       the MySQL server.
        N )selfr+   r,   r-   r.   r/   r0   s          r&   writezNetworkBroker.writei           readerread_timeoutc                    K   yw)aM  Get the next available packet from the MySQL server.

        Args:
            sock: Object holding the socket connection.
            address: Socket's location.
            read_timeout: Timeout in seconds before which reading a packet from the server
                          should finish.

        Returns:
            packet: A packet from the MySQL server.

        Raises:
            :class:`OperationalError`: If something goes wrong while receiving packets
                                       from the MySQL server.
            :class:`ReadTimeoutError`: If the time to receive a packet from the server takes
                                       longer than `read_timeout`.
            :class:`InterfaceError`: If something goes wrong while receiving packets
                                     from the MySQL server.
        Nr2   )r3   r7   r,   r8   s       r&   readzNetworkBroker.read   r5   r6   NNNN)__name__
__module____qualname____doc__r
   r   r$   bytesr   intr4   asyncioStreamReader	bytearrayr:   r2   r(   r&   r*   r*   Q   s    .  (,26'+  	
  } #+3-  } 
 > 
 '+	$$  sm	
 
 r(   r*   c                      e Zd ZdZddZededeeef   fd       Z	dde
e   ddfdZd	ed
ededdfdZ	 	 ddej                   dede
e   defdZ	 	 	 dd	ed
edede
e   de
e   de
e   ddfdZ	 ddej                   d
ede
e   defdZy)NetworkBrokerPlain,Broker class for MySQL socket communication.r    Nc                     d| _         y N_pktnrr3   s    r&   __init__zNetworkBrokerPlain.__init__   s	    r(   pktc                     t        |       t        k  rt        d      t        j                  d| dd dz         d   | d   }}||fS )-Recover the header information from a packet.3Can't recover header info from an incomplete packet<Ir          )lenr   
ValueErrorstructunpack)rP   pllseqids      r&   
get_headerzNetworkBrokerPlain.get_header   sW     s8**RSS MM$Aa7 23A6F 
 Ezr(   next_idc                 l    || xj                   dz  c_         n|| _         | xj                   dz  c_         yz:Set the given packet id, if any, else increment packet id.Nr      rL   r3   r^   s     r&   _set_next_pktnrz"NetworkBrokerPlain._set_next_pktnr   s*    ?KK1K!DKsr(   r+   r,   c                    K   	 |j                  |       |j                          d{    y7 # t        $ r}t        d|t	        |      f      |d}~wt
        $ r}t        d      |d}~ww xY ww)z!Write packet to the comm channel.N  r#   valuesi  r#   )r4   drainIOErrorr   r'   AttributeError)r3   r+   r,   rP   r   s        r&   
_write_pktzNetworkBrokerPlain._write_pkt   sl     	8LL,,.   	"G[-=#>  	8".C7	8s<   A3$0 .0 A30 	A0AA0A++A00A3r7   sizer8   c                 l  K   	 t        d      }t        |      |k  r[t        j                  |j	                  |t        |      z
        |       d{   }|st        d      ||z  }t        |      |k  r[|S 7 (# t        j                  t        j                  f$ r}t        d      |d}~ww xY ww)z(Read `size` bytes from the comm channel.r(   Ni  rh     )	rE   rW   rC   wait_forr:   r   CancelledErrorTimeoutErrorr   )r3   r7   rm   r8   rP   chunkr   s          r&   _read_chunkzNetworkBrokerPlain._read_chunk   s     	8C.Cc(T/%..KKs3x0,  (t44u c(T/ J &&(<(<= 	8".C7	8sA   B4AA< A:%A< 8A< 9B4:A< <#B1B,,B11B4r-   r.   r/   r0   c                   K   | j                  |       d}	 t        t        |      t        z        D ]x  }t	        j
                  | j                  ||dt        j                  d| j                        z   |||t        z    z         |       d{    | j                          |t        z  }z t	        j
                  | j                  ||t        j                  dt        |      |z
        dd t        j                  d| j                        z   ||d z         |       d{    y7 7 # t        j                  t        j                  f$ r}	t        d      |	d}	~	ww xY ww)	zSend payload to the MySQL server.

        If provided a payload whose length is greater than `MAX_PAYLOAD_LENGTH`, it is
        broken down into packets.
        r      <BNrT   rU   ro   rh   )rc   rangerW   r   rC   rp   rl   rY   packrM   rq   rr   r   )
r3   r+   r,   r-   r.   r/   r0   offset_r   s
             r&   r4   zNetworkBrokerPlain.write   sg     	]+ 	93w<+==> -&&OO# ++dDKK89!&64F+FGH "	 	 	 $$&,,- ""KKc'lV&;<QqAkk$45fg&' 	 	 			 &&(<(<= 	9#$/S8	9sN   E#A6D+ D'BD+ !D)"D+ &E#'D+ )D+ +#E EE  E#c                   K   	 | j                  |t        |       d{   }| j                  |      \  }| _        || j                  |||       d{   z   S 7 ;7 # t        $ r}t        d|t        |      f      |d}~ww xY ww)z+Receive `one` packet from the MySQL server.Nre   rf   )rt   r   r]   rM   rj   r   r'   )r3   r7   r,   r8   headerpayload_lenr   s          r&   r:   zNetworkBrokerPlain.read  s     	++F4H,WWF (,v'>$K $"2"26;"UUUU X V 	"G[-=#>	sJ   BA A4A AA BA A 	B'B  BBr    Nr<   )r   Nr;   )r=   r>   r?   r@   rO   staticmethodrA   r   rB   r]   r   rc   r   r$   rl   rC   rD   rE   rt   r4   r:   r2   r(   r&   rG   rG      sN   6 
 
%S/ 
 
x}  88 8 	8
 
8( &*	8$$8 8 sm	8
 
82 (,26'+-9-9 -9 	-9
  }-9 #+3--9  }-9 
-9f '+	$$  sm	
 
r(   rG   c                   X    e Zd ZdZd fdZedededee   fd       Z	edede
eeef   fd	       Zdd
ee   ddfdZdedededdf fdZ	 	 	 ddedededee   dee   dee   ddf fdZ	 ddej&                  dedee   ddf fdZ	 ddej&                  dedee   def fdZ xZS )NetworkBrokerCompressedrH   r    Nc                 N    t         |           d| _        t               | _        y rJ   )superrO   _compressed_pktnrr   _queue_read)r3   	__class__s    r&   rO   z NetworkBrokerCompressed.__init__-  s     !#-2Wr(   r-   pktnrc                 |   d}g }t        t        |       t        z        D ]H  }|j                  dt	        j
                  d|      z   | ||t        z    z          |dz   dz  }|t        z  }J |j                  t	        j
                  dt        |       |z
        dd t	        j
                  d|      z   | |d z          |S )	z2Prepare a payload for sending to the MySQL server.r   rv   rw   r   ra   rT   rU   N)rx   rW   r   appendrY   ry   )r-   r   rz   pktsr{   s        r&   _prepare_packetsz(NetworkBrokerCompressed._prepare_packets2  s     
 s7|'99: 	)AKK++dE*+&6,>#>?@
 QY#%E((F	) 	KKc'lV34Qq9kk$&'fg	

 r(   rP   c                     t        |       t        k  rt        d      t        j                  d| dd dz         d   | d   t        j                  d| dd dz         d   }}}|||fS )rR   rS   rT   r   rU   rV         )rW   r   rX   rY   rZ   )rP   compressed_pllr\   uncompressed_plls       r&   r]   z"NetworkBrokerCompressed.get_headerK  s{     s855RSS MM$Aa7 23A6FMM$Aa7 23A6  0 u&666r(   r^   c                 l    || xj                   dz  c_         n|| _         | xj                   dz  c_         yr`   )r   rb   s     r&   _set_next_compressed_pktnrz2NetworkBrokerCompressed._set_next_compressed_pktnrY  s1    ?""a'"%,D"#%r(   r+   r,   c                 >  K   t        j                  |      }t        j                  dt	        |            dd t        j                  d| j
                        z   t        j                  dt	        |            dd z   |z   }t        |   |||       d{   S 7 w)z1Compress packet and write it to the comm channel.rT   r   rU   rw   N)zlibcompressrY   ry   rW   r   r   rl   )r3   r+   r,   rP   compressed_pktr   s        r&   rl   z"NetworkBrokerCompressed._write_pkta  s      s+KKc.121Q7kk$ 6 678kk$C)!A./  	 W'====s   BBBBr.   r/   r0   c                 X  K   | j                  |       | j                  |       	 t        d      j                  | j	                  || j
                              }t        |      t        t        z
  k\  rd}t        t        |      t        z        D ]T  }	t        j                  | j                  |||||t        z          |       d{    | j                          |t        z  }V t        j                  | j                  ||||d       |       d{    yt        |      t        kD  r0t        j                  | j                  |||      |       d{    yt        j                  t        | 1  ||t        j                   dt        |            dd t        j                   d| j"                        z   t        j                   dd      dd z   |z         |       d{    y7 !7 7 7 # t        j$                  t        j&                  f$ r}
t)        d      |
d}
~
ww xY ww)	zSend `payload` as compressed packets to the MySQL server.

        If provided a payload whose length is greater than `MAX_PAYLOAD_LENGTH`, it is
        broken down into packets.
        r(   r   NrT   rU   rw   ro   rh   )rc   r   rE   joinr   rM   rW   r   r   rx   rC   rp   rl   r   r   rY   ry   r   rq   rr   r   )r3   r+   r,   r-   r.   r/   r0   payload_preprz   r{   r   r   s              r&   r4   zNetworkBrokerCompressed.writeq  s%     	]+''(@A6	9$S>..%%gt{{;L 7|14HHH  s<04FFG 
1A!**"#(&;M2MN
 &   33500F
1 &&OOFG\&'5JK!   w<"55!**F  
 "***"#"KKc,.?@1E$kk$0F0FGH$kk$21Q78 ++ &
 
 
5

 &&(<(<= 	9#$/S8	9s   #H*B!G2 G)	AG2 G,G2 H*<G2 G.G2 H*BG2 #G0$G2 (H*)G2 ,G2 .G2 0G2 2#H'H""H''H*r7   r   r8   c           	        K   t        t        j                  t        
|   |||       d{               }d}|t        |      k  rt        j                  d|||t        z   dz
   dz         d   }t        |z   t        |      |z
  kD  rot        
|   |t        |       d{   }| j                  |      \  }| _        }t        
|   |||       d{   }	||dk(  r|	nt        j                  |	      z  }| j                  j                  |||t        z   |z           |t        |z   z  }|t        |      k  ryy7 7 7 mw)z&Handle reading of a compressed packet.Nr   rT   r   rV   )rE   r   
decompressr   rt   rW   rY   rZ   r   r   r]   r   r   r   )r3   r7   r   r8   rP   rz   r[   r}   r   r   r   s             r&   _read_compressed_pktz,NetworkBrokerCompressed._read_compressed_pkt  sf     OOg)&.,OO

 s3x--c&6,@#@1#DEOC $c)CHv,==  %w2;\   OOF+	"*$',w':NL( " '1, #8 ##C:N1NQT1T$UV*S00F? s3x	 P"s;   )ED>A5E"E#2EEA%E<EEEc                    K   | j                   s	 t        	| 	  |t        |       d{   }| j	                  |      \  }| _        }|dk(  r3| j                   j                  t        	| 	  |||       d{          n| j                  |||       d{    | j                   sy| j                   j                         }|d   | _        |S 7 7 Y7 ;# t        $ r}t        d|t        |      f      |d}~ww xY ww)z{Receive `one` or `several` packets from the MySQL server, enqueue them, and
        return the packet at the head.
        Nr   re   rf   rU   )r   r   rt   r   r]   r   r   r   rj   r   r'   popleftrM   )
r3   r7   r,   r8   r}   r   r   r   rP   r   s
            r&   r:   zNetworkBrokerCompressed.read  s     $w2;\   OOF+	"*$ $q($$++#g1&.,WW
 33   &&(!f
A X  &S1A'Bs]   C>C CAC 6C7C CC 3C>C C C 	C;C66C;;C>r   r<   r;   )r=   r>   r?   r@   rO   r   rA   rB   r   r   r   r]   r   r   r   r$   rl   r4   rC   rD   r   rE   r:   __classcell__r   s   @r&   r   r   *  s   65
 %  U  0 7 7%S#"6 7 7&(3- &4 &>> > 	>
 
>* (,26'+G9G9 G9 	G9
  }G9 #+3-G9  }G9 
G9Z '+	-1$$-1 -1 sm	-1
 
-1f '+	-$$- - sm	-
 
- -r(   r   c                   h   e Zd ZdZddZedefd       Zede	ddfd       Z
ddZdefd	Zd
eddfdZddZdej$                  ddfdZ	 	 	 d dedee   dee   dee   ddf
dZd!dee   defdZdddddg g fdee   dee   dee   dee   dee   deee      deee      dej$                  fdZy)"MySQLSocketzMySQL socket communication interface.

    Examples:
        Subclasses: network.MySQLTCPSocket and network.MySQLUnixSocket.
    r    Nc                 h    d| _         d| _        d| _        d| _        t	               | _        d| _        y)zsNetwork layer where transactions are made with plain (uncompressed) packets
        is enabled by default.
        NF)_reader_writer_connection_timeout_addressrG   
_netbroker_is_connectedrN   s    r&   rO   zMySQLSocket.__init__   s3     8</326 '+);)=#(r(   c                     | j                   S )zSocket location.)r   rN   s    r&   r,   zMySQLSocket.address+  s     }}r(   kwargsc                    K   yw)zOpen the socket.Nr2   r3   r   s     r&   r   zMySQLSocket.open_connection0  r5   r6   c                 n  K   | j                   r	 | j                   j                          | j                   j                  $| j                   j                  j                          | j                   j	                          d{    d| _        yd| _        y7 # t
        $ r}Y d}~d| _        yd}~ww xY ww)zClose the connection.NF)r   close	transportabortwait_closed	Exceptionr   )r3   r{   s     r&   close_connectionzMySQLSocket.close_connection4  s     <<	""$<<))5LL**002ll..000
 #U 1  "	s;   B5A1B BB B5B 	B2!B5-B22B5c                     | j                   S )zCheck if the socket is connected.

        Return:
            bool: Returns `True` if the socket is connected to MySQL server.
        )r   rN   s    r&   is_connectedzMySQLSocket.is_connectedC  s     !!!r(   timeoutc                     || _         y)zSet the connection timeout.N)r   )r3   r   s     r&   set_connection_timeoutz"MySQLSocket.set_connection_timeoutK  s
    #* r(   c                 "    t               | _        y)zIEnable network layer where transactions are made with compressed packets.N)r   r   rN   s    r&   switch_to_compressed_modez%MySQLSocket.switch_to_compressed_modeO  s    13r(   ssl_contextc                    K   | j                   J | j                   j                  j                  d      }|j                  dk(  rt	        d      | j                   j                  |       d{    y7 w)a?  Upgrade an existing stream-based connection to TLS.

        The `start_tls()` method from `asyncio.streams.StreamWriter` is only available
        in Python 3.11. This method is used as a workaround.

        The MySQL TLS negotiation happens in the middle of the TCP connection.
        Therefore, passing a socket to open connection will cause it to negotiate
        TLS on an existing connection.

        Args:
            ssl_context: The SSL Context to be used.

        Raises:
            RuntimeError: If the transport does not expose the socket instance.
        Nsocketr   z,SSL is not supported when using Unix sockets)r   r   get_extra_infofamilyr   	start_tls)r3   r   r   s      r&   switch_to_sslzMySQLSocket.switch_to_sslS  s_     " ||''''66x@==A"#QRRll$$[111s   A,A6.A4/A6r-   r.   r/   r0   c                    K   | j                   j                  | j                  | j                  ||||       d{    y7 w)z!Send packets to the MySQL server.)r.   r/   r0   N)r   r4   r   r,   )r3   r-   r.   r/   r0   s        r&   r4   zMySQLSocket.writel  sC      oo##LLLL'%=' $ 
 	
 	
s   9AAAr8   c                    K   | j                   j                  | j                  | j                  |       d{   S 7 w)z#Read packets from the MySQL server.N)r   r:   r   r,   )r3   r8   s     r&   r:   zMySQLSocket.read}  s-     __))$,,lSSSSs   5><>Fssl_cassl_certssl_keyssl_verify_certssl_verify_identitytls_versionstls_cipher_suitesc                    d}| j                   st        d      t        t        d      	 |r|j	                  d       |d   }t
        |   }	t        j                  |	      }
|dk(  rd	|vr#|
xj                  t        j                  z  c_        d
|vr#|
xj                  t        j                  z  c_        d|vr8|
xj                  t        j                  z  c_        nt        j                         }
||
_        |rt        j                  |
_        n-|rt        j                  |
_        nt        j                   |
_        |
j#                          |r	 |
j%                  |       |r	 |
j+                  ||       |r%|d	k(  r |
j-                  dj/                  |             |
S # t&        t        j(                  f$ r}t        d|       |d}~ww xY w# t&        t        j(                  f$ r}t        d|       |d}~ww xY w# t0        $ r}t3        d      |d}~wt&        t4        t        j6                  t        j(                  f$ r}t        t9        |            |d}~ww xY w)zBuild a SSLContext.Ni   rh   z&Python installation has no SSL supportT)reverser   r   r   r   r   zInvalid CA Certificate: zInvalid Certificate/Key: :)r   r   sslRuntimeErrorsortTLS_VERSIONS
SSLContextoptionsOP_NO_TLSv1_2OP_NO_TLSv1_1OP_NO_TLSv1create_default_contextcheck_hostnameCERT_REQUIREDverify_modeCERT_OPTIONAL	CERT_NONEload_default_certsload_verify_locationsrj   SSLErrorload_cert_chainset_ciphersr   	NameErrorr   NotImplementedErrorCertificateErrorr$   )r3   r   r   r   r   r   r   r   tls_versionssl_protocolcontextr   s               r&   build_ssl_contextzMySQLSocket.build_ssl_context  s%    &*|| t,,;GHH5	4!!$!/*1o+K8..6)+ 43+<+<< 43+<+<<l23??:446%8G"&)&7&7#$&)&7&7#&)mm#&&(T11&9 U++Hg> ![I%=##CHH->$?@N  . T(+CC5)IJPSST
  . U(+DSE)JKQTTU  	W#$LMSVV  LL	
 	4 !S*3	4sl   D$H F! #H &G 8(H !G:G		GH G>*G99G>>H 	I 
H0I II r   r;   r<   )r=   r>   r?   r@   rO   propertyr$   r,   r
   r   r   r   boolr   rB   r   r   r   r   r   rA   r   r4   rE   r:   r   r   r2   r(   r&   r   r     s   	)    c d  #"d "+c +d +42s~~ 2$ 28 (,26'+

  }
 #+3-	

  }
 

"Tx} T	 T !%"&!%*/.3,.13H4H4 3-H4 #	H4
 "$H4 &d^H4 tCy)H4 $DI.H4 
H4r(   r   c                   D     e Zd ZdZ	 d
dededef fdZdeddfd	Z	 xZ
S )r   zMySQL socket class using TCP/IP.

    Args:
        host: MySQL host name.
        port: MySQL port.
        force_ipv6: Force IPv6 usage.
    hostport
force_ipv6c                 d    t         |           || _        || _        || _        | d| | _        y )Nr   )r   rO   _host_port_force_ipv6r   )r3   r   r   r   r   s       r&   rO   zMySQLTcpSocket.__init__  s8     	

!+ $vQtf-r(   r   r    Nc                    K   t        d| j                  | j                  d| d{   \  | _        | _        d| _        y7 w)zOpen TCP/IP connection.)r   r   NTr2   )r   r   r   r   r   r   r   s     r&   r   zMySQLTcpSocket.open_connection  sG     +: ,
$**,
06,
 &
"dl "&
s   &AAA)z	127.0.0.1i  F)r=   r>   r?   r@   r$   rB   r   rO   r   r   r   r   s   @r&   r   r     s?     MR..-0.EI."c "d "r(   c                   :     e Zd ZdZddef fdZdeddfdZ xZS )	r   zbMySQL socket class using UNIX sockets.

    Args:
        unix_socket: UNIX socket file path.
    unix_socketc                 0    t         |           || _        y r<   )r   rO   r   )r3   r   r   s     r&   rO   zMySQLUnixSocket.__init__  s    (r(   r   r    Nc                    K   t        j                  dd| j                  i| d{   \  | _        | _        d| _        y7 w)zOpen UNIX socket connection.pathNTr2   )rC   open_unix_connectionr   r   r   r   r   s     r&   r   zMySQLUnixSocket.open_connection  sM     
 .. 

"(
 
	
LL "
s   %AAA)z/tmp/mysql.sock)	r=   r>   r?   r@   r$   rO   r   r   r   r   s   @r&   r   r     s'    )C )"c "d "r(   )/r@   __all__rC   rY   r   r   PROTOCOL_TLSv1PROTOCOL_TLSv1_1PROTOCOL_TLSv1_2PROTOCOL_TLSr   ImportErrorabcr	   r
   collectionsr   typingr   r   r   r   r   errorsr   r   r   r   r   r   networkr   r   r   r   utilsr   r   rj   r$   r'   r*   rG   r   r   r   r   r2   r(   r&   <module>r     s   > M .
/   
 ##''''##	L $  4 4   1HW H HQC QhB BJl0 l^p4# p4f"[ "4"k "k  
Cs   3C CC