PageRenderTime 26ms CodeModel.GetById 1ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 1ms

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

#
C# | 234 lines | 192 code | 36 blank | 6 comment | 12 complexity | 53097796a31466e56d85c09a1ce3a6ef MD5 | raw file
  1using System.Collections.Generic;
  2using System.Diagnostics.CodeAnalysis;
  3using System.Diagnostics.Contracts;
  4
  5namespace System.Net.Http.Headers
  6{
  7    [SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix",
  8        Justification = "This is not a collection")]
  9    public sealed class HttpResponseHeaders : HttpHeaders
 10    {
 11        private static readonly Dictionary<string, HttpHeaderParser> parserStore;
 12        private static readonly HashSet<string> invalidHeaders;
 13
 14        private HttpGeneralHeaders generalHeaders;
 15        private HttpHeaderValueCollection<string> acceptRanges;
 16        private HttpHeaderValueCollection<AuthenticationHeaderValue> wwwAuthenticate;
 17        private HttpHeaderValueCollection<AuthenticationHeaderValue> proxyAuthenticate;
 18        private HttpHeaderValueCollection<ProductInfoHeaderValue> server;
 19        private HttpHeaderValueCollection<string> vary;
 20
 21        #region Response Headers
 22
 23        public HttpHeaderValueCollection<string> AcceptRanges
 24        {
 25            get
 26            {
 27                if (acceptRanges == null)
 28                {
 29                    acceptRanges = new HttpHeaderValueCollection<string>(HttpKnownHeaderNames.AcceptRanges,
 30                        this, HeaderUtilities.TokenValidator);
 31                }
 32                return acceptRanges;
 33            }
 34        }
 35
 36        public TimeSpan? Age
 37        {
 38            get { return HeaderUtilities.GetTimeSpanValue(HttpKnownHeaderNames.Age, this); }
 39            set { SetOrRemoveParsedValue(HttpKnownHeaderNames.Age, value); }
 40        }
 41
 42        public EntityTagHeaderValue ETag
 43        {
 44            get { return (EntityTagHeaderValue)GetParsedValues(HttpKnownHeaderNames.ETag); }
 45            set { SetOrRemoveParsedValue(HttpKnownHeaderNames.ETag, value); }
 46        }
 47
 48        public Uri Location
 49        {
 50            get { return (Uri)GetParsedValues(HttpKnownHeaderNames.Location); }
 51            // The RFC says that the Location header should be an absolute Uri, 
 52            // but IIS and HttpListener do not enforce this.
 53            set { SetOrRemoveParsedValue(HttpKnownHeaderNames.Location, value); }
 54        }
 55
 56        public HttpHeaderValueCollection<AuthenticationHeaderValue> ProxyAuthenticate
 57        {
 58            get
 59            {
 60                if (proxyAuthenticate == null)
 61                {
 62                    proxyAuthenticate = new HttpHeaderValueCollection<AuthenticationHeaderValue>(
 63                        HttpKnownHeaderNames.ProxyAuthenticate, this);
 64                }
 65                return proxyAuthenticate;
 66            }
 67        }
 68
 69        public RetryConditionHeaderValue RetryAfter
 70        {
 71            get { return (RetryConditionHeaderValue)GetParsedValues(HttpKnownHeaderNames.RetryAfter); }
 72            set { SetOrRemoveParsedValue(HttpKnownHeaderNames.RetryAfter, value); }
 73        }
 74
 75        public HttpHeaderValueCollection<ProductInfoHeaderValue> Server
 76        {
 77            get
 78            {
 79                if (server == null)
 80                {
 81                    server = new HttpHeaderValueCollection<ProductInfoHeaderValue>(HttpKnownHeaderNames.Server, this);
 82                }
 83                return server;
 84            }
 85        }
 86
 87        public HttpHeaderValueCollection<string> Vary
 88                {
 89            get
 90            {
 91                if (vary == null)
 92                {
 93                    vary = new HttpHeaderValueCollection<string>(HttpKnownHeaderNames.Vary,
 94                        this, HeaderUtilities.TokenValidator);
 95                }
 96                return vary;
 97            }
 98        }
 99
100        public HttpHeaderValueCollection<AuthenticationHeaderValue> WwwAuthenticate
101        {
102            get
103            {
104                if (wwwAuthenticate == null)
105                {
106                    wwwAuthenticate = new HttpHeaderValueCollection<AuthenticationHeaderValue>(
107                        HttpKnownHeaderNames.WWWAuthenticate, this);
108                }
109                return wwwAuthenticate;
110            }
111        }
112
113        #endregion
114
115        #region General Headers
116
117        public CacheControlHeaderValue CacheControl
118        {
119            get { return generalHeaders.CacheControl; }
120            set { generalHeaders.CacheControl = value; }
121        }
122
123        public HttpHeaderValueCollection<string> Connection
124        {
125            get { return generalHeaders.Connection; }
126        }
127
128        public bool? ConnectionClose
129        {
130            get { return generalHeaders.ConnectionClose; }
131            set { generalHeaders.ConnectionClose = value; }
132        }
133
134        public DateTimeOffset? Date
135        {
136            get { return generalHeaders.Date; }
137            set { generalHeaders.Date = value; }
138        }
139
140        public HttpHeaderValueCollection<NameValueHeaderValue> Pragma
141        {
142            get { return generalHeaders.Pragma; }
143        }
144
145        public HttpHeaderValueCollection<string> Trailer
146        {
147            get { return generalHeaders.Trailer; }
148        }
149
150        // Like ContentEncoding: Order matters!
151        public HttpHeaderValueCollection<TransferCodingHeaderValue> TransferEncoding
152        {
153            get { return generalHeaders.TransferEncoding; }
154        }
155
156        public bool? TransferEncodingChunked
157        {
158            get { return generalHeaders.TransferEncodingChunked; }
159            set { generalHeaders.TransferEncodingChunked = value; }
160        }
161
162        public HttpHeaderValueCollection<ProductHeaderValue> Upgrade
163        {
164            get { return generalHeaders.Upgrade; }
165        }
166
167        public HttpHeaderValueCollection<ViaHeaderValue> Via
168        {
169            get { return generalHeaders.Via; }
170        }
171
172        public HttpHeaderValueCollection<WarningHeaderValue> Warning
173        {
174            get { return generalHeaders.Warning; }
175        }
176
177        #endregion
178
179        internal HttpResponseHeaders()
180        {
181            this.generalHeaders = new HttpGeneralHeaders(this);
182
183            base.SetConfiguration(parserStore, invalidHeaders);
184        }
185
186        static HttpResponseHeaders()
187        {
188            parserStore = new Dictionary<string, HttpHeaderParser>(HeaderUtilities.CaseInsensitiveStringComparer);
189
190            parserStore.Add(HttpKnownHeaderNames.AcceptRanges, GenericHeaderParser.TokenListParser);
191            parserStore.Add(HttpKnownHeaderNames.Age, TimeSpanHeaderParser.Parser);
192            parserStore.Add(HttpKnownHeaderNames.ETag, GenericHeaderParser.SingleValueEntityTagParser);
193            // The RFC says that the Location header should be an absolute Uri, 
194            // but IIS and HttpListener do not enforce this.
195            parserStore.Add(HttpKnownHeaderNames.Location, UriHeaderParser.RelativeOrAbsoluteUriParser);
196            parserStore.Add(HttpKnownHeaderNames.ProxyAuthenticate, GenericHeaderParser.MultipleValueAuthenticationParser);
197            parserStore.Add(HttpKnownHeaderNames.RetryAfter, GenericHeaderParser.RetryConditionParser);
198            parserStore.Add(HttpKnownHeaderNames.Server, ProductInfoHeaderParser.MultipleValueParser);
199            parserStore.Add(HttpKnownHeaderNames.Vary, GenericHeaderParser.TokenListParser);
200            parserStore.Add(HttpKnownHeaderNames.WWWAuthenticate, GenericHeaderParser.MultipleValueAuthenticationParser);
201
202            HttpGeneralHeaders.AddParsers(parserStore);
203
204            invalidHeaders = new HashSet<string>(HeaderUtilities.CaseInsensitiveStringComparer);
205            HttpRequestHeaders.AddKnownHeaders(invalidHeaders);
206            HttpContentHeaders.AddKnownHeaders(invalidHeaders);
207        }
208
209        internal static void AddKnownHeaders(HashSet<string> headerSet)
210        {
211            Contract.Requires(headerSet != null);
212
213            headerSet.Add(HttpKnownHeaderNames.AcceptRanges);
214            headerSet.Add(HttpKnownHeaderNames.Age);
215            headerSet.Add(HttpKnownHeaderNames.ETag);
216            headerSet.Add(HttpKnownHeaderNames.Location);
217            headerSet.Add(HttpKnownHeaderNames.ProxyAuthenticate);
218            headerSet.Add(HttpKnownHeaderNames.RetryAfter);
219            headerSet.Add(HttpKnownHeaderNames.Server);
220            headerSet.Add(HttpKnownHeaderNames.Vary);
221            headerSet.Add(HttpKnownHeaderNames.WWWAuthenticate);
222        }
223
224        internal override void AddHeaders(HttpHeaders sourceHeaders)
225        {
226            base.AddHeaders(sourceHeaders);
227            HttpResponseHeaders sourceResponseHeaders = sourceHeaders as HttpResponseHeaders;
228            Contract.Assert(sourceResponseHeaders != null);
229
230            // Copy special values, but do not overwrite
231            generalHeaders.AddSpecialsFrom(sourceResponseHeaders.generalHeaders);
232        }
233    }
234}