Esempio n. 1
0
 /// <summary>
 /// Add a header.
 /// </summary>
 /// <param name="name">Header name.</param>
 /// <param name="value">Header value.</param>
 /// <returns>Current instance.</returns>
 public DreamHeaders Add(string name, string value)
 {
     if (name == COOKIE)
     {
         Cookies.AddRange(DreamCookie.ParseCookieHeader(value));
     }
     else if (name == SET_COOKIE)
     {
         Cookies.AddRange(DreamCookie.ParseSetCookieHeader(value));
     }
     else
     {
         Entry entry;
         if (_headers.TryGetValue(name, out entry))
         {
             entry.Last.Next = new Entry(value);
         }
         else
         {
             // create a new entry
             _headers[name] = new Entry(value);
         }
     }
     return(this);
 }
Esempio n. 2
0
        /// <summary>
        /// Add a range of headers.
        /// </summary>
        /// <param name="headers">Key value pair collection</param>
        /// <returns>Current instance.</returns>
        public DreamHeaders AddRange(IEnumerable <KeyValuePair <string, string> > headers)
        {
            if (headers != null)
            {
                foreach (var header in headers)
                {
                    if (header.Key.EqualsInvariantIgnoreCase(COOKIE))
                    {
                        if (!string.IsNullOrEmpty(header.Value))
                        {
                            Cookies.AddRange(DreamCookie.ParseCookieHeader(header.Value));
                        }
                    }
                    else if (header.Key.EqualsInvariantIgnoreCase(SET_COOKIE))
                    {
                        if (!string.IsNullOrEmpty(header.Value))
                        {
                            Cookies.AddRange(DreamCookie.ParseSetCookieHeader(header.Value));
                        }
                    }
                    else if (header.Key.EqualsInvariantIgnoreCase(ETAG) || header.Key.EndsWithInvariantIgnoreCase(IF_NONE_MATCH))
                    {
                        // Note: Since ETAG may be quoted, strip quotes from ETAG and its counter-part If-None-Match
                        var value = header.Value;
                        if (!string.IsNullOrEmpty(value))
                        {
                            if ((value.StartsWith("\"") && value.EndsWith("\"")) || (value.StartsWith("'") && value.EndsWith("'")))
                            {
                                value = value.Substring(1, value.Length - 2);
                            }
                            _headers[header.Key] = new Entry(value);
                        }
                    }
                    else
                    {
                        // Note (arnec): assume that there are no multi-value values
                        Entry existing;

                        // find existing entry
                        if (_headers.TryGetValue(header.Key, out existing))
                        {
                            existing = existing.Last;
                        }

                        // add new entries
                        if (existing == null)
                        {
                            existing             = new Entry(header.Value);
                            _headers[header.Key] = existing;
                        }
                        else
                        {
                            existing.Next = new Entry(header.Value);
                        }
                    }
                }
            }
            return(this);
        }
Esempio n. 3
0
        /// <summary>
        /// Add a range of headers.
        /// </summary>
        /// <param name="headers">Key value pair collection</param>
        /// <returns>Current instance.</returns>
        public DreamHeaders AddRange(IEnumerable <KeyValuePair <string, string> > headers)
        {
            if (headers != null)
            {
                foreach (var header in headers)
                {
                    if (header.Key.EqualsInvariant(COOKIE))
                    {
                        if (!string.IsNullOrEmpty(header.Value))
                        {
                            Cookies.AddRange(DreamCookie.ParseCookieHeader(header.Value));
                        }
                    }
                    else if (header.Key.EqualsInvariant(SET_COOKIE))
                    {
                        if (!string.IsNullOrEmpty(header.Value))
                        {
                            Cookies.AddRange(DreamCookie.ParseSetCookieHeader(header.Value));
                        }
                    }
                    else
                    {
                        // Note (arnec): assume that there are no multi-value values
                        Entry existing;

                        // find existing entry
                        if (_headers.TryGetValue(header.Key, out existing))
                        {
                            existing = existing.Last;
                        }

                        // add new entries
                        if (existing == null)
                        {
                            existing             = new Entry(header.Value);
                            _headers[header.Key] = existing;
                        }
                        else
                        {
                            existing.Next = new Entry(header.Value);
                        }
                    }
                }
            }
            return(this);
        }
