Skip to content

Conversation

christiangoerdes
Copy link
Collaborator

@christiangoerdes christiangoerdes commented Sep 26, 2025

Summary by CodeRabbit

  • Documentation
    • Clarified connection matching behavior in internal docs to explain how host/port comparisons are interpreted.
  • Tests
    • Updated unit tests to align expected host comparisons for loopback vs explicit addresses.

(Note: No functional changes; behavior remains the same.)

Copy link
Contributor

coderabbitai bot commented Sep 26, 2025

Walkthrough

Added detailed Javadoc to Connection.isSame(String host, int port) documenting its behavior; updated a unit test to correct which host strings are passed to isSame. No functional changes to production logic.

Changes

Cohort / File(s) Summary
Connection Javadoc
core/src/main/java/com/predic8/membrane/core/transport/http/Connection.java
Added a detailed Javadoc comment above isSame(String host, int port) describing the (host, port) matching semantics and rationale (no behavior change).
Connection test adjustment
core/src/test/java/com/predic8/membrane/core/transport/http/ConnectionTest.java
Updated testIsSame expectations by swapping the host strings passed to isSame for the two connection instances (conLocalhost now uses "localhost", con127_0_0_1 now uses "127.0.0.1").

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I twitch an ear at comments fine,
I hop through tests to mark the line;
A doc appears, no trick, no code—
Just clearer trails along the road. 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Title Check ⚠️ Warning The title “Enhance Connection.isSame (#2128)” implies a functional improvement to the isSame method, but the actual changes are limited to adding Javadoc comments and correcting test expectations without modifying behavior, so it does not accurately summarize the primary content of this PR. Please revise the PR title to clearly describe the actual changes, for example “Fix Connection.isSame test expectations and add Javadoc documentation” so that it reflects both the test adjustments and documentation update.
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch win-test-fix

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@christiangoerdes christiangoerdes linked an issue Sep 26, 2025 that may be closed by this pull request
@membrane-ci-server
Copy link

This pull request needs "/ok-to-test" from an authorized committer.

@christiangoerdes christiangoerdes changed the title Connection fixes for windows Enhance Connection.isSame (#2128) Sep 26, 2025
@christiangoerdes christiangoerdes marked this pull request as ready for review September 26, 2025 11:26
@christiangoerdes
Copy link
Collaborator Author

/ok-to-test

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e67e1fc and ce14d84.

📒 Files selected for processing (1)
  • core/src/main/java/com/predic8/membrane/core/transport/http/Connection.java (1 hunks)

Comment on lines 161 to 169
public boolean isSame(String host, int port) {
if (socket == null || port != socket.getPort()) return false;
try {
InetAddress target = InetAddress.getByName(host);
InetAddress remote = ((InetSocketAddress) socket.getRemoteSocketAddress()).getAddress();
return target.equals(remote) || (target.isLoopbackAddress() && remote.isLoopbackAddress());
} catch (UnknownHostException e) {
return host.equals(this.host);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Do not short-circuit on socket.getPort() when a proxy is involved.

When this connection was established via an explicit HTTP proxy (see proxyConfiguration), the socket is connected to the proxy host/port while this.host still holds the original target. In that scenario isSame() is invoked with the target port (e.g. 80) but socket.getPort() returns the proxy port (e.g. 3128). The new early return therefore always yields false, preventing pooled proxy connections from ever being reused. Please amend the logic so that proxy-backed connections continue to match (e.g. only enforce the port check when no proxy is configured, or compare against the proxy port accordingly) before returning false.

🤖 Prompt for AI Agents
In core/src/main/java/com/predic8/membrane/core/transport/http/Connection.java
around lines 161 to 169, the method short-circuits by comparing the passed port
to socket.getPort() which breaks reuse when the socket is connected to an
explicit HTTP proxy (socket port = proxy port, while the method is called with
the original target port). Update the logic to account for proxyConfiguration:
only enforce the socket port check when no proxy is configured (or compare
against the proxy's configured port when proxyConfiguration is present), and
otherwise proceed to the host/address comparison so proxy-backed connections can
still match and be reused.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
core/src/main/java/com/predic8/membrane/core/transport/http/Connection.java (1)

161-170: Optional: make host comparison case-insensitive.

Hostnames are case-insensitive. Consider equalsIgnoreCase to avoid surprising mismatches.

-        return host.equals(this.host) && port == this.port;
+        return host != null && host.equalsIgnoreCase(this.host) && port == this.port;
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ce14d84 and 6f094c8.

📒 Files selected for processing (2)
  • core/src/main/java/com/predic8/membrane/core/transport/http/Connection.java (1 hunks)
  • core/src/test/java/com/predic8/membrane/core/transport/http/ConnectionTest.java (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Automated tests
  • GitHub Check: Analyze (java)
🔇 Additional comments (2)
core/src/test/java/com/predic8/membrane/core/transport/http/ConnectionTest.java (1)

55-56: LGTM: assertions now match exact host strings.

These align with the current isSame semantics that compare host strings directly.

Consider adding a test for the explicit HTTP proxy case (no tunnel) to ensure pooled connections remain reusable behind a proxy once isSame compares against the original target port.

core/src/main/java/com/predic8/membrane/core/transport/http/Connection.java (1)

161-170: Fix isSame for explicit HTTP proxy: compare against the original target port (not socket.getPort()).

Plain HTTP via an explicit proxy connects the socket to the proxy port (e.g., 3128) while isSame() is called with the origin port (e.g., 80). Comparing against socket.getPort() prevents pooled connections from being reused. Compare against the originally requested target port instead, and align the Javadoc.

Apply this diff within this method and Javadoc:

-    /**
-     * Checks whether the (host,port) combination matches this combination.
-     *
-     * This does not directly check whether the same proxy is used, but that is actually irrelevant, since
-     * proxyConfiguration is constant per HttpClient and ergo per ConnectionManager. Since only Connections
-     * opened by the same HttpClient are managed by the same ConnectionManager, this is sufficient.
-     */
-    public boolean isSame(String host, int port) {
-        return socket != null && host.equals(this.host) && port == socket.getPort();
-    }
+    /**
+     * Checks whether the (host, port) pair matches the original target of this connection.
+     *
+     * Note: When an explicit HTTP proxy is used without tunneling, the underlying socket is connected
+     * to the proxy. We therefore compare against the originally requested target port captured at
+     * connect-time, not {@code socket.getPort()}.
+     */
+    public boolean isSame(String host, int port) {
+        if (socket == null) return false;
+        return host.equals(this.host) && port == this.port;
+    }

Additional changes required outside this hunk (constructor and field):

// 1) Add a field to store the original target port (near the existing 'host' field):
public final int port;

// 2) Change the constructor to accept and store 'port':
private Connection(ConnectionManager mgr, String host, int port, @Nullable SSLProvider sslProvider,
                   @Nullable String sniServerName, @Nullable ProxyConfiguration proxy,
                   SSLProvider proxySSLProvider) {
    this.mgr = mgr;
    this.host = host;
    this.port = port;
    this.sslProvider = sslProvider;
    this.sniServerName = sniServerName;
    this.proxyConfiguration = proxy;
    this.proxySSLProvider = proxySSLProvider;
}

// 3) Update the instantiation in open(...):
// before:
Connection con = new Connection(mgr, host, sslProvider, sniServername, proxy, proxySSLProvider);
// after (note: at this point 'host' and 'port' are still the original target values):
Connection con = new Connection(mgr, host, port, sslProvider, sniServername, proxy, proxySSLProvider);

Run to find and update all constructor call sites (should be only one, but confirm), and to review all usages of isSame:

#!/bin/bash
set -euo pipefail

echo "Constructor call sites:"
rg -nP -C2 '(?<![A-Za-z0-9_])new\s+Connection\s*\('

echo
echo "isSame call sites (context):"
rg -nP -C3 '(?<![A-Za-z0-9_])isSame\s*\('

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.

ConnectionTest.testIsSame() fail on Windows

2 participants