From dd6ad23179b964bfdc6c9e5f104946ae3552a950 Mon Sep 17 00:00:00 2001 From: = Date: Fri, 18 Mar 2016 08:41:54 +0000 Subject: [PATCH] Use HEAD request to determine if file should be downloaded, ensure download method is the same regardless of force mode. --- windows/win_get_url.ps1 | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/windows/win_get_url.ps1 b/windows/win_get_url.ps1 index a25ba055f9c..75bef615ab7 100644 --- a/windows/win_get_url.ps1 +++ b/windows/win_get_url.ps1 @@ -54,7 +54,7 @@ if($skip_certificate_validation){ $force = Get-Attr -obj $params -name "force" "yes" | ConvertTo-Bool -If ($force -or -not (Test-Path $dest)) { +Function Download-File($result, $url, $dest, $username, $password, $proxy_url, $proxy_username, $proxy_password) { $webClient = New-Object System.Net.WebClient if($proxy_url) { $proxy_server = New-Object System.Net.WebProxy($proxy_url, $true) @@ -76,8 +76,18 @@ If ($force -or -not (Test-Path $dest)) { Catch { Fail-Json $result "Error downloading $url to $dest $($_.Exception.Message)" } + +} + + +If ($force -or -not (Test-Path $dest)) { + Download-File -result $result -url $url -dest $dest -username $username -password $password -proxy_url $proxy_url -proxy_username $proxy_username -proxy_password $proxy_password + } Else { + $fileLastMod = ([System.IO.FileInfo]$dest).LastWriteTimeUtc + $webLastMod = $null + Try { $webRequest = [System.Net.HttpWebRequest]::Create($url) @@ -85,24 +95,22 @@ Else { $webRequest.Credentials = New-Object System.Net.NetworkCredential($username, $password) } - $webRequest.IfModifiedSince = ([System.IO.FileInfo]$dest).LastWriteTime - $webRequest.Method = "GET" + $webRequest.Method = "HEAD" [System.Net.HttpWebResponse]$webResponse = $webRequest.GetResponse() - - $stream = New-Object System.IO.StreamReader($webResponse.GetResponseStream()) - - $stream.ReadToEnd() | Set-Content -Path $dest -Force -ErrorAction Stop - - $result.changed = $true - } - Catch [System.Net.WebException] { - If ($_.Exception.Response.StatusCode -ne [System.Net.HttpStatusCode]::NotModified) { - Fail-Json $result "Error downloading $url to $dest $($_.Exception.Message)" - } + + $webLastMod = $webResponse.GetResponseHeader("Last-Modified") + $webResponse.Close() } Catch { - Fail-Json $result "Error downloading $url to $dest $($_.Exception.Message)" + Fail-Json $result "Error when requesting Last-Modified date from $url $($_.Exception.Message)" } + + If ((Get-Date -Date $webLastMod ) -lt $fileLastMod) { + $result.changed = $false + } Else { + Download-File -result $result -url $url -dest $dest -username $username -password $password -proxy_url $proxy_url -proxy_username $proxy_username -proxy_password $proxy_password + } + } Set-Attr $result.win_get_url "url" $url