Skip to content

Commit 122ea39

Browse files
Accept any HttpRequest instead of HttpUriRequest in CommonHttpClient (#305)
## Changes This PR addresses a regression reportedly introduced in PR #290 when connecting via a proxy that requires authentication (see context). The problem comes from a casting exception in `CommonHttpClient` which attempts to cast a `BasicHttpRequest` (which represents the next request after receiving the auth required response) into a `HttpUriRequest`. This PR solves the casting issue by processing the ancestor of both classes, `HttpRequest`. ### Context The regression was detected when trying to authenticate via a Proxy that requires authentication. In particular, the request to respond to the proxy's authentication request is automatically created as a `BasicHttpRequest` by the apache `HttpClient`. ## Tests Unit tests and integration tests are passing. The fix was also verified by the user who uncovered the issue. Note: we should add a regression test that simulate the proxy setting. However, I'd like to take the time to experiment with different configurations. Given that the change is relatively simple, I'd recommend to proceed with this PR and add the test in a follow-up PR.
1 parent 02bb906 commit 122ea39

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

databricks-sdk-java/src/main/java/com/databricks/sdk/core/commons/CommonsHttpClient.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@
2222
import java.util.Map;
2323
import java.util.stream.Collectors;
2424
import org.apache.commons.io.IOUtils;
25-
import org.apache.http.HttpEntity;
26-
import org.apache.http.HttpHost;
27-
import org.apache.http.NameValuePair;
28-
import org.apache.http.StatusLine;
25+
import org.apache.http.*;
2926
import org.apache.http.client.config.RequestConfig;
3027
import org.apache.http.client.methods.*;
3128
import org.apache.http.entity.InputStreamEntity;
@@ -105,16 +102,17 @@ public Response execute(Request in) throws IOException {
105102
private URL getTargetUrl(HttpContext context) {
106103
try {
107104
HttpHost targetHost = (HttpHost) context.getAttribute("http.target_host");
108-
HttpUriRequest request = (HttpUriRequest) context.getAttribute("http.request");
109-
URI uri =
105+
HttpRequest request = (HttpRequest) context.getAttribute("http.request");
106+
URI uri = new URI(request.getRequestLine().getUri());
107+
uri =
110108
new URI(
111109
targetHost.getSchemeName(),
112110
null,
113111
targetHost.getHostName(),
114112
targetHost.getPort(),
115-
request.getURI().getPath(),
116-
request.getURI().getQuery(),
117-
request.getURI().getFragment());
113+
uri.getPath(),
114+
uri.getQuery(),
115+
uri.getFragment());
118116
return uri.toURL();
119117
} catch (MalformedURLException | URISyntaxException e) {
120118
throw new DatabricksException("Unable to get target URL", e);

0 commit comments

Comments
 (0)