Пример #1
0
        /// <summary>Set 'effective host name' as defined in RFC 2965.</summary>
        /// <remarks>
        /// Set 'effective host name' as defined in RFC 2965.
        /// <p>
        /// If a host name contains no dots, the effective host name is
        /// that name with the string .local appended to it.  Otherwise
        /// the effective host name is the same as the host name.  Note
        /// that all effective host names contain at least one dot.
        /// </remarks>
        /// <param name="origin">origin where cookie is received from or being sent to.</param>
        private static CookieOrigin AdjustEffectiveHost(CookieOrigin origin)
        {
            string host = origin.GetHost();
            // Test if the host name appears to be a fully qualified DNS name,
            // IPv4 address or IPv6 address
            bool isLocalHost = true;

            for (int i = 0; i < host.Length; i++)
            {
                char ch = host[i];
                if (ch == '.' || ch == ':')
                {
                    isLocalHost = false;
                    break;
                }
            }
            if (isLocalHost)
            {
                host += ".local";
                return(new CookieOrigin(host, origin.GetPort(), origin.GetPath(), origin.IsSecure
                                            ()));
            }
            else
            {
                return(origin);
            }
        }
Пример #2
0
        /// <summary>Validate cookie port attribute.</summary>
        /// <remarks>
        /// Validate cookie port attribute. If the Port attribute was specified
        /// in header, the request port must be in cookie's port list.
        /// </remarks>
        /// <exception cref="Apache.Http.Cookie.MalformedCookieException"></exception>
        public virtual void Validate(Apache.Http.Cookie.Cookie cookie, CookieOrigin origin
                                     )
        {
            Args.NotNull(cookie, "Cookie");
            Args.NotNull(origin, "Cookie origin");
            int port = origin.GetPort();

            if (cookie is ClientCookie && ((ClientCookie)cookie).ContainsAttribute(ClientCookie
                                                                                   .PortAttr))
            {
                if (!PortMatch(port, cookie.GetPorts()))
                {
                    throw new CookieRestrictionViolationException("Port attribute violates RFC 2965: "
                                                                  + "Request port not found in cookie's port list.");
                }
            }
        }
Пример #3
0
        /// <exception cref="Apache.Http.Cookie.MalformedCookieException"></exception>
        private IList <Apache.Http.Cookie.Cookie> CreateCookies(HeaderElement[] elems, CookieOrigin
                                                                origin)
        {
            IList <Apache.Http.Cookie.Cookie> cookies = new AList <Apache.Http.Cookie.Cookie>(elems
                                                                                              .Length);

            foreach (HeaderElement headerelement in elems)
            {
                string name  = headerelement.GetName();
                string value = headerelement.GetValue();
                if (name == null || name.Length == 0)
                {
                    throw new MalformedCookieException("Cookie name may not be empty");
                }
                BasicClientCookie2 cookie = new BasicClientCookie2(name, value);
                cookie.SetPath(GetDefaultPath(origin));
                cookie.SetDomain(GetDefaultDomain(origin));
                cookie.SetPorts(new int[] { origin.GetPort() });
                // cycle through the parameters
                NameValuePair[] attribs = headerelement.GetParameters();
                // Eliminate duplicate attributes. The first occurrence takes precedence
                // See RFC2965: 3.2  Origin Server Role
                IDictionary <string, NameValuePair> attribmap = new Dictionary <string, NameValuePair
                                                                                >(attribs.Length);
                for (int j = attribs.Length - 1; j >= 0; j--)
                {
                    NameValuePair param = attribs[j];
                    attribmap.Put(param.GetName().ToLower(Sharpen.Extensions.GetEnglishCulture()), param
                                  );
                }
                foreach (KeyValuePair <string, NameValuePair> entry in attribmap.EntrySet())
                {
                    NameValuePair attrib = entry.Value;
                    string        s      = attrib.GetName().ToLower(Sharpen.Extensions.GetEnglishCulture());
                    cookie.SetAttribute(s, attrib.GetValue());
                    CookieAttributeHandler handler = FindAttribHandler(s);
                    if (handler != null)
                    {
                        handler.Parse(cookie, attrib.GetValue());
                    }
                }
                cookies.AddItem(cookie);
            }
            return(cookies);
        }
Пример #4
0
        /// <summary>Match cookie port attribute.</summary>
        /// <remarks>
        /// Match cookie port attribute. If the Port attribute is not specified
        /// in header, the cookie can be sent to any port. Otherwise, the request port
        /// must be in the cookie's port list.
        /// </remarks>
        public virtual bool Match(Apache.Http.Cookie.Cookie cookie, CookieOrigin origin)
        {
            Args.NotNull(cookie, "Cookie");
            Args.NotNull(origin, "Cookie origin");
            int port = origin.GetPort();

            if (cookie is ClientCookie && ((ClientCookie)cookie).ContainsAttribute(ClientCookie
                                                                                   .PortAttr))
            {
                if (cookie.GetPorts() == null)
                {
                    // Invalid cookie state: port not specified
                    return(false);
                }
                if (!PortMatch(port, cookie.GetPorts()))
                {
                    return(false);
                }
            }
            return(true);
        }