Ejemplo n.º 1
0
        public void Add(string header)
        {
            if (string.IsNullOrEmpty(header))
            {
                throw new ArgumentNullException(nameof(header));
            }
            int colpos = header.IndexOf(':');

            // check for badly formed header passed in
            if (colpos < 0)
            {
                throw new ArgumentException(SR.net_WebHeaderMissingColon, nameof(header));
            }
            string name  = header.Substring(0, colpos);
            string value = header.Substring(colpos + 1);

            name = HttpValidationHelpers.CheckBadHeaderNameChars(name);
            ThrowOnRestrictedHeader(name);
            value = HttpValidationHelpers.CheckBadHeaderValueChars(value);
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Info(this, $"Add({header}) calling InnerCollection.Add() key:[{name}], value:[{value}]");
            }
            if (_type == WebHeaderCollectionType.WebResponse)
            {
                if (value != null && value.Length > ushort.MaxValue)
                {
                    throw new ArgumentOutOfRangeException(nameof(value), value, string.Format(CultureInfo.InvariantCulture, SR.net_headers_toolong, ushort.MaxValue));
                }
            }
            InvalidateCachedArrays();
            InnerCollection.Add(name, value);
        }
Ejemplo n.º 2
0
        public override void Add(string name, string value)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (name.Length == 0)
            {
                throw new ArgumentException(SR.Format(SR.net_emptyStringCall, nameof(name)), nameof(name));
            }

            name = HttpValidationHelpers.CheckBadHeaderNameChars(name);
            ThrowOnRestrictedHeader(name);
            value = HttpValidationHelpers.CheckBadHeaderValueChars(value);
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Info(this, $"calling InnerCollection.Add() key:[{name}], value:[{value}]");
            }
            if (_type == WebHeaderCollectionType.WebResponse)
            {
                if (value != null && value.Length > ushort.MaxValue)
                {
                    throw new ArgumentOutOfRangeException(nameof(value), value, string.Format(CultureInfo.InvariantCulture, SR.net_headers_toolong, ushort.MaxValue));
                }
            }
            InvalidateCachedArrays();
            InnerCollection.Add(name, value);
        }
Ejemplo n.º 3
0
        // Remove -
        // Routine Description:
        //     Removes give header with validation to see if they are "proper" headers.
        //     If the header is a special header, listed in RestrictedHeaders object,
        //     then this call will cause an exception indicating as such.
        // Arguments:
        //     name - header-name to remove
        // Return Value:
        //     None

        /// <devdoc>
        ///    <para>Removes the specified header.</para>
        /// </devdoc>
        public void Remove(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            name = HttpValidationHelpers.CheckBadHeaderNameChars(name);

            if (IsInitialized)
            {
                InvalidateCachedArray();

                _entriesDictionary.Remove(name);

                List <string>  list     = _entriesList;
                StringComparer comparer = StringComparer.OrdinalIgnoreCase;
                for (int i = list.Count - 1; i >= 0; i--)
                {
                    if (comparer.Equals(name, list[i]))
                    {
                        list.RemoveAt(i);
                        break;
                    }
                }

                Debug.Assert(_entriesList.FindIndex(s => StringComparer.OrdinalIgnoreCase.Equals(s, name)) == -1,
                             $"'{name}' must not be in {nameof(_entriesList)}.");

                Debug.Assert(_entriesDictionary.Count == _entriesList.Count, "Counts must be equal.");
            }
        }
Ejemplo n.º 4
0
        public void AddRange(string rangeSpecifier, long from, long to)
        {
            //
            // Do some range checking before assembling the header
            //

            if (rangeSpecifier == null)
            {
                throw new ArgumentNullException("rangeSpecifier");
            }
            if ((from < 0) || (to < 0))
            {
                throw new ArgumentOutOfRangeException(from < 0 ? nameof(from) : nameof(to), SR.net_rangetoosmall);
            }
            if (from > to)
            {
                throw new ArgumentOutOfRangeException("from", SR.net_fromto);
            }
            if (!HttpValidationHelpers.IsValidToken(rangeSpecifier))
            {
                throw new ArgumentException(SR.net_nottoken, "rangeSpecifier");
            }
            if (!AddRange(rangeSpecifier, from.ToString(NumberFormatInfo.InvariantInfo), to.ToString(NumberFormatInfo.InvariantInfo)))
            {
                throw new InvalidOperationException(SR.net_rangetype);
            }
        }
Ejemplo n.º 5
0
#pragma warning disable CS8765 // Nullability of parameter 'name' doesn't match overridden member
        public override void Set(string name, string?value)
#pragma warning restore CS8765
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            name  = HttpValidationHelpers.CheckBadHeaderNameChars(name);
            value = HttpValidationHelpers.CheckBadHeaderValueChars(value);
            InvalidateCachedArrays();
            InnerCollection.Set(name, value);
        }
