< 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
0%
Covered lines: 0
Uncovered lines: 35
Coverable lines: 35
Total lines: 72
Line coverage: 0%
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    {
 010        static Regex linkHeaderRegex = new Regex(@"<([^>]+)>;\s*(?:rel=""([^""]+)"";\s*type=""([^""]+)""|type=""([^""]+)
 11        public static List<string> GetMetadataLocationsFromLinkHeader(HttpResponseMessage response)
 12        {
 013            List<string> locations = new List<string>();
 14            // Check if the request was successful
 015            if (response.IsSuccessStatusCode)
 16            {
 17
 18                // Check for Link headers
 019                if (response.Headers.TryGetValues("Link", out var linkHeaderValues))
 20                {
 021                    ProcessLinkHeaderValues(linkHeaderValues, locations);
 22                }
 23            }
 24
 025            return locations;
 26        }
 27        static void ProcessLinkHeaderValues(IEnumerable<string> linkHeaderValues, List<string> locations)
 28        {
 029            foreach (var linkHeaderValue in linkHeaderValues)
 30            {
 031                var matches = linkHeaderRegex.Matches(linkHeaderValue);
 032                for (int i = 0; i < matches.Count; i++)
 33                {
 034                    var match = matches[i];
 035                    var url = match.Groups[1].Value;
 036                    var rel = match.Groups[2].Success ? match.Groups[2].Value : match.Groups[4].Value;
 037                    var type = match.Groups[3].Success ? match.Groups[3].Value : match.Groups[5].Value;
 038                    if (isValidLinkHeader(url, rel, type))
 039                        locations.Add(url);
 40                }
 41            }
 042        }
 43        static bool isValidLinkHeader(string? url, string? rel, string? type)
 44        {
 045            string[] validTypes = new string[]
 046            {
 047                "application/csvm+json",
 048                "application/ld+json",
 049                "application/json"
 050            };
 051            if (url is null || rel is null || type is null)
 052                return false;
 053            if (rel == "describedby" && validTypes.Contains(type))
 054                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}