sendorder.py (1995B)
1 from typing import Optional, Tuple 2 from urllib.parse import urlsplit, parse_qsl 3 4 return_stream_id = 0; 5 summary : bytes = []; 6 7 def session_established(session): 8 # When a WebTransport session is established, a bidirectional stream is 9 # created by the server, which is used to echo back stream data from the 10 # client. 11 path: Optional[bytes] = None 12 for key, value in session.request_headers: 13 if key == b':path': 14 path = value 15 assert path is not None 16 qs = dict(parse_qsl(urlsplit(path).query)) 17 token = qs[b'token'] 18 if token is None: 19 raise Exception('token is missing, path = {}'.format(path)) 20 session.dict_for_handlers['token'] = token 21 global summary; 22 # need an initial value to replace 23 session.stash.put(key=token, value=summary) 24 25 def stream_data_received(session, 26 stream_id: int, 27 data: bytes, 28 stream_ended: bool): 29 # we want to record the order that data arrives, and feed that ordering back to 30 # the sender. Instead of echoing all the data, we'll send back 31 # just the first byte of each message. This requires the sender to send buffers 32 # filled with only a single byte value. 33 # The test can then examine the stream of data received by the server to 34 # determine if orderings are correct. 35 # note that the size in bytes received here can vary wildly 36 37 # Send back the data on the control stream 38 global summary 39 summary += data[0:1] 40 token = session.dict_for_handlers['token'] 41 old_data = session.stash.take(key=token) or {} 42 session.stash.put(key=token, value=summary) 43 44 def stream_reset(session, stream_id: int, error_code: int) -> None: 45 global summary; 46 token = session.dict_for_handlers['token'] 47 session.stash.put(key=token, value=summary) 48 summary = [] 49 50 # do something different to include datagrams... 51 def datagram_received(session, data: bytes): 52 session.send_datagram(data)