Esempio n. 4
0
        /// <summary>
        /// Add a range of headers.
        /// </summary>
        /// <param name="headers">Header collection.</param>
        /// <returns>Current instance.</returns>
        public DreamHeaders AddRange(NameValueCollection headers)
        {
            if (headers != null)
            {
                foreach (string key in headers.Keys)
                {
                    // Note: Per RFC2616, "Each header field consists of a name followed by a colon (":") and the field value. Field names are case-insensitive."
                    if (key.EqualsInvariantIgnoreCase(COOKIE))
                    {
                        var value = headers[key];
                        if (!string.IsNullOrEmpty(value))
                        {
                            Cookies.AddRange(DreamCookie.ParseCookieHeader(value));
                        }
                    }
                    else if (key.EqualsInvariantIgnoreCase(SET_COOKIE))
                    {
                        var value = headers[key];
                        if (!string.IsNullOrEmpty(value))
                        {
                            Cookies.AddRange(DreamCookie.ParseSetCookieHeader(value));
                        }
                    }
                    else if (key.EqualsInvariantIgnoreCase(ETAG) || key.EndsWithInvariantIgnoreCase(IF_NONE_MATCH))
                    {
                        // Note: Since ETAG may be quoted, strip quotes from ETAG and its counter-part If-None-Match
                        var value = headers[key];
                        if (!string.IsNullOrEmpty(value))
                        {
                            if ((value.StartsWith("\"") && value.EndsWith("\"")) || (value.StartsWith("'") && value.EndsWith("'")))
                            {
                                value = value.Substring(1, value.Length - 2);
                            }
                            _headers[key] = new Entry(value);
                        }
                    }
                    else
                    {
                        string[] values = headers.GetValues(key);
                        if (!ArrayUtil.IsNullOrEmpty(values))
                        {
                            if (key.EqualsInvariantIgnoreCase(FORWARDED_HOST) || key.EqualsInvariantIgnoreCase(FORWARDED_FOR))
                            {
                                // NOTE (steveb): 'X-Forwarded-Host', 'X-Forwarded-For' may contain one or more entries, but NameValueCollection doesn't seem to care :(

                                // initialize 'last' to be the last entry added (if any) for the current header key
                                Entry last;
                                _headers.TryGetValue(key, out last);
                                if (last != null)
                                {
                                    last = last.Last;
                                }

                                // loop over all header values
                                for (int i = 0; i < values.Length; ++i)
                                {
                                    foreach (string value in values[i].Split(' '))
                                    {
                                        string host = value;
                                        if ((host.Length > 0) && (host[host.Length - 1] == ','))
                                        {
                                            host = host.Substring(0, host.Length - 1);
                                        }
                                        host = host.Trim();
                                        if (!string.IsNullOrEmpty(host))
                                        {
                                            if (last != null)
                                            {
                                                last.Next = new Entry(host, null);
                                                last      = last.Next;
                                            }
                                            else
                                            {
                                                _headers[key] = last = new Entry(host, null);
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                Entry last = null;
                                for (int i = 0; i < values.Length; ++i)
                                {
                                    if (last != null)
                                    {
                                        last.Next = new Entry(values[i], null);
                                        last      = last.Next;
                                    }
                                    else
                                    {
                                        _headers[key] = last = new Entry(values[i], null);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(this);
        }
Esempio n. 5
0
        /// <summary>
        /// Add a range of headers.
        /// </summary>
        /// <param name="headers">Header collection.</param>
        /// <returns>Current instance.</returns>
        public DreamHeaders AddRange(NameValueCollection headers)
        {
            if (headers != null)
            {
                foreach (string key in headers.Keys)
                {
                    if (key.EqualsInvariant(COOKIE))
                    {
                        var value = headers[key];
                        if (!string.IsNullOrEmpty(value))
                        {
                            Cookies.AddRange(DreamCookie.ParseCookieHeader(value));
                        }
                    }
                    else if (key.EqualsInvariant(SET_COOKIE))
                    {
                        var value = headers[key];
                        if (!string.IsNullOrEmpty(value))
                        {
                            Cookies.AddRange(DreamCookie.ParseSetCookieHeader(value));
                        }
                    }
                    else
                    {
                        string[] values = headers.GetValues(key);
                        if (!ArrayUtil.IsNullOrEmpty(values))
                        {
                            if (key.EqualsInvariant(FORWARDED_HOST) || key.EqualsInvariant(FORWARDED_FOR))
                            {
                                // NOTE (steveb): 'X-Forwarded-Host', 'X-Forwarded-For' may contain one or more entries, but NameValueCollection doesn't seem to care :(

                                // initialize 'last' to be the last entry added (if any) for the current header key
                                Entry last;
                                _headers.TryGetValue(key, out last);
                                if (last != null)
                                {
                                    last = last.Last;
                                }

                                // loop over all header values
                                for (int i = 0; i < values.Length; ++i)
                                {
                                    foreach (string value in values[i].Split(' '))
                                    {
                                        string host = value;
                                        if ((host.Length > 0) && (host[host.Length - 1] == ','))
                                        {
                                            host = host.Substring(0, host.Length - 1);
                                        }
                                        host = host.Trim();
                                        if (!string.IsNullOrEmpty(host))
                                        {
                                            if (last != null)
                                            {
                                                last.Next = new Entry(host, null);
                                                last      = last.Next;
                                            }
                                            else
                                            {
                                                _headers[key] = last = new Entry(host, null);
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                Entry last = null;
                                for (int i = 0; i < values.Length; ++i)
                                {
                                    if (last != null)
                                    {
                                        last.Next = new Entry(values[i], null);
                                        last      = last.Next;
                                    }
                                    else
                                    {
                                        _headers[key] = last = new Entry(values[i], null);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(this);
        }