2010-01-20 Marek Habersack <mhabersack@novell.com>
[mono-project/dkf.git] / mcs / class / System.Web / System.Web / HttpCacheVaryByHeaders.cs
blob460963ff66eb342168f57179ca67dde17dcf7f82
1 //
2 // System.Web.HttpVaryByHeaders.cs
3 //
4 // Author:
5 // Chris Toshok (toshok@novell.com)
6 //
8 //
9 // Copyright (C) 2005-2009 Novell, Inc (http://www.novell.com)
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.
31 using System.Collections;
32 using System.Security.Permissions;
34 namespace System.Web
36 // CAS - no InheritanceDemand here as the class is sealed
37 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 public sealed class HttpCacheVaryByHeaders
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:
45 * vary_by_hdrs["User-Agent"] = true;
47 * then
49 * (vary_by_hdrs.UserAgent == true)
51 * will be false, which is completely counterintuitive
52 * and broken. The same holds true in reverse:
54 * vary_by_hdrs.UserAgent = true;
56 * does not mean
58 * vary_by_hdrs["User-Agent"] == true.
60 bool vary_by_unspecified;
62 bool vary_by_accept;
63 bool vary_by_user_agent;
64 bool vary_by_user_charset;
65 bool vary_by_user_language;
67 Hashtable fields;
69 #if NET_4_0
70 public
71 #else
72 internal
73 #endif
74 HttpCacheVaryByHeaders ()
76 /* the field names are meant to be case insensitive */
77 fields = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
80 internal string[] GetHeaderNames (bool omitVaryStar)
82 string[] names;
84 if (vary_by_unspecified && !omitVaryStar) {
85 names = new string[1];
86 names[0] = "*";
88 else {
89 int builtin_count = ((vary_by_accept ? 1 : 0)
90 + (vary_by_user_agent ? 1 : 0)
91 + (vary_by_user_charset ? 1 : 0)
92 + (vary_by_user_language ? 1 : 0));
94 names = new string [fields.Count + builtin_count];
96 int i = 0;
97 if (vary_by_accept) names[i++] = "Accept";
98 if (vary_by_user_agent) names[i++] = "User-Agent";
99 if (vary_by_user_charset) names[i++] = "Accept-Charset";
100 if (vary_by_user_language) names[i++] = "Accept-Language";
102 fields.Keys.CopyTo (names, builtin_count);
105 return names;
108 public bool AcceptTypes {
109 get {
110 return vary_by_accept;
112 set {
113 vary_by_unspecified = false;
114 vary_by_accept = value;
118 public bool UserAgent {
119 get {
120 return vary_by_user_agent;
122 set {
123 vary_by_unspecified = false;
124 vary_by_user_agent = value;
128 public bool UserCharSet {
129 get {
130 return vary_by_user_charset;
132 set {
133 vary_by_unspecified = false;
134 vary_by_user_charset = value;
138 public bool UserLanguage {
139 get {
140 return vary_by_user_language;
142 set {
143 vary_by_unspecified = false;
144 vary_by_user_language = value;
148 public bool this [ string header ] {
149 get {
150 if (header == null)
151 throw new ArgumentNullException ();
153 return fields.Contains (header);
155 set {
156 if (header == null)
157 throw new ArgumentNullException ();
159 vary_by_unspecified = false;
160 if (value)
161 if (!fields.Contains (header))
162 fields.Add (header, true);
163 else
164 fields.Remove (header);
168 public void VaryByUnspecifiedParameters ()
170 fields.Clear();
172 vary_by_unspecified =
173 vary_by_accept =
174 vary_by_user_agent =
175 vary_by_user_charset =
176 vary_by_user_language = false;
178 vary_by_unspecified = true;