Esempio n. 1
0
        /// <summary>
        /// Gets the best content type acceptable to the client.
        /// </summary>
        /// <param name="ContentTypes">Array of content types to choose from.</param>
        /// <returns>The best choice. If none are acceptable, null is returned.</returns>
        public string GetBestContentType(params string[] ContentTypes)
        {
            ContentTypeAcceptance BestAcceptance = ContentTypeAcceptance.Wildcard;
            ContentTypeAcceptance Acceptance;
            string Best        = null;
            double BestQuality = 0;
            double Quality;

            foreach (string ContentType in ContentTypes)
            {
                if (!this.IsAcceptable(ContentType, out Quality, out Acceptance, null))
                {
                    continue;
                }

                if (Quality > BestQuality || (Quality == BestQuality && Acceptance > BestAcceptance))
                {
                    Best           = ContentType;
                    BestQuality    = Quality;
                    BestAcceptance = Acceptance;
                }
            }

            return(Best);
        }
Esempio n. 2
0
        /// <summary>
        /// Gets the best content type acceptable to the client.
        /// </summary>
        /// <param name="ContentTypes">Array of content types to choose from, together with arrays of any parameters that might
        /// be relevant in the comparison.</param>
        /// <returns>The best choice. If none are acceptable, (null, null) is returned.</returns>
        public KeyValuePair <string, KeyValuePair <string, string>[]> GetBestContentType(
            params KeyValuePair <string, KeyValuePair <string, string>[]>[] ContentTypes)
        {
            ContentTypeAcceptance BestAcceptance = ContentTypeAcceptance.Wildcard;
            ContentTypeAcceptance Acceptance;
            KeyValuePair <string, KeyValuePair <string, string>[]> Best = new KeyValuePair <string, KeyValuePair <string, string>[]>(null, null);
            double BestQuality = 0;
            double Quality;

            foreach (KeyValuePair <string, KeyValuePair <string, string>[]> ContentType in ContentTypes)
            {
                if (!this.IsAcceptable(ContentType.Key, out Quality, out Acceptance, ContentType.Value))
                {
                    continue;
                }

                if (Quality > BestQuality || (Quality == BestQuality && Acceptance > BestAcceptance))
                {
                    Best           = ContentType;
                    BestQuality    = Quality;
                    BestAcceptance = Acceptance;
                }
            }

            return(Best);
        }
