2 // System.Web.Caching.CachedVaryBy
5 // Jackson Harper (jackson@ximian.com)
6 // Marek Habersack <mhabersack@novell.com>
8 // (C) 2003-2010 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Globalization;
36 using System.Collections;
37 using System.Collections.Generic;
38 using System.Web.Util;
40 namespace System.Web.Caching
45 sealed class CachedVaryBy
51 List <string> item_list;
54 internal CachedVaryBy (HttpCachePolicy policy, string key)
56 prms = policy.VaryByParams.GetParamNames ();
57 headers = policy.VaryByHeaders.GetHeaderNames (policy.OmitVaryStar);
58 custom = policy.GetVaryByCustom ();
60 item_list = new List <string> ();
61 wildCardParams = policy.VaryByParams ["*"];
64 internal List <string> ItemList {
65 get { return item_list; }
72 internal string CreateKey (string file_path, HttpContext context)
74 if (String.IsNullOrEmpty (file_path))
75 throw new ArgumentNullException ("file_path");
77 StringBuilder builder = new StringBuilder ("vbk"); // VaryBy Key
78 HttpRequest request = context != null ? context.Request : null;
81 builder.Append (file_path);
83 return builder.ToString ();
85 builder.Append (request.HttpMethod);
88 builder.Append ("WQ"); // Wildcard, Query
89 foreach (string p in request.QueryString) {
93 builder.Append ('N'); // Name
94 builder.Append (p.ToLowerInvariant ());
95 value = request.QueryString [p];
96 if (String.IsNullOrEmpty (value))
99 builder.Append ('V'); // Value
100 builder.Append (value);
103 builder.Append ('F'); // Form
104 foreach (string p in request.Form) {
108 builder.Append ('N'); // Name
109 builder.Append (p.ToLowerInvariant ());
111 value = request.Form [p];
112 if (String.IsNullOrEmpty (value))
115 builder.Append ('V'); // Value
116 builder.Append (value);
118 } else if (prms != null) {
119 StringBuilder fprms = null;
120 builder.Append ("SQ"); // Specified, Query
122 for (int i = 0; i < prms.Length; i++) {
124 if (String.IsNullOrEmpty (name))
127 value = request.QueryString [name];
129 builder.Append ('N'); // Name
130 builder.Append (name.ToLowerInvariant ());
132 if (value.Length > 0) {
133 builder.Append ('V'); // Value
134 builder.Append (value);
138 value = request.Form [name];
141 fprms = new StringBuilder ('F'); // Form
143 builder.Append ('N'); // Name
144 builder.Append (name.ToLowerInvariant ());
145 if (value.Length > 0) {
146 builder.Append ('V'); // Value
147 builder.Append (value);
152 builder.Append (fprms.ToString ());
155 if (headers != null) {
156 builder.Append ('H'); // Headers
158 for (int i=0; i < headers.Length; i++) {
159 builder.Append ('N'); // Name
162 builder.Append (name.ToLowerInvariant ());
164 value = request.Headers [name];
165 if (String.IsNullOrEmpty (value))
168 builder.Append ('V'); // Value
169 builder.Append (value);
173 if (custom != null) {
174 builder.Append ('C'); // Custom
175 string s = context.ApplicationInstance.GetVaryByCustomString (context, custom);
176 builder.Append ('N'); // Name
177 builder.Append (custom);
178 builder.Append ('V'); // Value
179 builder.Append (s != null ? s : "__null__");
182 return builder.ToString ();