forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebDownloader.cs
More file actions
278 lines (223 loc) · 10.3 KB
/
Copy pathWebDownloader.cs
File metadata and controls
278 lines (223 loc) · 10.3 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
namespace Microsoft.PackageManagement.Providers.Internal
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net;
using System.Globalization;
#if CORECLR
using System.Net.Http;
#endif
using System.Threading;
using System.Threading.Tasks;
using PackageManagement.Internal;
using PackageManagement.Internal.Implementation;
using PackageManagement.Internal.Utility.Extensions;
public class WebDownloader {
internal static string ProviderName = "WebDownloader";
private static readonly Dictionary<string, string[]> _features = new Dictionary<string, string[]> {
{Constants.Features.SupportedSchemes, new[] {"http", "https", "ftp", "file"}},
};
/// <summary>
/// Returns a collection of strings to the client advertizing features this provider supports.
/// </summary>
/// <param name="request">
/// An object passed in from the CORE that contains functions that can be used to interact with
/// the CORE and HOST
/// </param>
public void GetFeatures(Request request) {
if (request == null) {
throw new ArgumentNullException("request");
}
// Nice-to-have put a debug message in that tells what's going on.
request.Debug("Calling '{0}::GetFeatures' ", ProviderName);
foreach (var feature in _features) {
request.Yield(feature);
}
}
public void InitializeProvider(Request request) {
}
public string GetDownloaderName() {
return ProviderName;
}
public string DownloadFile(Uri remoteLocation, string localFilename, int timeoutMilliseconds, bool showProgress, Request request) {
if (request == null) {
throw new ArgumentNullException("request");
}
if (remoteLocation == null) {
throw new ArgumentNullException("remoteLocation");
}
request.Debug("Calling 'WebDownloader::DownloadFile' '{0}','{1}','{2}','{3}'", remoteLocation, localFilename, timeoutMilliseconds, showProgress);
if (remoteLocation.Scheme.ToLowerInvariant() != "http" && remoteLocation.Scheme.ToLowerInvariant() != "https" && remoteLocation.Scheme.ToLowerInvariant() != "ftp") {
request.Error(ErrorCategory.InvalidResult, remoteLocation.ToString(), Constants.Messages.SchemeNotSupported, remoteLocation.Scheme);
return null;
}
if (localFilename == null) {
localFilename = FilesystemExtensions.GenerateTemporaryFileOrDirectoryNameInTempDirectory();
}
localFilename = Path.GetFullPath(localFilename);
// did the caller pass us a directory name?
if (Directory.Exists(localFilename)) {
localFilename = Path.Combine(localFilename, "downloadedFile.tmp");
}
// make sure that the parent folder is created first.
var folder = Path.GetDirectoryName(localFilename);
if (!Directory.Exists(folder)) {
Directory.CreateDirectory(folder);
}
// clobber an existing file if it's already there.
// todo: in the future, we could check the md5 of the file and if the remote server supports it
// todo: we could skip the download.
if (File.Exists(localFilename)) {
localFilename.TryHardToDelete();
}
// setup the progress tracker if the caller wanted one.
int pid = 0;
if (showProgress) {
pid = request.StartProgress(0, "Downloading '{0}'", remoteLocation);
}
#if CORECLR
var task = Download(remoteLocation, localFilename, showProgress, request, pid);
task.Wait(timeoutMilliseconds);
if (!task.IsCompleted)
{
request.Warning(Constants.Status.TimedOut);
request.Debug("Timed out downloading '{0}'", remoteLocation.AbsoluteUri);
}
#else
var webClient = new WebClient();
// Mozilla/5.0 is the general token that says the browser is Mozilla compatible, and is common to almost every browser today.
webClient.Headers.Add("User-Agent", "Mozilla/5.0 PackageManagement");
if (request.WebProxy != null)
{
// if user supplies webproxy, use that
webClient.Proxy = request.WebProxy;
}
else
{
// get ie settings
webClient.Proxy = WebRequest.GetSystemWebProxy();
// set credentials to be user credentials
webClient.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
}
var done = new ManualResetEvent(false);
webClient.DownloadFileCompleted += (sender, args) => {
if (args.Cancelled || args.Error != null) {
localFilename = null;
}
done.Set();
};
var lastPercent = 0;
if (showProgress) {
webClient.DownloadProgressChanged += (sender, args) => {
// Progress(requestObject, 2, (int)percent, "Downloading {0} of {1} bytes", args.BytesReceived, args.TotalBytesToReceive);
var percent = (int)((args.BytesReceived*100)/args.TotalBytesToReceive);
if (percent > lastPercent) {
lastPercent = percent;
request.Progress(pid, (int)((args.BytesReceived*100)/args.TotalBytesToReceive), "To {0}", localFilename);
}
};
}
// start the download
webClient.DownloadFileAsync(remoteLocation, localFilename);
// wait for the completion
if (timeoutMilliseconds > 0) {
if (!done.WaitOne(timeoutMilliseconds))
{
webClient.CancelAsync();
request.Warning(Constants.Status.TimedOut);
request.Debug("Timed out downloading '{0}'", remoteLocation.AbsoluteUri);
return null;
}
} else {
// wait until it completes or fails on it's own
done.WaitOne();
}
#endif
// if we don't have the file by this point, we've failed.
if (localFilename == null || !File.Exists(localFilename)) {
request.CompleteProgress(pid, false);
request.Warning(Constants.Messages.UnableToDownload, remoteLocation.ToString(), localFilename);
return null;
}
if (showProgress) {
request.CompleteProgress(pid, true);
}
return localFilename;
}
#if CORECLR
private async Task<string> Download(Uri remoteLocation, string localFilename, bool showProgress, Request request, int pid)
{
var clientHandler = new HttpClientHandler();
clientHandler.UseDefaultCredentials = true;
// if user supplies web proxy, use that
if (request.WebProxy != null)
{
clientHandler.Proxy = request.WebProxy;
}
var httpClient = new HttpClient(clientHandler);
request.Debug("Calling httpclient with remotelocation {0}", remoteLocation.AbsoluteUri);
// Mozilla/5.0 is the general token that says the browser is Mozilla compatible, and is common to almost every browser today.
httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 PackageManagement");
httpClient.DefaultRequestHeaders.ExpectContinue = false;
long totalBytesToReceive = 0L;
HttpResponseMessage response = await httpClient.GetAsync(remoteLocation, HttpCompletionOption.ResponseHeadersRead);
if (!response.IsSuccessStatusCode)
{
return null;
}
if (response.Content != null && response.Content.Headers != null)
{
totalBytesToReceive = response.Content.Headers.ContentLength ?? 0;
}
try
{
Stream input = await response.Content.ReadAsStreamAsync();
byte[] bytes = new byte[1024 * 4];
FileStream output = File.Open(localFilename, FileMode.OpenOrCreate);
long totalDownloaded = 0;
int current = 0;
int lastPercent = 0;
current = await input.ReadAsync(bytes, 0, bytes.Length);
while (current > 0)
{
totalDownloaded += current;
await output.WriteAsync(bytes, 0, current);
if (showProgress && totalBytesToReceive != 0)
{
int percent = (int)((totalDownloaded * 100) / totalBytesToReceive);
if (percent > lastPercent)
{
lastPercent = percent;
request.Progress(pid, (int)percent, "To {0}", localFilename);
}
}
current = await input.ReadAsync(bytes, 0, bytes.Length);
}
input.Dispose();
output.Dispose();
}
catch (Exception e)
{
request.Debug(e.Message);
localFilename = null;
}
return localFilename;
}
#endif
}
}