Skip to content

HttpClient: omit default port 80 when rebuilding HTTP URLs#1108

Open
iroqueta wants to merge 1 commit intomasterfrom
fix/httpclient-omit-default-http-port
Open

HttpClient: omit default port 80 when rebuilding HTTP URLs#1108
iroqueta wants to merge 1 commit intomasterfrom
fix/httpclient-omit-default-http-port

Conversation

@iroqueta
Copy link
Copy Markdown
Collaborator

@iroqueta iroqueta commented Apr 29, 2026

Summary

When the original URL had no explicit port, HttpClientJavaLib was re-assembling it as http://host:80/path, which made Apache HttpClient send Host: host:80 in the request. Some servers (e.g. IIS) copy the request authority verbatim into the Location header of a redirect, producing broken redirects like https://host:80/... that fail the TLS handshake on port 80.

This change aligns the http branch with the https branch (which already omitted the default 443) so the port is only appended when it differs from the scheme default.

Background

Reproduced calling http://sdx.genexus.com/agetqrcode.aspx?.... The server replies with 301 Location: https://sdx.genexus.com:80/... because the incoming Host header contained :80. Apache HttpClient follows the redirect and tries to negotiate TLS against port 80, which fails with SSLException: Unsupported or unrecognized SSL message. The HttpClient then leaves response = null and downstream code (e.g. GXutil.ImageUrlToBytes) throws a cryptic NPE on IOUtils.toByteArray(null).

A browser hitting the same URL works because it never adds the default port to the Host header, so the server's redirect is well-formed.

Diff

-url = url.startsWith("http://") ? url : "http://" + getHost() + ":" + (getPort() == -1? "80" :getPort()) + url;
+url = url.startsWith("http://") ? url : "http://" + getHost() +  (getPort() != 80?":"+getPort(): "") + url;

Test plan

  • Hit a URL like http://sdx.genexus.com/agetqrcode.aspx?test from a proc using HttpClient and verify the wire log shows Host: sdx.genexus.com (no :80)
  • Verify the same call no longer fails with SSLException / NPE on the redirect
  • Hit a non-default port URL like http://localhost:8080/... and confirm the port is still preserved

Issue: 208470

When the original URL had no explicit port, HttpClientJavaLib was
re-assembling it as http://host:80/path, sending Host: host:80 in
the request. Some servers (e.g. IIS) copy the request authority
verbatim into the Location header of a redirect, producing broken
redirects to https://host:80/... that fail TLS handshake.

Aligns the http branch with the https branch (which already omitted
the default 443) so the port is only appended when it differs from
the scheme default.
@genexusbot
Copy link
Copy Markdown
Collaborator

Cherry pick to beta success

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.

2 participants