summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webtransport/handlers/client-close.py
blob: 01fb14b13430f73cab1562ccb0fedce528fe973e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from typing import Optional, Tuple
from urllib.parse import urlsplit, parse_qsl


def session_established(session):
    path: Optional[bytes] = None
    for key, value in session.request_headers:
        if key == b':path':
            path = value
    assert path is not None
    qs = dict(parse_qsl(urlsplit(path).query))
    token = qs[b'token']
    if token is None:
        raise Exception('token is missing, path = {}'.format(path))
    session.dict_for_handlers['token'] = token
    session.create_bidirectional_stream()


def stream_reset(session, stream_id: int, error_code: int) -> None:
    token = session.dict_for_handlers['token']
    data = session.stash.take(key=token) or {}

    data['stream-close-info'] = {
        'source': 'reset',
        'code': error_code
    }
    session.stash.put(key=token, value=data)


def stream_data_received(session,
                         stream_id: int,
                         data: bytes,
                         stream_ended: bool):
    if stream_ended:
        token = session.dict_for_handlers['token']
        stashed_data = session.stash.take(key=token) or {}
        stashed_data['stream-close-info'] = {
            'source': 'FIN',
        }
        session.stash.put(key=token, value=stashed_data)


def session_closed(
        session, close_info: Optional[Tuple[int, bytes]], abruptly: bool) -> None:
    token = session.dict_for_handlers['token']
    data = session.stash.take(key=token) or {}

    decoded_close_info: Optional[Dict[str, Any]] = None
    if close_info:
        decoded_close_info = {
            'code': close_info[0],
            'reason': close_info[1].decode()
        }

    data['session-close-info'] = {
        'abruptly': abruptly,
        'close_info': decoded_close_info
    }
    session.stash.put(key=token, value=data)