Skip to content

Commit 75bab82

Browse files
committed
URI bug
There is a problem with the calculation when the URL uses the default port.
1 parent ff47f61 commit 75bab82

File tree

1 file changed

+45
-33
lines changed

1 file changed

+45
-33
lines changed

src/EmbedIO/Net/Internal/ListenerPrefix.cs

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,62 @@
11
using System;
2-
using System.Globalization;
32

43
namespace EmbedIO.Net.Internal
54
{
65
internal sealed class ListenerPrefix
76
{
87
public ListenerPrefix(string uri)
98
{
10-
var defaultPort = 80;
9+
var ur = new Uri(uri);
10+
if (ur == null)
11+
throw new ArgumentException("Invalid prefix.");
1112

1213
if (uri.StartsWith("https://", StringComparison.Ordinal))
1314
{
14-
defaultPort = 443;
1515
Secure = true;
1616
}
1717

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+
//}
4860
}
4961

5062
public HttpListener? Listener { get; set; }

0 commit comments

Comments
 (0)