Skip to content

Expose remote connection port on RequestHeader#14077

Merged
mkurz merged 1 commit into
playframework:mainfrom
mkurz:remote-connection-port
Jul 8, 2026
Merged

Expose remote connection port on RequestHeader#14077
mkurz merged 1 commit into
playframework:mainfrom
mkurz:remote-connection-port

Conversation

@mkurz

@mkurz mkurz commented Jul 8, 2026

Copy link
Copy Markdown
Member

Expose the remote peer port for incoming requests.

This allows applications to log the same address:port tuple that can appear in lower-level server logs, making it easier to correlate request logs with connection-level diagnostics.

  • Add remotePort to RemoteConnection.
  • Add RequestHeader.remotePort in Scala.
  • Add Http.RequestHeader.remotePort() in Java.
  • Add remotePort(...) support to Http.RequestBuilder.
  • Populate the port from the raw socket in Netty and Pekko HTTP.
  • Preserve the port when converting between Scala and Java requests.
  • Clear the raw port when trusted forwarded headers replace the remote address, because forwarded port headers are not parsed yet.

The exposed value is the remote TCP peer port Play sees directly. With a proxy or load balancer, this is usually the proxy's source port, not the original client's port.

This PR does not add support for X-Forwarded-Port or RFC 7239 forwarded ports. That will be handled in a upcoming PR.

Add remotePort to RemoteConnection and RequestHeader so request code
can inspect the raw peer port when the server backend knows it.
Populate the value from Netty and Pekko HTTP socket addresses, keep
it through Java request conversion, and expose it on the Java request builder.
When trusted forwarded headers replace the remote address, clear the
raw port because forwarded port headers are not parsed yet. Add focused tests and docs.
@mkurz mkurz merged commit 4b0abd2 into playframework:main Jul 8, 2026
45 checks passed
@mkurz mkurz deleted the remote-connection-port branch July 8, 2026 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RFE: Add Port to play.api.mvc.request.RemoteConnection

1 participant