Skip to content

Commit

Permalink
[h3] make send_datagram raise InvalidStreamTypeError
Browse files Browse the repository at this point in the history
DATAGRAM frames may only be sent on client-initiated bidirectional
streams, raise an exception if `send_push_promise` is called on an
invalid stream ID.
  • Loading branch information
jlaine committed Jan 7, 2024
1 parent b31f7d0 commit 1f8735c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/aioquic/h3/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,12 +400,19 @@ def send_datagram(self, stream_id: int, data: bytes) -> None:
"""
Send a datagram for the specified stream.
If the stream ID is not a client-initiated bidirectional stream, an
:class:`~aioquic.h3.exceptions.InvalidStreamTypeError` exception is raised.
:param stream_id: The stream ID.
:param data: The HTTP/3 datagram payload.
"""
assert (
stream_id % 4 == 0
), "Datagrams can only be sent for client-initiated bidirectional streams"

# check stream ID is valid
if not stream_is_request_response(stream_id):
raise InvalidStreamTypeError(
"Datagrams can only be sent for client-initiated bidirectional streams"
)

self._quic.send_datagram_frame(encode_uint_var(stream_id // 4) + data)

def send_push_promise(self, stream_id: int, headers: Headers) -> int:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_webtransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
HeadersReceived,
WebTransportStreamDataReceived,
)
from aioquic.h3.exceptions import InvalidStreamTypeError
from aioquic.quic.configuration import QuicConfiguration
from aioquic.quic.events import DatagramFrameReceived

Expand Down Expand Up @@ -281,6 +282,10 @@ def test_datagram(self):
# create session
session_id = self._make_session(h3_client, h3_server)

# send datagram on a server-initiated stream
with self.assertRaises(InvalidStreamTypeError):
h3_client.send_datagram(data=b"foo", stream_id=1)

# send datagram
h3_client.send_datagram(data=b"foo", stream_id=session_id)

Expand Down

0 comments on commit 1f8735c

Please sign in to comment.