1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using Microsoft.Extensions.Logging;
namespace SocketHttpListener.Net
{
internal sealed class HttpEndPointManager
{
private static Dictionary<IPAddress, Dictionary<int, HttpEndPointListener>> s_ipEndPoints = new Dictionary<IPAddress, Dictionary<int, HttpEndPointListener>>();
private HttpEndPointManager()
{
}
public static void AddListener(ILogger logger, HttpListener listener)
{
var added = new List<string>();
try
{
lock ((s_ipEndPoints as ICollection).SyncRoot)
{
foreach (string prefix in listener.Prefixes)
{
AddPrefixInternal(logger, prefix, listener);
added.Add(prefix);
}
}
}
catch
{
foreach (string prefix in added)
{
RemovePrefix(logger, prefix, listener);
}
throw;
}
}
public static void AddPrefix(ILogger logger, string prefix, HttpListener listener)
{
lock ((s_ipEndPoints as ICollection).SyncRoot)
{
AddPrefixInternal(logger, prefix, listener);
}
}
private static void AddPrefixInternal(ILogger logger, string p, HttpListener listener)
{
int start = p.IndexOf(':') + 3;
int colon = p.IndexOf(':', start);
if (colon != -1)
{
// root can't be -1 here, since we've already checked for ending '/' in ListenerPrefix.
int root = p.IndexOf('/', colon, p.Length - colon);
string portString = p.Substring(colon + 1, root - colon - 1);
if (!int.TryParse(portString, out var port) || port <= 0 || port >= 65536)
{
throw new HttpListenerException((int)HttpStatusCode.BadRequest, "net_invalid_port");
}
}
var lp = new ListenerPrefix(p);
if (lp.Host != "*" && lp.Host != "+" && Uri.CheckHostName(lp.Host) == UriHostNameType.Unknown)
throw new HttpListenerException((int)HttpStatusCode.BadRequest, "net_listener_host");
if (lp.Path.IndexOf('%') != -1)
throw new HttpListenerException((int)HttpStatusCode.BadRequest, "net_invalid_path");
if (lp.Path.IndexOf("//", StringComparison.Ordinal) != -1)
throw new HttpListenerException((int)HttpStatusCode.BadRequest, "net_invalid_path");
// listens on all the interfaces if host name cannot be parsed by IPAddress.
var epl = GetEPListener(logger, lp.Host, lp.Port, listener, lp.Secure);
epl.AddPrefix(lp, listener);
}
private static IPAddress GetIpAnyAddress(HttpListener listener)
{
return listener.EnableDualMode ? IPAddress.IPv6Any : IPAddress.Any;
}
private static HttpEndPointListener GetEPListener(ILogger logger, string host, int port, HttpListener listener, bool secure)
{
IPAddress addr;
if (host == "*" || host == "+")
{
addr = GetIpAnyAddress(listener);
}
else
{
const int NotSupportedErrorCode = 50;
try
{
addr = Dns.GetHostAddresses(host)[0];
}
catch
{
// Throw same error code as windows, request is not supported.
throw new HttpListenerException(NotSupportedErrorCode, "net_listener_not_supported");
}
if (IPAddress.Any.Equals(addr))
{
// Don't support listening to 0.0.0.0, match windows behavior.
throw new HttpListenerException(NotSupportedErrorCode, "net_listener_not_supported");
}
}
Dictionary<int, HttpEndPointListener> p = null;
if (s_ipEndPoints.ContainsKey(addr))
{
p = s_ipEndPoints[addr];
}
else
{
p = new Dictionary<int, HttpEndPointListener>();
s_ipEndPoints[addr] = p;
}
HttpEndPointListener epl = null;
if (p.ContainsKey(port))
{
epl = p[port];
}
else
{
try
{
epl = new HttpEndPointListener(listener, addr, port, secure, listener.Certificate, logger, listener.CryptoProvider, listener.SocketFactory, listener.StreamHelper, listener.TextEncoding, listener.FileSystem, listener.EnvironmentInfo);
}
catch (SocketException ex)
{
throw new HttpListenerException(ex.ErrorCode, ex.Message);
}
p[port] = epl;
}
return epl;
}
public static void RemoveEndPoint(HttpEndPointListener epl, IPEndPoint ep)
{
lock ((s_ipEndPoints as ICollection).SyncRoot)
{
Dictionary<int, HttpEndPointListener> p = null;
p = s_ipEndPoints[ep.Address];
p.Remove(ep.Port);
if (p.Count == 0)
{
s_ipEndPoints.Remove(ep.Address);
}
epl.Close();
}
}
public static void RemoveListener(ILogger logger, HttpListener listener)
{
lock ((s_ipEndPoints as ICollection).SyncRoot)
{
foreach (string prefix in listener.Prefixes)
{
RemovePrefixInternal(logger, prefix, listener);
}
}
}
public static void RemovePrefix(ILogger logger, string prefix, HttpListener listener)
{
lock ((s_ipEndPoints as ICollection).SyncRoot)
{
RemovePrefixInternal(logger, prefix, listener);
}
}
private static void RemovePrefixInternal(ILogger logger, string prefix, HttpListener listener)
{
var lp = new ListenerPrefix(prefix);
if (lp.Path.IndexOf('%') != -1)
return;
if (lp.Path.IndexOf("//", StringComparison.Ordinal) != -1)
return;
var epl = GetEPListener(logger, lp.Host, lp.Port, listener, lp.Secure);
epl.RemovePrefix(lp, listener);
}
}
}
|