1
0
mirror of https://github.com/bitwarden/server synced 2026-01-07 02:53:38 +00:00

Changed all C# control flow block statements to include space between keyword and open paren

This commit is contained in:
Chad Scharf
2020-03-27 14:36:37 -04:00
parent 943aea9a12
commit 9800b752c0
243 changed files with 2258 additions and 2258 deletions

View File

@@ -41,13 +41,13 @@ namespace Bit.Icons.Controllers
[HttpGet("{hostname}/icon.png")]
public async Task<IActionResult> Get(string hostname)
{
if(string.IsNullOrWhiteSpace(hostname) || !hostname.Contains("."))
if (string.IsNullOrWhiteSpace(hostname) || !hostname.Contains("."))
{
return new BadRequestResult();
}
var url = $"http://{hostname}";
if(!Uri.TryCreate(url, UriKind.Absolute, out var uri))
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri))
{
return new BadRequestResult();
}
@@ -60,10 +60,10 @@ namespace Bit.Icons.Controllers
//}
var mappedDomain = _domainMappingService.MapDomain(domain);
if(!_iconsSettings.CacheEnabled || !_memoryCache.TryGetValue(mappedDomain, out Icon icon))
if (!_iconsSettings.CacheEnabled || !_memoryCache.TryGetValue(mappedDomain, out Icon icon))
{
var result = await _iconFetchingService.GetIconAsync(domain);
if(result == null)
if (result == null)
{
_logger.LogWarning("Null result returned for {0}.", domain);
icon = null;
@@ -74,7 +74,7 @@ namespace Bit.Icons.Controllers
}
// Only cache not found and smaller images (<= 50kb)
if(_iconsSettings.CacheEnabled && (icon == null || icon.Image.Length <= 50012))
if (_iconsSettings.CacheEnabled && (icon == null || icon.Image.Length <= 50012))
{
_logger.LogInformation("Cache icon for {0}.", domain);
_memoryCache.Set(mappedDomain, icon, new MemoryCacheEntryOptions
@@ -86,7 +86,7 @@ namespace Bit.Icons.Controllers
}
}
if(icon == null)
if (icon == null)
{
return new NotFoundResult();
}

View File

@@ -67,7 +67,7 @@ namespace Bit.Icons.Models
public static bool TryParseBaseDomain(string domainString, out string result)
{
if(Regex.IsMatch(domainString, IpRegex))
if (Regex.IsMatch(domainString, IpRegex))
{
result = domainString;
return true;
@@ -91,7 +91,7 @@ namespace Bit.Icons.Models
MatchingRule = null;
// If the fqdn is empty, we have a problem already
if(domainString.Trim() == string.Empty)
if (domainString.Trim() == string.Empty)
{
throw new ArgumentException("The domain cannot be blank");
}
@@ -100,7 +100,7 @@ namespace Bit.Icons.Models
MatchingRule = FindMatchingTLDRule(domainString);
// At this point, no rules match, we have a problem
if(MatchingRule == null)
if (MatchingRule == null)
{
throw new FormatException("The domain does not have a recognized TLD");
}
@@ -110,7 +110,7 @@ namespace Bit.Icons.Models
var tldIndex = 0;
// First, determine what type of rule we have, and set the TLD accordingly
switch(MatchingRule.Type)
switch (MatchingRule.Type)
{
case TLDRule.RuleType.Normal:
tldIndex = domainString.LastIndexOf("." + MatchingRule.Name);
@@ -140,13 +140,13 @@ namespace Bit.Icons.Models
// If we have 0 parts left, there is just a tld and no domain or subdomain
// If we have 1 part, it's the domain, and there is no subdomain
// If we have 2+ parts, the last part is the domain, the other parts (combined) are the subdomain
if(lstRemainingParts.Count > 0)
if (lstRemainingParts.Count > 0)
{
// Set the domain:
SLD = lstRemainingParts[lstRemainingParts.Count - 1];
// Set the subdomain, if there is one to set:
if(lstRemainingParts.Count > 1)
if (lstRemainingParts.Count > 1)
{
// We strip off the trailing period, too
SubDomain = tempSudomainAndDomain.Substring(0, tempSudomainAndDomain.Length - SLD.Length - 1);
@@ -169,23 +169,23 @@ namespace Bit.Icons.Models
// Our 'matches' collection:
var ruleMatches = new List<TLDRule>();
foreach(string domainPart in lstDomainParts)
foreach (string domainPart in lstDomainParts)
{
// Add on our next domain part:
checkAgainst = string.Format("{0}.{1}", domainPart, checkAgainst);
// If we end in a period, strip it off:
if(checkAgainst.EndsWith("."))
if (checkAgainst.EndsWith("."))
{
checkAgainst = checkAgainst.Substring(0, checkAgainst.Length - 1);
}
var rules = Enum.GetValues(typeof(TLDRule.RuleType)).Cast<TLDRule.RuleType>();
foreach(var rule in rules)
foreach (var rule in rules)
{
// Try to match rule:
TLDRule result;
if(TLDRulesCache.Instance.TLDRuleLists[rule].TryGetValue(checkAgainst, out result))
if (TLDRulesCache.Instance.TLDRuleLists[rule].TryGetValue(checkAgainst, out result))
{
ruleMatches.Add(result);
}
@@ -210,12 +210,12 @@ namespace Bit.Icons.Models
public TLDRule(string RuleInfo)
{
// Parse the rule and set properties accordingly:
if(RuleInfo.StartsWith("*"))
if (RuleInfo.StartsWith("*"))
{
Type = RuleType.Wildcard;
Name = RuleInfo.Substring(2);
}
else if(RuleInfo.StartsWith("!"))
else if (RuleInfo.StartsWith("!"))
{
Type = RuleType.Exception;
Name = RuleInfo.Substring(1);
@@ -229,7 +229,7 @@ namespace Bit.Icons.Models
public int CompareTo(TLDRule other)
{
if(other == null)
if (other == null)
{
return -1;
}
@@ -261,11 +261,11 @@ namespace Bit.Icons.Models
{
get
{
if(_uniqueInstance == null)
if (_uniqueInstance == null)
{
lock(_syncObj)
lock (_syncObj)
{
if(_uniqueInstance == null)
if (_uniqueInstance == null)
{
_uniqueInstance = new TLDRulesCache();
}
@@ -279,7 +279,7 @@ namespace Bit.Icons.Models
public static void Reset()
{
lock(_syncObj)
lock (_syncObj)
{
_uniqueInstance = null;
}
@@ -289,7 +289,7 @@ namespace Bit.Icons.Models
{
var results = new Dictionary<TLDRule.RuleType, IDictionary<string, TLDRule>>();
var rules = Enum.GetValues(typeof(TLDRule.RuleType)).Cast<TLDRule.RuleType>();
foreach(var rule in rules)
foreach (var rule in rules)
{
results[rule] = new Dictionary<string, TLDRule>(StringComparer.CurrentCultureIgnoreCase);
}
@@ -301,7 +301,7 @@ namespace Bit.Icons.Models
// b.) Blank
var rulesStrings = ruleStrings
.Where(ruleString => !ruleString.StartsWith("//") && ruleString.Trim().Length != 0);
foreach(var ruleString in rulesStrings)
foreach (var ruleString in rulesStrings)
{
var result = new TLDRule(ruleString);
results[result.Type][result.Name] = result;
@@ -318,9 +318,9 @@ namespace Bit.Icons.Models
var assembly = typeof(TLDRulesCache).GetTypeInfo().Assembly;
var stream = assembly.GetManifestResourceStream("Bit.Icons.Resources.public_suffix_list.dat");
string line;
using(var reader = new StreamReader(stream))
using (var reader = new StreamReader(stream))
{
while((line = reader.ReadLine()) != null)
while ((line = reader.ReadLine()) != null)
{
yield return line;
}

View File

@@ -7,30 +7,30 @@ namespace Bit.Icons.Models
public IconResult(string href, string sizes)
{
Path = href;
if(!string.IsNullOrWhiteSpace(sizes))
if (!string.IsNullOrWhiteSpace(sizes))
{
var sizeParts = sizes.Split('x');
if(sizeParts.Length == 2 && int.TryParse(sizeParts[0].Trim(), out var width) &&
if (sizeParts.Length == 2 && int.TryParse(sizeParts[0].Trim(), out var width) &&
int.TryParse(sizeParts[1].Trim(), out var height))
{
DefinedWidth = width;
DefinedHeight = height;
if(width == height)
if (width == height)
{
if(width == 32)
if (width == 32)
{
Priority = 1;
}
else if(width == 64)
else if (width == 64)
{
Priority = 2;
}
else if(width >= 24 && width <= 128)
else if (width >= 24 && width <= 128)
{
Priority = 3;
}
else if(width == 16)
else if (width == 16)
{
Priority = 4;
}
@@ -42,7 +42,7 @@ namespace Bit.Icons.Models
}
}
if(Priority == 0)
if (Priority == 0)
{
Priority = 200;
}

View File

@@ -15,7 +15,7 @@ namespace Bit.Icons.Services
public string MapDomain(string hostname)
{
if(_map.ContainsKey(hostname))
if (_map.ContainsKey(hostname))
{
return _map[hostname];
}

View File

@@ -64,13 +64,13 @@ namespace Bit.Icons.Services
public async Task<IconResult> GetIconAsync(string domain)
{
if(_ipRegex.IsMatch(domain))
if (_ipRegex.IsMatch(domain))
{
_logger.LogWarning("IP address: {0}.", domain);
return null;
}
if(!Uri.TryCreate($"https://{domain}", UriKind.Absolute, out var parsedHttpsUri))
if (!Uri.TryCreate($"https://{domain}", UriKind.Absolute, out var parsedHttpsUri))
{
_logger.LogWarning("Bad domain: {0}.", domain);
return null;
@@ -78,24 +78,24 @@ namespace Bit.Icons.Services
var uri = parsedHttpsUri;
var response = await GetAndFollowAsync(uri, 2);
if((response == null || !response.IsSuccessStatusCode) &&
if ((response == null || !response.IsSuccessStatusCode) &&
Uri.TryCreate($"http://{parsedHttpsUri.Host}", UriKind.Absolute, out var parsedHttpUri))
{
Cleanup(response);
uri = parsedHttpUri;
response = await GetAndFollowAsync(uri, 2);
if(response == null || !response.IsSuccessStatusCode)
if (response == null || !response.IsSuccessStatusCode)
{
var dotCount = domain.Count(c => c == '.');
if(dotCount > 1 && DomainName.TryParseBaseDomain(domain, out var baseDomain) &&
if (dotCount > 1 && DomainName.TryParseBaseDomain(domain, out var baseDomain) &&
Uri.TryCreate($"https://{baseDomain}", UriKind.Absolute, out var parsedBaseUri))
{
Cleanup(response);
uri = parsedBaseUri;
response = await GetAndFollowAsync(uri, 2);
}
else if(dotCount < 2 &&
else if (dotCount < 2 &&
Uri.TryCreate($"https://www.{parsedHttpsUri.Host}", UriKind.Absolute, out var parsedWwwUri))
{
Cleanup(response);
@@ -105,7 +105,7 @@ namespace Bit.Icons.Services
}
}
if(response?.Content == null || !response.IsSuccessStatusCode)
if (response?.Content == null || !response.IsSuccessStatusCode)
{
_logger.LogWarning("Couldn't load a website for {0}: {1}.", domain,
response?.StatusCode.ToString() ?? "null");
@@ -114,12 +114,12 @@ namespace Bit.Icons.Services
}
var parser = new HtmlParser();
using(response)
using(var htmlStream = await response.Content.ReadAsStreamAsync())
using(var document = await parser.ParseDocumentAsync(htmlStream))
using (response)
using (var htmlStream = await response.Content.ReadAsStreamAsync())
using (var document = await parser.ParseDocumentAsync(htmlStream))
{
uri = response.RequestMessage.RequestUri;
if(document.DocumentElement == null)
if (document.DocumentElement == null)
{
_logger.LogWarning("No DocumentElement for {0}.", domain);
return null;
@@ -127,10 +127,10 @@ namespace Bit.Icons.Services
var baseUrl = "/";
var baseUrlNode = document.QuerySelector("head base[href]");
if(baseUrlNode != null)
if (baseUrlNode != null)
{
var hrefAttr = baseUrlNode.Attributes["href"];
if(!string.IsNullOrWhiteSpace(hrefAttr?.Value))
if (!string.IsNullOrWhiteSpace(hrefAttr?.Value))
{
baseUrl = hrefAttr.Value;
}
@@ -141,33 +141,33 @@ namespace Bit.Icons.Services
var icons = new List<IconResult>();
var links = document.QuerySelectorAll("head link[href]");
if(links != null)
if (links != null)
{
foreach(var link in links.Take(200))
foreach (var link in links.Take(200))
{
var hrefAttr = link.Attributes["href"];
if(string.IsNullOrWhiteSpace(hrefAttr?.Value))
if (string.IsNullOrWhiteSpace(hrefAttr?.Value))
{
continue;
}
var relAttr = link.Attributes["rel"];
var sizesAttr = link.Attributes["sizes"];
if(relAttr != null && _iconRels.Contains(relAttr.Value.ToLower()))
if (relAttr != null && _iconRels.Contains(relAttr.Value.ToLower()))
{
icons.Add(new IconResult(hrefAttr.Value, sizesAttr?.Value));
}
else if(relAttr == null || !_blacklistedRels.Contains(relAttr.Value.ToLower()))
else if (relAttr == null || !_blacklistedRels.Contains(relAttr.Value.ToLower()))
{
try
{
var extension = Path.GetExtension(hrefAttr.Value);
if(_iconExtensions.Contains(extension.ToLower()))
if (_iconExtensions.Contains(extension.ToLower()))
{
icons.Add(new IconResult(hrefAttr.Value, sizesAttr?.Value));
}
}
catch(ArgumentException) { }
catch (ArgumentException) { }
}
sizesAttr = null;
@@ -179,29 +179,29 @@ namespace Bit.Icons.Services
}
var iconResultTasks = new List<Task>();
foreach(var icon in icons.OrderBy(i => i.Priority).Take(10))
foreach (var icon in icons.OrderBy(i => i.Priority).Take(10))
{
Uri iconUri = null;
if(icon.Path.StartsWith("//") && Uri.TryCreate($"{GetScheme(uri)}://{icon.Path.Substring(2)}",
if (icon.Path.StartsWith("//") && Uri.TryCreate($"{GetScheme(uri)}://{icon.Path.Substring(2)}",
UriKind.Absolute, out var slashUri))
{
iconUri = slashUri;
}
else if(Uri.TryCreate(icon.Path, UriKind.Relative, out var relUri))
else if (Uri.TryCreate(icon.Path, UriKind.Relative, out var relUri))
{
iconUri = ResolveUri($"{GetScheme(uri)}://{uri.Host}", baseUrl, relUri.OriginalString);
}
else if(Uri.TryCreate(icon.Path, UriKind.Absolute, out var absUri))
else if (Uri.TryCreate(icon.Path, UriKind.Absolute, out var absUri))
{
iconUri = absUri;
}
if(iconUri != null)
if (iconUri != null)
{
var task = GetIconAsync(iconUri).ContinueWith(async (r) =>
{
var result = await r;
if(result != null)
if (result != null)
{
icon.Path = iconUri.ToString();
icon.Icon = result.Icon;
@@ -212,11 +212,11 @@ namespace Bit.Icons.Services
}
await Task.WhenAll(iconResultTasks);
if(!icons.Any(i => i.Icon != null))
if (!icons.Any(i => i.Icon != null))
{
var faviconUri = ResolveUri($"{GetScheme(uri)}://{uri.Host}", "favicon.ico");
var result = await GetIconAsync(faviconUri);
if(result != null)
if (result != null)
{
icons.Add(result);
}
@@ -233,9 +233,9 @@ namespace Bit.Icons.Services
private async Task<IconResult> GetIconAsync(Uri uri)
{
using(var response = await GetAndFollowAsync(uri, 2))
using (var response = await GetAndFollowAsync(uri, 2))
{
if(response?.Content?.Headers == null || !response.IsSuccessStatusCode)
if (response?.Content?.Headers == null || !response.IsSuccessStatusCode)
{
response?.Content?.Dispose();
return null;
@@ -244,17 +244,17 @@ namespace Bit.Icons.Services
var format = response.Content.Headers?.ContentType?.MediaType;
var bytes = await response.Content.ReadAsByteArrayAsync();
response.Content.Dispose();
if(format == null || !_allowedMediaTypes.Contains(format))
if (format == null || !_allowedMediaTypes.Contains(format))
{
if(HeaderMatch(bytes, _icoHeader))
if (HeaderMatch(bytes, _icoHeader))
{
format = _icoMediaType;
}
else if(HeaderMatch(bytes, _pngHeader) || HeaderMatch(bytes, _webpHeader))
else if (HeaderMatch(bytes, _pngHeader) || HeaderMatch(bytes, _webpHeader))
{
format = _pngMediaType;
}
else if(HeaderMatch(bytes, _jpegHeader))
else if (HeaderMatch(bytes, _jpegHeader))
{
format = _jpegMediaType;
}
@@ -271,7 +271,7 @@ namespace Bit.Icons.Services
private async Task<HttpResponseMessage> GetAndFollowAsync(Uri uri, int maxRedirectCount)
{
var response = await GetAsync(uri);
if(response == null)
if (response == null)
{
return null;
}
@@ -280,7 +280,7 @@ namespace Bit.Icons.Services
private async Task<HttpResponseMessage> GetAsync(Uri uri)
{
using(var message = new HttpRequestMessage())
using (var message = new HttpRequestMessage())
{
message.RequestUri = uri;
message.Method = HttpMethod.Get;
@@ -309,12 +309,12 @@ namespace Bit.Icons.Services
private async Task<HttpResponseMessage> FollowRedirectsAsync(HttpResponseMessage response,
int maxFollowCount, int followCount = 0)
{
if(response == null || response.IsSuccessStatusCode || followCount > maxFollowCount)
if (response == null || response.IsSuccessStatusCode || followCount > maxFollowCount)
{
return response;
}
if(!(response.StatusCode == HttpStatusCode.Redirect ||
if (!(response.StatusCode == HttpStatusCode.Redirect ||
response.StatusCode == HttpStatusCode.MovedPermanently ||
response.StatusCode == HttpStatusCode.RedirectKeepVerb ||
response.StatusCode == HttpStatusCode.SeeOther) ||
@@ -325,11 +325,11 @@ namespace Bit.Icons.Services
}
Uri location = null;
if(response.Headers.Location.IsAbsoluteUri)
if (response.Headers.Location.IsAbsoluteUri)
{
if(response.Headers.Location.Scheme != "http" && response.Headers.Location.Scheme != "https")
if (response.Headers.Location.Scheme != "http" && response.Headers.Location.Scheme != "https")
{
if(Uri.TryCreate($"https://{response.Headers.Location.OriginalString}",
if (Uri.TryCreate($"https://{response.Headers.Location.OriginalString}",
UriKind.Absolute, out var newUri))
{
location = newUri;
@@ -349,13 +349,13 @@ namespace Bit.Icons.Services
Cleanup(response);
var newResponse = await GetAsync(location);
if(newResponse != null)
if (newResponse != null)
{
followCount++;
var redirectedResponse = await FollowRedirectsAsync(newResponse, maxFollowCount, followCount);
if(redirectedResponse != null)
if (redirectedResponse != null)
{
if(redirectedResponse != newResponse)
if (redirectedResponse != newResponse)
{
Cleanup(newResponse);
}
@@ -374,9 +374,9 @@ namespace Bit.Icons.Services
private Uri ResolveUri(string baseUrl, params string[] paths)
{
var url = baseUrl;
foreach(var path in paths)
foreach (var path in paths)
{
if(Uri.TryCreate(new Uri(url), path, out var r))
if (Uri.TryCreate(new Uri(url), path, out var r))
{
url = r.ToString();
}

View File

@@ -56,7 +56,7 @@ namespace Bit.Icons
{
app.UseSerilog(env, appLifetime, globalSettings);
if(env.IsDevelopment())
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}