예제 #1
0
        /// <summary>
        /// Checks if an alternative is acceptable to the client sending a request.
        /// </summary>
        /// <param name="Alternative">Alternative to check.</param>
        /// <param name="Quality">Quality level of client support.</param>
        /// <param name="Acceptance">How well the alternative was matched by the acceptance criteria.</param>
        /// <returns>If content of the given type is acceptable to the client.</returns>
        public bool IsAcceptable(string Alternative, out double Quality, out AcceptanceLevel Acceptance)
        {
            int i = Alternative.IndexOf(';');

            if (i < 0)
            {
                return(this.IsAcceptable(Alternative, out Quality, out Acceptance, null));
            }
            else
            {
                return(this.IsAcceptable(Alternative.Substring(0, i).Trim(), out Quality, out Acceptance,
                                         CommonTypes.ParseFieldValues(Alternative.Substring(i + 1).Trim())));
            }
        }
예제 #2
0
        /// <summary>
        /// Gets the best alternative acceptable to the client.
        /// </summary>
        /// <param name="Alternatives">Array of alternatives to choose from.</param>
        /// <returns>The best choice. If none are acceptable, null is returned.</returns>
        public string GetBestAlternative(params string[] Alternatives)
        {
            AcceptanceLevel BestAcceptance = AcceptanceLevel.Wildcard;
            string          Best           = null;
            double          BestQuality    = 0;

            foreach (string Alternative in Alternatives)
            {
                if (!this.IsAcceptable(Alternative, out double Quality, out AcceptanceLevel Acceptance, null))
                {
                    continue;
                }

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

            return(Best);
        }
예제 #3
0
        /// <summary>
        /// Gets the best alternative acceptable to the client.
        /// </summary>
        /// <param name="Alternatives">Array of alternatives 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>[]> GetBestAlternative(
            params KeyValuePair <string, KeyValuePair <string, string>[]>[] Alternatives)
        {
            AcceptanceLevel BestAcceptance = AcceptanceLevel.Wildcard;
            KeyValuePair <string, KeyValuePair <string, string>[]> Best = new KeyValuePair <string, KeyValuePair <string, string>[]>(null, null);
            double BestQuality = 0;

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

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

            return(Best);
        }
예제 #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>
        /// <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 AcceptanceLevel Acceptance, params KeyValuePair <string, string>[] Parameters)
        {
            AcceptanceLevel CurrentAcceptance;

            Quality    = 0;
            Acceptance = AcceptanceLevel.Wildcard;

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

                CurrentAcceptance = AcceptanceLevel.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 = AcceptanceLevel.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 = AcceptanceLevel.TopTypeOnly;
                }
                else if (this.item == "*/*")
                {
                    CurrentAcceptance = AcceptanceLevel.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);
        }
예제 #5
0
        /// <summary>
        /// Checks if an alternative is acceptable to the client sending a request.
        /// </summary>
        /// <param name="Alternative">Alternative to check.</param>
        /// <param name="Quality">Quality level of client support.</param>
        /// <param name="Acceptance">How well the alternative was matched by the acceptance criteria.</param>
        /// <param name="Parameters">Any alternative parameters that might be relevant.</param>
        /// <returns>If content of the given type is acceptable to the client.</returns>
        public bool IsAcceptable(string Alternative, out double Quality, out AcceptanceLevel Acceptance,
                                 params KeyValuePair <string, string>[] Parameters)
        {
            AcceptanceLevel CurrentAcceptance;
            string          s, TopType;
            int             i;
            bool?           Found;

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

            Quality    = 0;
            Acceptance = AcceptanceLevel.Wildcard;

            foreach (AcceptRecord Record in this.Records)
            {
                if (string.Compare(Alternative, s = Record.Item, true) == 0)
                {
                    CurrentAcceptance = AcceptanceLevel.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 = AcceptanceLevel.TopSubTypeAndParameters;
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                }
                else if (s.EndsWith("/*") && string.Compare(TopType, s.Substring(0, s.Length - 2)) == 0)
                {
                    CurrentAcceptance = AcceptanceLevel.TopTypeOnly;
                }
                else if (s == "*/*")
                {
                    CurrentAcceptance = AcceptanceLevel.Wildcard;
                }
                else
                {
                    continue;
                }

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

            return(Quality > 0);
        }