Skip to content

Commit

Permalink
Add Lazy cache for originalhost (#1766)
Browse files Browse the repository at this point in the history
* Lazy cache originalhost

* Update toString function

* Update unittests

---------

Co-authored-by: $(git --no-pager log --format=format:'%an' -n 1) <$(git --no-pager log --format=format:'%ae' -n 1)>
  • Loading branch information
karim-z authored Apr 30, 2024
1 parent a9fe1ca commit 28d7f60
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public class HttpRequestMessageImpl implements HttpRequestMessage {
private String reconstructedUri = null;
private String pathAndQuery = null;
private String infoForLogging = null;
private String originalHost = null;

private static final SocketAddress UNDEFINED_CLIENT_DEST_ADDRESS = new SocketAddress() {
@Override
Expand Down Expand Up @@ -510,7 +511,10 @@ protected String generateInfoForLogging() {
@Override
public String getOriginalHost() {
try {
return getOriginalHost(getHeaders(), getServerName());
if (originalHost == null) {
originalHost = getOriginalHost(getHeaders(), getServerName());
}
return originalHost;
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
Expand Down Expand Up @@ -662,7 +666,7 @@ protected String _reconstructURI() {

String scheme = getOriginalScheme().toLowerCase();
uri.append(scheme);
uri.append(URI_SCHEME_SEP).append(getOriginalHost(getHeaders(), getServerName()));
uri.append(URI_SCHEME_SEP).append(getOriginalHost());

int port = getOriginalPort();
if ((URI_SCHEME_HTTP.equals(scheme) && 80 == port) || (URI_SCHEME_HTTPS.equals(scheme) && 443 == port)) {
Expand All @@ -674,7 +678,7 @@ protected String _reconstructURI() {
uri.append(getPathAndQuery());

return uri.toString();
} catch (URISyntaxException e) {
} catch (IllegalArgumentException e) {
// This is not really so bad, just debug log it and move on.
LOG.debug("Error reconstructing request URI!", e);
return "";
Expand All @@ -700,7 +704,8 @@ public String toString() {
+ inboundRequest + ", parsedCookies="
+ parsedCookies + ", reconstructedUri='"
+ reconstructedUri + '\'' + ", pathAndQuery='"
+ pathAndQuery + '\'' + ", infoForLogging='"
+ pathAndQuery + '\'' + "/ originalHost='"
+ originalHost + '\'' + ", infoForLogging='"
+ infoForLogging + '\'' + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,34 @@ void testPathAndQuery_immutable() {
assertEquals("/blah", request.getPathAndQuery());
}

@Test
void testGetOriginalHost_immutable() {
HttpQueryParams queryParams = new HttpQueryParams();
Headers headers = new Headers();
headers.add("Host", "blah.netflix.com");
request = new HttpRequestMessageImpl(
new SessionContext(),
"HTTP/1.1",
"POST",
"/some/where",
queryParams,
headers,
"192.168.0.2",
"https",
7002,
"localhost",
new SocketAddress() {},
true);

// Check it's the same value 2nd time.
assertEquals("blah.netflix.com", request.getOriginalHost());
assertEquals("blah.netflix.com", request.getOriginalHost());

// Update the Host header value and ensure the result didn't change.
headers.set("Host", "testOriginalHost2");
assertEquals("blah.netflix.com", request.getOriginalHost());
}

@Test
void testGetOriginalHost() {
HttpQueryParams queryParams = new HttpQueryParams();
Expand Down

0 comments on commit 28d7f60

Please sign in to comment.