Skip to content

Implement a more comprehensive way to get connection information #1269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions lib/plug/adapters/test/conn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ defmodule Plug.Adapters.Test.Conn do
ref: make_ref(),
owner: owner,
http_protocol: get_from_adapter(conn, :get_http_protocol, :"HTTP/1.1"),
peer_data:
get_from_adapter(conn, :get_peer_data, %{
address: {127, 0, 0, 1},
port: 111_317,
ssl_cert: nil
connection_data:
get_from_adapter(conn, :get_connection_data, %{
peer_data: %{address: {127, 0, 0, 1}, port: 111_317, ssl_cert: nil},
sock_data: %{address: {127, 0, 0, 2}, port: 111_318, ssl_cert: nil},
ssl_data: nil
})
}

Expand Down Expand Up @@ -136,8 +136,8 @@ defmodule Plug.Adapters.Test.Conn do
:ok
end

def get_peer_data(payload) do
Map.fetch!(payload, :peer_data)
def get_connection_data(payload) do
Map.fetch!(payload, :connection_data)
end

def get_http_protocol(payload) do
Expand Down
15 changes: 12 additions & 3 deletions lib/plug/conn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -630,12 +630,21 @@ defmodule Plug.Conn do
%{conn | status: Plug.Conn.Status.code(status), resp_body: body, state: :set}
end

@doc """
Returns information such as the peer and local address, port and SSL cert as well as
details of the negotiated SSL connection, if present.
"""
@spec get_connection_data(t) :: Plug.Conn.Adapter.connection_data()
def get_connection_data(%Conn{adapter: {adapter, payload}}) do
adapter.get_connection_data(payload)
end

@doc """
Returns the request peer data if one is present.
"""
@spec get_peer_data(t) :: Plug.Conn.Adapter.peer_data()
def get_peer_data(%Conn{adapter: {adapter, payload}}) do
adapter.get_peer_data(payload)
@spec get_peer_data(t) :: Plug.Conn.Adapter.endpoint_data()
def get_peer_data(conn) do
get_connection_data(conn)[:peer_data]
end

@doc """
Expand Down
12 changes: 9 additions & 3 deletions lib/plug/conn/adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ defmodule Plug.Conn.Adapter do

@type http_protocol :: :"HTTP/1" | :"HTTP/1.1" | :"HTTP/2" | atom
@type payload :: term
@type peer_data :: %{
@type connection_data :: %{
peer_data: endpoint_data(),
sock_data: endpoint_data(),
ssl_data: :ssl.connection_info()
}
@type endpoint_data :: %{
address: :inet.ip_address(),
port: :inet.port_number(),
ssl_cert: binary | nil
Expand Down Expand Up @@ -162,9 +167,10 @@ defmodule Plug.Conn.Adapter do
@callback upgrade(payload, protocol :: atom, opts :: term) :: {:ok, payload} | {:error, term}

@doc """
Returns peer information such as the address, port and ssl cert.
Returns connection information such as the peer and local address, port and SSL cert as well as
details of the negotiated SSL connection, if present.
"""
@callback get_peer_data(payload) :: peer_data()
@callback get_connection_data(payload) :: endpoint_data()

@doc """
Returns the HTTP protocol and its version.
Expand Down
6 changes: 3 additions & 3 deletions lib/plug/test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ defmodule Plug.Test do
end

@doc """
Puts the peer data.
Puts the connection data.
"""
def put_peer_data(conn, peer_data) do
def put_connection_data(conn, connection_data) do
update_in(conn.adapter, fn {adapter, payload} ->
{adapter, Map.put(payload, :peer_data, peer_data)}
{adapter, Map.put(payload, :connection_data, connection_data)}
end)
end

Expand Down
13 changes: 9 additions & 4 deletions test/plug/adapters/test/conn_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,15 @@ defmodule Plug.Adapters.Test.ConnTest do
~s(the URI path used in plug tests must start with "/", got: "foo")
end

test "use custom peer data" do
peer_data = %{address: {127, 0, 0, 1}, port: 111_317}
conn = conn(:get, "/") |> put_peer_data(peer_data)
assert peer_data == Plug.Conn.get_peer_data(conn)
test "use custom connection data" do
connection_data = %{
peer_data: %{address: {127, 0, 0, 1}, port: 111_317},
sock_data: %{address: {127, 0, 0, 2}, port: 111_318},
ssl_data: %{ssl_info: :its_here}
}

conn = conn(:get, "/") |> put_connection_data(connection_data)
assert connection_data == Plug.Conn.get_connection_data(conn)
end

test "push/3 sends message including path and headers" do
Expand Down
6 changes: 6 additions & 0 deletions test/plug/conn_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ defmodule Plug.ConnTest do
port: 111_317,
ssl_cert: nil
}

assert get_connection_data(conn) == %{
peer_data: %{address: {127, 0, 0, 1}, port: 111_317, ssl_cert: nil},
sock_data: %{address: {127, 0, 0, 2}, port: 111_318, ssl_cert: nil},
ssl_data: nil
}
end

test "path_info" do
Expand Down