Skip to content

Support standard HTTP_PROXY, HTTPS_PROXY, and NO_PROXY environment variables#2631

Open
kavix wants to merge 1 commit into
ballerina-platform:masterfrom
kavix:feature/proxy-env-var-support
Open

Support standard HTTP_PROXY, HTTPS_PROXY, and NO_PROXY environment variables#2631
kavix wants to merge 1 commit into
ballerina-platform:masterfrom
kavix:feature/proxy-env-var-support

Conversation

@kavix

@kavix kavix commented Jun 12, 2026

Copy link
Copy Markdown

This PR adds support for the industry-standard HTTP_PROXY, HTTPS_PROXY, and NO_PROXY environment variables in the Ballerina HTTP client. Resolves ballerina-platform/ballerina-lang#44389.

Summary

This pull request adds support for standard HTTP proxy environment variables (HTTP_PROXY, HTTPS_PROXY, and NO_PROXY) to the Ballerina HTTP client. The implementation enables automatic proxy detection and configuration from environment variables, eliminating the need for proxy settings to be embedded in application code. This enhances deployment flexibility, particularly in environments that rely on system or container-level proxy configuration.

Changes

HttpUtil.java

  • Extended the populateSenderConfigurations method to accept an additional targetHost parameter, enabling host-aware proxy resolution
  • Implemented proxy resolution logic that checks environment variables (HTTP_PROXY, HTTPS_PROXY) for proxy configuration
  • Added helper methods to parse proxy URLs with automatic scheme handling and to evaluate NO_PROXY bypass rules
  • Implemented bypass matching logic supporting exact host matches (case-insensitive), wildcard patterns, and domain suffix matching with dot-prefix support
  • Added error handling for invalid proxy URL formats and host resolution failures

CreateSimpleHttpClient.java

  • Updated the call to populateSenderConfigurations to pass the target host extracted from the connection URL, enabling the proxy configuration logic to make host-specific decisions

ProxyEnvVarTest.java

  • Added comprehensive unit tests validating the proxy bypass logic and proxy URL parsing functionality

@CLAassistant

CLAassistant commented Jun 12, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds support for standard HTTP_PROXY, HTTPS_PROXY, and NO_PROXY environment variables to the Ballerina HTTP client. The implementation extends proxy configuration logic with environment-aware resolution, NO_PROXY bypass matching, and proper proxy URL parsing with error handling.

Changes

HTTP Proxy Environment Variable Support

Layer / File(s) Summary
Proxy resolution helpers and imports
native/src/main/java/io/ballerina/stdlib/http/api/HttpUtil.java
Adds MalformedURLException import and three helper methods: resolveProxyFromEnv(scheme, targetHost) reads HTTP_PROXY/HTTPS_PROXY environment variables and applies NO_PROXY bypass checks via shouldBypassProxy; shouldBypassProxy validates hostnames against bypass patterns (exact, case-insensitive, wildcard *, dot-prefixed suffix); parseProxyUrl extracts proxy host/port/credentials and defaults scheme to http://.
Signature update and proxy resolution integration
native/src/main/java/io/ballerina/stdlib/http/api/HttpUtil.java
populateSenderConfigurations signature now accepts targetHost parameter; proxy resolution branch calls resolveProxyFromEnv(scheme, targetHost) to apply environment-driven proxy configuration conditionally.
Client endpoint caller update
native/src/main/java/io/ballerina/stdlib/http/api/client/endpoint/CreateSimpleHttpClient.java
CreateSimpleHttpClient.createSimpleHttpClient now passes url.getHost() as the targetHost argument to populateSenderConfigurations.
Proxy environment variable tests
native/src/test/java/io/ballerina/stdlib/http/api/ProxyEnvVarTest.java
testShouldBypassProxy asserts bypass matching for exact/case-insensitive hosts, wildcard, suffix patterns, and comma-delimited lists; testParseProxyUrl asserts parsing of schemes, credentials, and default ports across various input formats.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related issues

Poem