Ejemplo n.º 6
0
 public void AddRange(string rangeSpecifier, long range)
 {
     if (rangeSpecifier == null)
     {
         throw new ArgumentNullException("rangeSpecifier");
     }
     if (!HttpValidationHelpers.IsValidToken(rangeSpecifier))
     {
         throw new ArgumentException(SR.net_nottoken, "rangeSpecifier");
     }
     if (!AddRange(rangeSpecifier, range.ToString(NumberFormatInfo.InvariantInfo), (range >= 0) ? "" : null))
     {
         throw new InvalidOperationException(SR.net_rangetype);
     }
 }
Ejemplo n.º 7
0
        // Remove -
        // Routine Description:
        //     Removes give header with validation to see if they are "proper" headers.
        //     If the header is a special header, listed in RestrictedHeaders object,
        //     then this call will cause an exception indicating as such.
        // Arguments:
        //     name - header-name to remove
        // Return Value:
        //     None

        /// <devdoc>
        ///    <para>Removes the specified header.</para>
        /// </devdoc>
        public override void Remove(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }
            ThrowOnRestrictedHeader(name);
            name = HttpValidationHelpers.CheckBadHeaderNameChars(name);
            if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"calling InnerCollection.Remove() key:[{name}]");
            if (_innerCollection != null)
            {
                InvalidateCachedArrays();
                _innerCollection.Remove(name);
            }
        }
Ejemplo n.º 8
0
 protected void AddWithoutValidate(string name, string value)
 {
     name = HttpValidationHelpers.CheckBadHeaderNameChars(name);
     value = HttpValidationHelpers.CheckBadHeaderValueChars(value);
     if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"calling InnerCollection.Add() key:[{name}], value:[{value}]");
     if (_type == WebHeaderCollectionType.WebResponse)
     {
         if (value != null && value.Length > ushort.MaxValue)
         {
             throw new ArgumentOutOfRangeException(nameof(value), value, string.Format(CultureInfo.InvariantCulture, SR.net_headers_toolong, ushort.MaxValue));
         }
     }
     InvalidateCachedArrays();
     InnerCollection.Add(name, value);
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Throws on invalid header name chars.
        /// </summary>
        private static string CheckBadHeaderNameChars(string name)
        {
            Debug.Assert(!string.IsNullOrEmpty(name));

            // First, check for absence of separators and spaces.
            if (HttpValidationHelpers.IsInvalidMethodOrHeaderString(name))
            {
                throw new ArgumentException(SR.Format(SR.net_WebHeaderInvalidHeaderChars, nameof(name)), nameof(name));
            }

            // Second, check for non CTL ASCII-7 characters (32-126).
            if (ContainsNonAsciiChars(name))
            {
                throw new ArgumentException(SR.Format(SR.net_WebHeaderInvalidNonAsciiChars, nameof(name)), nameof(name));
            }

            return(name);
        }
Ejemplo n.º 10
0
        // Remove -
        // Routine Description:
        //     Removes give header with validation to see if they are "proper" headers.
        //     If the header is a special header, listed in RestrictedHeaders object,
        //     then this call will cause an exception indicating as such.
        // Arguments:
        //     name - header-name to remove
        // Return Value:
        //     None

        /// <devdoc>
        ///    <para>Removes the specified header.</para>
        /// </devdoc>
        public override void Remove(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }
            ThrowOnRestrictedHeader(name);
            name = HttpValidationHelpers.CheckBadHeaderNameChars(name);
            if (GlobalLog.IsEnabled)
            {
                GlobalLog.Print("WebHeaderCollection::Remove() calling InnerCollection.Remove() key:[" + name + "]");
            }
            if (_innerCollection != null)
            {
                InvalidateCachedArrays();
                _innerCollection.Remove(name);
            }
        }
Ejemplo n.º 11
0
 public override void Add(string name, string value)
 {
     name = HttpValidationHelpers.CheckBadHeaderNameChars(name);
     ThrowOnRestrictedHeader(name);
     value = HttpValidationHelpers.CheckBadHeaderValueChars(value);
     if (GlobalLog.IsEnabled)
     {
         GlobalLog.Print("WebHeaderCollection::Add() calling InnerCollection.Add() key:[" + name + "], value:[" + value + "]");
     }
     if (_type == WebHeaderCollectionType.WebResponse)
     {
         if (value != null && value.Length > ushort.MaxValue)
         {
             throw new ArgumentOutOfRangeException(nameof(value), value, string.Format(CultureInfo.InvariantCulture, SR.net_headers_toolong, ushort.MaxValue));
         }
     }
     InvalidateCachedArrays();
     InnerCollection.Add(name, value);
 }
Ejemplo n.º 12
0
#pragma warning disable CS8765 // Nullability of parameter 'name' doesn't match overridden member
        public override void Set(string name, string?value)
