16
16
chardet = None
17
17
18
18
import aiohttp
19
- from . import helpers , streams
19
+ from . import hdrs , helpers , streams
20
20
from .log import client_logger
21
21
from .streams import EOF_MARKER , FlowControlStreamReader
22
- from .multidict import (CIMultiDictProxy , MultiDictProxy ,
23
- MultiDict , upstr )
22
+ from .multidict import CIMultiDictProxy , MultiDictProxy , MultiDict
24
23
25
24
HTTP_PORT = 80
26
25
HTTPS_PORT = 443
@@ -131,7 +130,8 @@ def request(method, url, *,
131
130
data = None
132
131
cookies = resp .cookies
133
132
134
- r_url = resp .headers .get ('LOCATION' ) or resp .headers .get ('URI' )
133
+ r_url = (resp .headers .get (hdrs .LOCATION ) or
134
+ resp .headers .get (hdrs .URI ))
135
135
136
136
scheme = urllib .parse .urlsplit (r_url )[0 ]
137
137
if scheme not in ('http' , 'https' , '' ):
@@ -156,20 +156,9 @@ class ClientRequest:
156
156
POST_METHODS = {'PATCH' , 'POST' , 'PUT' , 'TRACE' , 'DELETE' }
157
157
ALL_METHODS = GET_METHODS .union (POST_METHODS )
158
158
159
- ACCEPT_ID = upstr ('ACCEPT' )
160
- ACCEPT_ENCODING_ID = upstr ('ACCEPT-ENCODING' )
161
- AUTHORIZATION_ID = upstr ('AUTHORIZATION' )
162
- CONTENT_ENCODING_ID = upstr ('CONTENT-ENCODING' )
163
- CONTENT_LENGTH_ID = upstr ('CONTENT-LENGTH' )
164
- CONTENT_TYPE_ID = upstr ('CONTENT-TYPE' )
165
- COOKIE_ID = upstr ('COOKIE' )
166
- EXPECT_ID = upstr ('EXPECT' )
167
- HOST_ID = upstr ('HOST' )
168
- TRANSFER_ENCODING_ID = upstr ('TRANSFER-ENCODING' )
169
-
170
159
DEFAULT_HEADERS = {
171
- ACCEPT_ID : '*/*' ,
172
- ACCEPT_ENCODING_ID : 'gzip, deflate' ,
160
+ hdrs . ACCEPT : '*/*' ,
161
+ hdrs . ACCEPT_ENCODING : 'gzip, deflate' ,
173
162
}
174
163
175
164
body = b''
@@ -317,19 +306,18 @@ def update_headers(self, headers):
317
306
self .headers [hdr ] = val
318
307
319
308
# add host
320
- if self . HOST_ID not in self .headers :
321
- self .headers [self . HOST_ID ] = self .netloc
309
+ if hdrs . HOST not in self .headers :
310
+ self .headers [hdrs . HOST ] = self .netloc
322
311
323
312
def update_cookies (self , cookies ):
324
313
"""Update request cookies header."""
325
314
if not cookies :
326
315
return
327
316
328
- COOKIE_ID = self .COOKIE_ID
329
317
c = http .cookies .SimpleCookie ()
330
- if COOKIE_ID in self .headers :
331
- c .load (self .headers .get (COOKIE_ID , '' ))
332
- del self .headers [COOKIE_ID ]
318
+ if hdrs . COOKIE in self .headers :
319
+ c .load (self .headers .get (hdrs . COOKIE , '' ))
320
+ del self .headers [hdrs . COOKIE ]
333
321
334
322
if isinstance (cookies , dict ):
335
323
cookies = cookies .items ()
@@ -341,19 +329,18 @@ def update_cookies(self, cookies):
341
329
else :
342
330
c [name ] = value
343
331
344
- self .headers [COOKIE_ID ] = c .output (header = '' , sep = ';' ).strip ()
332
+ self .headers [hdrs . COOKIE ] = c .output (header = '' , sep = ';' ).strip ()
345
333
346
334
def update_content_encoding (self ):
347
335
"""Set request content encoding."""
348
- CONTENT_ENCODING_ID = self .CONTENT_ENCODING_ID
349
- enc = self .headers .get (CONTENT_ENCODING_ID , '' ).lower ()
336
+ enc = self .headers .get (hdrs .CONTENT_ENCODING , '' ).lower ()
350
337
if enc :
351
338
self .compress = enc
352
339
self .chunked = True # enable chunked, no need to deal with length
353
340
elif self .compress :
354
341
if not isinstance (self .compress , str ):
355
342
self .compress = 'deflate'
356
- self .headers [CONTENT_ENCODING_ID ] = self .compress
343
+ self .headers [hdrs . CONTENT_ENCODING ] = self .compress
357
344
self .chunked = True # enable chunked, no need to deal with length
358
345
359
346
def update_auth (self , auth ):
@@ -368,30 +355,29 @@ def update_auth(self, auth):
368
355
'BasicAuth() tuple is required instead ' , DeprecationWarning )
369
356
auth = helpers .BasicAuth (* auth )
370
357
371
- self .headers [self . AUTHORIZATION_ID ] = auth .encode ()
358
+ self .headers [hdrs . AUTHORIZATION ] = auth .encode ()
372
359
373
360
def update_body_from_data (self , data ):
374
361
if not data :
375
362
return
376
- CONTENT_LENGTH_ID = self .CONTENT_LENGTH_ID
377
- CONTENT_TYPE_ID = self .CONTENT_TYPE_ID
378
363
379
364
if isinstance (data , str ):
380
365
data = data .encode (self .encoding )
381
366
382
367
if isinstance (data , (bytes , bytearray )):
383
368
self .body = data
384
- if CONTENT_TYPE_ID not in self .headers :
385
- self .headers [CONTENT_TYPE_ID ] = 'application/octet-stream'
386
- if CONTENT_LENGTH_ID not in self .headers and not self .chunked :
387
- self .headers [CONTENT_LENGTH_ID ] = str (len (self .body ))
369
+ if hdrs . CONTENT_TYPE not in self .headers :
370
+ self .headers [hdrs . CONTENT_TYPE ] = 'application/octet-stream'
371
+ if hdrs . CONTENT_LENGTH not in self .headers and not self .chunked :
372
+ self .headers [hdrs . CONTENT_LENGTH ] = str (len (self .body ))
388
373
389
374
elif isinstance (data , (asyncio .StreamReader , streams .DataQueue )):
390
375
self .body = data
391
376
392
377
elif asyncio .iscoroutine (data ):
393
378
self .body = data
394
- if CONTENT_LENGTH_ID not in self .headers and self .chunked is None :
379
+ if (hdrs .CONTENT_LENGTH not in self .headers and
380
+ self .chunked is None ):
395
381
self .chunked = True
396
382
397
383
elif isinstance (data , io .IOBase ):
@@ -403,51 +389,51 @@ def update_body_from_data(self, data):
403
389
if data .mode == 'r' :
404
390
raise ValueError ('file {!r} should be open in binary mode'
405
391
'' .format (data ))
406
- if CONTENT_TYPE_ID not in self .headers and hasattr (data , 'name' ):
392
+ if (hdrs .CONTENT_TYPE not in self .headers and
393
+ hasattr (data , 'name' )):
407
394
mime = mimetypes .guess_type (data .name )[0 ]
408
395
mime = 'application/octet-stream' if mime is None else mime
409
- self .headers [CONTENT_TYPE_ID ] = mime
396
+ self .headers [hdrs . CONTENT_TYPE ] = mime
410
397
411
398
else :
412
399
if not isinstance (data , helpers .FormData ):
413
400
data = helpers .FormData (data )
414
401
415
402
self .body = data (self .encoding )
416
403
417
- if CONTENT_TYPE_ID not in self .headers :
418
- self .headers [CONTENT_TYPE_ID ] = data .content_type
404
+ if hdrs . CONTENT_TYPE not in self .headers :
405
+ self .headers [hdrs . CONTENT_TYPE ] = data .content_type
419
406
420
407
if data .is_multipart :
421
408
self .chunked = self .chunked or 8192
422
409
else :
423
- if CONTENT_LENGTH_ID not in self .headers and not self .chunked :
424
- self .headers [CONTENT_LENGTH_ID ] = str (len (self .body ))
410
+ if (hdrs .CONTENT_LENGTH not in self .headers and
411
+ not self .chunked ):
412
+ self .headers [hdrs .CONTENT_LENGTH ] = str (len (self .body ))
425
413
426
414
def update_transfer_encoding (self ):
427
415
"""Analyze transfer-encoding header."""
428
- CONTENT_LENGTH_ID = self .CONTENT_LENGTH_ID
429
- TRANSFER_ENCODING_ID = self .TRANSFER_ENCODING_ID
430
- te = self .headers .get (TRANSFER_ENCODING_ID , '' ).lower ()
416
+ te = self .headers .get (hdrs .TRANSFER_ENCODING , '' ).lower ()
431
417
432
418
if self .chunked :
433
- if CONTENT_LENGTH_ID in self .headers :
434
- del self .headers [CONTENT_LENGTH_ID ]
419
+ if hdrs . CONTENT_LENGTH in self .headers :
420
+ del self .headers [hdrs . CONTENT_LENGTH ]
435
421
if 'chunked' not in te :
436
- self .headers [TRANSFER_ENCODING_ID ] = 'chunked'
422
+ self .headers [hdrs . TRANSFER_ENCODING ] = 'chunked'
437
423
438
424
self .chunked = self .chunked if type (self .chunked ) is int else 8192
439
425
else :
440
426
if 'chunked' in te :
441
427
self .chunked = 8192
442
428
else :
443
429
self .chunked = None
444
- if CONTENT_LENGTH_ID not in self .headers :
445
- self .headers [CONTENT_LENGTH_ID ] = str (len (self .body ))
430
+ if hdrs . CONTENT_LENGTH not in self .headers :
431
+ self .headers [hdrs . CONTENT_LENGTH ] = str (len (self .body ))
446
432
447
433
def update_expect_continue (self , expect = False ):
448
434
if expect :
449
- self .headers [self . EXPECT_ID ] = '100-continue'
450
- elif self .headers .get (self . EXPECT_ID , '' ).lower () == '100-continue' :
435
+ self .headers [hdrs . EXPECT ] = '100-continue'
436
+ elif self .headers .get (hdrs . EXPECT , '' ).lower () == '100-continue' :
451
437
expect = True
452
438
453
439
if expect :
@@ -580,9 +566,6 @@ def close(self):
580
566
581
567
class ClientResponse :
582
568
583
- CONTENT_TYPE_ID = upstr ('CONTENT-TYPE' )
584
- SET_COOKIE_ID = upstr ('SET-COOKIE' )
585
-
586
569
message = None # RawResponseMessage object
587
570
588
571
# from the Status-Line of the response
@@ -675,8 +658,8 @@ def start(self, connection, read_until_eof=False):
675
658
676
659
# cookies
677
660
self .cookies = http .cookies .SimpleCookie ()
678
- if self . SET_COOKIE_ID in self .headers :
679
- for hdr in self .headers .getall (self . SET_COOKIE_ID ):
661
+ if hdrs . SET_COOKIE in self .headers :
662
+ for hdr in self .headers .getall (hdrs . SET_COOKIE ):
680
663
try :
681
664
self .cookies .load (hdr )
682
665
except http .cookies .CookieError as exc :
@@ -751,7 +734,7 @@ def read_and_close(self, decode=False):
751
734
return (yield from self .read (decode ))
752
735
753
736
def _get_encoding (self , encoding ):
754
- ctype = self .headers .get (self . CONTENT_TYPE_ID , '' ).lower ()
737
+ ctype = self .headers .get (hdrs . CONTENT_TYPE , '' ).lower ()
755
738
mtype , stype , _ , params = helpers .parse_mimetype (ctype )
756
739
757
740
if not encoding :
@@ -777,7 +760,7 @@ def json(self, *, encoding=None, loads=json.loads):
777
760
if self ._content is None :
778
761
yield from self .read ()
779
762
780
- ctype = self .headers .get (self . CONTENT_TYPE_ID , '' ).lower ()
763
+ ctype = self .headers .get (hdrs . CONTENT_TYPE , '' ).lower ()
781
764
if 'json' not in ctype :
782
765
client_logger .warning (
783
766
'Attempt to decode JSON with unexpected mimetype: %s' , ctype )
0 commit comments