1 //------------------------------------------------------------------------------
2 // <copyright file="HttpCacheVary.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
8 * Cache Vary class. Wraps Vary header
10 * Copyright (c) 1998 Microsoft Corporation
13 namespace System.Web {
15 using System.Runtime.InteropServices;
16 using System.Web.Util;
17 using System.Security.Permissions;
21 /// <para>Indicates that a cache should contain multiple
22 /// representations for a particular Uri. This class is an encapsulation that
23 /// provides a rich, type-safe way to set the Vary header.</para>
25 public sealed class HttpCacheVaryByHeaders {
28 HttpDictionary _headers;
30 public HttpCacheVaryByHeaders() {
34 internal void Reset() {
41 /// Set the Headers in Cache Vary
43 /// <param name="headers"></param>
44 public void SetHeaders(string[] headers) {
48 if (headers == null) {
55 if (headers[0].Equals("*")) {
56 Debug.Assert(headers.Length == 1, "headers.Length == 1");
63 _headers = new HttpDictionary();
64 for (i = 0, n = headers.Length; i < n; i++) {
65 _headers.SetValue(headers[i], headers[i]);
71 internal bool IsModified() {
76 * Construct header value string
78 internal String ToHeaderString() {
86 else if (_headers != null) {
87 s = new StringBuilder();
89 for (i = 0, n = _headers.Size; i < n; i++) {
90 item = _headers.GetValue(i);
92 HttpCachePolicy.AppendValueToHeader(s, (String)item);
104 /// Get the Headers in Cache Vary
106 /// <returns></returns>
107 public string[] GetHeaders() {
114 return new string[1] {"*"};
116 else if (_headers != null) {
119 for (i = 0; i < n; i++) {
120 item = _headers.GetValue(i);
129 for (i = 0; i < n; i++) {
130 item = _headers.GetValue(i);
132 s[j] = (string) item;
137 Debug.Assert(j == c, "j == c");
145 // Public methods and properties
150 /// <para>Sets the "Vary: *" header and causes all other Vary:
151 /// header information to be dropped.</para>
153 public void VaryByUnspecifiedParameters() {
159 internal bool GetVaryByUnspecifiedParameters() {
164 * Vary by accept types
168 /// <para>Retrieves or assigns a value indicating whether the cache should vary by Accept types. This causes the
169 /// Vary: header to include an Accept field.</para>
171 public bool AcceptTypes {
173 return this["Accept"];
178 this["Accept"] = value;
183 * Vary by accept language
187 /// <para> Retrieves or assigns a Boolean value indicating whether
188 /// the cache should vary by user language.</para>
190 public bool UserLanguage {
192 return this["Accept-Language"];
197 this["Accept-Language"] = value;
206 /// <para> Retrieves or assigns a Boolean value indicating whether
207 /// the cache should vary by user agent.</para>
209 public bool UserAgent {
211 return this["User-Agent"];
216 this["User-Agent"] = value;
225 /// <para> Retrieves or assigns a value indicating whether the
226 /// cache should vary by browser character set.</para>
228 public bool UserCharSet {
230 return this["Accept-Charset"];
235 this["Accept-Charset"] = value;
240 * Vary by a given header
244 /// <para> Default property.
245 /// Indexed property indicating that a cache should (or should not) vary according
246 /// to a custom header.</para>
248 public bool this[String header]
251 if (header == null) {
252 throw new ArgumentNullException("header");
255 if (header.Equals("*")) {
259 return (_headers != null && _headers.GetValue(header) != null);
264 if (header == null) {
265 throw new ArgumentNullException("header");
269 * Since adding a Vary header is more restrictive, we don't
270 * want components to be able to set a Vary header to false
271 * if another component has set it to true.
273 if (value == false) {
279 if (header.Equals("*")) {
280 VaryByUnspecifiedParameters();
283 // set value to header if true or null if false
285 if (_headers == null) {
286 _headers = new HttpDictionary();
289 _headers.SetValue(header, header);