aioquic Documentation

repository·main·Indexed 24 days ago

https://github.com/aiortc/aioquic

A Python library providing a QUIC network protocol stack, a minimal TLS 1.3 implementation, and an HTTP/3 stack. It offers three API patterns: a high-level asyncio QUIC API for asynchronous applications, and I/O-agnostic QUIC and HTTP/3 APIs following the sans-io pattern, allowing integration with any concurrency model by letting the user manage the underlying transport.

Tokens
13.3K
Snippets
10
Records
98
Agent score
84%

What's inside aioquic

  1. Overview of aioquic APIs

    main

    aioquic is a Python library for the QUIC network protocol. It provides three distinct API patterns depending on your integration needs:

    1. QUIC API: Follows a "bring your own I/O" pattern. This is designed for embedding the protocol logic into any existing I/O framework or event loop.
    2. HTTP/3 API: Also follows the "bring your own I/O" pattern, allowing you to implement HTTP/3 over custom I/O layers.
    3. asyncio QUIC API: A convenience API built specifically on top of Python's standard asyncio framework for easier use in standard asynchronous applications.
  2. Understand the Sans-IO API pattern in aioquic

    main

    Both the QUIC and HTTP/3 APIs in aioquic follow the sans-io pattern. This means the library does not perform actual I/O operations (like reading from or writing to a socket) itself. Instead, it manages the protocol logic and state, leaving the responsibility of moving bytes to and from the network to the API user.

    This design allows you to integrate aioquic with any concurrency model or I/O loop and makes the protocol logic highly testable.

  3. How TLS and encryption are handled in aioquic

    main

    Encryption in aioquic is split into two layers to meet QUIC's specific requirements:

    1. TLS 1.3 Logic: A minimal TLS 1.3 implementation is used, built on top of the cryptography library. This is necessary because QUIC requires specific capabilities not found in standard OpenSSL implementations, such as extracting traffic secrets and operating directly on TLS messages without the standard TLS record layer.

    2. Packet Protection: Header protection and payload encryption are implemented as a C extension linked to OpenSSL to ensure high performance, as these operations occur for every single packet.

  4. Handle QUIC events with QuicEvent

    main

    The QUIC API communicates state changes and incoming data through QuicEvent objects (from aioquic.quic.events). When you feed received data into the connection, it will produce events that you must handle. Key events include:

    • HandshakeCompleted: The TLS handshake is finished.
    • StreamDataReceived: New data has arrived on a specific stream.
    • StreamReset: A stream was reset.
    • ConnectionTerminated: The connection was closed.
    • PingAcknowledged: A ping was acknowledged.
    • StopSendingReceived: The peer requested to stop receiving data.
  5. Integrate HTTP/3 using the aioquic HTTP/3 API

    main
    The aioquic HTTP/3 API is designed to be I/O agnostic. It performs no I/O operations itself, which allows you to integrate HTTP/3 into any Python application regardless of your specific concurrency model (e.g., asyncio, threads, or custom event loops). You are responsible for managing the underlying transport and performing the actual I/O operations.
  6. Handle HTTP/3 events with H3Event

    main

    The HTTP/3 API communicates via events defined in aioquic.h3.events. When processing data from the connection, you should check the type of H3Event received to determine how to react. Supported event types include:

    • DatagramReceived: Triggered when a QUIC datagram is received.
    • DataReceived: Triggered when stream data is received.
    • HeadersReceived: Triggered when HTTP headers are received.
    • PushPromiseReceived: Triggered when a server push promise is received.
    • WebTransportStreamDataReceived: Triggered when WebTransport stream data is received.
  7. How QUIC and HTTP/3 APIs work in aioquic

    main
    The QUIC and HTTP/3 APIs in aioquic follow a "bring your own I/O" pattern. This means the library handles the protocol logic, but the actual I/O operations (reading from and writing to sockets) are left to the API user. This design allows the library to be easily embedded into different concurrency models and makes the protocol logic highly testable.
  8. Integrate QUIC with any concurrency model

    main
    The aioquic QUIC API is designed to be I/O agnostic. It performs no I/O operations itself, which means you are responsible for handling the actual sending and receiving of bytes. This design allows you to integrate QUIC into any Python application, whether you are using asyncio, threads, or other concurrency models.
  9. Run HTTP/3 server and client examples

    main

    The HTTP/3 examples include a server capable of handling both HTTP/0.9 and HTTP/3, and a client for making requests.

    Server usage: Run the server by providing a TLS certificate and private key.

    Client usage:

    • Standard HTTP/3: Perform a standard request using --ca-certs.
    • Legacy HTTP/0.9: Use the --legacy-http flag to perform an HTTP/0.9 request.
    • WebSockets: Use the wss:// scheme to open a WebSocket over HTTP/3.