@@ -81,13 +81,15 @@ class CurlInfo(TypedDict):
8181 namelookup_time : int
8282 has_used_proxy : int
8383
84+
85+ def get_curl_info (response : requests .Response ) -> CurlInfo :
86+ return response .curl_info
87+
8488class Response (requests .Response ):
8589 curl_info : CurlInfo
8690 wait_for_body : typing .Callable [[], None ]
8791
8892class BaseCurlAdapter (BaseAdapter ):
89-
90- _local = threading .local ()
9193
9294 def __init__ (self ,
9395 curl_class : typing .Union [curl_cffi .Curl , pycurl .Curl ],
@@ -96,7 +98,6 @@ def __init__(self,
9698 use_thread_local_curl = True ,
9799 stream_handler : CurlStreamHandlerBase = None
98100 ):
99-
100101 self .curl_class : typing .Union [curl_cffi .Curl , pycurl .Curl ] = curl_class
101102 self .debug = debug
102103 self .use_curl_content_decoding = use_curl_content_decoding
@@ -106,6 +107,14 @@ def __init__(self,
106107 self .stream_handler = (stream_handler or CurlStreamHandler )
107108
108109 if self .use_thread_local_curl :
110+ self ._local = threading .local ()
111+ try :
112+ from .stream .handler ._thread_env import _THREAD_ENV
113+ if _THREAD_ENV == "gevent" :
114+ from gevent .local import local as gevent_local
115+ self ._local = gevent_local ()
116+ except Exception :
117+ pass
109118 self ._local .curl = self .curl_class ()
110119 else :
111120 self ._curl = self .curl_class ()
@@ -121,16 +130,20 @@ def curl(self) -> typing.Union[curl_cffi.Curl, pycurl.Curl]:
121130 return self ._local .curl
122131 return self ._curl
123132
124- def reset_curl (self ):
133+ def reset_curl (self , curl : typing . Union [ curl_cffi . Curl , pycurl . Curl , None ] = None ):
125134 '''
126135 Close current handle and open a new curl handle
127136 '''
128- self .curl .close ()
137+ if curl is None :
138+ curl = self .curl
139+ curl .close ()
129140
130141 if self .use_thread_local_curl :
131142 self ._local .curl = self .curl_class ()
143+ return self ._local .curl
132144 else :
133145 self ._curl = self .curl_class ()
146+ return self ._curl
134147
135148 def enable_debug (self ):
136149 if self .debug :
@@ -566,14 +579,14 @@ def set_curl_options(self,
566579 def send (
567580 self , request : requests .PreparedRequest , stream = False , timeout = None , verify = True , cert = None , proxies = None
568581 ):
569- self .reset_curl ()
582+ curl = self .reset_curl ()
570583
571- self .cert_verify (self . curl , request .url , verify , cert )
584+ self .cert_verify (curl , request .url , verify , cert )
572585
573586 url = self .request_url (request , proxies )
574587
575588 self .set_curl_options (
576- self . curl ,
589+ curl ,
577590 request = request ,
578591 url = url ,
579592 timeout = timeout ,
@@ -583,29 +596,29 @@ def send(
583596 try :
584597 # Save headers when received
585598 header_buffer = BytesIO ()
586- self . curl .setopt (CurlOpt .HEADERDATA , header_buffer )
599+ curl .setopt (CurlOpt .HEADERDATA , header_buffer )
587600
588601 # Callbacks for retrieving & saving curl info object
589602 curl_info_dict : CurlInfo = {}
590603
591604 # Perform curl request with threading, and return body in a 'read' like class type (by simply using Curl.WRITEFUNCTION callback)
592605 start_curl_stream = (
593606 self .stream_handler (
594- curl_instance = self . curl ,
607+ curl_instance = curl ,
595608 callback_after_perform = lambda curl : curl_info_dict .update (self .parse_info (curl )),
596609 timeout = timeout ,
597610 debug = self .debug
598611 )
599612 ).start ()
600613
601614 # Headers are already available
602- curl_info_dict .update (self .parse_info (self . curl , headers_only = True ))
615+ curl_info_dict .update (self .parse_info (curl , headers_only = True ))
603616
604617 if self .debug :
605618 print ("[DEBUG] Curl Start Elapsed Time: " , time .time () - a )
606619
607620 # Headers are available after start, parse them
608- parsed_headers = self .parse_headers (self . curl , header_buffer )
621+ parsed_headers = self .parse_headers (curl , header_buffer )
609622
610623 curl_stream_res = CurlStreamResponse (
611624 url = url ,
@@ -616,7 +629,7 @@ def send(
616629 ** parsed_headers
617630 )
618631
619- return self .build_response (self . curl , curl_stream_res , parsed_headers , request , wait_for_body = start_curl_stream ._wait_for_body , curl_info_dict = curl_info_dict )
632+ return self .build_response (curl , curl_stream_res , parsed_headers , request , wait_for_body = start_curl_stream ._wait_for_body , curl_info_dict = curl_info_dict )
620633 except OSError as e :
621634 raise ConnectionError (e , request = request )
622635
0 commit comments