Skip to content

Commit

Permalink
Use a proxy server if specified
Browse files Browse the repository at this point in the history
Fixes: dotnet#2123

Implement proxy support for the Java HTTP client by "translating" the proxy
information specified in `HttpClientHandler.Proxy` to the format accepted and
expected by the Java client. Since the translation may involve a DNS lookup, it
is performed in a separate task. No caching is attempted in our client code.
  • Loading branch information
grendello committed Nov 5, 2018
1 parent 8082af4 commit 7561170
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ protected virtual IHostnameVerifier GetSSLHostnameVerifier (HttpsURLConnection c
URL java_url = new URL (EncodeUrl (redirectState.NewUrl));
URLConnection java_connection;
if (UseProxy)
java_connection = java_url.OpenConnection ();
java_connection = java_url.OpenConnection (await GetJavaProxy (redirectState.NewUrl, cancellationToken));
else
java_connection = java_url.OpenConnection (Java.Net.Proxy.NoProxy);

Expand All @@ -287,7 +287,30 @@ protected virtual IHostnameVerifier GetSSLHostnameVerifier (HttpsURLConnection c
request.Method = redirectState.Method;
}
}


protected virtual async Task <Java.Net.Proxy> GetJavaProxy (Uri destination, CancellationToken cancellationToken)
{
Java.Net.Proxy proxy = Java.Net.Proxy.NoProxy;

if (destination == null || Proxy == null) {
goto done;
}

Uri puri = Proxy.GetProxy (destination);
if (puri == null) {
goto done;
}

proxy = await Task <Java.Net.Proxy>.Run (() => {
// Let the Java code resolve the address, if necessary
var addr = new Java.Net.InetSocketAddress (puri.Host, puri.Port);
return new Java.Net.Proxy (Java.Net.Proxy.Type.Http, addr);
}, cancellationToken);

done:
return proxy;
}

Task <HttpResponseMessage> ProcessRequest (HttpRequestMessage request, URL javaUrl, HttpURLConnection httpConnection, CancellationToken cancellationToken, RequestRedirectionState redirectState)
{
cancellationToken.ThrowIfCancellationRequested ();
Expand Down

0 comments on commit 7561170

Please sign in to comment.