@@ -11,6 +11,7 @@ namespace Microsoft.AspNetCore.Http;
11
11
public class BindingAddress
12
12
{
13
13
private const string UnixPipeHostPrefix = "unix:/" ;
14
+ private const string NamedPipeHostPrefix = "pipe:" ;
14
15
15
16
private BindingAddress ( string host , string pathBase , int port , string scheme )
16
17
{
@@ -57,6 +58,14 @@ public BindingAddress()
57
58
/// </summary>
58
59
public bool IsUnixPipe => Host . StartsWith ( UnixPipeHostPrefix , StringComparison . Ordinal ) ;
59
60
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
+
60
69
/// <summary>
61
70
/// Gets the unix pipe path if this instance represents a Unix pipe.
62
71
/// </summary>
@@ -73,6 +82,22 @@ public string UnixPipePath
73
82
}
74
83
}
75
84
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
+
76
101
private static string GetUnixPipePath ( string host )
77
102
{
78
103
var unixPipeHostPrefixLength = UnixPipeHostPrefix . Length ;
@@ -84,10 +109,12 @@ private static string GetUnixPipePath(string host)
84
109
return host . Substring ( unixPipeHostPrefixLength ) ;
85
110
}
86
111
112
+ private static string GetNamedPipePath ( string host ) => host . Substring ( NamedPipeHostPrefix . Length ) ;
113
+
87
114
/// <inheritdoc />
88
115
public override string ToString ( )
89
116
{
90
- if ( IsUnixPipe )
117
+ if ( IsUnixPipe || IsNamedPipe )
91
118
{
92
119
return Scheme . ToLowerInvariant ( ) + Uri . SchemeDelimiter + Host . ToLowerInvariant ( ) ;
93
120
}
@@ -135,15 +162,11 @@ public static BindingAddress Parse(string address)
135
162
var schemeDelimiterEnd = schemeDelimiterStart + Uri . SchemeDelimiter . Length ;
136
163
137
164
var isUnixPipe = address . IndexOf ( UnixPipeHostPrefix , schemeDelimiterEnd , StringComparison . Ordinal ) == schemeDelimiterEnd ;
165
+ var isNamedPipe = address . IndexOf ( NamedPipeHostPrefix , schemeDelimiterEnd , StringComparison . Ordinal ) == schemeDelimiterEnd ;
138
166
139
167
int pathDelimiterStart ;
140
168
int pathDelimiterEnd ;
141
- if ( ! isUnixPipe )
142
- {
143
- pathDelimiterStart = address . IndexOf ( "/" , schemeDelimiterEnd , StringComparison . Ordinal ) ;
144
- pathDelimiterEnd = pathDelimiterStart ;
145
- }
146
- else
169
+ if ( isUnixPipe )
147
170
{
148
171
var unixPipeHostPrefixLength = UnixPipeHostPrefix . Length ;
149
172
if ( OperatingSystem . IsWindows ( ) )
@@ -159,6 +182,16 @@ public static BindingAddress Parse(string address)
159
182
pathDelimiterStart = address . IndexOf ( ":" , schemeDelimiterEnd + unixPipeHostPrefixLength , StringComparison . Ordinal ) ;
160
183
pathDelimiterEnd = pathDelimiterStart + ":" . Length ;
161
184
}
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
+ }
162
195
163
196
if ( pathDelimiterStart < 0 )
164
197
{
@@ -215,6 +248,11 @@ public static BindingAddress Parse(string address)
215
248
throw new FormatException ( $ "Invalid url, unix socket path must be absolute: '{ address } '") ;
216
249
}
217
250
251
+ if ( isNamedPipe && GetNamedPipePath ( host ) . Contains ( '\\ ' ) )
252
+ {
253
+ throw new FormatException ( $ "Invalid url, pipe name must not contain backslashes: '{ address } '") ;
254
+ }
255
+
218
256
string pathBase ;
219
257
if ( address [ address . Length - 1 ] == '/' )
220
258
{
0 commit comments