dabeaz / curio

Good Curio!
Other
4.01k stars 240 forks source link

Question: how to check the selected alpn protocol using curio.io.Socket? #333

Closed cdeler closed 3 years ago

cdeler commented 3 years ago

Hello,

I have a question about curio.io.Socket

Once it's created, let's say using curio.open_connection, I want to check selected alpn protocol.

I'm doing that using the following code snippet:

def get_selected_alpn_proto(socket: curio.io.Socket) -> str:
        ident = "http/1.1"

        if hasattr(socket, "_socket"):
            raw_socket = socket._socket

            if isinstance(raw_socket, ssl.SSLSocket):
                ident = raw_socket.selected_alpn_protocol()

        return "HTTP/2" if ident == "h2" else "HTTP/1.1"

But it's a bit uncomfortable for me to rely on the protected API (if hasattr(socket, "_socket"))

Do you know if there another way to check the selected alpn protocol once socket has been created?

dabeaz commented 3 years ago

Curio sockets delegate methods via __getattr__(). It might be possible to call sock.selected_alpn_protocol() directly on the Curio socket.