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
43 sealed class CachedVaryBy
49 List <string> item_list;
52 internal CachedVaryBy (HttpCachePolicy policy, string key)
54 prms = policy.VaryByParams.GetParamNames ();
55 headers = policy.VaryByHeaders.GetHeaderNames (policy.OmitVaryStar);
56 custom = policy.GetVaryByCustom ();
58 item_list = new List <string> ();
59 wildCardParams = policy.VaryByParams ["*"];
62 internal List <string> ItemList {
63 get { return item_list; }
70 internal string CreateKey (string file_path, HttpContext context)
72 if (String.IsNullOrEmpty (file_path))
73 throw new ArgumentNullException ("file_path");
75 StringBuilder builder = new StringBuilder ("vbk"); // VaryBy Key
76 HttpRequest request = context != null ? context.Request : null;
79 builder.Append (file_path);
81 return builder.ToString ();
83 builder.Append (request.HttpMethod);
86 builder.Append ("WQ"); // Wildcard, Query
87 foreach (string p in request.QueryString) {
91 builder.Append ('N'); // Name
92 builder.Append (p.ToLowerInvariant ());
93 value = request.QueryString [p];
94 if (String.IsNullOrEmpty (value))
97 builder.Append ('V'); // Value
98 builder.Append (value);
101 builder.Append ('F'); // Form
102 foreach (string p in request.Form) {
106 builder.Append ('N'); // Name
107 builder.Append (p.ToLowerInvariant ());
109 value = request.Form [p];
110 if (String.IsNullOrEmpty (value))
113 builder.Append ('V'); // Value
114 builder.Append (value);
116 } else if (prms != null) {
117 StringBuilder fprms = null;
118 builder.Append ("SQ"); // Specified, Query
120 for (int i = 0; i < prms.Length; i++) {
122 if (String.IsNullOrEmpty (name))
125 value = request.QueryString [name];
127 builder.Append ('N'); // Name
128 builder.Append (name.ToLowerInvariant ());
130 if (value.Length > 0) {
131 builder.Append ('V'); // Value
132 builder.Append (value);
136 value = request.Form [name];
139 fprms = new StringBuilder ('F'); // Form
141 builder.Append ('N'); // Name
142 builder.Append (name.ToLowerInvariant ());
143 if (value.Length > 0) {
144 builder.Append ('V'); // Value
145 builder.Append (value);
150 builder.Append (fprms.ToString ());
153 if (headers != null) {
154 builder.Append ('H'); // Headers
156 for (int i=0; i < headers.Length; i++) {
157 builder.Append ('N'); // Name
160 builder.Append (name.ToLowerInvariant ());
162 value = request.Headers [name];
163 if (String.IsNullOrEmpty (value))
166 builder.Append ('V'); // Value
167 builder.Append (value);
171 if (custom != null) {
172 builder.Append ('C'); // Custom
173 string s = context.ApplicationInstance.GetVaryByCustomString (context, custom);
174 builder.Append ('N'); // Name
175 builder.Append (custom);
176 builder.Append ('V'); // Value
177 builder.Append (s != null ? s : "__null__");
180 return builder.ToString ();