-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebAddress.cs
More file actions
67 lines (58 loc) · 2.28 KB
/
WebAddress.cs
File metadata and controls
67 lines (58 loc) · 2.28 KB
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
namespace ValueObjects.Network
{
public class WebAddress : ValueObject
{
public string Address { get; private set; }
public string Scheme { get; private set; }
public string Host { get; private set; }
private WebAddress(string url)
{
Ensure.Argument.NotNullOrEmpty(url, nameof(url));
Ensure.Argument.Is(IsValudUrl(ref url), $"The website address '{url}' is invalid.");
var uri = new Uri(url);
Address = url;
Scheme = uri.Scheme;
Host = uri.Host;
}
public static WebAddress FromUrl(string url)
{
return new WebAddress(url);
}
/// <summary>
/// Checks if a given Url is valid.
/// Valid URLs should include all of the following “prefixes”: https, http, www
/// - Url must contain http:// or https://
/// - Url may contain only one instance of www.
/// - Url Host name type must be Dns
/// - Url max length is 100
/// </summary>
/// <param name="url">Web site Url</param>
/// <returns><c>true</c> if the Url is valid, otherwise <c>false</c></returns>
private static bool IsValudUrl(ref string url)
{
if (url.StartsWith("www."))
{
url = "http://" + url;
}
return Uri.TryCreate(url, UriKind.Absolute, out Uri? uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttp
|| uriResult.Scheme == Uri.UriSchemeHttps)
&& uriResult.Host.Replace("www.", "").Split('.').Count() > 1
&& uriResult.HostNameType == UriHostNameType.Dns
&& uriResult.Host.Length > uriResult.Host.LastIndexOf(".") + 1
&& 100 >= url.Length;
}
protected override IEnumerable<object> GetAtomicValues()
{
yield return Address.ToLower();
}
public static implicit operator string(WebAddress webAddress)
{
return webAddress.Address;
}
public static explicit operator WebAddress(string url)
{
return new WebAddress(url);
}
}
}