merge -r 60439:60440
[mono.git] / mcs / class / System.Web / System.Web.Caching / CachedVaryBy.cs
1 //
2 // System.Web.Caching.CachedVaryBy
3 //
4 // Author(s):
5 //  Jackson Harper (jackson@ximian.com)
6 //
7 // (C) 2003 Novell, Inc (http://www.novell.com)
8 //
9
10 //
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.
29 //
30
31
32 using System;
33 using System.Globalization;
34 using System.Text;
35 using System.Collections;
36
37 namespace System.Web.Caching {
38
39         internal class CachedVaryBy {
40
41                 private string[] prms;
42                 private string[] headers;
43                 private string custom;
44                 private string key;
45                 private ArrayList item_list;
46                 private bool wildCardParams;
47                 
48                 internal CachedVaryBy (HttpCachePolicy policy, string key)
49                 {
50                         prms = policy.VaryByParams.GetParamNames ();
51                         headers = policy.VaryByHeaders.GetHeaderNames ();
52                         custom = policy.GetVaryByCustom ();
53                         this.key = key;
54                         item_list = new ArrayList ();
55                         wildCardParams = policy.VaryByParams ["*"];
56                 }
57
58                 internal ArrayList ItemList {
59                         get { return item_list; }
60                 }
61
62                 internal string Key {
63                         get { return key; }
64                 }
65                 
66                 internal string CreateKey (string file_path, HttpContext context)
67                 {
68                         StringBuilder builder = new StringBuilder ();
69                         HttpApplication app = context.ApplicationInstance;
70                         HttpRequest request = context.Request;
71
72                         builder.Append ("CachedRawResponse\n");
73                         builder.Append (file_path);
74                         builder.Append ('\n');
75                         builder.Append ("METHOD:" + request.HttpMethod);
76                         builder.Append ('\n');
77
78                         if (wildCardParams) {
79                                 foreach (string p in request.QueryString) {
80                                         // FIXME: QueryString might contain a null key if a page gets called like this: page.aspx?arg (w/out the "=")
81                                         if (p == null) continue;
82                                         builder.Append ("VPQ:");
83                                         builder.Append (p.ToLower (CultureInfo.InvariantCulture));
84                                         builder.Append ('=');
85                                         builder.Append (request.QueryString [p]);
86                                         builder.Append ('\n');
87                                 }
88                                 foreach (string p in request.Form) {
89                                         // FIXME: can this be null, too?
90                                         if (p == null) continue;
91                                         builder.Append ("VPF:");
92                                         builder.Append (p.ToLower (CultureInfo.InvariantCulture));
93                                         builder.Append ('=');
94                                         builder.Append (request.Form [p]);
95                                         builder.Append ('\n');
96                                 }
97                         } else if (prms != null) {
98                                 for (int i=0; i<prms.Length; i++) {
99                                         if (request.QueryString [prms [i]] != null) {
100                                                 builder.Append ("VPQ:");
101                                                 builder.Append (prms [i].ToLower (CultureInfo.InvariantCulture));
102                                                 builder.Append ('=');
103                                                 builder.Append (request.QueryString [prms [i]]);
104                                                 builder.Append ('\n');
105                                         }
106                                         if (request.Form [prms [i]] != null) {
107                                                 builder.Append ("VPF:");
108                                                 builder.Append (prms [i].ToLower (CultureInfo.InvariantCulture));
109                                                 builder.Append ('=');
110                                                 builder.Append (request.Form [prms [i]]);
111                                                 builder.Append ('\n');
112                                         }
113                                 }
114                         }
115                         
116                         if (headers != null) {
117                                 for (int i=0; i<headers.Length; i++) {
118                                         builder.Append ("VH:");
119                                         builder.Append (headers [i].ToLower (CultureInfo.InvariantCulture));
120                                         builder.Append ('=');
121                                         builder.Append (request.Headers [headers [i]]);
122                                         builder.Append ('\n');
123                                 }
124                         }
125
126                         if (custom != null) {
127                                 string s = app.GetVaryByCustomString (context, custom);
128                                 builder.Append ("VC:");
129                                 builder.Append (custom);
130                                 builder.Append ('=');
131                                 builder.Append (s != null ? s : "__null__");
132                                 builder.Append ('\n');
133                         }
134
135                         return builder.ToString ();
136                 }
137         }
138 }
139