@echo off
set "ARG_0=%~dpnx0"
powershell.exe -STA -NoProfile "iex (([IO.File]::ReadAllText($Env:ARG_0)) -replace '^(.*\n)*?.*<::::>.*\n', '')" & goto :EOF
Add-Type -Assembly System.Windows.Forms;
$workerCount = 8;
$passTimeouts = @(2000, 5000, 10000, 20000);
function testUrls($defs, $timeout, $workerCount = 1) {
$job = @{
queue = [Collections.Queue]::Synchronized((New-Object Collections.Queue (, $defs)));
timeout = $timeout;
};
$script = {
$job = $args[0];
while (1) {
try { $def = $job.queue.Dequeue(); } catch { return; }
try {
$req = [Net.WebRequest]::Create($def.url);
$req.Method = 'HEAD';
$req.Timeout = $job.timeout;
$rsp = $req.GetResponse();
$def.err = '';
}
catch {
$e = $_.Exception;
while ($e -and $e -isnot [Net.WebException]) { $e = $e.InnerException; }
$rsp = $e.Response;
$def.err = $e.Message;
}
if ($rsp) {
$def.code = [int]$rsp.StatusCode;
$rsp.Close();
}
}
};
if ($workerCount -gt 1) {
$workers = @((1 .. $workerCount) | % { $w = [PowerShell]::Create().AddScript($script).AddArgument($job); @{ ps = $w; r = $w.BeginInvoke() } });
$workers | % { $_.ps.EndInvoke($_.r) };
}
else {
. $script $job;
}
}
$defs = @([Windows.Forms.Clipboard]::GetText() -split '\r?\n' | ? { $_ -match '^\w+://' } | % { @{ url = $_; } });
$leftDefs = $defs;
$startTime = [DateTime]::Now;
Write-Host "Validating $($defs.Length) urls using $workerCount workers...`n";
$testDef = @{ url = 'http://google.com' };
testUrls @($testDef) 10000 1;
if (!$testDef.code) {
Write-Host "Oops, self-check failed!";
$host.SetShouldExit(1);
return;
}
for ($step = 0; $step -lt $passTimeouts.Length -and $leftDefs.Length; ++$step) {
$passStartTime = [DateTime]::Now;
Write-Host "Executing pass #$step using timeout $($passTimeouts[$step])...";
testUrls $leftDefs $passTimeouts[$step] $workerCount;
$leftDefs = @($leftDefs | ? { !$_.code; if ($_.code) { Write-Host "$($_.url): $($_.code)" } });
Write-Host "Pass #$step done in $([int]([DateTime]::Now - $passStartTime).TotalSeconds) seconds, $($leftDefs.Length) urls left.";
'';
}
if ($leftDefs) {
Write-Host 'Unresolved urls:';
$leftDefs | % { Write-Host "$($_.url): $($_.err)" };
'';
}
$goodUrls = @($defs | ? { $_.code -ge 200 -and $_.code -le 399 } | % { $_.url });
Write-Host "Validation done in $([int]([DateTime]::Now - $startTime).TotalSeconds) seconds.";
if ([Windows.Forms.MessageBox]::Show("Click OK to copy $($goodUrls.Length) good urls.", 'Url Validator', 1) -eq 1) {
[Windows.Forms.Clipboard]::SetText(($goodUrls -join "`n") + "`n");
} |