Example #1
0
        private static bool TryParseInternal(string value, HttpRangeSpecifier specifier, out string parseFailureReason)
        {
            if (specifier == null)
            {
                throw new ArgumentNullException("specifier");
            }
            HttpRangeSpecifier.StrSegment strSegment = new HttpRangeSpecifier.StrSegment(value);
            HttpRangeSpecifier.ParseState parseState = HttpRangeSpecifier.ParseState.Start;
            parseFailureReason = string.Empty;
            int  i          = 0;
            int  length     = value.Length;
            long rangeStart = -1L;

            while (i < length)
            {
                char c = value[i];
                switch (parseState)
                {
                case HttpRangeSpecifier.ParseState.Start:
                    if (c != ' ' && c != '\t')
                    {
                        if (strSegment.Start == -1)
                        {
                            strSegment.Start = i;
                        }
                        if (c == '=')
                        {
                            strSegment.SetLengthFromTerminatingIndex(i);
                            strSegment.Trim();
                            specifier.RangeUnitSpecifier = strSegment.ToString();
                            parseState = HttpRangeSpecifier.ParseState.RangeStart;
                            rangeStart = -1L;
                            strSegment.Reset();
                        }
                    }
                    break;

                case HttpRangeSpecifier.ParseState.RangeStart:
                    if (c != ' ' && c != '\t')
                    {
                        if (strSegment.Start == -1)
                        {
                            strSegment.Start = i;
                        }
                        if (c == '-' || c == ',')
                        {
                            strSegment.SetLengthFromTerminatingIndex(i);
                            strSegment.Trim();
                            if (c != '-')
                            {
                                parseFailureReason = "Invalid range, missing '-' character at " + (strSegment.Start + strSegment.Length);
                                return(false);
                            }
                            if (strSegment.Length > 0 && !long.TryParse(strSegment.ToString(), NumberStyles.Integer, CultureInfo.InvariantCulture, out rangeStart))
                            {
                                parseFailureReason = "Could not parse first-byte-pos at " + strSegment.Start;
                                return(false);
                            }
                            parseState = HttpRangeSpecifier.ParseState.RangeEnd;
                            strSegment.Reset();
                        }
                    }
                    break;

                case HttpRangeSpecifier.ParseState.RangeEnd:
                    if (c != ' ' && c != '\t')
                    {
                        if (strSegment.Start == -1)
                        {
                            strSegment.Start = i;
                        }
                        if (c == ',')
                        {
                            strSegment.SetLengthFromTerminatingIndex(i);
                            strSegment.Trim();
                            if (!HttpRangeSpecifier.ProcessRangeEnd(specifier, ref parseFailureReason, strSegment, rangeStart))
                            {
                                return(false);
                            }
                            rangeStart = -1L;
                            parseState = HttpRangeSpecifier.ParseState.RangeStart;
                            strSegment.Reset();
                        }
                    }
                    break;
                }
                i++;
            }
            if (strSegment.Start != -1)
            {
                strSegment.SetLengthFromTerminatingIndex(i);
                strSegment.Trim();
                if (parseState == HttpRangeSpecifier.ParseState.Start)
                {
                    specifier.RangeUnitSpecifier = strSegment.ToString();
                }
                if (parseState == HttpRangeSpecifier.ParseState.RangeStart)
                {
                    parseFailureReason = "Invalid range, missing '-' character at " + (strSegment.Start + strSegment.Length);
                    return(false);
                }
            }
            else
            {
                if (parseState == HttpRangeSpecifier.ParseState.Start)
                {
                    parseFailureReason = "Did not find range unit specifier";
                    return(false);
                }
                if (parseState == HttpRangeSpecifier.ParseState.RangeStart)
                {
                    parseFailureReason = "Expected range value at the end.";
                    return(false);
                }
            }
            if (parseState == HttpRangeSpecifier.ParseState.RangeEnd && !HttpRangeSpecifier.ProcessRangeEnd(specifier, ref parseFailureReason, strSegment, rangeStart))
            {
                return(false);
            }
            if (specifier.RangeCollection.Count == 0)
            {
                parseFailureReason = "No ranges found.";
                return(false);
            }
            return(true);
        }
Example #2
0
        private static bool ProcessRangeEnd(HttpRangeSpecifier specifier, ref string parseFailureReason, HttpRangeSpecifier.StrSegment currentSegment, long rangeStart)
        {
            long rangeEnd = -1L;

            if (currentSegment.Start >= 0 && currentSegment.Length > 0 && !long.TryParse(currentSegment.ToString(), NumberStyles.Integer, CultureInfo.InvariantCulture, out rangeEnd))
            {
                parseFailureReason = "Could not parse last-byte-pos at " + currentSegment.Start;
                return(false);
            }
            if (!HttpRangeSpecifier.AddRange(specifier, rangeStart, rangeEnd))
            {
                parseFailureReason = "Invalid range specification near " + currentSegment.Start;
                return(false);
            }
            return(true);
        }