|
1 | 1 | using System; |
2 | | -using System.Globalization; |
3 | 2 |
|
4 | 3 | namespace EmbedIO.Net.Internal |
5 | 4 | { |
6 | 5 | internal sealed class ListenerPrefix |
7 | 6 | { |
8 | 7 | public ListenerPrefix(string uri) |
9 | 8 | { |
10 | | - var defaultPort = 80; |
| 9 | + var ur = new Uri(uri); |
| 10 | + if (ur == null) |
| 11 | + throw new ArgumentException("Invalid prefix."); |
11 | 12 |
|
12 | 13 | if (uri.StartsWith("https://", StringComparison.Ordinal)) |
13 | 14 | { |
14 | | - defaultPort = 443; |
15 | 15 | Secure = true; |
16 | 16 | } |
17 | 17 |
|
18 | | - var length = uri.Length; |
19 | | - var startHost = uri.IndexOf(':') + 3; |
20 | | - |
21 | | - if (startHost >= length) |
22 | | - { |
23 | | - throw new ArgumentException("No host specified."); |
24 | | - } |
25 | | - |
26 | | - var colon = uri.LastIndexOf(':'); |
27 | | - int root; |
28 | | - |
29 | | - if (colon > 0) |
30 | | - { |
31 | | - Host = uri.Substring(startHost, colon - startHost); |
32 | | - root = uri.IndexOf('/', colon, length - colon); |
33 | | - Port = int.Parse(uri.Substring(colon + 1, root - colon - 1), CultureInfo.InvariantCulture); |
34 | | - } |
35 | | - else |
36 | | - { |
37 | | - root = uri.IndexOf('/', startHost, length - startHost); |
38 | | - Host = uri.Substring(startHost, root - startHost); |
39 | | - Port = defaultPort; |
40 | | - } |
41 | | - |
42 | | - Path = uri.Substring(root); |
43 | | - |
44 | | - if (Path.Length != 1) |
45 | | - { |
46 | | - Path = Path.Substring(0, Path.Length - 1); |
47 | | - } |
| 18 | + Host = ur.Host; |
| 19 | + Port = ur.Port; |
| 20 | + Path = ur.PathAndQuery + ur.Fragment; |
| 21 | + |
| 22 | + //var defaultPort = 80; |
| 23 | + |
| 24 | + //if (uri.StartsWith("https://", StringComparison.Ordinal)) |
| 25 | + //{ |
| 26 | + // defaultPort = 443; |
| 27 | + // Secure = true; |
| 28 | + //} |
| 29 | + |
| 30 | + //var length = uri.Length; |
| 31 | + //var startHost = uri.IndexOf(':') + 3; |
| 32 | + |
| 33 | + //if (startHost >= length) |
| 34 | + //{ |
| 35 | + // throw new ArgumentException("No host specified."); |
| 36 | + //} |
| 37 | + |
| 38 | + //var colon = uri.LastIndexOf(':'); |
| 39 | + //int root; |
| 40 | + |
| 41 | + //if (colon > 0) |
| 42 | + //{ |
| 43 | + // Host = uri.Substring(startHost, colon - startHost); |
| 44 | + // root = uri.IndexOf('/', colon, length - colon); |
| 45 | + // Port = int.Parse(uri.Substring(colon + 1, root - colon - 1), CultureInfo.InvariantCulture); |
| 46 | + //} |
| 47 | + //else |
| 48 | + //{ |
| 49 | + // root = uri.IndexOf('/', startHost, length - startHost); |
| 50 | + // Host = uri.Substring(startHost, root - startHost); |
| 51 | + // Port = defaultPort; |
| 52 | + //} |
| 53 | + |
| 54 | + //Path = uri.Substring(root); |
| 55 | + |
| 56 | + //if (Path.Length != 1) |
| 57 | + //{ |
| 58 | + // Path = Path.Substring(0, Path.Length - 1); |
| 59 | + //} |
48 | 60 | } |
49 | 61 |
|
50 | 62 | public HttpListener? Listener { get; set; } |
|
0 commit comments