#pragma warning restore CS8765
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            name = HttpValidationHelpers.CheckBadHeaderNameChars(name);
            ThrowOnRestrictedHeader(name);
            value = HttpValidationHelpers.CheckBadHeaderValueChars(value);
            if (_type == WebHeaderCollectionType.WebResponse)
            {
                if (value != null && value.Length > ushort.MaxValue)
                {
                    throw new ArgumentOutOfRangeException(nameof(value), value, SR.Format(CultureInfo.InvariantCulture, SR.net_headers_toolong, ushort.MaxValue));
                }
            }
            InvalidateCachedArrays();
            InnerCollection.Set(name, value);
        }
Ejemplo n.º 13
0
        public string this[string name]
        {
            get
            {
                string entry = null;
                _entriesDictionary?.TryGetValue(name, out entry);
                return(entry);
            }
            set
            {
                if (string.IsNullOrEmpty(name))
                {
                    throw new ArgumentNullException(nameof(name));
                }

                name  = HttpValidationHelpers.CheckBadHeaderNameChars(name);
                value = HttpValidationHelpers.CheckBadHeaderValueChars(value);

                InvalidateCachedArray();
                EnsureInitialized();

                if (!_entriesDictionary.ContainsKey(name))
                {
                    Debug.Assert(_entriesList.FindIndex(s => StringComparer.OrdinalIgnoreCase.Equals(s, name)) == -1,
                                 $"'{name}' must not be in {nameof(_entriesList)}.");

                    // Only add the name to the list if it isn't already in the dictionary.
                    _entriesList.Add(name);
                }

                _entriesDictionary[name] = value;

                Debug.Assert(_entriesList.FindIndex(s => StringComparer.OrdinalIgnoreCase.Equals(s, name)) != -1,
                             $"'{name}' must be in {nameof(_entriesList)}.");

                Debug.Assert(_entriesDictionary.Count == _entriesList.Count, "Counts must be equal.");
            }
        }
Ejemplo n.º 14
0
 public static bool IsRestricted(string headerName, bool response)
 {
     headerName = HttpValidationHelpers.CheckBadHeaderNameChars(headerName);
     return(response ? HeaderInfo[headerName].IsResponseRestricted : HeaderInfo[headerName].IsRequestRestricted);
 }
Ejemplo n.º 15
0
        // CheckBadChars - throws on invalid chars to be not found in header name/value
        internal static string CheckBadChars(string name, bool isHeaderValue)
        {
            if (name == null || name.Length == 0)
            {
                // empty name is invalid
                if (!isHeaderValue)
                {
                    throw name == null ? new ArgumentNullException("name") :
                          new ArgumentException(SR.Format(SR.net_emptystringcall, "name"), "name");
                }
                // empty value is OK
                return(string.Empty);
            }

            if (isHeaderValue)
            {
                // VALUE check
                // Trim spaces from both ends
                name = name.Trim(s_httpTrimCharacters);

                // First, check for correctly formed multi-line value
                // Second, check for absence of CTL characters
                int crlf = 0;
                for (int i = 0; i < name.Length; ++i)
                {
                    char c = (char)(0x000000ff & (uint)name[i]);
                    switch (crlf)
                    {
                    case 0:
                        if (c == '\r')
                        {
                            crlf = 1;
                        }
                        else if (c == '\n')
                        {
                            // Technically this is bad HTTP, but we want to be permissive in what we accept.
                            // It is important to note that it would be a breaking change to reject this.
                            crlf = 2;
                        }
                        else if (c == 127 || (c < ' ' && c != '\t'))
                        {
                            throw new ArgumentException(SR.Format(SR.net_WebHeaderInvalidControlChars, "value"));
                        }
                        break;

                    case 1:
                        if (c == '\n')
                        {
                            crlf = 2;
                            break;
                        }
                        throw new ArgumentException(SR.Format(SR.net_WebHeaderInvalidCRLFChars, "value"));

                    case 2:
                        if (c == ' ' || c == '\t')
                        {
                            crlf = 0;
                            break;
                        }
                        throw new ArgumentException(SR.Format(SR.net_WebHeaderInvalidCRLFChars, "value"));
                    }
                }
                if (crlf != 0)
                {
                    throw new ArgumentException(SR.Format(SR.net_WebHeaderInvalidCRLFChars, "value"));
                }
            }
            else
            {
                // NAME check
                // First, check for absence of separators and spaces
                if (HttpValidationHelpers.IsInvalidMethodOrHeaderString(name))
                {
                    throw new ArgumentException(SR.Format(SR.net_WebHeaderInvalidHeaderChars, "name"));
                }

                // Second, check for non CTL ASCII-7 characters (32-126)
                if (ContainsNonAsciiChars(name))
                {
                    throw new ArgumentException(SR.Format(SR.net_WebHeaderInvalidNonAsciiChars, "name"));
                }
            }
            return(name);
        }