mirror of
https://github.com/bitwarden/android.git
synced 2025-01-11 18:57:39 +03:00
Url helpers
This commit is contained in:
parent
576f44a924
commit
40598721f1
2 changed files with 73 additions and 12 deletions
|
@ -1,9 +1,19 @@
|
|||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Bit.Core.Utilities
|
||||
{
|
||||
public static class CoreHelpers
|
||||
{
|
||||
private static string IpRegex =
|
||||
"^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
|
||||
"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
|
||||
"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
|
||||
"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
|
||||
|
||||
private static string TldEndingRegex =
|
||||
".*\\.(com|net|org|edu|uk|gov|ca|de|jp|fr|au|ru|ch|io|es|us|co|xyz|info|ly|mil)$";
|
||||
|
||||
public static readonly DateTime Epoc = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
public static bool InDebugMode()
|
||||
|
@ -14,5 +24,68 @@ namespace Bit.Core.Utilities
|
|||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static string GetHostname(string uriString)
|
||||
{
|
||||
return GetUri(uriString)?.Host;
|
||||
}
|
||||
|
||||
public static string GetHost(string uriString)
|
||||
{
|
||||
var uri = GetUri(uriString);
|
||||
if(uri != null)
|
||||
{
|
||||
if(uri.IsDefaultPort)
|
||||
{
|
||||
return uri.Host;
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Format("{0}:{1}", uri.Host, uri.Port);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static string GetDomain(string uriString)
|
||||
{
|
||||
var uri = GetUri(uriString);
|
||||
if(uri == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if(uri.Host == "localhost" || Regex.IsMatch(uriString, IpRegex))
|
||||
{
|
||||
return uri.Host;
|
||||
}
|
||||
try
|
||||
{
|
||||
if(DomainName.TryParseBaseDomain(uri.Host, out var baseDomain))
|
||||
{
|
||||
return baseDomain ?? uri.Host;
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Uri GetUri(string uriString)
|
||||
{
|
||||
if(string.IsNullOrWhiteSpace(uriString))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var httpUrl = uriString.StartsWith("https://") || uriString.StartsWith("http://");
|
||||
if(!httpUrl && !uriString.Contains("://") && Regex.IsMatch(uriString, TldEndingRegex))
|
||||
{
|
||||
uriString = "http://" + uriString;
|
||||
}
|
||||
if(Uri.TryCreate(uriString, UriKind.Absolute, out var uri))
|
||||
{
|
||||
return uri;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,19 +4,12 @@ using System.Diagnostics;
|
|||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Bit.Core.Utilities
|
||||
{
|
||||
// ref: https://github.com/danesparza/domainname-parser
|
||||
public class DomainName
|
||||
{
|
||||
private const string IpRegex =
|
||||
"^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
|
||||
"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
|
||||
"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
|
||||
"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
|
||||
|
||||
private readonly string _subDomain = string.Empty;
|
||||
private readonly string _domain = string.Empty;
|
||||
private readonly string _tld = string.Empty;
|
||||
|
@ -66,11 +59,6 @@ namespace Bit.Core.Utilities
|
|||
|
||||
public static bool TryParseBaseDomain(string domainString, out string result)
|
||||
{
|
||||
if(Regex.IsMatch(domainString, IpRegex))
|
||||
{
|
||||
result = domainString;
|
||||
return true;
|
||||
}
|
||||
var retVal = TryParse(domainString, out DomainName domain);
|
||||
result = domain?.BaseDomain;
|
||||
return retVal;
|
||||
|
|
Loading…
Reference in a new issue