🐰 A proxy hops through env vars, smart and spry,
NO_PROXY says "this host? we'll bypass!"
Schemes and schemes are parsed with care,
Now clients configure without despair.
Standards dance—HTTP_PROXY takes the stage! 🎪

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 11.11% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive The PR description is minimal but relevant, mentioning the feature and linking to the resolved issue. However, it does not follow the provided template structure with Purpose, Examples, and Checklist sections. Expand the description to include the Purpose section (explaining why this change matters), Examples section (demonstrating usage), and a completed Checklist confirming linked issue, changelog updates, tests added, and compatibility checks.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding support for standard HTTP_PROXY, HTTPS_PROXY, and NO_PROXY environment variables in the HTTP client.
Linked Issues check ✅ Passed The PR code changes fully implement the objectives from issue #44389: HTTP_PROXY/HTTPS_PROXY support via scheme-based proxy resolution and NO_PROXY bypass logic with wildcard and suffix matching.
Out of Scope Changes check ✅ Passed All code changes are focused on proxy environment variable support as specified in the linked issue. No unrelated modifications are present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@sonarqubecloud

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
native/src/test/java/io/ballerina/stdlib/http/api/ProxyEnvVarTest.java (1)

55-84: ⚡ Quick win

Add a negative-path assertion for invalid proxy URLs.

Line 55 introduces parse tests, but Line 84 ends without covering the documented error-handling branch (Invalid proxy URL). Add at least one malformed input assertion to lock down exception behavior and prevent regressions.

Proposed test addition
 `@Test`
 public void testParseProxyUrl() {
@@
     ProxyServerConfiguration config5 = HttpUtil.parseProxyUrl("https://proxy.example.com");
     Assert.assertEquals(config5.getProxyHost(), "proxy.example.com");
     Assert.assertEquals(config5.getProxyPort(), 443);
+
+    Assert.expectThrows(BallerinaConnectorException.class,
+            () -> HttpUtil.parseProxyUrl("http://:invalid"));
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@native/src/test/java/io/ballerina/stdlib/http/api/ProxyEnvVarTest.java`
around lines 55 - 84, Add a negative-path assertion for HttpUtil.parseProxyUrl
by verifying it rejects malformed proxy URLs (e.g., "http://:8080" or "not a
url") and raises the documented Invalid proxy URL error; update the
ProxyEnvVarTest (either extend testParseProxyUrl or add a new test like
testParseProxyUrlInvalid) to assert that calling HttpUtil.parseProxyUrl with an
invalid string throws the expected exception (e.g., IllegalArgumentException) so
the invalid-URL branch is covered.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@native/src/main/java/io/ballerina/stdlib/http/api/HttpUtil.java`:
- Around line 1417-1419: The catch block in HttpUtil.java that throws new
BallerinaConnectorException("Invalid proxy URL: " + proxyUrl, e) can leak
embedded credentials in proxyUrl; update the code to sanitize the proxyUrl
before including it in the exception message (e.g., remove or mask URI
user-info) and use the sanitized string in the thrown
BallerinaConnectorException; locate the proxy handling in HttpUtil (the catch
for MalformedURLException | UnknownHostException) and replace the direct
proxyUrl usage with a sanitizedProxyUrl produced by parsing the URL/URI and
stripping userInfo (or masking it) so sensitive credentials are not logged or
returned.

---

Nitpick comments:
In `@native/src/test/java/io/ballerina/stdlib/http/api/ProxyEnvVarTest.java`:
- Around line 55-84: Add a negative-path assertion for HttpUtil.parseProxyUrl by
verifying it rejects malformed proxy URLs (e.g., "http://:8080" or "not a url")
and raises the documented Invalid proxy URL error; update the ProxyEnvVarTest
(either extend testParseProxyUrl or add a new test like
testParseProxyUrlInvalid) to assert that calling HttpUtil.parseProxyUrl with an
invalid string throws the expected exception (e.g., IllegalArgumentException) so
the invalid-URL branch is covered.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: bd3debb4-3d00-449e-af2c-b45c38f4e80c

📥 Commits

Reviewing files that changed from the base of the PR and between 9b84a8b and a635753.

📒 Files selected for processing (3)
  • native/src/main/java/io/ballerina/stdlib/http/api/HttpUtil.java
  • native/src/main/java/io/ballerina/stdlib/http/api/client/endpoint/CreateSimpleHttpClient.java
  • native/src/test/java/io/ballerina/stdlib/http/api/ProxyEnvVarTest.java

Comment thread native/src/main/java/io/ballerina/stdlib/http/api/HttpUtil.java
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.

[New Feature]: Support Standard http_proxy/https_proxy Environment Variables for HTTP Client

2 participants