Skip to content

Commit fef804d

Browse files
committed
Merge branch 'master' of github.com:KeepSafe/aiohttp
2 parents 39ab304 + e02c8b7 commit fef804d

6 files changed

Lines changed: 67 additions & 33 deletions

File tree

CHANGES.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ CHANGES
3939

4040
- Close connection on websocket hadshake error #703
4141

42-
- Implement calss based views #684
42+
- Implement class based views #684

aiohttp/helpers.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -208,34 +208,6 @@ def guess_filename(obj, default=None):
208208
return default
209209

210210

211-
def parse_remote_addr(forward):
212-
if isinstance(forward, str):
213-
# we only took the last one
214-
# http://en.wikipedia.org/wiki/X-Forwarded-For
215-
if ',' in forward:
216-
forward = forward.rsplit(',', 1)[-1].strip()
217-
218-
# find host and port on ipv6 address
219-
if '[' in forward and ']' in forward:
220-
host = forward.split(']')[0][1:].lower()
221-
elif ':' in forward and forward.count(':') == 1:
222-
host = forward.split(':')[0].lower()
223-
else:
224-
host = forward
225-
226-
forward = forward.split(']')[-1]
227-
if ':' in forward and forward.count(':') == 1:
228-
port = forward.split(':', 1)[1]
229-
else:
230-
port = 80
231-
232-
remote = (host, port)
233-
else:
234-
remote = forward
235-
236-
return remote[0], str(remote[1])
237-
238-
239211
class AccessLogger:
240212
"""Helper object to log access.
241213

aiohttp/websocket.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,12 @@ def _websocket_mask_python(mask, data):
188188
version when available.
189189
190190
"""
191-
assert len(mask) == 4
191+
assert isinstance(data, bytearray), data
192+
assert len(mask) == 4, mask
192193
datalen = len(data)
193194
if datalen == 0:
194195
# everything work without this, but may be changed later in Python.
195-
return b''
196+
return bytearray()
196197
data = int.from_bytes(data, native_byteorder)
197198
mask = int.from_bytes(mask * (datalen // 4) + mask[: datalen % 4],
198199
native_byteorder)

docs/web.rst

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ After that, create a server and run the *asyncio loop* as usual::
4444
except KeyboardInterrupt:
4545
pass
4646
finally:
47-
loop.run_until_complete(handler.finish_connections(1.0))
4847
srv.close()
4948
loop.run_until_complete(srv.wait_closed())
49+
loop.run_until_complete(handler.finish_connections(1.0))
5050
loop.run_until_complete(app.finish())
5151
loop.close()
5252

@@ -188,6 +188,33 @@ application developers can organize handlers in classes if they so wish::
188188
app.router.add_route('GET', '/greet/{name}', handler.handle_greeting)
189189

190190

191+
Class Based Views
192+
^^^^^^^^^^^^^^^^^
193+
194+
:mod:`aiohttp.web` has support for django-style class based views.
195+
196+
You can derive from :class:`View` and define methods for handling http
197+
requests::
198+
199+
class MyView(web.View):
200+
async def get(self):
201+
return await get_resp(self.request)
202+
203+
async def post(self):
204+
return await post_resp(self.request)
205+
206+
Handlers should be coroutines accepting self only and returning
207+
response object as regular :term:`web-handler`. Request object can be
208+
retrieved by :attr:`View.request` property.
209+
210+
After implementing the view (``MyView`` from example above) should be
211+
registered in application's router::
212+
213+
app.router.add_route('*', '/path/to', MyView)
214+
215+
Example will process GET and POST requests for */path/to* but raise
216+
*405 Method not allowed* exception for unimplemented HTTP methods.
217+
191218
Route Views
192219
^^^^^^^^^^^
193220

tests/post_data.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/test_websocket_parser.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,3 +459,38 @@ def parse_frame(buf, cont=False):
459459
assert res, (Message(websocket.OPCODE_CLOSE, 0, ''), 0)
460460
res = out._buffer[1]
461461
assert res == (Message(websocket.OPCODE_TEXT, 'line1line2', ''), 10)
462+
463+
464+
websocket_mask_data = bytearray(
465+
b'some very long data for masking by websocket')
466+
websocket_mask_mask = b'1234'
467+
websocket_mask_masked = (b'B]^Q\x11DVFH\x12_[_U\x13PPFR\x14W]A\x14\\S@_X'
468+
b'\\T\x14SK\x13CTP@[RYV@')
469+
470+
471+
def test_websocket_mask_python():
472+
ret = websocket._websocket_mask_python(websocket_mask_mask,
473+
websocket_mask_data)
474+
assert ret == websocket_mask_masked
475+
476+
477+
@pytest.mark.skipif(not hasattr(websocket, '_websocket_mask_cython'),
478+
reason='Requires Cython')
479+
def test_websocket_mask_cython():
480+
ret = websocket._websocket_mask_cython(websocket_mask_mask,
481+
websocket_mask_data)
482+
assert ret == websocket_mask_masked
483+
484+
485+
def test_websocket_mask_python_empty():
486+
ret = websocket._websocket_mask_python(websocket_mask_mask,
487+
bytearray())
488+
assert ret == bytearray()
489+
490+
491+
@pytest.mark.skipif(not hasattr(websocket, '_websocket_mask_cython'),
492+
reason='Requires Cython')
493+
def test_websocket_mask_cython_empty():
494+
ret = websocket._websocket_mask_cython(websocket_mask_mask,
495+
bytearray())
496+
assert ret == bytearray()

0 commit comments

Comments
 (0)