2010-01-20 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / System.Web / System.Web / HttpCacheVaryByHeaders.cs
1 //
2 // System.Web.HttpVaryByHeaders.cs 
3 //
4 // Author:
5 //      Chris Toshok (toshok@novell.com)
6 //
7
8 //
9 // Copyright (C) 2005-2009 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Collections;
32 using System.Security.Permissions;
33
34 namespace System.Web
35 {
36         // CAS - no InheritanceDemand here as the class is sealed
37         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         public sealed class HttpCacheVaryByHeaders
39         {
40                 /* I would have much rather seen this class just use the
41                  * Hashtable, and have the getter/setters for the builtin
42                  * fields just manipulate that Hashtable, but that doesn't
43                  * appear to work in MS's implementation.  If you do:
44                  *
45                  *         vary_by_hdrs["User-Agent"] = true;
46                  *
47                  * then
48                  *
49                  *         (vary_by_hdrs.UserAgent == true)
50                  *
51                  * will be false, which is completely counterintuitive
52                  * and broken. The same holds true in reverse:
53                  *
54                  *         vary_by_hdrs.UserAgent = true;
55                  *
56                  * does not mean
57                  *
58                  *         vary_by_hdrs["User-Agent"] == true.
59                  */
60                 bool vary_by_unspecified;
61
62                 bool vary_by_accept;
63                 bool vary_by_user_agent;
64                 bool vary_by_user_charset;
65                 bool vary_by_user_language;
66
67                 Hashtable fields;
68
69                 internal HttpCacheVaryByHeaders ()
70                 {
71                         /* the field names are meant to be case insensitive */
72                         fields = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
73                 }
74
75                 internal string[] GetHeaderNames (bool omitVaryStar)
76                 {
77                         string[] names;
78
79                         if (vary_by_unspecified && !omitVaryStar) {
80                                 names = new string[1];
81                                 names[0] = "*";
82                         }
83                         else {
84                                 int builtin_count = ((vary_by_accept ? 1 : 0)
85                                                      + (vary_by_user_agent ? 1 : 0)
86                                                      + (vary_by_user_charset ? 1 : 0)
87                                                      + (vary_by_user_language ? 1 : 0));
88
89                                 names = new string [fields.Count + builtin_count];
90
91                                 int i = 0;
92                                 if (vary_by_accept) names[i++] = "Accept";
93                                 if (vary_by_user_agent) names[i++] = "User-Agent";
94                                 if (vary_by_user_charset) names[i++] = "Accept-Charset";
95                                 if (vary_by_user_language) names[i++] = "Accept-Language";
96
97                                 fields.Keys.CopyTo (names, builtin_count);
98                         }
99
100                         return names;
101                 }
102
103                 public bool AcceptTypes {
104                         get {
105                                 return vary_by_accept;
106                         }
107                         set {
108                                 vary_by_unspecified = false;
109                                 vary_by_accept = value;
110                         }
111                 }
112
113                 public bool UserAgent {
114                         get {
115                                 return vary_by_user_agent;
116                         }
117                         set {
118                                 vary_by_unspecified = false;
119                                 vary_by_user_agent = value;
120                         }
121                 }
122
123                 public bool UserCharSet {
124                         get {
125                                 return vary_by_user_charset;
126                         }
127                         set {
128                                 vary_by_unspecified = false;
129                                 vary_by_user_charset = value;
130                         }
131                 }
132
133                 public bool UserLanguage {
134                         get {
135                                 return vary_by_user_language;
136                         }
137                         set {
138                                 vary_by_unspecified = false;
139                                 vary_by_user_language = value;
140                         }
141                 }
142
143                 public bool this [ string header ] {
144                         get {
145                                 if (header == null)
146                                         throw new ArgumentNullException ();
147
148                                 return fields.Contains (header);
149                         }
150                         set {
151                                 if (header == null)
152                                         throw new ArgumentNullException ();
153
154                                 vary_by_unspecified = false;
155                                 if (value)
156                                         if (!fields.Contains (header))
157                                                 fields.Add (header, true);
158                                 else
159                                         fields.Remove (header);
160                         }
161                 }
162
163                 public void VaryByUnspecifiedParameters ()
164                 {
165                         fields.Clear();
166
167                         vary_by_unspecified =
168                           vary_by_accept = 
169                           vary_by_user_agent =
170                           vary_by_user_charset =
171                           vary_by_user_language = false;
172
173                         vary_by_unspecified = true;
174                 }
175         }
176
177 }