Skip to content

Commit

Permalink
[asyncio] make serve() take a QuicConfiguration
Browse files Browse the repository at this point in the history
This cuts down the number of keyword arguments.
  • Loading branch information
jlaine committed Aug 12, 2019
1 parent b18fa38 commit f2bc4de
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
4 changes: 4 additions & 0 deletions aioquic/asyncio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ async def connect(
* ``configuration`` is a :class:`~aioquic.quic.configuration.QuicConfiguration`
configuration object.
* ``create_protocol`` allows customizing the :class:`~asyncio.Protocol` that
manages the connection. It should be a callable or class accepting the same
arguments as :class:`~aioquic.asyncio.QuicConnectionProtocol` and returning
an instance of :class:`~aioquic.asyncio.QuicConnectionProtocol` or a subclass.
* ``session_ticket_handler`` is a callback which is invoked by the TLS
engine when a new session ticket is received.
* ``stream_handler`` is a callback which is invoked whenever a stream is
Expand Down
38 changes: 9 additions & 29 deletions aioquic/asyncio/server.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import asyncio
import os
from functools import partial
from typing import Any, Callable, Dict, List, Optional, Text, TextIO, Union, cast
from typing import Callable, Dict, Optional, Text, Union, cast

from ..buffer import Buffer
from ..quic.configuration import QuicConfiguration
from ..quic.connection import NetworkAddress, QuicConnection
from ..quic.logger import QuicLogger
from ..quic.packet import (
PACKET_TYPE_INITIAL,
encode_quic_retry,
Expand Down Expand Up @@ -152,33 +151,25 @@ async def serve(
host: str,
port: int,
*,
certificate: Any,
private_key: Any,
alpn_protocols: Optional[List[str]] = None,
configuration: QuicConfiguration,
create_protocol: Callable = QuicConnectionProtocol,
idle_timeout: Optional[float] = None,
quic_logger: Optional[QuicLogger] = None,
stream_handler: QuicStreamHandler = None,
secrets_log_file: Optional[TextIO] = None,
session_ticket_fetcher: Optional[SessionTicketFetcher] = None,
session_ticket_handler: Optional[SessionTicketHandler] = None,
stateless_retry: bool = False,
stream_handler: QuicStreamHandler = None,
) -> QuicServer:
"""
Start a QUIC server at the given `host` and `port`.
:func:`serve` requires a TLS certificate and private key, which can be
specified using the following arguments:
* ``certificate`` is the server's TLS certificate.
See :func:`cryptography.x509.load_pem_x509_certificate`.
* ``private_key`` is the server's private key.
See :func:`cryptography.hazmat.primitives.serialization.load_pem_private_key`.
:func:`serve` requires a :class:`~aioquic.quic.configuration.QuicConfiguration`
containing TLS certificate and private key as the ``configuration`` argument.
:func:`serve` also accepts the following optional arguments:
* ``secrets_log_file`` is a file-like object in which to log traffic
secrets. This is useful to analyze traffic captures with Wireshark.
* ``create_protocol`` allows customizing the :class:`~asyncio.Protocol` that
manages the connection. It should be a callable or class accepting the same
arguments as :class:`~aioquic.asyncio.QuicConnectionProtocol` and returning
an instance of :class:`~aioquic.asyncio.QuicConnectionProtocol` or a subclass.
* ``session_ticket_fetcher`` is a callback which is invoked by the TLS
engine when a session ticket is presented by the peer. It should return
the session ticket with the specified ID or `None` if it is not found.
Expand All @@ -194,17 +185,6 @@ async def serve(

loop = asyncio.get_event_loop()

configuration = QuicConfiguration(
alpn_protocols=alpn_protocols,
certificate=certificate,
is_client=False,
private_key=private_key,
quic_logger=quic_logger,
secrets_log_file=secrets_log_file,
)
if idle_timeout is not None:
configuration.idle_timeout = idle_timeout

_, protocol = await loop.create_datagram_endpoint(
lambda: QuicServer(
configuration=configuration,
Expand Down
20 changes: 16 additions & 4 deletions tests/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,17 @@ async def serve():
asyncio.ensure_future(serve())


async def run_server(**kwargs):
async def run_server(configuration=None, **kwargs):
if configuration is None:
configuration = QuicConfiguration(
certificate=SERVER_CERTIFICATE,
private_key=SERVER_PRIVATE_KEY,
is_client=False,
)
return await serve(
host="::",
port="4433",
certificate=SERVER_CERTIFICATE,
private_key=SERVER_PRIVATE_KEY,
configuration=configuration,
stream_handler=handle_stream,
**kwargs
)
Expand Down Expand Up @@ -110,7 +115,14 @@ def test_connect_and_serve_with_packet_loss(self, mock_sendto):
server, response = run(
asyncio.gather(
run_server(
idle_timeout=300.0, quic_logger=QuicLogger(), stateless_retry=True
configuration=QuicConfiguration(
certificate=SERVER_CERTIFICATE,
idle_timeout=300.0,
is_client=False,
private_key=SERVER_PRIVATE_KEY,
quic_logger=QuicLogger(),
),
stateless_retry=True,
),
run_client(
"127.0.0.1",
Expand Down

0 comments on commit f2bc4de

Please sign in to comment.