Skip to content

Commit bf7199b

Browse files
authored
Enforce Get-Help offline dropback when page or internet is down (#752)
* Try to ping the help page we're about to open with 5s timeout * If it's up, open in browser * Otherwise show local help in the terminal
1 parent 834ae3d commit bf7199b

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/PowerShellEditorServices.Protocol/LanguageServer/ShowHelpRequest.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
44
//
55

6-
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
76
using System;
7+
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
88

99
namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer
1010
{
@@ -16,6 +16,7 @@ public static readonly
1616
RequestType<string, object, object, object> Type =
1717
RequestType<string, object, object, object>.Create("powerShell/showOnlineHelp");
1818
}
19+
1920
public class ShowHelpRequest
2021
{
2122
public static readonly

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

+22-5
Original file line numberDiff line numberDiff line change
@@ -254,22 +254,39 @@ protected async Task HandleShowHelpRequest(
254254
[String]$CommandName
255255
)
256256
try {
257-
$null = Microsoft.PowerShell.Core\Get-Command $CommandName -ErrorAction Stop
257+
$command = Microsoft.PowerShell.Core\Get-Command $CommandName -ErrorAction Stop
258258
} catch [System.Management.Automation.CommandNotFoundException] {
259259
$PSCmdlet.ThrowTerminatingError($PSItem)
260260
}
261261
try {
262-
$null = Microsoft.PowerShell.Core\Get-Help $CommandName -Online
263-
} catch [System.Management.Automation.PSInvalidOperationException] {
264-
Microsoft.PowerShell.Core\Get-Help $CommandName -Full
265-
}";
262+
$helpUri = [Microsoft.PowerShell.Commands.GetHelpCodeMethods]::GetHelpUri($command)
263+
264+
$oldSslVersion = [System.Net.ServicePointManager]::SecurityProtocol
265+
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
266+
267+
# HEAD means we don't need the content itself back, just the response header
268+
$status = (Microsoft.PowerShell.Utility\Invoke-WebRequest -Method Head -Uri $helpUri -TimeoutSec 5 -ErrorAction Stop).StatusCode
269+
if ($status -lt 400) {
270+
$null = Microsoft.PowerShell.Core\Get-Help $CommandName -Online
271+
return
272+
}
273+
} catch {
274+
# Ignore - we want to drop out to Get-Help -Full
275+
} finally {
276+
[System.Net.ServicePointManager]::SecurityProtocol = $oldSslVersion
277+
}
278+
279+
return Microsoft.PowerShell.Core\Get-Help $CommandName -Full
280+
";
266281

267282
if (string.IsNullOrEmpty(helpParams)) { helpParams = "Get-Help"; }
268283

269284
PSCommand checkHelpPSCommand = new PSCommand()
270285
.AddScript(CheckHelpScript, useLocalScope: true)
271286
.AddArgument(helpParams);
272287

288+
// TODO: Rather than print the help in the console, we should send the string back
289+
// to VSCode to display in a help pop-up (or similar)
273290
await editorSession.PowerShellContext.ExecuteCommand<PSObject>(checkHelpPSCommand, sendOutputToHost: true);
274291
await requestContext.SendResult(null);
275292
}

0 commit comments

Comments
 (0)