Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[aws-xray-recorder-sdk-apache-http] Don't return a null target to avoid possible NPE #338

Merged
merged 1 commit into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,11 @@ public TracedHttpClient(
}

public static HttpHost determineTarget(final HttpUriRequest request) throws ClientProtocolException {
// A null target may be acceptable if there is a default target.
// Otherwise, the null target is detected in the director.
HttpHost target = null;

final URI requestUri = request.getURI();
if (requestUri.isAbsolute()) {
target = URIUtils.extractHost(requestUri);
if (target == null) {
throw new ClientProtocolException("URI does not specify a valid host name: "
+ requestUri);
}
HttpHost target = URIUtils.extractHost(requestUri);
if (target == null) {
throw new ClientProtocolException("URI does not specify a valid host name: "
+ requestUri);
}
return target;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import com.amazonaws.xray.AWSXRay;
import com.amazonaws.xray.entities.Segment;
Expand All @@ -34,6 +36,7 @@
import com.amazonaws.xray.entities.TraceID;
import com.github.tomakehurst.wiremock.junit.WireMockClassRule;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
Expand Down Expand Up @@ -113,4 +116,20 @@ public void unsampledButForcedPropagation() throws Exception {
.withHeader(TraceHeader.HEADER_KEY,
equalTo("Root=1-67891233-abcdef012345678912345678;Sampled=0")));
}

@Test
public void testExceptionOnRelativeUrl() throws Exception {
stubFor(any(anyUrl()).willReturn(ok()));

TraceID traceID = TraceID.fromString("1-67891233-abcdef012345678912345678");
AWSXRay.beginSegment("test", traceID, null);
Exception exception = assertThrows(ClientProtocolException.class, () -> {
client.execute(new HttpGet("/hello/world/"));
});
AWSXRay.endSegment();

String expectedMessage = "URI does not specify a valid host name:";
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(expectedMessage));
}
}