Skip to content

Commit edaf3b5

Browse files
committed
Reverted changes done to UWC file as they are not linked to Relations nor this PR
1 parent ad37f3e commit edaf3b5

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

Parse/Infrastructure/Execution/UniversalWebClient.cs

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public UniversalWebClient() : this(new BCLWebClient { }) { }
3838
public UniversalWebClient(BCLWebClient client) => Client = client;
3939

4040
BCLWebClient Client { get; set; }
41-
/// <inheritdoc/>
4241
public async Task<Tuple<HttpStatusCode, string>> ExecuteAsync(
4342
WebRequest httpRequest,
4443
IProgress<IDataTransferLevel> uploadProgress,
@@ -51,7 +50,7 @@ public async Task<Tuple<HttpStatusCode, string>> ExecuteAsync(
5150
HttpRequestMessage message = new HttpRequestMessage(new HttpMethod(httpRequest.Method), httpRequest.Target);
5251

5352
Stream data = httpRequest.Data;
54-
if (data != null || httpRequest.Method.Equals("POST", StringComparison.OrdinalIgnoreCase))
53+
if (data != null || httpRequest.Method.Equals("POST", StringComparison.OrdinalIgnoreCase))
5554
{
5655
message.Content = new StreamContent(data ?? new MemoryStream(new byte[0]));
5756
}
@@ -80,48 +79,53 @@ public async Task<Tuple<HttpStatusCode, string>> ExecuteAsync(
8079
HttpResponseMessage response = await Client.SendAsync(message, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
8180
uploadProgress.Report(new DataTransferLevel { Amount = 1 });
8281

83-
long? totalLength = response.Content.Headers.ContentLength;
82+
Stream responseStream = await response.Content.ReadAsStreamAsync(cancellationToken);
83+
84+
8485

8586
MemoryStream resultStream = new MemoryStream { };
87+
int bufferSize = 4096, bytesRead = 0;
88+
byte[] buffer = new byte[bufferSize];
89+
long totalLength = -1, readSoFar = 0;
90+
8691
try
8792
{
88-
using (var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken))
89-
{
90-
byte[] buffer = new byte[4096];
91-
int bytesRead;
92-
long readSoFar = 0;
93-
while ((bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) > 0)
94-
{
95-
cancellationToken.ThrowIfCancellationRequested();
96-
97-
await resultStream.WriteAsync(buffer, 0, bytesRead, cancellationToken);
98-
cancellationToken.ThrowIfCancellationRequested();
99-
readSoFar += bytesRead;
100-
93+
totalLength = responseStream.Length;
94+
}
95+
catch
96+
{
97+
Console.WriteLine("Unsupported length...");
98+
};
10199

102-
if (totalLength.HasValue && totalLength > 0)
103-
{
104-
downloadProgress.Report(new DataTransferLevel { Amount = (double) readSoFar / totalLength.Value });
105-
}
106100

101+
while ((bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) > 0)
102+
{
103+
cancellationToken.ThrowIfCancellationRequested();
107104

108-
}
109-
}
105+
await resultStream.WriteAsync(buffer, 0, bytesRead, cancellationToken);
106+
cancellationToken.ThrowIfCancellationRequested();
107+
readSoFar += bytesRead;
110108

111-
if (!totalLength.HasValue || totalLength <= 0)
109+
if (totalLength > -1)
112110
{
113-
downloadProgress.Report(new DataTransferLevel { Amount = 1.0 }); // Report completion if total length is unknown
111+
downloadProgress.Report(new DataTransferLevel { Amount = (double) readSoFar / totalLength });
114112
}
113+
}
115114

115+
responseStream.Dispose();
116116

117-
byte[] resultAsArray = resultStream.ToArray();
118-
string resultString = Encoding.UTF8.GetString(resultAsArray, 0, resultAsArray.Length);
119-
//think of throwing better error when login fails for non verified
120-
return new Tuple<HttpStatusCode, string>(response.StatusCode, resultString);
121-
}
122-
finally
117+
if (totalLength == -1)
123118
{
124-
resultStream.Dispose();
119+
downloadProgress.Report(new DataTransferLevel { Amount = 1.0 });
125120
}
121+
122+
byte[] resultAsArray = resultStream.ToArray();
123+
resultStream.Dispose();
124+
125+
// Assume UTF-8 encoding.
126+
string resultString = Encoding.UTF8.GetString(resultAsArray, 0, resultAsArray.Length);
127+
128+
return new Tuple<HttpStatusCode, string>(response.StatusCode, resultString);
126129
}
127-
}
130+
131+
}

0 commit comments

Comments
 (0)