protected internal static string GetDefaultDomain(CookieOrigin origin)
 {
     return(origin.GetHost());
 }
示例#2
0
 public override bool Match(Apache.Http.Cookie.Cookie cookie, CookieOrigin origin)
 {
     Args.NotNull(cookie, "Cookie");
     Args.NotNull(origin, "Cookie origin");
     return(!cookie.IsSecure() || origin.IsSecure());
 }
示例#3
0
        /// <summary>Validate cookie domain attribute.</summary>
        /// <remarks>Validate cookie domain attribute.</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");
            string host = origin.GetHost().ToLower(Sharpen.Extensions.GetEnglishCulture());

            if (cookie.GetDomain() == null)
            {
                throw new CookieRestrictionViolationException("Invalid cookie state: " + "domain not specified"
                                                              );
            }
            string cookieDomain = cookie.GetDomain().ToLower(Sharpen.Extensions.GetEnglishCulture()
                                                             );

            if (cookie is ClientCookie && ((ClientCookie)cookie).ContainsAttribute(ClientCookie
                                                                                   .DomainAttr))
            {
                // Domain attribute must start with a dot
                if (!cookieDomain.StartsWith("."))
                {
                    throw new CookieRestrictionViolationException("Domain attribute \"" + cookie.GetDomain
                                                                      () + "\" violates RFC 2109: domain must start with a dot");
                }
                // Domain attribute must contain at least one embedded dot,
                // or the value must be equal to .local.
                int dotIndex = cookieDomain.IndexOf('.', 1);
                if (((dotIndex < 0) || (dotIndex == cookieDomain.Length - 1)) && (!cookieDomain.Equals
                                                                                      (".local")))
                {
                    throw new CookieRestrictionViolationException("Domain attribute \"" + cookie.GetDomain
                                                                      () + "\" violates RFC 2965: the value contains no embedded dots " + "and the value is not .local"
                                                                  );
                }
                // The effective host name must domain-match domain attribute.
                if (!DomainMatch(host, cookieDomain))
                {
                    throw new CookieRestrictionViolationException("Domain attribute \"" + cookie.GetDomain
                                                                      () + "\" violates RFC 2965: effective host name does not " + "domain-match domain attribute."
                                                                  );
                }
                // effective host name minus domain must not contain any dots
                string effectiveHostWithoutDomain = Sharpen.Runtime.Substring(host, 0, host.Length
                                                                              - cookieDomain.Length);
                if (effectiveHostWithoutDomain.IndexOf('.') != -1)
                {
                    throw new CookieRestrictionViolationException("Domain attribute \"" + cookie.GetDomain
                                                                      () + "\" violates RFC 2965: " + "effective host minus domain may not contain any dots"
                                                                  );
                }
            }
            else
            {
                // Domain was not specified in header. In this case, domain must
                // string match request host (case-insensitive).
                if (!cookie.GetDomain().Equals(host))
                {
                    throw new CookieRestrictionViolationException("Illegal domain attribute: \"" + cookie
                                                                  .GetDomain() + "\"." + "Domain of origin: \"" + host + "\"");
                }
            }
        }
示例#4
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);
        }
