From 0be7f5c878b2718886c7451cf083b1fdf255db13 Mon Sep 17 00:00:00 2001 From: Alexander Georgievskiy Date: Sat, 24 Mar 2018 12:11:34 +0400 Subject: [PATCH] Perform download url check with HTTP headers from chocolateyInstall.ps1 --- AU/Private/check_url.ps1 | 4 ++-- AU/Private/request.ps1 | 3 ++- AU/Public/Update-Package.ps1 | 2 +- CHANGELOG.md | 1 + README.md | 10 ++++++++++ 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/AU/Private/check_url.ps1 b/AU/Private/check_url.ps1 index 81dd868d..5f42a2f4 100644 --- a/AU/Private/check_url.ps1 +++ b/AU/Private/check_url.ps1 @@ -1,10 +1,10 @@ # Returns nothing if url is valid, error otherwise -function check_url( [string] $Url, [int]$Timeout, $ExcludeType='text/html' ) { +function check_url( [string] $Url, [int]$Timeout, $ExcludeType='text/html', $Options ) { if (!(is_url $Url)) { return "URL syntax is invalid" } try { - $response = request $url $Timeout + $response = request $url $Timeout -Options $Options if ($response.ContentType -like "*${ExcludeType}*") { return "Bad content type '$ExcludeType'" } } catch { diff --git a/AU/Private/request.ps1 b/AU/Private/request.ps1 index 9fab8f09..f36c4344 100644 --- a/AU/Private/request.ps1 +++ b/AU/Private/request.ps1 @@ -1,7 +1,8 @@ -function request( [string]$Url, [int]$Timeout ) { +function request( [string]$Url, [int]$Timeout, $Options ) { if ([string]::IsNullOrWhiteSpace($url)) {throw 'The URL is empty'} $request = [System.Net.WebRequest]::Create($Url) if ($Timeout) { $request.Timeout = $Timeout*1000 } + if ($Options.Headers) { $Options.Headers.Keys | %{ $request.Headers.add($_, $Options.Headers[$_]) } } $response = $request.GetResponse() $response.Close() diff --git a/AU/Public/Update-Package.ps1 b/AU/Public/Update-Package.ps1 index e609a0eb..328ca69f 100644 --- a/AU/Public/Update-Package.ps1 +++ b/AU/Public/Update-Package.ps1 @@ -107,7 +107,7 @@ function Update-Package { "URL check" | result $Latest.Keys | ? {$_ -like 'url*' } | % { $url = $Latest[ $_ ] - if ($res = check_url $url) { throw "${res}:$url" } else { " $url" | result } + if ($res = check_url $url -Options $Latest.Options) { throw "${res}:$url" } else { " $url" | result } } } diff --git a/CHANGELOG.md b/CHANGELOG.md index 058071de..a43225cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Next - Git plugin: add `Branch` parameter to specify a branch name +- `Update-Package`: Now you can pass HTTP/HTTPS headers to `$Latest.Options.Headers` to avoid `Unauthorized` errors while checking URLs. ### Bugfixes diff --git a/README.md b/README.md index 192c0858..1cfe3a10 100644 --- a/README.md +++ b/README.md @@ -516,6 +516,16 @@ RepeatCount = 2 #How many times to repeat on - If the same error is both in `RepeatOn` and `IgnoreOn` list, the package will first be repeated and if the error persists, it will be ignored. - The last line returned by the package prior to the word 'ignore' is used as `IgnoreMessage` for that package and shown in reports. +##### Can't validate URL error + +If you encounter `Can't validate URL` error like + +```bash +Can't validate URL +Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (401) Unauthorized.": +``` + +you need to pass HTTP/HTTPS headers used for retreiving `url`/`url64bit` to `$Latest.Options.Headers` as `Hashtable`, where key is header name, and value are header itself. This may be `Authorization` or `Referer` header or any others. ## Other functions