Merge pull request #2735 from xmcclure/early-lookup-addr
[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         [Serializable]
43         sealed class CachedVaryBy
44         {
45                 string[] prms;
46                 string[] headers;
47                 string custom;
48                 string key;
49                 List <string> item_list;
50                 bool wildCardParams;
51                 
52                 internal CachedVaryBy (HttpCachePolicy policy, string key)
53                 {
54                         prms = policy.VaryByParams.GetParamNames ();
55                         headers = policy.VaryByHeaders.GetHeaderNames (policy.OmitVaryStar);
56                         custom = policy.GetVaryByCustom ();
57                         this.key = key;
58                         item_list = new List <string> ();
59                         wildCardParams = policy.VaryByParams ["*"];
60                 }
61
62                 internal List <string> ItemList {
63                         get { return item_list; }
64                 }
65
66                 internal string Key {
67                         get { return key; }
68                 }
69                 
70                 internal string CreateKey (string file_path, HttpContext context)
71                 {
72                         if (String.IsNullOrEmpty (file_path))
73                                 throw new ArgumentNullException ("file_path");
74
75                         StringBuilder builder = new StringBuilder ("vbk"); // VaryBy Key
76                         HttpRequest request = context != null ? context.Request : null;
77                         string name, value;
78                         
79                         builder.Append (file_path);
80                         if (request == null)
81                                 return builder.ToString ();
82                         
83                         builder.Append (request.HttpMethod);
84                         
85                         if (wildCardParams) {
86                                 builder.Append ("WQ"); // Wildcard, Query
87                                 foreach (string p in request.QueryString) {
88                                         if (p == null)
89                                                 continue;
90                                         
91                                         builder.Append ('N'); // Name
92                                         builder.Append (p.ToLowerInvariant ());
93                                         value = request.QueryString [p];
94                                         if (String.IsNullOrEmpty (value))
95                                                 continue;
96                                         
97                                         builder.Append ('V'); // Value
98                                         builder.Append (value);
99                                 }
100
101                                 builder.Append ('F'); // Form
102                                 foreach (string p in request.Form) {
103                                         if (p == null)
104                                                 continue;
105                                         
106                                         builder.Append ('N'); // Name
107                                         builder.Append (p.ToLowerInvariant ());
108
109                                         value = request.Form [p];
110                                         if (String.IsNullOrEmpty (value))
111                                                 continue;
112                                         
113                                         builder.Append ('V'); // Value
114                                         builder.Append (value);
115                                 }
116                         } else if (prms != null) {
117                                 StringBuilder fprms = null;
118                                 builder.Append ("SQ"); // Specified, Query
119                                 
120                                 for (int i = 0; i < prms.Length; i++) {
121                                         name = prms [i];
122                                         if (String.IsNullOrEmpty (name))
123                                                 continue;
124
125                                         value = request.QueryString [name];
126                                         if (value != null) {
127                                                 builder.Append ('N'); // Name
128                                                 builder.Append (name.ToLowerInvariant ());
129
130                                                 if (value.Length > 0) {
131                                                         builder.Append ('V'); // Value
132                                                         builder.Append (value);
133                                                 }
134                                         }
135
136                                         value = request.Form [name];
137                                         if (value != null) {
138                                                 if (fprms == null)
139                                                         fprms = new StringBuilder ('F'); // Form
140                                                 
141                                                 builder.Append ('N'); // Name
142                                                 builder.Append (name.ToLowerInvariant ());
143                                                 if (value.Length > 0) {
144                                                         builder.Append ('V'); // Value
145                                                         builder.Append (value);
146                                                 }
147                                         }
148                                 }
149                                 if (fprms != null)
150                                         builder.Append (fprms.ToString ());
151                         }
152                         
153                         if (headers != null) {
154                                 builder.Append ('H'); // Headers
155                                 
156                                 for (int i=0; i < headers.Length; i++) {
157                                         builder.Append ('N'); // Name
158
159                                         name = headers [i];
160                                         builder.Append (name.ToLowerInvariant ());
161
162                                         value = request.Headers [name];
163                                         if (String.IsNullOrEmpty (value))
164                                                 continue;
165                                         
166                                         builder.Append ('V'); // Value
167                                         builder.Append (value);
168                                 }
169                         }
170
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__");
178                         }
179                         
180                         return builder.ToString ();
181                 }
182         }
183 }
184