< Summary

Information
Class: ValidateLib.Metadata.MetdataLocation.LinkHeaderProcessor
Assembly: validatelib.dll
File(s): C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\Metadata\MetadataLocation\LinkHeaderProcessor.cs
Line coverage
71%
Covered lines: 25
Uncovered lines: 10
Coverable lines: 35
Total lines: 72
Line coverage: 71.4%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\Metadata\MetadataLocation\LinkHeaderProcessor.cs

#LineLine coverage
 1using System.Text.RegularExpressions;
 2
 3namespace ValidateLib.Metadata.MetdataLocation
 4{
 5    /// <summary>
 6    /// Processes HTTP link headers and extracts possible metadata locations.
 7    /// </summary>
 8    public class LinkHeaderProcessor : ILinkHeaderProcessor
 9    {
 110        static Regex linkHeaderRegex = new Regex(@"<([^>]+)>;\s*(?:rel=""([^""]+)"";\s*type=""([^""]+)""|type=""([^""]+)
 11        public static List<string> GetMetadataLocationsFromLinkHeader(HttpResponseMessage response)
 12        {
 113            List<string> locations = new List<string>();
 14            // Check if the request was successful
 115            if (response.IsSuccessStatusCode)
 16            {
 17
 18                // Check for Link headers
 119                if (response.Headers.TryGetValues("Link", out var linkHeaderValues))
 20                {
 121                    ProcessLinkHeaderValues(linkHeaderValues, locations);
 22                }
 23            }
 24
 125            return locations;
 26        }
 27        static void ProcessLinkHeaderValues(IEnumerable<string> linkHeaderValues, List<string> locations)
 28        {
 129            foreach (var linkHeaderValue in linkHeaderValues)
 30            {
 131                var matches = linkHeaderRegex.Matches(linkHeaderValue);
 132                for (int i = 0; i < matches.Count; i++)
 33                {
 134                    var match = matches[i];
 135                    var url = match.Groups[1].Value;
 136                    var rel = match.Groups[2].Success ? match.Groups[2].Value : match.Groups[4].Value;
 137                    var type = match.Groups[3].Success ? match.Groups[3].Value : match.Groups[5].Value;
 138                    if (isValidLinkHeader(url, rel, type))
 139                        locations.Add(url);
 40                }
 41            }
 142        }
 43        static bool isValidLinkHeader(string? url, string? rel, string? type)
 44        {
 145            string[] validTypes = new string[]
 146            {
 147                "application/csvm+json",
 148                "application/ld+json",
 149                "application/json"
 150            };
 151            if (url is null || rel is null || type is null)
 052                return false;
 153            if (rel == "describedby" && validTypes.Contains(type))
 154                return true;
 055            return false;
 56        }
 57        class LinkHeader
 58        {
 059            public string Url { get; }
 060            public string RelationType { get; }
 061            public string MediaType { get; }
 62
 063            public LinkHeader(string url, string relationType, string mediaType)
 64            {
 065                Url = url;
 066                RelationType = relationType;
 067                MediaType = mediaType;
 068            }
 69        }
 70
 71    }
 72}