Merge pull request #3389 from lambdageek/bug-43099
[mono.git] / mcs / class / referencesource / System.Web / HttpCacheParams.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="HttpCacheParams.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 //------------------------------------------------------------------------------
6
7 /*
8  * Cache Vary class.  Wraps Vary header
9  * 
10  * Copyright (c) 1998 Microsoft Corporation
11  */
12
13 namespace System.Web {
14     using System.Text;
15     using System.Runtime.InteropServices;
16     using System.Web.Util;
17     using System.Security.Permissions;
18
19
20     /// <devdoc>
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>
24     /// </devdoc>
25     public sealed class HttpCacheVaryByParams {
26         HttpDictionary  _parameters;
27         int             _ignoreParams;
28         bool            _isModified;
29         bool            _paramsStar;
30
31         public HttpCacheVaryByParams() {
32             Reset();
33         }
34
35         internal void Reset() {
36             _isModified = false;
37             _paramsStar = false;
38             _parameters = null;
39             _ignoreParams = -1;
40         }
41
42         /// <summary>
43         /// Set the Parameters in Cache Vary 
44         /// </summary>
45         /// <param name="parameters"></param>
46         public void SetParams(string[] parameters) {
47             int i, n;
48
49             Reset();
50             if (parameters != null) {
51                 _isModified = true;
52                 if (parameters[0].Length == 0) {
53                     Debug.Assert(parameters.Length == 1, "parameters.Length == 1");
54
55                     IgnoreParams = true;
56                 }
57                 else if (parameters[0].Equals("*")) {
58                     Debug.Assert(parameters.Length == 1, "parameters.Length == 1");
59
60                     _paramsStar = true;
61                 }
62                 else {
63                     _parameters = new HttpDictionary();
64                     for (i = 0, n = parameters.Length; i < n; i++) {
65                         _parameters.SetValue(parameters[i], parameters[i]);
66                     }
67                 }
68             }
69         }
70
71         internal bool IsModified() {
72             return _isModified;
73         }
74
75         internal bool AcceptsParams() {
76             return _ignoreParams == 1 || _paramsStar || _parameters != null;
77         }
78
79         /// <summary>
80         /// Get the Parameters in Cache Vary
81         /// </summary>
82         /// <returns></returns>
83         public string[] GetParams() {
84             string[]    s = null;
85             Object      item;
86             int         i, j, c, n;
87
88             if (_ignoreParams == 1) {
89                 s =  new string[1] {string.Empty};
90             }
91             else if (_paramsStar) {
92                 s =  new string[1] {"*"};
93             }
94             else if (_parameters != null) {
95                 n = _parameters.Size;
96                 c = 0;
97                 for (i = 0; i < n; i++) {
98                     item = _parameters.GetValue(i);
99                     if (item != null) {
100                         c++;
101                     }
102                 }
103
104                 if (c > 0) {
105                     s = new string[c];
106                     j = 0;
107                     for (i = 0; i < n; i++) {
108                         item = _parameters.GetValue(i);
109                         if (item != null) {
110                             s[j] = (string) item;
111                             j++;
112                         }
113                     }
114
115                     Debug.Assert(j == c, "j == c");
116                 }
117             }
118
119             return s;
120         }
121
122         //
123         // Public methods and properties
124         // 
125
126
127         /// <devdoc>
128         ///    <para> Default property.
129         ///       Indexed property indicating that a cache should (or should not) vary according
130         ///       to a custom header.</para>
131         /// </devdoc>
132         public bool this[String header]
133         {
134             get {
135                 if (header == null) {
136                     throw new ArgumentNullException("header");
137                 }
138
139                 if (header.Length == 0) {
140                     return _ignoreParams == 1;
141                 }
142                 else {
143                     return _paramsStar || 
144                            (_parameters != null && _parameters.GetValue(header) != null);
145                 }
146             }
147
148             set {
149                 if (header == null) {
150                     throw new ArgumentNullException("header");
151                 }
152
153                 if (header.Length == 0) {
154                     IgnoreParams = value;
155                 }
156                 /*
157                  * Since adding a Vary parameter is more restrictive, we don't
158                  * want components to be able to set a Vary parameter to false
159                  * if another component has set it to true.
160                  */
161                 else if (value) {
162                     _isModified = true;
163                     _ignoreParams = 0;
164                     if (header.Equals("*")) {
165                         _paramsStar = true;
166                         _parameters = null;
167                     }
168                     else {
169                         // set value to header if true or null if false
170                         if (!_paramsStar) {
171                             if (_parameters == null) {
172                                 _parameters = new HttpDictionary();
173                             }
174
175                             _parameters.SetValue(header, header);
176                         }
177                     }
178                 }
179             }
180         }
181
182
183         public bool IgnoreParams {
184             get {
185                 return _ignoreParams == 1;
186             }
187
188             set {
189                 // Don't ignore if params have been added
190                 if (_paramsStar || _parameters != null) {
191                     return;
192                 }
193
194                 if (_ignoreParams == -1 || _ignoreParams == 1) {
195                     _ignoreParams = value ? 1 : 0;
196                     _isModified = true;
197                 }
198             }
199         }
200
201         internal bool IsVaryByStar { 
202             get { 
203                 return _paramsStar; 
204             } 
205         }
206     }
207 }