浏览代码

Ignore querystring

master
Roderic Day 5 年前
父节点
当前提交
c38ba323bd
共有 3 个文件被更改,包括 30 次插入2 次删除
  1. +2
    -1
      makefile
  2. +6
    -1
      pico.py
  3. +22
    -0
      test.py

+ 2
- 1
makefile 查看文件

default: test deploy
default: test
make deploy


test: venv/ libs/ test: venv/ libs/
venv/bin/python -u test.py venv/bin/python -u test.py

+ 6
- 1
pico.py 查看文件

import uuid import uuid
from functools import partial from functools import partial
from pathlib import Path from pathlib import Path
from urllib.parse import urlsplit, urlunsplit


import websockets import websockets


rooms = collections.defaultdict(dict) rooms = collections.defaultdict(dict)




def ignore_querystring(path):
return urlunsplit(urlsplit(path)[:3] + ('', ''))


class PicoProtocol(websockets.WebSocketServerProtocol): class PicoProtocol(websockets.WebSocketServerProtocol):


def serve_file(self, path): def serve_file(self, path):




async def handle(ws, path, server_name): async def handle(ws, path, server_name):
room = rooms[path]
room = rooms[ignore_querystring(path)]
usernames = room.keys() usernames = room.keys()
sockets = room.values() sockets = room.values()
username = None username = None

+ 22
- 0
test.py 查看文件

return _test(client1, client2, client3) return _test(client1, client2, client3)




def test_query_is_irrelevant():
client1 = _make_client('ws://localhost:8642/x?a=0', 0.10, Script()
+ {'kind': 'login', 'value': 'Alice'}
- {'kind': 'state', 'username': 'Alice'}
- {'kind': 'state', 'online': ['Alice']}
- {'kind': 'state', 'online': ['Alice', 'Bob']}
+ {'kind': 'post', 'value': 'Hey Bob!'}
- {'kind': 'post', 'value': 'Hey Bob!', 'source': 'Alice'}
- {'kind': 'post', 'value': 'Howdy!', 'source': 'Bob'}
)
client2 = _make_client('ws://localhost:8642/x?v=0', 0.11, Script()
+ {'kind': 'login', 'value': 'Bob'}
- {'kind': 'state', 'username': 'Bob'}
- {'kind': 'state', 'online': ['Alice', 'Bob']}
- {'kind': 'post', 'value': 'Hey Bob!', 'source': 'Alice'}
+ {'kind': 'post', 'value': 'Howdy!'}
- {'kind': 'post', 'value': 'Howdy!', 'source': 'Bob'}
- {'kind': 'state', 'online': ['Bob']}
)
return _test(client1, client2)


if __name__ == '__main__': if __name__ == '__main__':
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
for fn_name, fn in list(locals().items()): for fn_name, fn in list(locals().items()):

正在加载...
取消
保存