1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

win_get_url: ExtendedWebClient with Timeout support (#28755)

This commit is contained in:
Dag Wieers 2017-08-30 00:52:39 +02:00 committed by Jordan Borean
parent d9ee3c09ca
commit 4ba7d05e9d
2 changed files with 44 additions and 27 deletions

View file

@ -11,6 +11,25 @@
$ErrorActionPreference = 'Stop' $ErrorActionPreference = 'Stop'
$webclient_util = @"
using System.Net;
public class ExtendedWebClient : WebClient {
public int Timeout;
public ExtendedWebClient() {
Timeout = 600000; // Default timeout value
}
protected override WebRequest GetWebRequest(System.Uri address) {
WebRequest request = base.GetWebRequest(address);
request.Timeout = Timeout;
return request;
}
}
"@
Add-Type -TypeDefinition $webclient_util
Function CheckModified-File($url, $dest, $headers, $credentials, $timeout, $use_proxy, $proxy) { Function CheckModified-File($url, $dest, $headers, $credentials, $timeout, $use_proxy, $proxy) {
$fileLastMod = ([System.IO.FileInfo]$dest).LastWriteTimeUtc $fileLastMod = ([System.IO.FileInfo]$dest).LastWriteTimeUtc
@ -48,7 +67,7 @@ Function CheckModified-File($url, $dest, $headers, $credentials, $timeout, $use_
} Catch { } Catch {
Fail-Json -obj $result -message "Error when requesting 'Last-Modified' date from '$url'. $($_.Exception.Message)" Fail-Json -obj $result -message "Error when requesting 'Last-Modified' date from '$url'. $($_.Exception.Message)"
} }
$result.status_code = $webResponse.StatusCode $result.status_code = [int] $webResponse.StatusCode
$result.msg = $webResponse.StatusDescription $result.msg = $webResponse.StatusDescription
$webResponse.Close() $webResponse.Close()
@ -69,41 +88,40 @@ Function Download-File($result, $url, $dest, $headers, $credentials, $timeout, $
} }
# TODO: Replace this with WebRequest # TODO: Replace this with WebRequest
$webClient = New-Object System.Net.WebClient $extWebClient = New-Object ExtendedWebClient
foreach ($header in $headers.GetEnumerator()) { foreach ($header in $headers.GetEnumerator()) {
$webClient.Headers.Add($header.Name, $header.Value) $extWebClient.Headers.Add($header.Name, $header.Value)
} }
# FIXME: WebClient has no Timeout property ? Should be replaced with WebRequest if ($timeout) {
# if ($timeout) { $extWebClient.Timeout = $timeout * 1000
# $webClient.Timeout = $timeout * 1000 }
# }
if (-not $use_proxy) { if (-not $use_proxy) {
# Ignore the system proxy settings # Ignore the system proxy settings
$webClient.Proxy = $null $extWebClient.Proxy = $null
} elseif ($proxy) { } elseif ($proxy) {
$webClient.Proxy = $proxy $extWebClient.Proxy = $proxy
} }
if ($credentials) { if ($credentials) {
$webClient.Credentials = $credentials $extWebClient.Credentials = $credentials
} }
Try { if (-not $whatif) {
if (-not $whatif) { Try {
$webClient.DownloadFile($url, $dest) $extWebClient.DownloadFile($url, $dest)
} Catch [System.Net.WebException] {
$result.status_code = [int] $_.Exception.Response.StatusCode
Fail-Json -obj $result -message "Error downloading '$url' to '$dest': $($_.Exception.Message)"
} Catch {
Fail-Json -obj $result -message "Unknown error downloading '$url' to '$dest': $($_.Exception.Message)"
} }
$result.changed = $true
} Catch [System.Net.WebException] {
$result.status_code = $_.Exception.Response.StatusCode
Fail-Json -obj $result -message "Error downloading '$url' to '$dest'. $($_.Exception.Message)"
} Catch {
Fail-Json -obj $result -message "Unknown error downloading '$url' to '$dest'. $($_.Exception.Message)"
} }
# FIXME: Reimplement DownloadFile() using WebRequest so we get the real information
$result.status_code = 200 $result.status_code = 200
$result.changed = $true
$result.msg = 'OK' $result.msg = 'OK'
$result.dest = $dest $result.dest = $dest
} }
@ -212,4 +230,4 @@ if ($force -or -not (Test-Path -LiteralPath $dest)) {
} }
} }
Exit-Json $result Exit-Json -obj $result

View file

@ -103,12 +103,11 @@ options:
type: bool type: bool
default: 'yes' default: 'yes'
version_added: '2.4' version_added: '2.4'
# TODO: Once we have implemented DownloadFile() using $WebRequest, enable timeout again timeout:
# timeout: description:
# description: - Timeout in seconds for URL request.
# - Timeout in seconds for URL request. default: 10
# default: 10 version_added : '2.4'
# version_added : '2.5'
notes: notes:
- For non-Windows targets, use the M(get_url) module instead. - For non-Windows targets, use the M(get_url) module instead.
''' '''