PageRenderTime 17ms CodeModel.GetById 2ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/WCFWebApi/src/System.Net.Http/System/Net/Http/Headers/ProductInfoHeaderValue.cs

#
C# | 176 lines | 139 code | 30 blank | 7 comment | 23 complexity | dec9d7cd30f9a6d8aad32a4fab77aa2f MD5 | raw file
  1using System.Diagnostics.Contracts;
  2
  3namespace System.Net.Http.Headers
  4{
  5    public class ProductInfoHeaderValue : ICloneable
  6    {
  7        private ProductHeaderValue product;
  8        private string comment;
  9
 10        public ProductHeaderValue Product 
 11        {
 12            get { return product; }
 13        }
 14
 15        public string Comment 
 16        {
 17            get { return comment; }
 18        }
 19
 20        public ProductInfoHeaderValue(string productName, string productVersion)
 21            : this(new ProductHeaderValue(productName, productVersion))
 22        {            
 23        }
 24
 25        public ProductInfoHeaderValue(ProductHeaderValue product)
 26        {
 27            if (product == null)
 28            {
 29                throw new ArgumentNullException("product");
 30            }
 31
 32            this.product = product;
 33        }
 34
 35        public ProductInfoHeaderValue(string comment)
 36        {
 37            HeaderUtilities.CheckValidComment(comment, "comment");   
 38            this.comment = comment;
 39        }
 40
 41        private ProductInfoHeaderValue(ProductInfoHeaderValue source)
 42        {
 43            Contract.Requires(source != null);
 44
 45            this.product = source.product;
 46            this.comment = source.comment;
 47        }
 48
 49        private ProductInfoHeaderValue()
 50        {
 51        }
 52
 53        public override string ToString()
 54        {
 55            if (product == null)
 56            {
 57                return comment;
 58            }
 59            return product.ToString();
 60        }
 61
 62        public override bool Equals(object obj)
 63        {
 64            ProductInfoHeaderValue other = obj as ProductInfoHeaderValue;
 65
 66            if (other == null)
 67            {
 68                return false;
 69            }
 70
 71            if (product == null)
 72            {
 73                // We compare comments using case-sensitive comparison.
 74                return string.CompareOrdinal(comment, other.comment) == 0;
 75            }
 76
 77            return product.Equals(other.product);
 78        }
 79
 80        public override int GetHashCode()
 81        {
 82            if (product == null)
 83            {
 84                return comment.GetHashCode();
 85            }
 86            return product.GetHashCode();
 87        }
 88
 89        public static ProductInfoHeaderValue Parse(string input)
 90        {
 91            int index = 0;
 92            object result = ProductInfoHeaderParser.SingleValueParser.ParseValue(
 93                input, null, ref index);
 94            if (index < input.Length)
 95            {
 96                // There is some leftover data, invalid.  Normaly BaseHeaderParser.TryParseValue would 
 97                // handle this, but ProductInfoHeaderValue does not derive from BaseHeaderParser.
 98                throw new FormatException(string.Format(System.Globalization.CultureInfo.InvariantCulture, SR.net_http_headers_invalid_value, input.Substring(index)));
 99            }
100            return (ProductInfoHeaderValue)result;
101        }
102
103        public static bool TryParse(string input, out ProductInfoHeaderValue parsedValue)
104        {
105            int index = 0;
106            object output;
107            parsedValue = null;
108
109            if (ProductInfoHeaderParser.SingleValueParser.TryParseValue(input, null, ref index, out output))
110            {
111                if (index < input.Length)
112                {
113                    // There is some leftover data, invalid.  Normaly BaseHeaderParser.TryParseValue would 
114                    // handle this, but ProductInfoHeaderValue does not derive from BaseHeaderParser.
115                    return false;
116                }
117                parsedValue = (ProductInfoHeaderValue)output;
118                return true;
119            }
120            return false;
121        }
122
123        internal static int GetProductInfoLength(string input, int startIndex, out ProductInfoHeaderValue parsedValue)
124        {
125            Contract.Requires(startIndex >= 0);
126
127            parsedValue = null;
128
129            if (string.IsNullOrEmpty(input) || (startIndex >= input.Length))
130            {
131                return 0;
132            }
133            
134            int current = startIndex;
135
136            // Caller must remove leading whitespaces.
137            string comment = null;
138            ProductHeaderValue product = null;
139            if (input[current] == '(')
140            {
141                int commentLength = 0;
142                if (HttpRuleParser.GetCommentLength(input, current, out commentLength) != HttpParseResult.Parsed)
143                {
144                    return 0;
145                }
146
147                comment = input.Substring(current, commentLength);
148
149                current = current + commentLength;
150                current = current + HttpRuleParser.GetWhitespaceLength(input, current);
151            }
152            else
153            { 
154                // trailing whitespaces are removed by GetProductLength()
155                int productLength = ProductHeaderValue.GetProductLength(input, current, out product);
156
157                if (productLength == 0)
158                {
159                    return 0;
160                }
161
162                current = current + productLength;
163            }
164
165            parsedValue = new ProductInfoHeaderValue();
166            parsedValue.product = product;
167            parsedValue.comment = comment;
168            return current - startIndex;
169        }
170
171        object ICloneable.Clone()
172        {
173            return new ProductInfoHeaderValue(this);
174        }
175    }
176}