Skip to content

Commit

Permalink
Update CHANGES
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Dec 27, 2015
2 parents 5635451 + a9054da commit 4124c4f
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 46 deletions.
9 changes: 8 additions & 1 deletion CHANGES.txt
Expand Up @@ -41,4 +41,11 @@ CHANGES

- Implement class based views #684

- Add *headers* parameter to ws_connect()
- Add *headers* parameter to ws_connect() #709

- Drop unused function `parse_remote_addr()` #708

- Close session on exception #707

- Store http code and headers in WSServerHandshakeError #706
>>>>>>> master
36 changes: 32 additions & 4 deletions aiohttp/client.py
Expand Up @@ -273,20 +273,32 @@ def _ws_connect(self, url, *,
try:
# check handshake
if resp.status != 101:
raise WSServerHandshakeError('Invalid response status')
raise WSServerHandshakeError(
message='Invalid response status',
code=resp.status,
headers=resp.headers)

if resp.headers.get(hdrs.UPGRADE, '').lower() != 'websocket':
raise WSServerHandshakeError('Invalid upgrade header')
raise WSServerHandshakeError(
message='Invalid upgrade header',
code=resp.status,
headers=resp.headers)

if resp.headers.get(hdrs.CONNECTION, '').lower() != 'upgrade':
raise WSServerHandshakeError('Invalid connection header')
raise WSServerHandshakeError(
message='Invalid connection header',
code=resp.status,
headers=resp.headers)

# key calculation
key = resp.headers.get(hdrs.SEC_WEBSOCKET_ACCEPT, '')
match = base64.b64encode(
hashlib.sha1(sec_key + WS_KEY).digest()).decode()
if key != match:
raise WSServerHandshakeError('Invalid challenge response')
raise WSServerHandshakeError(
message='Invalid challenge response',
code=resp.status,
headers=resp.headers)

# websocket protocol
protocol = None
Expand Down Expand Up @@ -526,6 +538,22 @@ def __init__(self, coro, session):
super().__init__(coro)
self._session = session

@asyncio.coroutine
def __iter__(self):
try:
return (yield from self._coro)
except:
self._session.close()
raise

if PY_35:
def __await__(self):
try:
return (yield from self._coro)
except:
self._session.close()
raise

def __del__(self):
self._session.detach()

Expand Down
3 changes: 0 additions & 3 deletions aiohttp/errors.py
Expand Up @@ -96,9 +96,6 @@ def __init__(self, *, code=None, message='', headers=None):
class WSServerHandshakeError(HttpProcessingError):
"""websocket server handshake error."""

def __init__(self, message, *, headers=None):
super().__init__(message=message, headers=headers)


class HttpProxyError(HttpProcessingError):
"""Http proxy error.
Expand Down
28 changes: 0 additions & 28 deletions aiohttp/helpers.py
Expand Up @@ -208,34 +208,6 @@ def guess_filename(obj, default=None):
return default


def parse_remote_addr(forward):
if isinstance(forward, str):
# we only took the last one
# http://en.wikipedia.org/wiki/X-Forwarded-For
if ',' in forward:
forward = forward.rsplit(',', 1)[-1].strip()

# find host and port on ipv6 address
if '[' in forward and ']' in forward:
host = forward.split(']')[0][1:].lower()
elif ':' in forward and forward.count(':') == 1:
host = forward.split(':')[0].lower()
else:
host = forward

forward = forward.split(']')[-1]
if ':' in forward and forward.count(':') == 1:
port = forward.split(':', 1)[1]
else:
port = 80

remote = (host, port)
else:
remote = forward

return remote[0], str(remote[1])


class AccessLogger:
"""Helper object to log access.
Expand Down
2 changes: 2 additions & 0 deletions aiohttp/server.py
Expand Up @@ -359,6 +359,8 @@ def handle_error(self, status=500, message=None,
response.send_headers()

response.write(html)
# disable CORK, enable NODELAY if needed
self.writer.set_tcp_nodelay(True)
drain = response.write_eof()

self.log_access(message, None, response, self._loop.time() - now)
Expand Down
14 changes: 8 additions & 6 deletions aiohttp/web_reqrep.py
Expand Up @@ -701,8 +701,6 @@ def _start(self, request):
request.version,
not keep_alive,
self._reason)
resp_impl.transport.set_tcp_nodelay(self._tcp_nodelay)
resp_impl.transport.set_tcp_cork(self._tcp_cork)

self._copy_cookies()

Expand All @@ -722,6 +720,8 @@ def _start(self, request):
for key, val in headers:
resp_impl.add_header(key, val)

resp_impl.transport.set_tcp_nodelay(self._tcp_nodelay)
resp_impl.transport.set_tcp_cork(self._tcp_cork)
resp_impl.send_headers()
return resp_impl

Expand Down Expand Up @@ -847,10 +847,12 @@ def text(self, text):

@asyncio.coroutine
def write_eof(self):
body = self._body
if body is not None:
self.write(body)
self.set_tcp_nodelay(True)
try:
body = self._body
if body is not None:
self.write(body)
finally:
self.set_tcp_nodelay(True)
yield from super().write_eof()


Expand Down
10 changes: 6 additions & 4 deletions aiohttp/web_urldispatcher.py
Expand Up @@ -288,12 +288,14 @@ def handle(self, request):

resp.content_length = file_size
resp.set_tcp_cork(True)
yield from resp.prepare(request)
try:
yield from resp.prepare(request)

with open(filepath, 'rb') as f:
yield from self._sendfile(request, resp, f, file_size)
with open(filepath, 'rb') as f:
yield from self._sendfile(request, resp, f, file_size)

resp.set_tcp_nodelay(True)
finally:
resp.set_tcp_nodelay(True)

return resp

Expand Down

0 comments on commit 4124c4f

Please sign in to comment.