示例#5
0
        /// <exception cref="Apache.Http.HttpException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        public virtual void Process(IHttpRequest request, HttpContext context)
        {
            Args.NotNull(request, "HTTP request");
            Args.NotNull(context, "HTTP context");
            string method = request.GetRequestLine().GetMethod();

            if (Sharpen.Runtime.EqualsIgnoreCase(method, "CONNECT"))
            {
                return;
            }
            HttpClientContext clientContext = ((HttpClientContext)HttpClientContext.Adapt(context
                                                                                          ));
            // Obtain cookie store
            CookieStore cookieStore = clientContext.GetCookieStore();

            if (cookieStore == null)
            {
                this.log.Debug("Cookie store not specified in HTTP context");
                return;
            }
            // Obtain the registry of cookie specs
            Lookup <CookieSpecProvider> registry = clientContext.GetCookieSpecRegistry();

            if (registry == null)
            {
                this.log.Debug("CookieSpec registry not specified in HTTP context");
                return;
            }
            // Obtain the target host, possibly virtual (required)
            HttpHost targetHost = clientContext.GetTargetHost();

            if (targetHost == null)
            {
                this.log.Debug("Target host not set in the context");
                return;
            }
            // Obtain the route (required)
            RouteInfo route = clientContext.GetHttpRoute();

            if (route == null)
            {
                this.log.Debug("Connection route not set in the context");
                return;
            }
            RequestConfig config = clientContext.GetRequestConfig();
            string        policy = config.GetCookieSpec();

            if (policy == null)
            {
                policy = CookieSpecs.BestMatch;
            }
            if (this.log.IsDebugEnabled())
            {
                this.log.Debug("CookieSpec selected: " + policy);
            }
            URI requestURI = null;

            if (request is IHttpUriRequest)
            {
                requestURI = ((IHttpUriRequest)request).GetURI();
            }
            else
            {
                try
                {
                    requestURI = new URI(request.GetRequestLine().GetUri());
                }
                catch (URISyntaxException)
                {
                }
            }
            string path = requestURI != null?requestURI.GetPath() : null;

            string hostName = targetHost.GetHostName();
            int    port     = targetHost.GetPort();

            if (port < 0)
            {
                port = route.GetTargetHost().GetPort();
            }
            CookieOrigin cookieOrigin = new CookieOrigin(hostName, port >= 0 ? port : 0, !TextUtils
                                                         .IsEmpty(path) ? path : "/", route.IsSecure());
            // Get an instance of the selected cookie policy
            CookieSpecProvider provider = registry.Lookup(policy);

            if (provider == null)
            {
                throw new HttpException("Unsupported cookie policy: " + policy);
            }
            CookieSpec cookieSpec = provider.Create(clientContext);
            // Get all cookies available in the HTTP state
            IList <Apache.Http.Cookie.Cookie> cookies = new AList <Apache.Http.Cookie.Cookie>(cookieStore
                                                                                              .GetCookies());
            // Find cookies matching the given origin
            IList <Apache.Http.Cookie.Cookie> matchedCookies = new AList <Apache.Http.Cookie.Cookie
                                                                          >();
            DateTime now = new DateTime();

            foreach (Apache.Http.Cookie.Cookie cookie in cookies)
            {
                if (!cookie.IsExpired(now))
                {
                    if (cookieSpec.Match(cookie, cookieOrigin))
                    {
                        if (this.log.IsDebugEnabled())
                        {
                            this.log.Debug("Cookie " + cookie + " match " + cookieOrigin);
                        }
                        matchedCookies.AddItem(cookie);
                    }
                }
                else
                {
                    if (this.log.IsDebugEnabled())
                    {
                        this.log.Debug("Cookie " + cookie + " expired");
                    }
                }
            }
            // Generate Cookie request headers
            if (!matchedCookies.IsEmpty())
            {
                IList <Header> headers = cookieSpec.FormatCookies(matchedCookies);
                foreach (Header header in headers)
                {
                    request.AddHeader(header);
                }
            }
            int ver = cookieSpec.GetVersion();

            if (ver > 0)
            {
                bool needVersionHeader = false;
                foreach (Apache.Http.Cookie.Cookie cookie_1 in matchedCookies)
                {
                    if (ver != cookie_1.GetVersion() || !(cookie_1 is SetCookie2))
                    {
                        needVersionHeader = true;
                    }
                }
                if (needVersionHeader)
                {
                    Header header = cookieSpec.GetVersionHeader();
                    if (header != null)
                    {
                        // Advertise cookie version support
                        request.AddHeader(header);
                    }
                }
            }
            // Stick the CookieSpec and CookieOrigin instances to the HTTP context
            // so they could be obtained by the response interceptor
            context.SetAttribute(HttpClientContext.CookieSpec, cookieSpec);
            context.SetAttribute(HttpClientContext.CookieOrigin, cookieOrigin);
        }
示例#6
0
 /// <exception cref="Apache.Http.Cookie.MalformedCookieException"></exception>
 protected internal override IList <Apache.Http.Cookie.Cookie> Parse(HeaderElement[]
                                                                     elems, CookieOrigin origin)
 {
     return(CreateCookies(elems, AdjustEffectiveHost(origin)));
 }
示例#7
0
 public override bool Match(Apache.Http.Cookie.Cookie cookie, CookieOrigin origin)
 {
     Args.NotNull(cookie, "Cookie");
     Args.NotNull(origin, "Cookie origin");
     return(base.Match(cookie, AdjustEffectiveHost(origin)));
 }
 public virtual bool Match(Apache.Http.Cookie.Cookie cookie, CookieOrigin origin)
 {
     return(true);
 }
 /// <exception cref="Apache.Http.Cookie.MalformedCookieException"></exception>
 public virtual void Validate(Apache.Http.Cookie.Cookie cookie, CookieOrigin origin
                              )
 {
     wrapped.Validate(cookie, origin);
 }
示例#10
0
 /// <exception cref="Apache.Http.Cookie.MalformedCookieException"></exception>
 public virtual void Validate(Apache.Http.Cookie.Cookie cookie, CookieOrigin origin
                              )
 {
 }