Skip to content

Commit f55866e

Browse files
committed
Merge branch 'master' of github.com:KeepSafe/aiohttp
2 parents b9cbebc + fa10b7f commit f55866e

File tree

6 files changed

+128
-133
lines changed

6 files changed

+128
-133
lines changed

aiohttp/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
__version__ = '0.14.0a0'
44

55

6+
from . import hdrs # noqa
67
from .protocol import * # noqa
78
from .connector import * # noqa
89
from .client import * # noqa
@@ -21,4 +22,4 @@
2122
connector.__all__ +
2223
streams.__all__ +
2324
multidict.__all__ +
24-
['__version__'])
25+
['hdrs', '__version__'])

aiohttp/client.py

Lines changed: 41 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
chardet = None
1717

1818
import aiohttp
19-
from . import helpers, streams
19+
from . import hdrs, helpers, streams
2020
from .log import client_logger
2121
from .streams import EOF_MARKER, FlowControlStreamReader
22-
from .multidict import (CIMultiDictProxy, MultiDictProxy,
23-
MultiDict, upstr)
22+
from .multidict import CIMultiDictProxy, MultiDictProxy, MultiDict
2423

2524
HTTP_PORT = 80
2625
HTTPS_PORT = 443
@@ -131,7 +130,8 @@ def request(method, url, *,
131130
data = None
132131
cookies = resp.cookies
133132

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))
135135

136136
scheme = urllib.parse.urlsplit(r_url)[0]
137137
if scheme not in ('http', 'https', ''):
@@ -156,20 +156,9 @@ class ClientRequest:
156156
POST_METHODS = {'PATCH', 'POST', 'PUT', 'TRACE', 'DELETE'}
157157
ALL_METHODS = GET_METHODS.union(POST_METHODS)
158158

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-
170159
DEFAULT_HEADERS = {
171-
ACCEPT_ID: '*/*',
172-
ACCEPT_ENCODING_ID: 'gzip, deflate',
160+
hdrs.ACCEPT: '*/*',
161+
hdrs.ACCEPT_ENCODING: 'gzip, deflate',
173162
}
174163

175164
body = b''
@@ -317,19 +306,18 @@ def update_headers(self, headers):
317306
self.headers[hdr] = val
318307

319308
# 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
322311

323312
def update_cookies(self, cookies):
324313
"""Update request cookies header."""
325314
if not cookies:
326315
return
327316

328-
COOKIE_ID = self.COOKIE_ID
329317
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]
333321

334322
if isinstance(cookies, dict):
335323
cookies = cookies.items()
@@ -341,19 +329,18 @@ def update_cookies(self, cookies):
341329
else:
342330
c[name] = value
343331

344-
self.headers[COOKIE_ID] = c.output(header='', sep=';').strip()
332+
self.headers[hdrs.COOKIE] = c.output(header='', sep=';').strip()
345333

