Skip to content

Commit

Permalink
Merge pull request #1722 from newrelic/httpclient-npe
Browse files Browse the repository at this point in the history
Check HttpHost instance for null prior to dereferencing it
  • Loading branch information
jtduffy authored Feb 5, 2024
2 parents 0e8a844 + e5f0945 commit fb9a813
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package org.apache.hc.client5.http.classic;

import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
Expand All @@ -24,6 +25,7 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.logging.Level;

@Weave(type = MatchType.Interface, originalName = "org.apache.hc.client5.http.classic.HttpClient")
public class HttpClient_Instrumentation {
Expand Down Expand Up @@ -181,9 +183,17 @@ public <T> T execute(HttpHost target, ClassicHttpRequest request, HttpContext co
}

private static URI getUri(HttpHost target, HttpRequest request) throws URISyntaxException {
URI requestURI = new URI(request.getRequestUri());
String scheme = requestURI.getScheme() == null ? target.getSchemeName() : requestURI.getScheme();
return new URI(scheme, null, target.getHostName(), target.getPort(), requestURI.getPath(), null, null);
}
String path = request.getPath();

// Prefer pulling the remainder of the host info from the HttpPost instance
if (target != null) {
return new URI(target.getSchemeName(), null, target.getHostName(), target.getPort(), path, null, null);
} else { // Pull from the HttpRequest object
URI requestURI = new URI(request.getUri().toString());
String scheme = request.getScheme();
int port = requestURI.getPort() != -1 ? requestURI.getPort() :
("http".equals(scheme) ? 80 : 443);
return new URI(scheme, null, requestURI.getHost(), port, path, null, null);
}
}
}

0 comments on commit fb9a813

Please sign in to comment.