-
-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathUniversalWebClient.cs
More file actions
132 lines (106 loc) · 4.21 KB
/
UniversalWebClient.cs
File metadata and controls
132 lines (106 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Parse.Abstractions.Infrastructure;
using Parse.Abstractions.Infrastructure.Execution;
using BCLWebClient = System.Net.Http.HttpClient;
namespace Parse.Infrastructure.Execution;
/// <summary>
/// A universal implementation of <see cref="IWebClient"/>.
/// </summary>
public class UniversalWebClient : IWebClient
{
static HashSet<string> ContentHeaders { get; } = new HashSet<string>
{
{ "Allow" },
{ "Content-Disposition" },
{ "Content-Encoding" },
{ "Content-Language" },
{ "Content-Length" },
{ "Content-Location" },
{ "Content-MD5" },
{ "Content-Range" },
{ "Content-Type" },
{ "Expires" },
{ "Last-Modified" }
};
public UniversalWebClient() : this(new BCLWebClient { }) { }
public UniversalWebClient(BCLWebClient client) => Client = client;
BCLWebClient Client { get; set; }
public async Task<Tuple<HttpStatusCode, string>> ExecuteAsync(
WebRequest httpRequest,
IProgress<IDataTransferLevel> uploadProgress,
IProgress<IDataTransferLevel> downloadProgress,
CancellationToken cancellationToken)
{
uploadProgress ??= new Progress<IDataTransferLevel> { };
downloadProgress ??= new Progress<IDataTransferLevel> { };
HttpRequestMessage message = new HttpRequestMessage(new HttpMethod(httpRequest.Method), httpRequest.Target);
if ((httpRequest.Data is null && httpRequest.Method.ToLower().Equals("post")
? new MemoryStream(new byte[0])
: httpRequest.Data) is Stream { } data)
{
message.Content = new StreamContent(data);
}
if (httpRequest.Headers != null)
{
foreach (KeyValuePair<string, string> header in httpRequest.Headers)
{
if (ContentHeaders.Contains(header.Key))
{
message.Content.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
else
{
message.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
}
}
// Avoid aggressive caching on Windows Phone 8.1.
message.Headers.Add("Cache-Control", "no-cache");
message.Headers.IfModifiedSince = DateTimeOffset.UtcNow;
uploadProgress.Report(new DataTransferLevel { Amount = 0 });
HttpResponseMessage response = await Client.SendAsync(message, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
uploadProgress.Report(new DataTransferLevel { Amount = 1 });
Stream responseStream = await response.Content.ReadAsStreamAsync(cancellationToken);
MemoryStream resultStream = new MemoryStream { };
int bufferSize = 4096, bytesRead = 0;
byte[] buffer = new byte[bufferSize];
long totalLength = -1, readSoFar = 0;
try
{
totalLength = responseStream.Length;
}
catch
{
Console.WriteLine("Unsupported length...");
};
while ((bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) > 0)
{
cancellationToken.ThrowIfCancellationRequested();
await resultStream.WriteAsync(buffer, 0, bytesRead, cancellationToken);
cancellationToken.ThrowIfCancellationRequested();
readSoFar += bytesRead;
if (totalLength > -1)
{
downloadProgress.Report(new DataTransferLevel { Amount = (double) readSoFar / totalLength });
}
}
responseStream.Dispose();
if (totalLength == -1)
{
downloadProgress.Report(new DataTransferLevel { Amount = 1.0 });
}
byte[] resultAsArray = resultStream.ToArray();
resultStream.Dispose();
// Assume UTF-8 encoding.
string resultString = Encoding.UTF8.GetString(resultAsArray, 0, resultAsArray.Length);
return new Tuple<HttpStatusCode, string>(response.StatusCode, resultString);
}
}