Add -PipeName Support to Web Cmdlets#26024
Add -PipeName Support to Web Cmdlets#26024mdaneri wants to merge 25 commits intoPowerShell:masterfrom
Conversation
Introduces a PipeName property to web cmdlets, enabling HTTP-like communication over named pipes. Updates WebRequestSession to handle PipeName connections, adds tests for PipeName functionality, and provides a test module for named pipe server operations.
Refactored PipeName test setup in WebCmdlets.Tests.ps1 to use local variables instead of script-scoped variables and simplified test tags. Added a README.md for the PipeName module with usage instructions.
|
@microsoft-github-policy-service agree |
…thods for Unix sockets and named pipes
Added null checks for UnixSocket and PipeName before connecting. Updated socket and pipe connection methods to use cancellation tokens and properly calculate timeout values for named pipes. Reinsert WebSession.PipeName = PipeName; that was removed by mistake
Replaces the ConnectToUnixSocketAsync method and rollback to the previous implementation because it's not part of the PR .
Introduces a PipeName property to web cmdlets, enabling HTTP-like communication over named pipes. Updates WebRequestSession to handle PipeName connections, adds tests for PipeName functionality, and provides a test module for named pipe server operations.
Refactored PipeName test setup in WebCmdlets.Tests.ps1 to use local variables instead of script-scoped variables and simplified test tags. Added a README.md for the PipeName module with usage instructions.
…thods for Unix sockets and named pipes
Added null checks for UnixSocket and PipeName before connecting. Updated socket and pipe connection methods to use cancellation tokens and properly calculate timeout values for named pipes. Reinsert WebSession.PipeName = PipeName; that was removed by mistake
Replaces the ConnectToUnixSocketAsync method and rollback to the previous implementation because it's not part of the PR .
|
Hi @iSazonov, Thanks |
src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs
Outdated
Show resolved
Hide resolved
| /// </summary> | ||
| [Parameter] | ||
| [ValidateNotNullOrEmpty] | ||
| public virtual string PipeName { get; set; } |
There was a problem hiding this comment.
nit: We should think about removing this virtual.
There was a problem hiding this comment.
I used virtual because all other properties are virtual. But I agree with you it's useless
There was a problem hiding this comment.
This should be wrapped in an IFDEF since it is Windows only. You should not see this parameter on non-Windows platforms. Same wrapper should be applied to all code paths that touch the pipename value.
There was a problem hiding this comment.
We have no standard pattern for this but I believe it is better for UX to get specific message like "The parameter is supported only on Windows. On Unix you can use UnixSocket parameter." instead of generic one "A parameter cannot be found ..."
src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs
Show resolved
Hide resolved
Removed redundant null check and used null-forgiving operator on _pipeName in NamedPipeClientStream constructor to address potential nullability warnings.
|
@PowerShell/wg-powershell-cmdlets had already approved the original issue being resolved by this PR, no further review needed by WG unless it deviates from the proposal in the isuse |
There was a problem hiding this comment.
Pull Request Overview
This PR adds Named Pipe transport support to PowerShell's web cmdlets (Invoke-WebRequest and Invoke-RestMethod) on Windows, providing functionality similar to the existing -UnixSocket parameter. This enables HTTP communication over Windows named pipes for scenarios requiring local IPC without TCP networking.
Key Changes
- Added
-PipeNameparameter to web cmdlets with corresponding connection handling logic - Created test infrastructure (PipeName module) to validate named pipe HTTP communication
- Implemented
ConnectToNamedPipeAsyncmethod mirroring the Unix socket pattern
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
WebRequestPSCmdlet.Common.cs |
Adds -PipeName parameter definition and session preparation logic |
WebRequestSession.cs |
Implements named pipe connection callback and session state management |
WebCmdlets.Tests.ps1 |
Adds CI tests validating both web cmdlets with named pipe transport |
PipeName.psm1 |
Provides test helper functions for managing named pipe HTTP server |
PipeName.psd1 |
Module manifest for the PipeName test module |
README.md |
Documentation for using the PipeName test module |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/WebRequestSession.cs
Outdated
Show resolved
Hide resolved
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…Cmdlet/WebRequestSession.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…Cmdlet/WebRequestSession.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Reformatted class and function definitions to use consistent brace placement and spacing. Improved code readability by updating formatting in loops and function parameters, and added a suppression attribute to Start-PipeServer. No functional changes were made.
Removed unnecessary blank line and adjusted indentation for delegate assignment and method call arguments to improve code readability in WebRequestSession.cs.
Corrected delegate and method call formatting in WebRequestSession.cs for improved code readability and consistency.
Unified and improved connection handling for Unix Domain Sockets and Named Pipes in WebRequestSession. Added dedicated async methods for each connection type, clarified precedence, and updated parameter names for clarity. This address the CodeFactor issue Complex Method(20)
Clarified code by adding comments to distinguish Unix Domain Socket and Named Pipe connection configuration in WebRequestSession. Also reformatted the certificate validation callback for improved readability.
Added blank lines after timeout calculation and socket connection steps to improve code readability in WebRequestSession.cs.
|
|
||
| // Create the pipe client stream and connect | ||
| var stream = new System.IO.Pipes.NamedPipeClientStream(".", _pipeName!, System.IO.Pipes.PipeDirection.InOut, System.IO.Pipes.PipeOptions.Asynchronous); | ||
| await stream.ConnectAsync(timeoutMs, cancellationToken).ConfigureAwait(false); |
There was a problem hiding this comment.
Do we really need the await? Can we just return ValueTask without async in the method header?
Add -PipeName Support to Web Cmdlets
PR Summary
Fix #24475
Add Named Pipe (
-PipeName) transport support toInvoke-WebRequestandInvoke-RestMethod, enablingHTTP request/response exchange over a local Windows named pipe (parity with existing
-UnixSocketsupport). Includes test infrastructure (PipeName test module) and Pester tests validating basic GET
via both cmdlets.
PR Context
Some hosting / sandbox / constrained Windows scenarios (service isolation, container-like dev flows,
locked-down loopback policies) benefit from intra-host communication without TCP. PowerShell already
supports Unix domain sockets cross‑platform; this extends similar functionality on Windows via named
pipes for web cmdlets. The implementation reuses the existing request pipeline with a custom stream
bound to the named pipe. Tests confirm successful 200 responses and content handling over the pipe.
Summary of Changes
-PipeNameparameter to web cmdlets (Invoke-WebRequest,Invoke-RestMethod).test/tools/Modules/PipeName/(PipeName.psm1,PipeName.psd1).-PipeNameis not specified.Design / Considerations
-UnixSocketdesign for consistency.Validation
Start-PSBuild).Tests Added
Describe "Web cmdlets PipeName tests" (Tags: CI):
Invoke-WebRequest -PipeNamereturns 200 and expected body.Invoke-RestMethod -PipeNamereturns expected body.Potential Follow-Ups
Release Notes (Suggested)
Add
-PipeNamesupport toInvoke-WebRequestandInvoke-RestMethodon Windows, enablingHTTP-over-named-pipe scenarios similar to existing
-UnixSocketsupport.PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headertest/tools/Modules/PipeName/(PipeName.psm1,PipeName.psd1).