Skip to content

Commit

Permalink
Add timeout on Invoke-request call (#27196)
Browse files Browse the repository at this point in the history
Co-authored-by: sima-zhu <sizhu@microsoft.com>
  • Loading branch information
azure-sdk and sima-zhu authored Feb 18, 2022
1 parent ac9b5f6 commit 3bcc5ee
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions eng/common/scripts/Verify-Links.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
.PARAMETER outputCacheFile
Path to a file that the script will output all the validated links after running all checks.
.PARAMETER requestTimeoutSec
The number of seconds before we timeout when sending an individual web request. Default is 15 seconds.
.EXAMPLE
PS> .\Verify-Links.ps1 C:\README.md
Expand All @@ -67,7 +70,8 @@ param (
[bool] $checkLinkGuidance = $false,
[string] $userAgent,
[string] $inputCacheFile,
[string] $outputCacheFile
[string] $outputCacheFile,
[string] $requestTimeoutSec = 15
)

$ProgressPreference = "SilentlyContinue"; # Disable invoke-webrequest progress dialog
Expand Down Expand Up @@ -220,14 +224,14 @@ function CheckLink ([System.Uri]$linkUri, $allowRetry=$true)
$headRequestSucceeded = $true
try {
# Attempt HEAD request first
$response = Invoke-WebRequest -Uri $linkUri -Method HEAD -UserAgent $userAgent
$response = Invoke-WebRequest -Uri $linkUri -Method HEAD -UserAgent $userAgent -TimeoutSec $requestTimeoutSec
}
catch {
$headRequestSucceeded = $false
}
if (!$headRequestSucceeded) {
# Attempt a GET request if the HEAD request failed.
$response = Invoke-WebRequest -Uri $linkUri -Method GET -UserAgent $userAgent
$response = Invoke-WebRequest -Uri $linkUri -Method GET -UserAgent $userAgent -TimeoutSec $requestTimeoutSec
}
$statusCode = $response.StatusCode
if ($statusCode -ne 200) {
Expand Down Expand Up @@ -328,7 +332,7 @@ function GetLinks([System.Uri]$pageUri)
{
if ($pageUri.Scheme.StartsWith("http")) {
try {
$response = Invoke-WebRequest -Uri $pageUri -UserAgent $userAgent
$response = Invoke-WebRequest -Uri $pageUri -UserAgent $userAgent -TimeoutSec $requestTimeoutSec
$content = $response.Content

if ($pageUri.ToString().EndsWith(".md")) {
Expand Down Expand Up @@ -392,7 +396,7 @@ if ($inputCacheFile)
$cacheContent = ""
if ($inputCacheFile.StartsWith("http")) {
try {
$response = Invoke-WebRequest -Uri $inputCacheFile
$response = Invoke-WebRequest -Uri $inputCacheFile -TimeoutSec $requestTimeoutSec
$cacheContent = $response.Content
}
catch {
Expand Down

0 comments on commit 3bcc5ee

Please sign in to comment.