@@ -88,26 +88,32 @@ private static void addPrefix (string uriPrefix, HttpListener httpListener)
8888 throw new HttpListenerException ( 400 , "Invalid path." ) ; // TODO: Code?
8989
9090 // Listens on all the interfaces if host name cannot be parsed by IPAddress.
91- var epl = getEndPointListener ( pref . Host , pref . Port , httpListener , pref . IsSecure ) ;
92- epl . AddPrefix ( pref , httpListener ) ;
91+ var lsnr = getEndPointListener ( pref . Host , pref . Port , httpListener , pref . IsSecure ) ;
92+ lsnr . AddPrefix ( pref , httpListener ) ;
9393 }
9494
95- private static EndPointListener getEndPointListener (
96- string host , int port , HttpListener httpListener , bool secure )
95+ private static IPAddress convertToAddress ( string hostName )
9796 {
97+ if ( hostName == "*" )
98+ return IPAddress . Any ;
99+
98100 IPAddress addr ;
99- if ( host == "*" ) {
100- addr = IPAddress . Any ;
101+ if ( IPAddress . TryParse ( hostName , out addr ) )
102+ return addr ;
103+
104+ try {
105+ var host = Dns . GetHostEntry ( hostName ) ;
106+ return host != null ? host . AddressList [ 0 ] : IPAddress . Any ;
101107 }
102- else if ( ! IPAddress . TryParse ( host , out addr ) ) {
103- try {
104- var iphost = Dns . GetHostEntry ( host ) ;
105- addr = iphost != null ? iphost . AddressList [ 0 ] : IPAddress . Any ;
106- }
107- catch {
108- addr = IPAddress . Any ;
109- }
108+ catch {
109+ return IPAddress . Any ;
110110 }
111+ }
112+
113+ private static EndPointListener getEndPointListener (
114+ string host , int port , HttpListener httpListener , bool secure )
115+ {
116+ var addr = convertToAddress ( host ) ;
111117
112118 Dictionary < int , EndPointListener > eps = null ;
113119 if ( _ipToEndpoints . ContainsKey ( addr ) ) {
@@ -118,23 +124,23 @@ private static EndPointListener getEndPointListener (
118124 _ipToEndpoints [ addr ] = eps ;
119125 }
120126
121- EndPointListener epl = null ;
127+ EndPointListener lsnr = null ;
122128 if ( eps . ContainsKey ( port ) ) {
123- epl = eps [ port ] ;
129+ lsnr = eps [ port ] ;
124130 }
125131 else {
126- epl = new EndPointListener (
132+ lsnr = new EndPointListener (
127133 addr ,
128134 port ,
129135 secure ,
130136 httpListener . CertificateFolderPath ,
131137 httpListener . SslConfiguration ,
132138 httpListener . ReuseAddress ) ;
133139
134- eps [ port ] = epl ;
140+ eps [ port ] = lsnr ;
135141 }
136142
137- return epl ;
143+ return lsnr ;
138144 }
139145
140146 private static void removePrefix ( string uriPrefix , HttpListener httpListener )
@@ -146,8 +152,24 @@ private static void removePrefix (string uriPrefix, HttpListener httpListener)
146152 if ( pref . Path . IndexOf ( "//" , StringComparison . Ordinal ) != - 1 )
147153 return ;
148154
149- var epl = getEndPointListener ( pref . Host , pref . Port , httpListener , pref . IsSecure ) ;
150- epl . RemovePrefix ( pref , httpListener ) ;
155+ var lsnr = getEndPointListener ( pref . Host , pref . Port , httpListener , pref . IsSecure ) ;
156+ lsnr . RemovePrefix ( pref , httpListener ) ;
157+ }
158+
159+ #endregion
160+
161+ #region Internal Methods
162+
163+ internal static void RemoveEndPoint ( IPEndPoint endpoint , EndPointListener endpointListener )
164+ {
165+ lock ( ( ( ICollection ) _ipToEndpoints ) . SyncRoot ) {
166+ var eps = _ipToEndpoints [ endpoint . Address ] ;
167+ eps . Remove ( endpoint . Port ) ;
168+ if ( eps . Count == 0 )
169+ _ipToEndpoints . Remove ( endpoint . Address ) ;
170+
171+ endpointListener . Close ( ) ;
172+ }
151173 }
152174
153175 #endregion
@@ -179,18 +201,6 @@ public static void AddPrefix (string uriPrefix, HttpListener httpListener)
179201 addPrefix ( uriPrefix , httpListener ) ;
180202 }
181203
182- public static void RemoveEndPoint ( EndPointListener epListener , IPEndPoint endpoint )
183- {
184- lock ( ( ( ICollection ) _ipToEndpoints ) . SyncRoot ) {
185- var eps = _ipToEndpoints [ endpoint . Address ] ;
186- eps . Remove ( endpoint . Port ) ;
187- if ( eps . Count == 0 )
188- _ipToEndpoints . Remove ( endpoint . Address ) ;
189-
190- epListener . Close ( ) ;
191- }
192- }
193-
194204 public static void RemoveListener ( HttpListener httpListener )
195205 {
196206 lock ( ( ( ICollection ) _ipToEndpoints ) . SyncRoot )
0 commit comments