@@ -11,6 +11,7 @@ namespace Microsoft.AspNetCore.Http;
1111public class BindingAddress
1212{
1313 private const string UnixPipeHostPrefix = "unix:/" ;
14+ private const string NamedPipeHostPrefix = "pipe:" ;
1415
1516 private BindingAddress ( string host , string pathBase , int port , string scheme )
1617 {
@@ -57,6 +58,14 @@ public BindingAddress()
5758 /// </summary>
5859 public bool IsUnixPipe => Host . StartsWith ( UnixPipeHostPrefix , StringComparison . Ordinal ) ;
5960
61+ /// <summary>
62+ /// Gets a value that determines if this instance represents a named pipe.
63+ /// <para>
64+ /// Returns <see langword="true"/> if <see cref="Host"/> starts with <c>pipe:</c> prefix.
65+ /// </para>
66+ /// </summary>
67+ public bool IsNamedPipe => Host . StartsWith ( NamedPipeHostPrefix , StringComparison . Ordinal ) ;
68+
6069 /// <summary>
6170 /// Gets the unix pipe path if this instance represents a Unix pipe.
6271 /// </summary>
@@ -73,6 +82,22 @@ public string UnixPipePath
7382 }
7483 }
7584
85+ /// <summary>
86+ /// Gets the named pipe path if this instance represents a named pipe.
87+ /// </summary>
88+ public string NamedPipePath
89+ {
90+ get
91+ {
92+ if ( ! IsNamedPipe )
93+ {
94+ throw new InvalidOperationException ( "Binding address is not a named pipe." ) ;
95+ }
96+
97+ return GetNamedPipePath ( Host ) ;
98+ }
99+ }
100+
76101 private static string GetUnixPipePath ( string host )
77102 {
78103 var unixPipeHostPrefixLength = UnixPipeHostPrefix . Length ;
@@ -84,10 +109,12 @@ private static string GetUnixPipePath(string host)
84109 return host . Substring ( unixPipeHostPrefixLength ) ;
85110 }
86111
112+ private static string GetNamedPipePath ( string host ) => host . Substring ( NamedPipeHostPrefix . Length ) ;
113+
87114 /// <inheritdoc />
88115 public override string ToString ( )
89116 {
90- if ( IsUnixPipe )
117+ if ( IsUnixPipe || IsNamedPipe )
91118 {
92119 return Scheme . ToLowerInvariant ( ) + Uri . SchemeDelimiter + Host . ToLowerInvariant ( ) ;
93120 }
@@ -135,15 +162,11 @@ public static BindingAddress Parse(string address)
135162 var schemeDelimiterEnd = schemeDelimiterStart + Uri . SchemeDelimiter . Length ;
136163
137164 var isUnixPipe = address . IndexOf ( UnixPipeHostPrefix , schemeDelimiterEnd , StringComparison . Ordinal ) == schemeDelimiterEnd ;
165+ var isNamedPipe = address . IndexOf ( NamedPipeHostPrefix , schemeDelimiterEnd , StringComparison . Ordinal ) == schemeDelimiterEnd ;
138166
139167 int pathDelimiterStart ;
140168 int pathDelimiterEnd ;
141- if ( ! isUnixPipe )
142- {
143- pathDelimiterStart = address . IndexOf ( "/" , schemeDelimiterEnd , StringComparison . Ordinal ) ;
144- pathDelimiterEnd = pathDelimiterStart ;
145- }
146- else
169+ if ( isUnixPipe )
147170 {
148171 var unixPipeHostPrefixLength = UnixPipeHostPrefix . Length ;
149172 if ( OperatingSystem . IsWindows ( ) )
@@ -159,6 +182,16 @@ public static BindingAddress Parse(string address)
159182 pathDelimiterStart = address . IndexOf ( ":" , schemeDelimiterEnd + unixPipeHostPrefixLength , StringComparison . Ordinal ) ;
160183 pathDelimiterEnd = pathDelimiterStart + ":" . Length ;
161184 }
185+ else if ( isNamedPipe )
186+ {
187+ pathDelimiterStart = address . IndexOf ( ":" , schemeDelimiterEnd + NamedPipeHostPrefix . Length , StringComparison . Ordinal ) ;
188+ pathDelimiterEnd = pathDelimiterStart + ":" . Length ;
189+ }
190+ else
191+ {
192+ pathDelimiterStart = address . IndexOf ( "/" , schemeDelimiterEnd , StringComparison . Ordinal ) ;
193+ pathDelimiterEnd = pathDelimiterStart ;
194+ }
162195
163196 if ( pathDelimiterStart < 0 )
164197 {
@@ -215,6 +248,11 @@ public static BindingAddress Parse(string address)
215248 throw new FormatException ( $ "Invalid url, unix socket path must be absolute: '{ address } '") ;
216249 }
217250
251+ if ( isNamedPipe && GetNamedPipePath ( host ) . Contains ( '\\ ' ) )
252+ {
253+ throw new FormatException ( $ "Invalid url, pipe name must not contain backslashes: '{ address } '") ;
254+ }
255+
218256 string pathBase ;
219257 if ( address [ address . Length - 1 ] == '/' )
220258 {
0 commit comments