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

[Mono.Android] Fix race condition in AndroidMessageHandler #8753

Merged
Changes from 1 commit
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
76 changes: 41 additions & 35 deletions src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,14 @@ public void Reset ()
DecompressionMethods _decompressionMethods;

bool disposed;
bool started;

// Now all hail Java developers! Get this... HttpURLClient defaults to accepting AND
// uncompressing the gzip content encoding UNLESS you set the Accept-Encoding header to ANY
// value. So if we set it to 'gzip' below we WILL get gzipped stream but HttpURLClient will NOT
// uncompress it any longer, doh. And they don't support 'deflate' so we need to handle it ourselves.
bool decompress_here;
string? _acceptEncoding;

public bool SupportsAutomaticDecompression => true;
public bool SupportsProxy => true;
Expand All @@ -152,7 +154,33 @@ public void Reset ()
public DecompressionMethods AutomaticDecompression
{
get => _decompressionMethods;
set => _decompressionMethods = value;
set
{
CheckDisposedOrStarted ();

_decompressionMethods = value;
_acceptEncoding = null;
decompress_here = false;

if (value == DecompressionMethods.None) {
_acceptEncoding = IDENTITY_ENCODING;
} else {
if ((value & DecompressionMethods.GZip) != 0) {
_acceptEncoding = GZIP_ENCODING;
decompress_here = true;
}

if ((value & DecompressionMethods.Deflate) != 0) {
_acceptEncoding = _acceptEncoding is null ? DEFLATE_ENCODING : $"{_acceptEncoding}, {DEFLATE_ENCODING}";
decompress_here = true;
}

if ((value & DecompressionMethods.Brotli) != 0) {
_acceptEncoding = _acceptEncoding is null ? BROTLI_ENCODING : $"{_acceptEncoding}, {BROTLI_ENCODING}";
decompress_here = true;
}
}
}
}

public CookieContainer CookieContainer
Expand Down Expand Up @@ -334,7 +362,7 @@ public bool RequestNeedsAuthorization {

protected override void Dispose (bool disposing)
{
disposed = true;
disposed = true;

base.Dispose (disposing);
}
Expand All @@ -346,6 +374,14 @@ protected void AssertSelf ()
throw new ObjectDisposedException (nameof (AndroidMessageHandler));
}

void CheckDisposedOrStarted ()
{
AssertSelf ();
if (started) {
throw new InvalidOperationException ("The handler has already started sending requests");
}
}

string EncodeUrl (Uri url)
{
if (url == null)
Expand Down Expand Up @@ -407,6 +443,7 @@ string EncodeUrl (Uri url)

internal async Task <HttpResponseMessage> DoSendAsync (HttpRequestMessage request, CancellationToken cancellationToken)
{
started = true;
AssertSelf ();
if (request == null)
throw new ArgumentNullException (nameof (request));
Expand Down Expand Up @@ -1051,15 +1088,6 @@ internal Task SetupRequestInternal (HttpRequestMessage request, HttpURLConnectio
internal TrustManagerFactory? ConfigureTrustManagerFactoryInternal (KeyStore? keyStore)
=> ConfigureTrustManagerFactory (keyStore);

void AppendEncoding (string encoding, ref List <string>? list)
{
if (list == null)
list = new List <string> ();
if (list.Contains (encoding))
return;
list.Add (encoding);
}

async Task <HttpURLConnection> SetupRequestInternal (HttpRequestMessage request, URLConnection conn)
{
if (conn == null)
Expand All @@ -1081,30 +1109,8 @@ void AppendEncoding (string encoding, ref List <string>? list)
AddHeaders (httpConnection, request.Content.Headers);
AddHeaders (httpConnection, request.Headers);

List <string>? accept_encoding = null;

decompress_here = false;
if (AutomaticDecompression == DecompressionMethods.None) {
AppendEncoding (IDENTITY_ENCODING, ref accept_encoding); // Turns off compression for the Java client
} else {
if ((AutomaticDecompression & DecompressionMethods.GZip) != 0) {
AppendEncoding (GZIP_ENCODING, ref accept_encoding);
decompress_here = true;
}

if ((AutomaticDecompression & DecompressionMethods.Deflate) != 0) {
AppendEncoding (DEFLATE_ENCODING, ref accept_encoding);
decompress_here = true;
}

if ((AutomaticDecompression & DecompressionMethods.Brotli) != 0) {
AppendEncoding (BROTLI_ENCODING, ref accept_encoding);
decompress_here = true;
}
}

if (accept_encoding?.Count > 0)
httpConnection.SetRequestProperty ("Accept-Encoding", String.Join (",", accept_encoding));
if (_acceptEncoding is not null)
httpConnection.SetRequestProperty ("Accept-Encoding", _acceptEncoding);

if (UseCookies && CookieContainer != null && request.RequestUri is not null) {
string cookieHeaderValue = CookieContainer.GetCookieHeader (request.RequestUri);
Expand Down
Loading