Skip to content

Commit

Permalink
[http-client] Fix NPE on attempt to execute HTTP request with URI hav…
Browse files Browse the repository at this point in the history
…ing invalid protocol (#5217)
  • Loading branch information
valfirst authored Jul 15, 2024
1 parent e8a7d5b commit 6df2444
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,12 @@ private HttpResponse execute(ClassicHttpRequest request, HttpClientContext conte

HttpClientContext internalContext = Optional.ofNullable(context).orElseGet(HttpClientContext::create);

String userInfo = request.getAuthority().getUserInfo();
if (userInfo != null)
Optional.ofNullable(request.getAuthority()).map(URIAuthority::getUserInfo).ifPresent(userInfo ->
{
HttpHost host = RoutingSupport.normalize(HttpHost.create(uri), DefaultSchemePortResolver.INSTANCE);
configureBasicAuth(usePreemptiveBasicAuthIfAvailable, userInfo, host, internalContext);
request.setAuthority(new URIAuthority(host));
}
});

HttpClientResponseHandler<HttpResponse> responseHandler = response -> {
HttpResponse httpResponse = new HttpResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@
import java.util.Base64;
import java.util.List;

import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.AuthenticationException;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpHead;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.client5.http.protocol.RedirectLocations;
import org.apache.hc.core5.http.ClassicHttpResponse;
Expand Down Expand Up @@ -243,6 +245,15 @@ void shouldDoHttpGetAndSaveRedirects() throws IOException
verify(handler).handle(httpResponse);
}

@Test
void shouldThrowClientProtocolExceptionOnUnsupportedUrl()
{
httpClient.setCloseableHttpClient(HttpClients.createDefault());
var invalidUrl = URI.create("file:///C:/Users/77805/Downloads/privacy@abc.com");
var exception = assertThrows(ClientProtocolException.class, () -> httpClient.doHttpGet(invalidUrl));
assertEquals("Target host is not specified", exception.getMessage());
}

private ArgumentMatcher<HttpClientResponseHandler<HttpResponse>> responseHandlerMatcher(String httpMethod,
int statusCode, HttpEntity httpEntity)
{
Expand Down

0 comments on commit 6df2444

Please sign in to comment.