1
- $useTimeOutWebClient = $false
2
- if ($PSVersionTable.PSVersion -lt " 6.0.0" -or $useTimeOutWebClient ) {
3
- $timeoutWebClientCode = @"
4
- using System.Net;
5
-
6
- public class TimeoutWebClient : WebClient
7
- {
8
- int theTimeout;
9
-
10
- public TimeoutWebClient(int timeout)
11
- {
12
- theTimeout = timeout;
13
- }
14
-
15
- protected override WebRequest GetWebRequest(System.Uri address)
16
- {
17
- WebRequest request = base.GetWebRequest(address);
18
- if (request != null)
19
- {
20
- request.Timeout = theTimeout;
21
- }
22
- return request;
23
- }
24
- }
25
- "@ ;
26
- if (-not ([System.Management.Automation.PSTypeName ]" TimeoutWebClient" ).Type) {
27
- Add-Type - TypeDefinition $timeoutWebClientCode - Language CSharp - WarningAction SilentlyContinue | Out-Null
28
- $useTimeOutWebClient = $true
29
- }
30
- }
31
-
32
- $sslCallbackCode = @"
1
+ $sslCallbackCode = @"
33
2
using System.Net.Security;
34
3
using System.Security.Cryptography.X509Certificates;
35
4
@@ -967,27 +936,6 @@ function GetHash {
967
936
(Get-FileHash - InputStream $stream - Algorithm SHA256).Hash
968
937
}
969
938
970
- function Wait-Task {
971
- param (
972
- [Parameter (Mandatory , ValueFromPipeline )]
973
- [System.Threading.Tasks.Task []]$Task
974
- )
975
-
976
- Begin {
977
- $Tasks = @ ()
978
- }
979
-
980
- Process {
981
- $Tasks += $Task
982
- }
983
-
984
- End {
985
- While (-not [System.Threading.Tasks.Task ]::WaitAll($Tasks , 200 )) {}
986
- $Tasks.ForEach ( { $_.GetAwaiter ().GetResult() })
987
- }
988
- }
989
- Set-Alias - Name await - Value Wait-Task - Force
990
-
991
939
function DownloadFileLow {
992
940
Param (
993
941
[string ] $sourceUrl ,
@@ -999,71 +947,41 @@ function DownloadFileLow {
999
947
[int ] $timeout = 100
1000
948
)
1001
949
1002
- if ($useTimeOutWebClient ) {
1003
- Write-Host " Downloading using WebClient"
1004
- if ($skipCertificateCheck ) {
1005
- Write-Host " Disabling SSL Verification"
1006
- [SslVerification ]::Disable()
1007
- }
1008
- $webClient = New-Object TimeoutWebClient - ArgumentList (1000 * $timeout )
1009
- $headers.Keys | ForEach-Object {
1010
- $webClient.Headers.Add ($_ , $headers ." $_ " )
1011
- }
1012
- $webClient.UseDefaultCredentials = $useDefaultCredentials
1013
- if (Test-Path $destinationFile - PathType Leaf) {
1014
- if ($dontOverwrite ) {
1015
- return
1016
- }
1017
- Remove-Item - Path $destinationFile - Force
1018
- }
1019
- try {
1020
- $webClient.DownloadFile ($sourceUrl , $destinationFile )
1021
- }
1022
- finally {
1023
- $webClient.Dispose ()
1024
- if ($skipCertificateCheck ) {
1025
- Write-Host " Restoring SSL Verification"
1026
- [SslVerification ]::Enable()
1027
- }
1028
- }
950
+ $handler = New-Object System.Net.Http.HttpClientHandler
951
+ if ($skipCertificateCheck ) {
952
+ Write-Host " Disabling SSL Verification on HttpClient"
953
+ [SslVerification ]::DisableSsl($handler )
954
+ }
955
+ if ($useDefaultCredentials ) {
956
+ $handler.UseDefaultCredentials = $true
957
+ }
958
+ $httpClient = New-Object System.Net.Http.HttpClient - ArgumentList $handler
959
+ $httpClient.Timeout = [Timespan ]::FromSeconds($timeout )
960
+ $headers.Keys | ForEach-Object {
961
+ $httpClient.DefaultRequestHeaders.Add ($_ , $headers ." $_ " )
962
+ }
963
+ $stream = $null
964
+ $fileStream = $null
965
+ if ($dontOverwrite ) {
966
+ $fileMode = [System.IO.FileMode ]::CreateNew
1029
967
}
1030
968
else {
1031
- Write-Host " Downloading using HttpClient"
1032
-
1033
- $handler = New-Object System.Net.Http.HttpClientHandler
1034
- if ($skipCertificateCheck ) {
1035
- Write-Host " Disabling SSL Verification on HttpClient"
1036
- [SslVerification ]::DisableSsl($handler )
1037
- }
1038
- if ($useDefaultCredentials ) {
1039
- $handler.UseDefaultCredentials = $true
1040
- }
1041
- $httpClient = New-Object System.Net.Http.HttpClient - ArgumentList $handler
1042
- $httpClient.Timeout = [Timespan ]::FromSeconds($timeout )
1043
- $headers.Keys | ForEach-Object {
1044
- $httpClient.DefaultRequestHeaders.Add ($_ , $headers ." $_ " )
1045
- }
1046
- $stream = $null
1047
- $fileStream = $null
1048
- if ($dontOverwrite ) {
1049
- $fileMode = [System.IO.FileMode ]::CreateNew
1050
- }
1051
- else {
1052
- $fileMode = [System.IO.FileMode ]::Create
969
+ $fileMode = [System.IO.FileMode ]::Create
970
+ }
971
+ try {
972
+ $stream = $httpClient.GetStreamAsync ($sourceUrl ).GetAwaiter().GetResult()
973
+ $fileStream = New-Object System.IO.Filestream($destinationFile , $fileMode )
974
+ if (-not $stream.CopyToAsync ($fileStream ).Wait($timeout * 1000 )) {
975
+ throw " Timeout downloading file"
1053
976
}
1054
- try {
1055
- $stream = $httpClient.GetStreamAsync ($sourceUrl ).GetAwaiter().GetResult()
1056
- $fileStream = New-Object System.IO.Filestream($destinationFile , $fileMode )
1057
- $stream.CopyToAsync ($fileStream ).GetAwaiter().GetResult() | Out-Null
977
+ }
978
+ finally {
979
+ if ($fileStream ) {
1058
980
$fileStream.Close ()
981
+ $fileStream.Dispose ()
1059
982
}
1060
- finally {
1061
- if ($fileStream ) {
1062
- $fileStream.Dispose ()
1063
- }
1064
- if ($stream ) {
1065
- $stream.Dispose ()
1066
- }
983
+ if ($stream ) {
984
+ $stream.Dispose ()
1067
985
}
1068
986
}
1069
987
}
0 commit comments