From 4ba7d05e9dd33d842a07287f4308413e53c216e8 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Wed, 30 Aug 2017 00:52:39 +0200 Subject: [PATCH] win_get_url: ExtendedWebClient with Timeout support (#28755) --- lib/ansible/modules/windows/win_get_url.ps1 | 60 +++++++++++++-------- lib/ansible/modules/windows/win_get_url.py | 11 ++-- 2 files changed, 44 insertions(+), 27 deletions(-) diff --git a/lib/ansible/modules/windows/win_get_url.ps1 b/lib/ansible/modules/windows/win_get_url.ps1 index bd2134c200..130ded2779 100644 --- a/lib/ansible/modules/windows/win_get_url.ps1 +++ b/lib/ansible/modules/windows/win_get_url.ps1 @@ -11,6 +11,25 @@ $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) { $fileLastMod = ([System.IO.FileInfo]$dest).LastWriteTimeUtc @@ -48,7 +67,7 @@ Function CheckModified-File($url, $dest, $headers, $credentials, $timeout, $use_ } Catch { 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 $webResponse.Close() @@ -69,41 +88,40 @@ Function Download-File($result, $url, $dest, $headers, $credentials, $timeout, $ } # TODO: Replace this with WebRequest - $webClient = New-Object System.Net.WebClient + $extWebClient = New-Object ExtendedWebClient 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) { -# $webClient.Timeout = $timeout * 1000 -# } + if ($timeout) { + $extWebClient.Timeout = $timeout * 1000 + } if (-not $use_proxy) { # Ignore the system proxy settings - $webClient.Proxy = $null + $extWebClient.Proxy = $null } elseif ($proxy) { - $webClient.Proxy = $proxy + $extWebClient.Proxy = $proxy } if ($credentials) { - $webClient.Credentials = $credentials + $extWebClient.Credentials = $credentials } - Try { - if (-not $whatif) { - $webClient.DownloadFile($url, $dest) + if (-not $whatif) { + Try { + $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.changed = $true $result.msg = 'OK' $result.dest = $dest } @@ -212,4 +230,4 @@ if ($force -or -not (Test-Path -LiteralPath $dest)) { } } -Exit-Json $result +Exit-Json -obj $result diff --git a/lib/ansible/modules/windows/win_get_url.py b/lib/ansible/modules/windows/win_get_url.py index 82e542f3b0..72e83dcfb8 100644 --- a/lib/ansible/modules/windows/win_get_url.py +++ b/lib/ansible/modules/windows/win_get_url.py @@ -103,12 +103,11 @@ options: type: bool default: 'yes' version_added: '2.4' -# TODO: Once we have implemented DownloadFile() using $WebRequest, enable timeout again -# timeout: -# description: -# - Timeout in seconds for URL request. -# default: 10 -# version_added : '2.5' + timeout: + description: + - Timeout in seconds for URL request. + default: 10 + version_added : '2.4' notes: - For non-Windows targets, use the M(get_url) module instead. '''