diff --git a/lib/mint/http.ex b/lib/mint/http.ex index c1a092fa..f8ca9ec2 100644 --- a/lib/mint/http.ex +++ b/lib/mint/http.ex @@ -377,6 +377,26 @@ defmodule Mint.HTTP do def upgrade(old_transport, transport_state, scheme, hostname, port, opts), do: Mint.Negotiate.upgrade(old_transport, transport_state, scheme, hostname, port, opts) + @doc """ + Returns the protocol used by the current connection. + + ## Examples + + iex> Mint.HTTP.protocol(%Mint.HTTP1{}) + :http1 + + iex> Mint.HTTP.protocol(%Mint.HTTP2{}) + :http2 + """ + if Version.compare(System.version(), "1.7.0") in [:eq, :gt] do + @doc since: "1.4.0" + end + + @spec protocol(t()) :: :http1 | :http2 + def protocol(%Mint.HTTP1{}), do: :http1 + def protocol(%Mint.HTTP2{}), do: :http2 + def protocol(%Mint.UnsafeProxy{state: internal_conn}), do: protocol(internal_conn) + @doc false @impl true @spec initiate( diff --git a/test/http_test.exs b/test/http_test.exs new file mode 100644 index 00000000..70e307c2 --- /dev/null +++ b/test/http_test.exs @@ -0,0 +1,4 @@ +defmodule Mint.HTTPTest do + use ExUnit.Case, async: true + doctest Mint.HTTP +end