346334
def update_content_encoding(self):
347335
"""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()
350337
if enc:
351338
self.compress = enc
352339
self.chunked = True # enable chunked, no need to deal with length
353340
elif self.compress:
354341
if not isinstance(self.compress, str):
355342
self.compress = 'deflate'
356-
self.headers[CONTENT_ENCODING_ID] = self.compress
343+
self.headers[hdrs.CONTENT_ENCODING] = self.compress
357344
self.chunked = True # enable chunked, no need to deal with length
358345

359346
def update_auth(self, auth):
@@ -368,30 +355,29 @@ def update_auth(self, auth):
368355
'BasicAuth() tuple is required instead ', DeprecationWarning)
369356
auth = helpers.BasicAuth(*auth)
370357

371-
self.headers[self.AUTHORIZATION_ID] = auth.encode()
358+
self.headers[hdrs.AUTHORIZATION] = auth.encode()
372359

373360
def update_body_from_data(self, data):
374361
if not data:
375362
return
376-
CONTENT_LENGTH_ID = self.CONTENT_LENGTH_ID
377-
CONTENT_TYPE_ID = self.CONTENT_TYPE_ID
378363

379364
if isinstance(data, str):
380365
data = data.encode(self.encoding)
381366

382367
if isinstance(data, (bytes, bytearray)):
383368
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))
388373

389374
elif isinstance(data, (asyncio.StreamReader, streams.DataQueue)):
390375
self.body = data
391376

392377
elif asyncio.iscoroutine(data):
393378
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):
395381
self.chunked = True
396382

397383
elif isinstance(data, io.IOBase):
@@ -403,51 +389,51 @@ def update_body_from_data(self, data):
403389
if data.mode == 'r':
404390
raise ValueError('file {!r} should be open in binary mode'
405391
''.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')):
407394
mime = mimetypes.guess_type(data.name)[0]
408395
mime = 'application/octet-stream' if mime is None else mime
409-
self.headers[CONTENT_TYPE_ID] = mime
396+
self.headers[hdrs.CONTENT_TYPE] = mime
410397

411398
else:
412399
if not isinstance(data, helpers.FormData):
413400
data = helpers.FormData(data)
414401

415402
self.body = data(self.encoding)
416403

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
419406

420407
if data.is_multipart:
421408
self.chunked = self.chunked or 8192
422409
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))
425413

426414
def update_transfer_encoding(self):
427415
"""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()
431417

432418
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]
435421
if 'chunked' not in te:
436-
self.headers[TRANSFER_ENCODING_ID] = 'chunked'
422+
self.headers[hdrs.TRANSFER_ENCODING] = 'chunked'
437423

438424
self.chunked = self.chunked if type(self.chunked) is int else 8192
439425
else:
440426
if 'chunked' in te:
441427
self.chunked = 8192
442428
else:
443429
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))
446432

447433
def update_expect_continue(self, expect=False):
448434
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':
451437
expect = True
452438

453439
if expect:
@@ -580,9 +566,6 @@ def close(self):
580566

581567
class ClientResponse:
582568

583-
CONTENT_TYPE_ID = upstr('CONTENT-TYPE')
584-
SET_COOKIE_ID = upstr('SET-COOKIE')
585-
586569
message = None # RawResponseMessage object
587570

588571
# from the Status-Line of the response
@@ -675,8 +658,8 @@ def start(self, connection, read_until_eof=False):
675658

676659
# cookies
677660
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):
680663
try:
681664
self.cookies.load(hdr)
682665
except http.cookies.CookieError as exc:
@@ -751,7 +734,7 @@ def read_and_close(self, decode=False):
751734
return (yield from self.read(decode))
752735

753736
def _get_encoding(self, encoding):
754-
ctype = self.headers.get(self.CONTENT_TYPE_ID, '').lower()
737+
ctype = self.headers.get(hdrs.CONTENT_TYPE, '').lower()
755738
mtype, stype, _, params = helpers.parse_mimetype(ctype)
756739

757740
if not encoding:
@@ -777,7 +760,7 @@ def json(self, *, encoding=None, loads=json.loads):
777760
if self._content is None:
778761
yield from self.read()
779762

780-
ctype = self.headers.get(self.CONTENT_TYPE_ID, '').lower()
763+
ctype = self.headers.get(hdrs.CONTENT_TYPE, '').lower()
781764
if 'json' not in ctype:
782765
client_logger.warning(
783766
'Attempt to decode JSON with unexpected mimetype: %s', ctype)

