Wed Feb 24 15:47:16 CET 2010 Paolo Molaro <lupus@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.Caching / CachedVaryBy.cs
1 //
2 // System.Web.Caching.CachedVaryBy
3 //
4 // Authors:
5 //  Jackson Harper (jackson@ximian.com)
6 //  Marek Habersack <mhabersack@novell.com>
7 //
8 // (C) 2003-2010 Novell, Inc (http://www.novell.com)
9 //
10
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32
33 using System;
34 using System.Globalization;
35 using System.Text;
36 using System.Collections;
37 using System.Collections.Generic;
38 using System.Web.Util;
39
40 namespace System.Web.Caching
41 {
42 #if NET_4_0
43         [Serializable]
44 #endif
45         sealed class CachedVaryBy
46         {
47                 string[] prms;
48                 string[] headers;
49                 string custom;
50                 string key;
51                 List <string> item_list;
52                 bool wildCardParams;
53                 
54                 internal CachedVaryBy (HttpCachePolicy policy, string key)
55                 {
56                         prms = policy.VaryByParams.GetParamNames ();
57                         headers = policy.VaryByHeaders.GetHeaderNames (policy.OmitVaryStar);
58                         custom = policy.GetVaryByCustom ();
59                         this.key = key;
60                         item_list = new List <string> ();
61                         wildCardParams = policy.VaryByParams ["*"];
62                 }
63
64                 internal List <string> ItemList {
65                         get { return item_list; }
66                 }
67
68                 internal string Key {
69                         get { return key; }
70                 }
71                 
72                 internal string CreateKey (string file_path, HttpContext context)
73                 {
74                         if (String.IsNullOrEmpty (file_path))
75                                 throw new ArgumentNullException ("file_path");
76
77                         StringBuilder builder = new StringBuilder ("vbk"); // VaryBy Key
78                         HttpRequest request = context != null ? context.Request : null;
79                         string name, value;
80                         
81                         builder.Append (file_path);
82                         if (request == null)
83                                 return builder.ToString ();
84                         
85                         builder.Append (request.HttpMethod);
86                         
87                         if (wildCardParams) {
88                                 builder.Append ("WQ"); // Wildcard, Query
89                                 foreach (string p in request.QueryString) {
90                                         if (p == null)
91                                                 continue;
92                                         
93                                         builder.Append ('N'); // Name
94                                         builder.Append (p.ToLowerInvariant ());
95                                         value = request.QueryString [p];
96                                         if (String.IsNullOrEmpty (value))
97                                                 continue;
98                                         
99                                         builder.Append ('V'); // Value
100                                         builder.Append (value);
101                                 }
102
103                                 builder.Append ('F'); // Form
104                                 foreach (string p in request.Form) {
105                                         if (p == null)
106                                                 continue;
107                                         
108                                         builder.Append ('N'); // Name
109                                         builder.Append (p.ToLowerInvariant ());
110
111                                         value = request.Form [p];
112                                         if (String.IsNullOrEmpty (value))
113                                                 continue;
114                                         
115                                         builder.Append ('V'); // Value
116                                         builder.Append (value);
117                                 }
118                         } else if (prms != null) {
119                                 StringBuilder fprms = null;
120                                 builder.Append ("SQ"); // Specified, Query
121                                 
122                                 for (int i = 0; i < prms.Length; i++) {
123                                         name = prms [i];
124                                         if (String.IsNullOrEmpty (name))
125                                                 continue;
126
127                                         value = request.QueryString [name];
128                                         if (value != null) {
129                                                 builder.Append ('N'); // Name
130                                                 builder.Append (name.ToLowerInvariant ());
131
132                                                 if (value.Length > 0) {
133                                                         builder.Append ('V'); // Value
134                                                         builder.Append (value);
135                                                 }
136                                         }
137
138                                         value = request.Form [name];
139                                         if (value != null) {
140                                                 if (fprms == null)
141                                                         fprms = new StringBuilder ('F'); // Form
142                                                 
143                                                 builder.Append ('N'); // Name
144                                                 builder.Append (name.ToLowerInvariant ());
145                                                 if (value.Length > 0) {
146                                                         builder.Append ('V'); // Value
147                                                         builder.Append (value);
148                                                 }
149                                         }
150                                 }
151                                 if (fprms != null)
152                                         builder.Append (fprms.ToString ());
153                         }
154                         
155                         if (headers != null) {
156                                 builder.Append ('H'); // Headers
157                                 
158                                 for (int i=0; i < headers.Length; i++) {
159                                         builder.Append ('N'); // Name
160
161                                         name = headers [i];
162                                         builder.Append (name.ToLowerInvariant ());
163
164                                         value = request.Headers [name];
165                                         if (String.IsNullOrEmpty (value))
166                                                 continue;
167                                         
168                                         builder.Append ('V'); // Value
169                                         builder.Append (value);
170                                 }
171                         }
172
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__");
180                         }
181                         
182                         return builder.ToString ();
183                 }
184         }
185 }
186