Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
Perform download url check with HTTP headers from chocolateyInstall.ps1
Browse files Browse the repository at this point in the history
  • Loading branch information
galeksandrp committed May 4, 2018
1 parent 4d1302e commit 0be7f5c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions AU/Private/check_url.ps1
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion AU/Private/request.ps1
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
2 changes: 1 addition & 1 deletion AU/Public/Update-Package.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.":<url>
```
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
Expand Down

0 comments on commit 0be7f5c

Please sign in to comment.