2 // System.Web.HttpVaryByHeaders.cs
5 // Chris Toshok (toshok@novell.com)
9 // Copyright (C) 2005 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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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;
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;
58 * vary_by_hdrs["User-Agent"] == true.
60 bool vary_by_unspecified;
63 bool vary_by_user_agent;
64 bool vary_by_user_charset;
65 bool vary_by_user_language;
69 internal HttpCacheVaryByHeaders ()
71 /* the field names are meant to be case insensitive */
72 fields = new Hashtable(CaseInsensitiveHashCodeProvider.Default,
73 CaseInsensitiveComparer.Default);
76 internal string[] GetHeaderNames ()
80 if (vary_by_unspecified) {
81 names = new string[1];
85 int builtin_count = ((vary_by_accept ? 1 : 0)
86 + (vary_by_user_agent ? 1 : 0)
87 + (vary_by_user_charset ? 1 : 0)
88 + (vary_by_user_language ? 1 : 0));
90 names = new string [fields.Count + builtin_count];
93 if (vary_by_accept) names[i++] = "Accept";
94 if (vary_by_user_agent) names[i++] = "User-Agent";
95 if (vary_by_user_charset) names[i++] = "Accept-Charset";
96 if (vary_by_user_language) names[i++] = "Accept-Language";
98 fields.Keys.CopyTo (names, builtin_count);
104 public bool AcceptTypes {
106 return vary_by_accept;
109 vary_by_unspecified = false;
110 vary_by_accept = value;
114 public bool UserAgent {
116 return vary_by_user_agent;
119 vary_by_unspecified = false;
120 vary_by_user_agent = value;
124 public bool UserCharSet {
126 return vary_by_user_charset;
129 vary_by_unspecified = false;
130 vary_by_user_charset = value;
134 public bool UserLanguage {
136 return vary_by_user_language;
139 vary_by_unspecified = false;
140 vary_by_user_language = value;
144 public bool this [ string header ] {
147 throw new ArgumentNullException ();
149 return fields.Contains (header);
153 throw new ArgumentNullException ();
155 vary_by_unspecified = false;
157 if (!fields.Contains (header))
158 fields.Add (header, true);
160 fields.Remove (header);
164 public void VaryByUnspecifiedParameters ()
168 vary_by_unspecified =
171 vary_by_user_charset =
172 vary_by_user_language = false;
174 vary_by_unspecified = true;