Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(GH-70) Provide Chocolatey Language Server support #98

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/Chocolatey.Language.Server/Utility/Utility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Net;

namespace Chocolatey.Language.Server.Utility
{

public class Utility
{

/// <summary>
/// Tries to validate an URL
/// </summary>
/// <param name="url">Uri object</param>
public static bool url_is_valid(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;
try
{
using (var response = (HttpWebResponse)request.GetResponse())
{
return response.StatusCode == HttpStatusCode.OK;
}
}
catch (WebException)
{
return false;
}
}
}
}
54 changes: 54 additions & 0 deletions src/Chocolatey.Language.Server/Validations/UrlValidValidation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Chocolatey.Language.Server.Utility;
using Microsoft.Language.Xml;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using DiagnosticSeverity = OmniSharp.Extensions.LanguageServer.Protocol.Models.DiagnosticSeverity;

namespace Chocolatey.Language.Server.Validations
{
/// <summary>
/// Handler to validate the length of description in the package metadata.
/// </summary>
/// TODO: Add <seealso> elements once PR is merged
public class UrlValidValidation : INuSpecRule
{

private static readonly IReadOnlyCollection<string> UrlElements = new []
{
"bugTrackerUrl",
"docsUrl",
"iconUrl",
"licenseUrl",
"mailingListUrl",
"packageSourceUrl",
"projectSourceUrl",
"projectUrl",
"wikiUrl",
};

public IEnumerable<Diagnostic> Validate(XmlDocumentSyntax syntaxTree, TextPositions textPositions)
{
foreach (var elementName in UrlElements) {
var element = syntaxTree.DescendantNodes().OfType<XmlElementSyntax>().FirstOrDefault(x => string.Equals(x.Name, elementName, StringComparison.OrdinalIgnoreCase));
if (element != null) {
var uriString = element.GetContentValue().Trim();
Uri uri;
if(
!Uri.IsWellFormedUriString(uriString, UriKind.Absolute) ||
!Uri.TryCreate(uriString, UriKind.Absolute, out uri) ||
!Utility.Utility.url_is_valid(uri)
) {
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,
Range = range
};
}
}
}
}
}
}