aiohttp/hdrs.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""HTTP Headers constants."""
2+
from .multidict import upstr
3+
4+
5+
ACCEPT = upstr('ACCEPT')
6+
ACCEPT_ENCODING = upstr('ACCEPT-ENCODING')
7+
AUTHORIZATION = upstr('AUTHORIZATION')
8+
CONNECTION = upstr('CONNECTION')
9+
CONTENT_ENCODING = upstr('CONTENT-ENCODING')
10+
CONTENT_LENGTH = upstr('CONTENT-LENGTH')
11+
CONTENT_TYPE = upstr('CONTENT-TYPE')
12+
CONTENT_TRANSFER_ENCODING = upstr('CONTENT-TRANSFER-ENCODING')
13+
COOKIE = upstr('COOKIE')
14+
DATE = upstr('DATE')
15+
EXPECT = upstr('EXPECT')
16+
HOST = upstr('HOST')
17+
KEEP_ALIVE = upstr('KEEP-ALIVE')
18+
LOCATION = upstr('LOCATION')
19+
PROXY_AUTHENTICATE = upstr('PROXY_AUTHENTICATE')
20+
PROXY_AUTHORIZATION = upstr('PROXY-AUTHORIZATION')
21+
REFERER = upstr('REFERER')
22+
SET_COOKIE = upstr('SET-COOKIE')
23+
SEC_WEBSOCKET_KEY1 = upstr('SEC-WEBSOCKET-KEY1')
24+
SERVER = upstr('SERVER')
25+
TE = upstr('TE')
26+
TRAILERS = upstr('TRAILERS')
27+
TRANSFER_ENCODING = upstr('TRANSFER-ENCODING')
28+
USER_AGENT = upstr('USER-AGENT')
29+
URI = upstr('URI')
30+
UPGRADE = upstr('UPGRADE')

aiohttp/helpers.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from collections import namedtuple
1111
from wsgiref.handlers import format_date_time
1212

13-
from . import multidict
13+
from . import hdrs, multidict
1414

1515

1616
class BasicAuth(namedtuple('BasicAuth', ['login', 'password', 'encoding'])):
@@ -40,9 +40,6 @@ class FormData:
4040
"""Helper class for multipart/form-data and
4141
application/x-www-form-urlencoded body generation."""
4242

43-
CONTENT_TYPE_ID = multidict.upstr('Content-Type')
44-
CONTENT_TRANSFER_ENCODING_ID = multidict.upstr('Content-Transfer-Encoding')
45-
4643
def __init__(self, fields=()):
4744
self._fields = []
4845
self._is_multipart = False
@@ -80,11 +77,10 @@ def add_field(self, name, value, *, content_type=None, filename=None,
8077

8178
headers = {}
8279
if content_type is not None:
83-
headers[self.CONTENT_TYPE_ID] = content_type
80+
headers[hdrs.CONTENT_TYPE] = content_type
8481
self._is_multipart = True
8582
if content_transfer_encoding is not None:
86-
CTE = self.CONTENT_TRANSFER_ENCODING_ID
87-
headers[CTE] = content_transfer_encoding
83+
headers[hdrs.CONTENT_TRANSFER_ENCODING] = content_transfer_encoding
8884
self._is_multipart = True
8985
supported_tranfer_encoding = {
9086
'base64': binascii.b2a_base64,
@@ -123,7 +119,7 @@ def add_fields(self, *fields):
123119
def _gen_form_urlencoded(self, encoding):
124120
# form data (x-www-form-urlencoded)
125121
data = []
126-
for type_options, headers, value in self._fields:
122+
for type_options, _, value in self._fields:
127123
data.append((type_options['name'], value))
128124

129125
data = urllib.parse.urlencode(data, doseq=True)
@@ -275,8 +271,8 @@ def atoms(message, environ, response, transport, request_time):
275271
'r': r,
276272
's': str(getattr(response, 'status', '')),
277273
'b': str(getattr(response, 'output_length', '')),
278-
'f': headers.get('REFERER', '-'),
279-
'a': headers.get('USER-AGENT', '-'),
274+
'f': headers.get(hdrs.REFERER, '-'),
275+
'a': headers.get(hdrs.USER_AGENT, '-'),
280276
'T': str(int(request_time)),
281277
'D': str(request_time).split('.', 1)[-1][:5],
282278
'p': "<%s>" % os.getpid()

0 commit comments

Comments
 (0)