private void AddHeaderInfo(
     HeaderDescriptor descriptor,
     HttpHeaders.HeaderStoreItemInfo sourceInfo)
 {
     HttpHeaders.HeaderStoreItemInfo addHeaderToStore = this.CreateAndAddHeaderToStore(descriptor);
     if (descriptor.Parser == null)
     {
         addHeaderToStore.ParsedValue = HttpHeaders.CloneStringHeaderInfoValues(sourceInfo.ParsedValue);
     }
     else
     {
         addHeaderToStore.InvalidValue = HttpHeaders.CloneStringHeaderInfoValues(sourceInfo.InvalidValue);
         if (sourceInfo.ParsedValue == null)
         {
             return;
         }
         if (!(sourceInfo.ParsedValue is List <object> parsedValue))
         {
             HttpHeaders.CloneAndAddValue(addHeaderToStore, sourceInfo.ParsedValue);
         }
         else
         {
             foreach (object source in parsedValue)
             {
                 HttpHeaders.CloneAndAddValue(addHeaderToStore, source);
             }
         }
     }
 }
        internal bool ContainsParsedValue(HeaderDescriptor descriptor, object value)
        {
            if (this._headerStore == null)
            {
                return(false);
            }
            HttpHeaders.HeaderStoreItemInfo info = (HttpHeaders.HeaderStoreItemInfo)null;
            if (!this.TryGetAndParseHeaderInfo(descriptor, out info) || info.ParsedValue == null)
            {
                return(false);
            }
            List <object>     parsedValue = info.ParsedValue as List <object>;
            IEqualityComparer comparer    = descriptor.Parser.Comparer;

            if (parsedValue == null)
            {
                return(this.AreEqual(value, info.ParsedValue, comparer));
            }
            foreach (object storeValue in parsedValue)
            {
                if (this.AreEqual(value, storeValue, comparer))
                {
                    return(true);
                }
            }
            return(false);
        }
 private bool ParseRawHeaderValues(
     HeaderDescriptor descriptor,
     HttpHeaders.HeaderStoreItemInfo info,
     bool removeEmptyHeader)
 {
     lock (info)
     {
         if (info.RawValue != null)
         {
             if (!(info.RawValue is List <string> rawValue))
             {
                 HttpHeaders.ParseSingleRawHeaderValue(descriptor, info);
             }
             else
             {
                 HttpHeaders.ParseMultipleRawHeaderValues(descriptor, info, rawValue);
             }
             info.RawValue = (object)null;
             if (info.InvalidValue == null)
             {
                 if (info.ParsedValue == null)
                 {
                     if (removeEmptyHeader)
                     {
                         this._headerStore.Remove(descriptor);
                     }
                     return(false);
                 }
             }
         }
     }
     return(true);
 }
        private static void AddValue(
            HttpHeaders.HeaderStoreItemInfo info,
            object value,
            HttpHeaders.StoreLocation location)
        {
            switch (location)
            {
            case HttpHeaders.StoreLocation.Raw:
                object rawValue = info.RawValue;
                HttpHeaders.AddValueToStoreValue <string>(value, ref rawValue);
                info.RawValue = rawValue;
                break;

            case HttpHeaders.StoreLocation.Invalid:
                object invalidValue = info.InvalidValue;
                HttpHeaders.AddValueToStoreValue <string>(value, ref invalidValue);
                info.InvalidValue = invalidValue;
                break;

            case HttpHeaders.StoreLocation.Parsed:
                object parsedValue = info.ParsedValue;
                HttpHeaders.AddValueToStoreValue <object>(value, ref parsedValue);
                info.ParsedValue = parsedValue;
                break;
            }
        }
 private static void ParseMultipleRawHeaderValues(
     HeaderDescriptor descriptor,
     HttpHeaders.HeaderStoreItemInfo info,
     List <string> rawValues)
 {
     if (descriptor.Parser == null)
     {
         foreach (string rawValue in rawValues)
         {
             if (!HttpHeaders.ContainsInvalidNewLine(rawValue, descriptor.Name))
             {
                 HttpHeaders.AddValue(info, (object)rawValue, HttpHeaders.StoreLocation.Parsed);
             }
         }
     }
     else
     {
         foreach (string rawValue in rawValues)
         {
             if (!HttpHeaders.TryParseAndAddRawHeaderValue(descriptor, info, rawValue, true) && NetEventSource.IsEnabled)
             {
                 NetEventSource.Log.HeadersInvalidValue(descriptor.Name, rawValue);
             }
         }
     }
 }
        private static string[] GetValuesAsStrings(
            HeaderDescriptor descriptor,
            HttpHeaders.HeaderStoreItemInfo info,
            object exclude = null)
        {
            int valueCount = HttpHeaders.GetValueCount(info);

            string[] values;
            if (valueCount > 0)
            {
                values = new string[valueCount];
                int currentIndex = 0;
                HttpHeaders.ReadStoreValues <string>(values, info.RawValue, (HttpHeaderParser)null, (string)null, ref currentIndex);
                HttpHeaders.ReadStoreValues <object>(values, info.ParsedValue, descriptor.Parser, exclude, ref currentIndex);
                HttpHeaders.ReadStoreValues <string>(values, info.InvalidValue, (HttpHeaderParser)null, (string)null, ref currentIndex);
                if (currentIndex < valueCount)
                {
                    string[] strArray = new string[currentIndex];
                    Array.Copy((Array)values, 0, (Array)strArray, 0, currentIndex);
                    values = strArray;
                }
            }
            else
            {
                values = Array.Empty <string>();
            }
            return(values);
        }
 private HttpHeaders.HeaderStoreItemInfo CreateAndAddHeaderToStore(
     HeaderDescriptor descriptor)
 {
     HttpHeaders.HeaderStoreItemInfo info = new HttpHeaders.HeaderStoreItemInfo();
     this.AddHeaderToStore(descriptor, info);
     return(info);
 }
 internal void SetParsedValue(HeaderDescriptor descriptor, object value)
 {
     HttpHeaders.HeaderStoreItemInfo headerInfo = this.GetOrCreateHeaderInfo(descriptor, true);
     headerInfo.InvalidValue = (object)null;
     headerInfo.ParsedValue  = (object)null;
     headerInfo.RawValue     = (object)null;
     HttpHeaders.AddValue(headerInfo, value, HttpHeaders.StoreLocation.Parsed);
 }
 private void AddHeaderToStore(HeaderDescriptor descriptor, HttpHeaders.HeaderStoreItemInfo info)
 {
     if (this._headerStore == null)
     {
         this._headerStore = new Dictionary <HeaderDescriptor, HttpHeaders.HeaderStoreItemInfo>();
     }
     this._headerStore.Add(descriptor, info);
 }
 internal bool Contains(HeaderDescriptor descriptor)
 {
     if (this._headerStore == null)
     {
         return(false);
     }
     HttpHeaders.HeaderStoreItemInfo info = (HttpHeaders.HeaderStoreItemInfo)null;
     return(this.TryGetAndParseHeaderInfo(descriptor, out info));
 }
        private static int GetValueCount(HttpHeaders.HeaderStoreItemInfo info)
        {
            int valueCount = 0;

            HttpHeaders.UpdateValueCount <string>(info.RawValue, ref valueCount);
            HttpHeaders.UpdateValueCount <string>(info.InvalidValue, ref valueCount);
            HttpHeaders.UpdateValueCount <object>(info.ParsedValue, ref valueCount);
            return(valueCount);
        }
 private bool TryGetHeaderInfo(
     HeaderDescriptor descriptor,
     out HttpHeaders.HeaderStoreItemInfo info)
 {
     if (this._headerStore != null)
     {
         return(this._headerStore.TryGetValue(descriptor, out info));
     }
     info = (HttpHeaders.HeaderStoreItemInfo)null;
     return(false);
 }
 private HttpHeaders.HeaderStoreItemInfo GetOrCreateHeaderInfo(
     HeaderDescriptor descriptor,
     bool parseRawValues)
 {
     HttpHeaders.HeaderStoreItemInfo info = (HttpHeaders.HeaderStoreItemInfo)null;
     if (!(!parseRawValues ? this.TryGetHeaderInfo(descriptor, out info) : this.TryGetAndParseHeaderInfo(descriptor, out info)))
     {
         info = this.CreateAndAddHeaderToStore(descriptor);
     }
     return(info);
 }
 internal bool TryAddWithoutValidation(HeaderDescriptor descriptor, IEnumerable <string> values)
 {
     if (values == null)
     {
         throw new ArgumentNullException(nameof(values));
     }
     HttpHeaders.HeaderStoreItemInfo headerInfo = this.GetOrCreateHeaderInfo(descriptor, false);
     foreach (string str in values)
     {
         HttpHeaders.AddValue(headerInfo, (object)(str ?? string.Empty), HttpHeaders.StoreLocation.Raw);
     }
     return(true);
 }
 private static void CloneAndAddValue(
     HttpHeaders.HeaderStoreItemInfo destinationInfo,
     object source)
 {
     if (source is ICloneable cloneable)
     {
         HttpHeaders.AddValue(destinationInfo, cloneable.Clone(), HttpHeaders.StoreLocation.Parsed);
     }
     else
     {
         HttpHeaders.AddValue(destinationInfo, source, HttpHeaders.StoreLocation.Parsed);
     }
 }
 private void PrepareHeaderInfoForAdd(
     HeaderDescriptor descriptor,
     out HttpHeaders.HeaderStoreItemInfo info,
     out bool addToStore)
 {
     info       = (HttpHeaders.HeaderStoreItemInfo)null;
     addToStore = false;
     if (this.TryGetAndParseHeaderInfo(descriptor, out info))
     {
         return;
     }
     info       = new HttpHeaders.HeaderStoreItemInfo();
     addToStore = true;
 }
 private void ParseAndAddValue(
     HeaderDescriptor descriptor,
     HttpHeaders.HeaderStoreItemInfo info,
     string value)
 {
     if (descriptor.Parser == null)
     {
         HttpHeaders.CheckInvalidNewLine(value);
         HttpHeaders.AddValue(info, (object)(value ?? string.Empty), HttpHeaders.StoreLocation.Parsed);
     }
     else
     {
         if (!info.CanAddValue(descriptor.Parser))
         {
             throw new FormatException(SR.Format((IFormatProvider)CultureInfo.InvariantCulture, SR.net_http_headers_single_value_header, (object)descriptor.Name));
         }
         int    index = 0;
         object obj1  = descriptor.Parser.ParseValue(value, info.ParsedValue, ref index);
         if (value == null || index == value.Length)
         {
             if (obj1 == null)
             {
                 return;
             }
             HttpHeaders.AddValue(info, obj1, HttpHeaders.StoreLocation.Parsed);
         }
         else
         {
             List <object> objectList = new List <object>();
             if (obj1 != null)
             {
                 objectList.Add(obj1);
             }
             while (index < value.Length)
             {
                 object obj2 = descriptor.Parser.ParseValue(value, info.ParsedValue, ref index);
                 if (obj2 != null)
                 {
                     objectList.Add(obj2);
                 }
             }
             foreach (object obj2 in objectList)
             {
                 HttpHeaders.AddValue(info, obj2, HttpHeaders.StoreLocation.Parsed);
             }
         }
     }
 }
        internal bool RemoveParsedValue(HeaderDescriptor descriptor, object value)
        {
            if (this._headerStore == null)
            {
                return(false);
            }
            HttpHeaders.HeaderStoreItemInfo info = (HttpHeaders.HeaderStoreItemInfo)null;
            if (!this.TryGetAndParseHeaderInfo(descriptor, out info))
            {
                return(false);
            }
            bool flag = false;

            if (info.ParsedValue == null)
            {
                return(false);
            }
            IEqualityComparer comparer = descriptor.Parser.Comparer;

            if (!(info.ParsedValue is List <object> parsedValue))
            {
                if (this.AreEqual(value, info.ParsedValue, comparer))
                {
                    info.ParsedValue = (object)null;
                    flag             = true;
                }
            }
            else
            {
                foreach (object storeValue in parsedValue)
                {
                    if (this.AreEqual(value, storeValue, comparer))
                    {
                        flag = parsedValue.Remove(storeValue);
                        break;
                    }
                }
                if (parsedValue.Count == 0)
                {
                    info.ParsedValue = (object)null;
                }
            }
            if (info.IsEmpty)
            {
                this.Remove(descriptor);
            }
            return(flag);
        }
 internal bool TryGetValues(HeaderDescriptor descriptor, out IEnumerable <string> values)
 {
     if (this._headerStore == null)
     {
         values = (IEnumerable <string>)null;
         return(false);
     }
     HttpHeaders.HeaderStoreItemInfo info = (HttpHeaders.HeaderStoreItemInfo)null;
     if (this.TryGetAndParseHeaderInfo(descriptor, out info))
     {
         values = (IEnumerable <string>)HttpHeaders.GetValuesAsStrings(descriptor, info, (object)null);
         return(true);
     }
     values = (IEnumerable <string>)null;
     return(false);
 }
 private IEnumerable <KeyValuePair <HeaderDescriptor, string[]> > GetHeaderDescriptorsAndValuesCore()
 {
     foreach (KeyValuePair <HeaderDescriptor, HttpHeaders.HeaderStoreItemInfo> keyValuePair in this._headerStore)
     {
         HeaderDescriptor key = keyValuePair.Key;
         HttpHeaders.HeaderStoreItemInfo info = keyValuePair.Value;
         if (!this.ParseRawHeaderValues(key, info, false))
         {
             this._headerStore.Remove(key);
         }
         else
         {
             string[] valuesAsStrings = HttpHeaders.GetValuesAsStrings(key, info, (object)null);
             yield return(new KeyValuePair <HeaderDescriptor, string[]>(key, valuesAsStrings));
         }
     }
 }
 internal virtual void AddHeaders(HttpHeaders sourceHeaders)
 {
     if (sourceHeaders._headerStore == null)
     {
         return;
     }
     foreach (KeyValuePair <HeaderDescriptor, HttpHeaders.HeaderStoreItemInfo> keyValuePair in sourceHeaders._headerStore)
     {
         if (this._headerStore == null || !this._headerStore.ContainsKey(keyValuePair.Key))
         {
             HttpHeaders.HeaderStoreItemInfo headerStoreItemInfo = keyValuePair.Value;
             if (!sourceHeaders.ParseRawHeaderValues(keyValuePair.Key, headerStoreItemInfo, false))
             {
                 sourceHeaders._headerStore.Remove(keyValuePair.Key);
             }
             else
             {
                 this.AddHeaderInfo(keyValuePair.Key, headerStoreItemInfo);
             }
         }
     }
 }
        private string GetHeaderString(
            HeaderDescriptor descriptor,
            HttpHeaders.HeaderStoreItemInfo info,
            object exclude = null)
        {
            string[] valuesAsStrings = HttpHeaders.GetValuesAsStrings(descriptor, info, exclude);
            string   str;

            if (valuesAsStrings.Length == 1)
            {
                str = valuesAsStrings[0];
            }
            else
            {
                string separator = ", ";
                if (descriptor.Parser != null && descriptor.Parser.SupportsMultipleValues)
                {
                    separator = descriptor.Parser.Separator;
                }
                str = string.Join(separator, valuesAsStrings);
            }
            return(str);
        }
        private static void ParseSingleRawHeaderValue(
            HeaderDescriptor descriptor,
            HttpHeaders.HeaderStoreItemInfo info)
        {
            string rawValue = info.RawValue as string;

            if (descriptor.Parser == null)
            {
                if (HttpHeaders.ContainsInvalidNewLine(rawValue, descriptor.Name))
                {
                    return;
                }
                HttpHeaders.AddValue(info, (object)rawValue, HttpHeaders.StoreLocation.Parsed);
            }
            else
            {
                if (HttpHeaders.TryParseAndAddRawHeaderValue(descriptor, info, rawValue, true) || !NetEventSource.IsEnabled)
                {
                    return;
                }
                NetEventSource.Log.HeadersInvalidValue(descriptor.Name, rawValue);
            }
        }
 internal object GetParsedValues(HeaderDescriptor descriptor)
 {
     HttpHeaders.HeaderStoreItemInfo info = (HttpHeaders.HeaderStoreItemInfo)null;
     return(!this.TryGetAndParseHeaderInfo(descriptor, out info) ? (object)null : info.ParsedValue);
 }
        private static bool TryParseAndAddRawHeaderValue(
            HeaderDescriptor descriptor,
            HttpHeaders.HeaderStoreItemInfo info,
            string value,
            bool addWhenInvalid)
        {
            if (!info.CanAddValue(descriptor.Parser))
            {
                if (addWhenInvalid)
                {
                    HttpHeaders.AddValue(info, (object)(value ?? string.Empty), HttpHeaders.StoreLocation.Invalid);
                }
                return(false);
            }
            int    index       = 0;
            object parsedValue = (object)null;

            if (descriptor.Parser.TryParseValue(value, info.ParsedValue, ref index, out parsedValue))
            {
                if (value == null || index == value.Length)
                {
                    if (parsedValue != null)
                    {
                        HttpHeaders.AddValue(info, parsedValue, HttpHeaders.StoreLocation.Parsed);
                    }
                    return(true);
                }
                List <object> objectList = new List <object>();
                if (parsedValue != null)
                {
                    objectList.Add(parsedValue);
                }
                while (index < value.Length)
                {
                    if (descriptor.Parser.TryParseValue(value, info.ParsedValue, ref index, out parsedValue))
                    {
                        if (parsedValue != null)
                        {
                            objectList.Add(parsedValue);
                        }
                    }
                    else
                    {
                        if (!HttpHeaders.ContainsInvalidNewLine(value, descriptor.Name) & addWhenInvalid)
                        {
                            HttpHeaders.AddValue(info, (object)value, HttpHeaders.StoreLocation.Invalid);
                        }
                        return(false);
                    }
                }
                foreach (object obj in objectList)
                {
                    HttpHeaders.AddValue(info, obj, HttpHeaders.StoreLocation.Parsed);
                }
                return(true);
            }
            if (!HttpHeaders.ContainsInvalidNewLine(value, descriptor.Name) & addWhenInvalid)
            {
                HttpHeaders.AddValue(info, (object)(value ?? string.Empty), HttpHeaders.StoreLocation.Invalid);
            }
            return(false);
        }
 private bool TryGetAndParseHeaderInfo(
     HeaderDescriptor key,
     out HttpHeaders.HeaderStoreItemInfo info)
 {
     return(this.TryGetHeaderInfo(key, out info) && this.ParseRawHeaderValues(key, info, true));
 }