Example #1
0
        internal static IEnumerable <IfHeaderCondition> Parse([NotNull] StringSource source, [NotNull] EntityTagComparer entityTagComparer)
        {
            while (!source.SkipWhiteSpace())
            {
                var       isNot = false;
                EntityTag?etag  = null;
                if (source.AdvanceIf("Not", StringComparison.OrdinalIgnoreCase))
                {
                    isNot = true;
                    source.SkipWhiteSpace();
                }

                Uri stateToken;
                if (CodedUrlParser.TryParse(source, out stateToken))
                {
                    // Coded-URL found
                }
                else if (source.Get() == '[')
                {
                    // Entity-tag found
                    etag = EntityTag.Parse(source).Single();
                    if (!source.AdvanceIf("]"))
                    {
                        throw new ArgumentException($"{source.Remaining} is not a valid condition (ETag not ending with ']')", nameof(source));
                    }
                }
                else
                {
                    source.Back();
                    break;
                }

                yield return(new IfHeaderCondition(isNot, stateToken, etag, entityTagComparer));
            }
        }
        internal static IEnumerable <EntityTag> Parse(StringSource source)
        {
            while (!source.SkipWhiteSpace())
            {
                bool isWeak;
                if (source.AdvanceIf("W/\"", StringComparison.OrdinalIgnoreCase))
                {
                    isWeak = true;
                }
                else if (!source.AdvanceIf("\""))
                {
                    break;
                }
                else
                {
                    isWeak = false;
                }

                var etagText = source.GetUntil('"');
                if (etagText == null)
                {
                    throw new ArgumentException($"{source.Remaining} is not a valid ETag", nameof(source));
                }

                yield return(new EntityTag(isWeak, etagText));

                if (source.Advance(1).SkipWhiteSpace())
                {
                    break;
                }

                if (!source.AdvanceIf(","))
                {
                    break;
                }
            }
        }
        internal static IEnumerable <IfHeaderList> Parse(StringSource source, EntityTagComparer etagComparer, IWebDavContext context)
        {
            Uri previousResourceTag = context.PublicAbsoluteRequestUrl;

            while (!source.SkipWhiteSpace())
            {
                if (CodedUrlParser.TryParse(source, out var resourceTag))
                {
                    // Coded-URL found
                    if (!resourceTag.IsAbsoluteUri)
                    {
                        resourceTag = new Uri(context.PublicRootUrl, resourceTag);
                    }
                    previousResourceTag = resourceTag;
                    source.SkipWhiteSpace();
                }
                else
                {
                    resourceTag = previousResourceTag;
                }

                if (!source.AdvanceIf("("))
                {
                    throw new ArgumentException($"{source.Remaining} is not a valid list (not starting with a '(')", nameof(source));
                }
                var conditions = IfHeaderCondition.Parse(source, etagComparer).ToList();
                if (!source.AdvanceIf(")"))
                {
                    throw new ArgumentException($"{source.Remaining} is not a valid list (not ending with a ')')", nameof(source));
                }

                var relativeHref = context.PublicControllerUrl.IsBaseOf(resourceTag) ? AddRootSlashToUri(context.PublicControllerUrl.MakeRelativeUri(resourceTag)) : resourceTag;
                var path         = context.PublicControllerUrl.IsBaseOf(resourceTag) ? context.PublicControllerUrl.MakeRelativeUri(resourceTag) : resourceTag;
                yield return(new IfHeaderList(resourceTag, relativeHref, path, conditions));
            }
        }
        /// <summary>
        /// Tries to parse the <c>Coded-URL</c> from a <see cref="StringSource"/>
        /// </summary>
        /// <param name="source">The <see cref="StringSource"/> to parse the <c>Coded-URL</c> from</param>
        /// <param name="codedUrl">The parsed <c>Coded-URL</c></param>
        /// <returns><see langword="true"/> when the <c>Coded-URL</c> could be parsed successfully</returns>
        internal static bool TryParse([NotNull] StringSource source, out Uri codedUrl)
        {
            if (!source.AdvanceIf("<"))
            {
                codedUrl = null;
                return(false);
            }

            // Coded-URL found
            var codedUrlText = source.GetUntil('>');

            if (codedUrlText == null)
            {
                throw new ArgumentException($"{source.Remaining} is not a valid Coded-URL (not ending with '>')", nameof(source));
            }
            source.Advance(1);
            codedUrl = new Uri(codedUrlText, UriKind.RelativeOrAbsolute);
            return(true);
        }