11import logging
2+ from ssl import create_default_context
23
34import aiohttp
45
@@ -64,30 +65,23 @@ def prepare_auth(auth, username, password):
6465
6566
6667def prepare_verify (verify , verify_fingerprint ):
67- if isinstance (verify , ( str , bytes ) ):
68- verify = expand_path (verify )
69- elif not isinstance ( verify , bool ) :
68+ if isinstance (verify , str ):
69+ return create_default_context ( cafile = expand_path (verify ) )
70+ elif verify is not None :
7071 raise exceptions .UserError (
71- "Invalid value for verify ({}), "
72- "must be a path to a PEM-file or boolean." .format (verify )
72+ f"Invalid value for verify ({ verify } ), must be a path to a PEM-file."
7373 )
7474
7575 if verify_fingerprint is not None :
76- if not isinstance (verify_fingerprint , ( bytes , str ) ):
76+ if not isinstance (verify_fingerprint , str ):
7777 raise exceptions .UserError (
7878 "Invalid value for verify_fingerprint "
79- "({}), must be a string or null." . format ( verify_fingerprint )
79+ f "({ verify_fingerprint } ), must be a string."
8080 )
81- elif not verify :
82- raise exceptions .UserError (
83- "Disabling all SSL validation is forbidden. Consider setting "
84- "verify_fingerprint if you have a broken or self-signed cert."
85- )
8681
87- return {
88- "verify" : verify ,
89- "verify_fingerprint" : verify_fingerprint ,
90- }
82+ return aiohttp .Fingerprint (bytes .fromhex (verify_fingerprint .replace (":" , "" )))
83+
84+ return None
9185
9286
9387def prepare_client_cert (cert ):
@@ -99,15 +93,18 @@ def prepare_client_cert(cert):
9993
10094
10195async def request (
102- method , url , session , latin1_fallback = True , verify_fingerprint = None , ** kwargs
96+ method ,
97+ url ,
98+ session ,
99+ latin1_fallback = True ,
100+ ** kwargs ,
103101):
104- """
105- Wrapper method for requests, to ease logging and mocking. Parameters should
106- be the same as for ``requests .request``, except :
102+ """Wrapper method for requests, to ease logging and mocking.
103+
104+ Parameters should be the same as for ``aiohttp .request``, as well as :
107105
108106 :param session: A requests session object to use.
109- :param verify_fingerprint: Optional. SHA1 or MD5 fingerprint of the
110- expected server certificate.
107+ :param verify_fingerprint: Optional. SHA256 of the expected server certificate.
111108 :param latin1_fallback: RFC-2616 specifies the default Content-Type of
112109 text/* to be latin1, which is not always correct, but exactly what
113110 requests is doing. Setting this parameter to False will use charset
@@ -116,13 +113,7 @@ async def request(
116113 https://github.com/kennethreitz/requests/issues/2042
117114 """
118115
119- if verify_fingerprint is not None :
120- ssl = aiohttp .Fingerprint (bytes .fromhex (verify_fingerprint .replace (":" , "" )))
121- kwargs .pop ("verify" , None )
122- elif kwargs .pop ("verify" , None ) is False :
123- ssl = False
124- else :
125- ssl = None # TODO XXX: Check all possible values for this
116+ # TODO: Support for client-side certifications.
126117
127118 session .hooks = {"response" : _fix_redirects }
128119
@@ -138,29 +129,29 @@ async def request(
138129
139130 kwargs .pop ("cert" , None ) # TODO XXX FIXME!
140131
141- r = await session .request (method , url , ssl = ssl , ** kwargs )
132+ response = await session .request (method , url , ** kwargs )
142133
143134 # See https://github.com/kennethreitz/requests/issues/2042
144- content_type = r .headers .get ("Content-Type" , "" )
135+ content_type = response .headers .get ("Content-Type" , "" )
145136 if (
146137 not latin1_fallback
147138 and "charset" not in content_type
148139 and content_type .startswith ("text/" )
149140 ):
150141 logger .debug ("Removing latin1 fallback" )
151- r .encoding = None
142+ response .encoding = None
152143
153- logger .debug (r .status )
154- logger .debug (r .headers )
155- logger .debug (r .content )
144+ logger .debug (response .status )
145+ logger .debug (response .headers )
146+ logger .debug (response .content )
156147
157- if r .status == 412 :
158- raise exceptions .PreconditionFailed (r .reason )
159- if r .status in (404 , 410 ):
160- raise exceptions .NotFoundError (r .reason )
148+ if response .status == 412 :
149+ raise exceptions .PreconditionFailed (response .reason )
150+ if response .status in (404 , 410 ):
151+ raise exceptions .NotFoundError (response .reason )
161152
162- r .raise_for_status ()
163- return r
153+ response .raise_for_status ()
154+ return response
164155
165156
166157def _fix_redirects (r , * args , ** kwargs ):
0 commit comments