Skip to content

Commit

Permalink
(GH-70) Minor tweaks
Browse files Browse the repository at this point in the history
- Switch to using an extension method
- Corrected whitespace
  • Loading branch information
gep13 committed Feb 14, 2019
1 parent 25c6604 commit e755e69
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@

namespace Chocolatey.Language.Server.Utility
{

public class Utility
/// <summary>
/// Extensions for Uri
/// </summary>
public static class UriExtensions
{

/// <summary>
/// Tries to validate an URL
/// </summary>
/// <param name="url">Uri object</param>
public static bool url_is_valid(Uri url)
public static bool IsValid(this Uri url)
{
var request = (HttpWebRequest)WebRequest.Create(url);

request.Timeout = 15000;
//This would allow 301 and 302 to be valid as well
request.AllowAutoRedirect = true;
request.Timeout = 15000;

try
{
using (var response = (HttpWebResponse)request.GetResponse())
Expand All @@ -29,6 +31,6 @@ public static bool url_is_valid(Uri url)
{
return false;
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace Chocolatey.Language.Server.Validations
/// TODO: Add <seealso> elements once PR is merged
public class UrlValidValidation : INuSpecRule
{

private static readonly IReadOnlyCollection<string> UrlElements = new []
{
"bugTrackerUrl",
Expand All @@ -37,10 +36,11 @@ public IEnumerable<Diagnostic> Validate(XmlDocumentSyntax syntaxTree, TextPositi
Uri uri;
if(
!Uri.IsWellFormedUriString(uriString, UriKind.Absolute) ||
!Uri.TryCreate(uriString, UriKind.Absolute, out uri) ||
!Utility.Utility.url_is_valid(uri)
!Uri.TryCreate(uriString, UriKind.Absolute, out uri) ||
!uri.IsValid()
) {
var range = textPositions.GetRange(element.StartTag.End, element.EndTag.Start);

yield return new Diagnostic {
Message = "Url in " + elementName + " is invalid. See https://github.com/chocolatey/package-validator/wiki/InvalidUrlProvided",
Severity = DiagnosticSeverity.Error,
Expand All @@ -51,4 +51,4 @@ public IEnumerable<Diagnostic> Validate(XmlDocumentSyntax syntaxTree, TextPositi
}
}
}
}
}

0 comments on commit e755e69

Please sign in to comment.