From 3bcc5eed403269b99e42755839aecc9b909c98fa Mon Sep 17 00:00:00 2001 From: Azure SDK Bot <53356347+azure-sdk@users.noreply.github.com> Date: Thu, 17 Feb 2022 22:48:16 -0800 Subject: [PATCH] Add timeout on Invoke-request call (#27196) Co-authored-by: sima-zhu --- eng/common/scripts/Verify-Links.ps1 | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/eng/common/scripts/Verify-Links.ps1 b/eng/common/scripts/Verify-Links.ps1 index 275d4bbde5bd4..bdb4762a8ef0e 100644 --- a/eng/common/scripts/Verify-Links.ps1 +++ b/eng/common/scripts/Verify-Links.ps1 @@ -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 @@ -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 @@ -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) { @@ -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")) { @@ -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 {