-
Notifications
You must be signed in to change notification settings - Fork 1
/
HostNameUtil.cs
51 lines (46 loc) · 1.62 KB
/
HostNameUtil.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
namespace DotStd
{
/// <summary>
/// Util/Helper for host names.
/// </summary>
public class HostNameUtil
{
public static bool IsValidHostName([NotNullWhen(true)] string? hostName)
{
if (string.IsNullOrWhiteSpace(hostName))
return false;
// made of valid chars ?
return true;
}
/// <summary>
/// Get Subdomain from hostname if it has one.
/// ASSUME no protocol prefix "http://" etc. ASSUME not /Path\''
/// </summary>
/// <param name="reqHost">context.Request.Host.ToString().ToLower(). e.g. "subdom.test.com:443" or special "test.localhost:80"</param>
/// <returns>null for "test.com" or "localhost:44322" (has no subdomain)</returns>
public static string? GetSubDomain(string? reqHost)
{
if (string.IsNullOrWhiteSpace(reqHost))
return null;
int i = reqHost.IndexOf(':'); // chop off port.
if (i >= 0)
{
reqHost = reqHost.Substring(0, i);
}
i = reqHost.IndexOf('.');
if (i < 0) // no dots.
return null; // no subdomain
if (!reqHost.EndsWith("localhost"))
{
int j = reqHost.IndexOf('.', i + 1); // MUST have a second dot. subdomain.maindomain.com
if (j < 0)
return null; // no subdomain
}
return reqHost.Substring(0, i);
}
}
}