private static WebSubDiscoveredUrls ParseWebLinkHeader(WebSubDiscoveredUrls webSubDiscovery, string linkHeader) { string inputString = linkHeader; int inputPos = 0; Token nextToken = new Token { Type = TokenType.None }; while (true) { try { nextToken = ReadToken(inputString, ref inputPos); if (nextToken.Type == TokenType.Url) { webSubDiscovery = ParseWebLinkHeaderValue(webSubDiscovery, inputString, ref inputPos, ref nextToken); } else if (nextToken.Type == TokenType.EOF) { break; } else { ThrowFormatException(String.Format("Unexpected token '{0}' (expected URL)", nextToken.Type), inputString, inputPos); } if (nextToken.Type == TokenType.Comma) { continue; } else if (nextToken.Type == TokenType.EOF) { break; } else { ThrowFormatException(String.Format("Unexpected token '{0}' (expected comma)", nextToken.Type), inputString, inputPos); } } catch (FormatException) { while (nextToken.Type != TokenType.Comma && nextToken.Type != TokenType.EOF) { try { nextToken = ReadToken(inputString, ref inputPos); } catch (FormatException) { } } } } return(webSubDiscovery); }
public static WebSubDiscoveredUrls ParseWebLinkHeaders(IEnumerable <string> linkHeaders) { WebSubDiscoveredUrls webSubDiscovery = new WebSubDiscoveredUrls(); foreach (string linkHeader in linkHeaders) { webSubDiscovery = ParseWebLinkHeader(webSubDiscovery, linkHeader); } return(webSubDiscovery); }
private static WebSubDiscoveredUrls ParseWebLinkHeaderValue(WebSubDiscoveredUrls webSubDiscovery, string inputString, ref int inputPos, ref Token nextToken) { string url = nextToken.Value; string rel = null; nextToken = ReadToken(inputString, ref inputPos); while (nextToken.Type == TokenType.Semicolon) { try { nextToken = ReadToken(inputString, ref inputPos); var(id, value) = ParseParameter(inputString, ref inputPos, ref nextToken); if (id == "rel" && rel == null) { rel = value; } } catch (FormatException) { while (nextToken.Type != TokenType.Semicolon && nextToken.Type != TokenType.Comma && nextToken.Type != TokenType.EOF) { try { nextToken = ReadToken(inputString, ref inputPos); } catch (FormatException) { } } } } if (rel == HUB_RELATION) { webSubDiscovery.AddHub(url); } else if (rel == TOPIC_RELATION) { if (!String.IsNullOrWhiteSpace(webSubDiscovery.Topic)) { throw new WebSubDiscoveryException("Multiple canonical URLs for the topic have been found."); } webSubDiscovery.Topic = url; } return(webSubDiscovery); }
public async Task <WebSubDiscoveredUrls> DiscoverAsync(string requestUri, CancellationToken cancellationToken) { HttpResponseMessage discoveryResponse = await _httpClient.GetAsync(requestUri, HttpCompletionOption.ResponseHeadersRead, cancellationToken); if (discoveryResponse.StatusCode == HttpStatusCode.OK) { if (discoveryResponse.Headers.Contains(LINK_HEADER)) { WebSubDiscoveredUrls webSubDiscovery = WebLinkParser.ParseWebLinkHeaders(discoveryResponse.Headers.GetValues(LINK_HEADER)); if (RequiredUrlsIdentified(webSubDiscovery)) { return(webSubDiscovery); } } } throw new WebSubDiscoveryException("The discovery mechanism haven't identified required URLs.", discoveryResponse); }
private static bool RequiredUrlsIdentified(WebSubDiscoveredUrls webSubDiscovery) { return(!String.IsNullOrWhiteSpace(webSubDiscovery.Topic) && (webSubDiscovery.Hubs != null) && (webSubDiscovery.Hubs.Any())); }