Format: application/x-python-code
⬇ Download File (12.4 KB)
   1  o
   2  
   3  7'h;B�@s�ddlmZddlZddlZddlZddlZddlmZddlm	Z	m
   4  Z
   5  ddlmZm
   6  Z
   7  ddlmZmZmZmZddlmZmZmZd	d
   8  lmZd	dlmZmZGdd
   9  �d
  10  �ZGdd�de�ZGdd�d�ZdS)�)�annotationsN�)�SyncBackend)�
  11  SOCKET_OPTION�NetworkBackend)�ConnectionNotAvailable�UnsupportedProtocol)�Origin�Proxy�Request�Response)�Event�ShieldCancellation�
  12  ThreadLock�)�HTTPConnection)�ConnectionInterface�RequestInterfacec@sBeZdZddd�Zdd	d
  13  �Zddd�Z	
  14  dddd�Zddd�Zd
  15  S)�PoolRequest�requestr�return�NonecCs||_d|_t�|_dS�N)r�
  16  connectionr
  17  �_connection_acquired)�selfr�r�TC:\Users\shubh\Videos\agent\nova\Lib\site-packages\httpcore/_sync/connection_pool.py�__init__szPoolRequest.__init__r�ConnectionInterface | NonecCs||_|j��dSr)rr�set)rrrrr�assign_to_connectionsz PoolRequest.assign_to_connectioncCsd|_t�|_dSr)rr
  18  r�rrrr�clear_connectionszPoolRequest.clear_connectionN�timeout�float | NonercCs,|jdur|jj|d�|jdusJ�|jS)N�r$)rr�wait)rr$rrr�wait_for_connections
  19  zPoolRequest.wait_for_connection�boolcCs
  20  |jduSr)rr"rrr�	is_queued's
  21  zPoolRequest.is_queued)rrrr)rrrr�rrr)r$r%rr)rr))�__name__�
  22  __module__�__qualname__rr!r#r(r*rrrrrs
  23  
  24  
  25  �rc@s�eZdZdZ												dAdBdd�ZdCd#d$�ZedDd&d'��ZdEd+d,�ZdDd-d.�Z	dFd0d1�Z
  26  dGd2d3�ZdHd4d5�Z			dIdJd<d=�Z
  27  dKd?d@�ZdS)L�ConnectionPoolz5
  28      A connection pool for making HTTP requests.
  29      N�
  30  TFr�ssl_context�ssl.SSLContext | None�proxy�Proxy | None�max_connections�
  31  int | None�max_keepalive_connections�keepalive_expiryr%�http1r)�http2�retries�int�
  32  local_address�
  33  str | None�uds�network_backend�NetworkBackend | None�socket_options�%typing.Iterable[SOCKET_OPTION] | Nonerrc
  34  
  35  Cs�||_||_|dur
  36  tjn||_|durtjn||_t|j|j�|_||_||_||_	||_
  37  |	|_|
  38  |_|dur;t
  39  �n||_||_g|_g|_t�|_dS)a
  40          A connection pool for making HTTP requests.
  41  
  42          Parameters:
  43              ssl_context: An SSL context to use for verifying connections.
  44                  If not specified, the default `httpcore.default_ssl_context()`
  45                  will be used.
  46              max_connections: The maximum number of concurrent HTTP connections that
  47                  the pool should allow. Any attempt to send a request on a pool that
  48                  would exceed this amount will block until a connection is available.
  49              max_keepalive_connections: The maximum number of idle HTTP connections
  50                  that will be maintained in the pool.
  51              keepalive_expiry: The duration in seconds that an idle HTTP connection
  52                  may be maintained for before being expired from the pool.
  53              http1: A boolean indicating if HTTP/1.1 requests should be supported
  54                  by the connection pool. Defaults to True.
  55              http2: A boolean indicating if HTTP/2 requests should be supported by
  56                  the connection pool. Defaults to False.
  57              retries: The maximum number of retries when trying to establish a
  58                  connection.
  59              local_address: Local address to connect from. Can also be used to connect
  60                  using a particular address family. Using `local_address="0.0.0.0"`
  61                  will connect using an `AF_INET` address (IPv4), while using
  62                  `local_address="::"` will connect using an `AF_INET6` address (IPv6).
  63              uds: Path to a Unix Domain Socket to use instead of TCP sockets.
  64              network_backend: A backend instance to use for handling network I/O.
  65              socket_options: Socket options that have to be included
  66               in the TCP socket when the connection was established.
  67          N)�_ssl_context�_proxy�sys�maxsize�_max_connections�_max_keepalive_connections�min�_keepalive_expiry�_http1�_http2�_retries�_local_address�_udsr�_network_backend�_socket_options�_connections�	_requestsr�_optional_thread_lock)
  68  rr1r3r5r7r8r9r:r;r=r?r@rBrrrr0s.,�����zConnectionPool.__init__�originr	rcCs�|jdure|jjjdvr(ddlm}||jjj|jj||j|j|j	|j
  69  |jd�S|jdkrFddlm
  70  }||jjj|jj|jj||j|jd�Sddlm}||jjj|jj|jj||j|j|j	|j
  71  |jd	�	St||j|j|j	|j
  72  |j|j|j|j|jd
  73  �
  74  S)N)ssocks5ssocks5hr)�Socks5Connection)�proxy_origin�
  75  proxy_auth�
  76  remote_originr1r8r9r:r@shttp)�ForwardHTTPConnection)rX�
  77  proxy_headers�proxy_ssl_contextrZr8r@)�TunnelHTTPConnection)	rXr\r]rZr1r8r9r:r@)
  78  rVr1r8r9r:r;r=r?r@rB)rE�url�schemeZsocks_proxyrWrV�authrDrKrLrMrQZ
  79  http_proxyr[�headersr1r^rrNrOrPrR)rrVrWr[r^rrr�create_connection�s^
  80  �
  81  
  82  ���z ConnectionPool.create_connection�list[ConnectionInterface]cCs
  83  t|j�S)a�
  84          Return a list of the connections currently in the pool.
  85  
  86          For example:
  87  
  88          ```python
  89          >>> pool.connections
  90          [
  91              <HTTPConnection ['https://example.com:443', HTTP/1.1, ACTIVE, Request Count: 6]>,
  92              <HTTPConnection ['https://example.com:443', HTTP/1.1, IDLE, Request Count: 9]> ,
  93              <HTTPConnection ['http://example.com:80', HTTP/1.1, IDLE, Request Count: 1]>,
  94          ]
  95          ```
  96          )�listrSr"rrr�connections�s
  97  zConnectionPool.connectionsrrrc
  98  Cs�|jj��}|dkrtd��|dvrtd|�d���|j�di�}|�dd�}|j�t|�}|j�	|�Wd�n1s?wYz=	|j�|�
  99  �}Wd�n1sXwY|�|�|j|d
 100  �}z|�
 101  |j�}Wn
 102  ty}|��YnwnqFWn1ty�}	z%|j�|j�|�|�
 103  �}Wd�n1s�wY|�|�|	d�d}	~	wwt|jtj�s�J�t|j|jt|j||d�|jd�S)
 104  z�
 105          Send an HTTP request, and return an HTTP response.
 106  
 107          This is the core implementation that is called into by `.request()` or `.stream()`.
 108          �z;Request URL is missing an 'http://' or 'https://' protocol.)�http�https�ws�wssz)Request URL has an unsupported protocol 'z://'.r$�poolNTr&)�stream�pool_requestrl)�statusrb�content�
 109  extensions)r_r`�decoderrq�getrUrrT�append�_assign_requests_to_connections�_close_connectionsr(�handle_requestrrr#�
 110  BaseException�remove�
 111  isinstancerm�typing�Iterablerrorb�PoolByteStream)
 112  rrr`Ztimeoutsr$rn�closingr�response�excrrrrw�sd�
 113  ��
 114  �
 115  ���
 116  �
 117  ����zConnectionPool.handle_requestcsZg}t|j�D]9}|��r|j�|�q|��r$|j�|�|�|�q|��r@tdd�|jD��|jkr@|j�|�|�|�qdd�|j	D�}|D]_}|j
 118  jj��fdd�|jD�}dd�|jD�}|rp|d}|�
 119  |�qKt|j�|jkr�|���}|j�|�|�
 120  |�qK|r�|d}|j�|�|�|�|���}|j�|�|�
 121  |�qK|S)aE
 122          Manage the state of the connection pool, assigning incoming
 123          requests to connections as available.
 124  
 125          Called whenever a new request is added or removed from the pool.
 126  
 127          Any closing connections are returned, allowing the I/O for closing
 128          those connections to be handled seperately.
 129          cS�g|]}|���qSr��is_idle��.0rrrr�
 130  <listcomp>&�zBConnectionPool._assign_requests_to_connections.<locals>.<listcomp>cS�g|]}|��r|�qSr�r*�r�rrrrr�.scs"g|]
 131  }|���r|��r|�qSr)Zcan_handle_requestZis_availabler��rVrrr�1s���cSr�rr�r�rrrr�6s
 132  ��r)rerS�	is_closedryZhas_expiredrtr��lenrIrTrr_rVr!rHrc)r�closing_connectionsrZqueued_requestsrnZavailable_connectionsZidle_connectionsrr�rrusP
 133  ��
 134  �
 135  
 136  ��
 137  
 138  
 139  
 140  
 141  �z.ConnectionPool._assign_requests_to_connectionsr~cCs>t��|D]}|��qWd�dS1swYdSr)r�close)rr~rrrrrvUs
 142  
 143  �"�z!ConnectionPool._close_connectionscCsD|j�t|j�}g|_Wd�n1swY|�|�dSr)rUrerSrv)rr�rrrr�[s
 144  
 145  �zConnectionPool.closecCs|Srrr"rrr�	__enter__cszConnectionPool.__enter__�exc_type�type[BaseException] | None�	exc_value�BaseException | None�	traceback�types.TracebackType | NonecCs|��dSr)r�)rr�r�r�rrr�__exit__fszConnectionPool.__exit__�strc
 146  Cs�|jj}|j�,dd�|jD�}dd�|jD�}|�d�}|�d�}|�d�}|�d�}Wd�n1s6wYd|�d|�d�}d	|�d|�d
 147  �}	d|�d|�d
 148  |	�d�S)NcSr�rr�r�rrrr�qr�z+ConnectionPool.__repr__.<locals>.<listcomp>cSr�rr�r�rrrr�rs�FTz
 149  Requests: z	 active, z queuedz
 150  Connections: z idle�<z [z | z]>)�	__class__r,rUrTrS�count)
 151  r�
 152  class_nameZrequest_is_queuedZconnection_is_idleZnum_active_requestsZnum_queued_requestsZnum_active_connectionsZnum_idle_connectionsZ
 153  requests_infoZconnection_inforrr�__repr__ns �
 154  
 155  
 156  ���zConnectionPool.__repr__)NNr0NNTFrNNNN)r1r2r3r4r5r6r7r6r8r%r9r)r:r)r;r<r=r>r?r>r@rArBrCrr)rVr	rr)rrd)rrrr)r~rdrrr+)rr/)NNN)r�r�r�r�r�r�rr)rr�)r,r-r.�__doc__rrc�propertyrfrwrurvr�r�r�r�rrrrr/+s8�
 157  P5
 158  
 159  G
 160  G
 161  
 162  �r/c@s*eZdZdd	d
 163  �Zddd
 164  �Zddd�ZdS)r}rm�typing.Iterable[bytes]rnrrlr/rrcCs||_||_||_d|_dS)NF)�_stream�
 165  _pool_request�_pool�_closed)rrmrnrlrrrr�s
 166  zPoolByteStream.__init__�typing.Iterator[bytes]c
 167  csB�z|jD]}|VqWdSty }z|��|d�d}~wwr)r�rxr�)r�partr�rrr�__iter__�s�
 168  ���zPoolByteStream.__iter__cCs�|jsMd|_t��t|jd�r|j��Wd�n1swY|jj�|jj�|j	�|j�
 169  �}Wd�n1s@wY|j�|�dSdS)NTr�)r�r�hasattrr�r�r�rUrTryr�rurv)rr~rrrr��s
 170  ��
 171  ��zPoolByteStream.closeN)rmr�rnrrlr/rr)rr�r+)r,r-r.rr�r�rrrrr}�s
 172  
 173  r})�
 174  __future__r�sslrF�typesr{Z_backends.syncrZ_backends.baserr�_exceptionsrrZ_modelsr	r
 175  rrZ_synchronizationr
 176  rrrrZ
 177  interfacesrrrr/r}rrrr�<module>s"\