Esempio n. 3
0
        /// <summary>
        /// Checks if a content type is acceptable to the client sending a request.
        /// </summary>
        /// <param name="ContentType">Content Type to check.</param>
        /// <param name="Quality">Quality level of client support.</param>
        /// <param name="Acceptance">How well the content type was matched by the acceptance criteria.</param>
        /// <param name="Parameters">Any content type parameters that might be relevant.</param>
        /// <returns>If content of the given type is acceptable to the client.</returns>
        public bool IsAcceptable(string ContentType, out double Quality, out ContentTypeAcceptance Acceptance, params KeyValuePair <string, string>[] Parameters)
        {
            ContentTypeAcceptance CurrentAcceptance;
            string s, TopType;
            int    i;
            bool?  Found;

            i = ContentType.IndexOf('/');
            if (i < 0)
            {
                TopType = ContentType;
            }
            else
            {
                TopType = ContentType.Substring(0, i);
            }

            Quality    = 0;
            Acceptance = ContentTypeAcceptance.Wildcard;

            foreach (AcceptRecord Record in this.Records)
            {
                if (string.Compare(ContentType, s = Record.Item, true) == 0)
                {
                    CurrentAcceptance = ContentTypeAcceptance.TopAndSubType;

                    if (Record.Parameters != null && Parameters != null)
                    {
                        Found = null;

                        foreach (KeyValuePair <string, string> P in Record.Parameters)
                        {
                            foreach (KeyValuePair <string, string> P2 in Parameters)
                            {
                                if (string.Compare(P.Key, P2.Key, true) == 0)
                                {
                                    if (string.Compare(P.Value, P2.Value, true) == 0)
                                    {
                                        Found = true;
                                    }
                                    else
                                    {
                                        Found = false;
                                    }

                                    break;
                                }
                            }

                            if (Found.HasValue)
                            {
                                break;
                            }
                        }

                        if (Found.HasValue)
                        {
                            if (Found.Value)
                            {
                                CurrentAcceptance = ContentTypeAcceptance.TopSubTypeAndParameters;
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                }
                else if (s.EndsWith("/*") && string.Compare(TopType, s.Substring(0, s.Length - 2)) == 0)
                {
                    CurrentAcceptance = ContentTypeAcceptance.TopTypeOnly;
                }
                else if (s == "*/*")
                {
                    CurrentAcceptance = ContentTypeAcceptance.Wildcard;
                }
                else
                {
                    continue;
                }

                if (Record.Quality > Quality)
                {
                    Quality    = Record.Quality;
                    Acceptance = CurrentAcceptance;
                }
                else if (Record.Quality == Quality && CurrentAcceptance > Acceptance)
                {
                    Acceptance = CurrentAcceptance;
                }
            }

            return(Quality > 0);
        }
Esempio n. 4
0
 /// <summary>
 /// Checks if a content type is acceptable to the client sending a request.
 /// </summary>
 /// <param name="ContentType">Content Type to check.</param>
 /// <param name="Quality">Quality level of client support.</param>
 /// <param name="Acceptance">How well the content type was matched by the acceptance criteria.</param>
 /// <returns>If content of the given type is acceptable to the client.</returns>
 public bool IsAcceptable(string ContentType, out double Quality, out ContentTypeAcceptance Acceptance)
 {
     return(this.IsAcceptable(ContentType, out Quality, out Acceptance, null));
 }
Esempio n. 5
0
        /// <summary>
        /// Checks if a content type is acceptable to the client sending a request.
        /// </summary>
        /// <param name="ContentType">Content Type to check.</param>
        /// <param name="Quality">Quality level of client support.</param>
        /// <param name="Acceptance">How well the content type was matched by the acceptance criteria.</param>
        /// <param name="Parameters">Any content type parameters that might be relevant.</param>
        /// <returns>If content of the given type is acceptable to the client.</returns>
        public bool IsAcceptable(string ContentType, out double Quality, out ContentTypeAcceptance Acceptance, params KeyValuePair <string, string>[] Parameters)
        {
            ContentTypeAcceptance CurrentAcceptance;

            Quality    = 0;
            Acceptance = ContentTypeAcceptance.Wildcard;

            if (string.Compare(ContentType, this.item, true) == 0)
            {
                bool?Found;

                CurrentAcceptance = ContentTypeAcceptance.TopAndSubType;

                if (this.parameters != null && Parameters != null)
                {
                    Found = null;

                    foreach (KeyValuePair <string, string> P in this.parameters)
                    {
                        foreach (KeyValuePair <string, string> P2 in Parameters)
                        {
                            if (string.Compare(P.Key, P2.Key, true) == 0)
                            {
                                if (string.Compare(P.Value, P2.Value, true) == 0)
                                {
                                    Found = true;
                                }
                                else
                                {
                                    Found = false;
                                }

                                break;
                            }
                        }

                        if (Found.HasValue)
                        {
                            break;
                        }
                    }

                    if (Found.HasValue)
                    {
                        if (Found.Value)
                        {
                            CurrentAcceptance = ContentTypeAcceptance.TopSubTypeAndParameters;
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }
            }
            else
            {
                string TopType;
                int    i = ContentType.IndexOf('/');

                if (i < 0)
                {
                    TopType = ContentType;
                }
                else
                {
                    TopType = ContentType.Substring(0, i);
                }

                if (this.item.EndsWith("/*") && string.Compare(TopType, this.item.Substring(0, this.item.Length - 2)) == 0)
                {
                    CurrentAcceptance = ContentTypeAcceptance.TopTypeOnly;
                }
                else if (this.item == "*/*")
                {
                    CurrentAcceptance = ContentTypeAcceptance.Wildcard;
                }
                else
                {
                    return(false);
                }
            }

            if (this.q > Quality)
            {
                Quality    = this.q;
                Acceptance = CurrentAcceptance;
            }
            else if (this.q == Quality && CurrentAcceptance > Acceptance)
            {
                Acceptance = CurrentAcceptance;
            }

            return(Quality > 0);
        }