* OutputCacheModule.cs: VaryBy keys now need the HttpContext not just the request.
[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 using System;
12 using System.Text;
13
14 namespace System.Web.Caching {
15
16         internal class CachedVaryBy {
17
18                 private string[] prms;
19                 private string[] headers;
20                 private string custom;
21
22                 internal CachedVaryBy (HttpCachePolicy policy)
23                 {
24                         prms = policy.VaryByParams.GetParamNames ();
25                         headers = policy.VaryByHeaders.GetHeaderNames ();
26                         custom = policy.GetVaryByCustom ();
27                 }
28
29                 internal string CreateKey (string file_path, HttpContext context)
30                 {
31                         StringBuilder builder = new StringBuilder ();
32                         HttpApplication app = context.ApplicationInstance;
33                         HttpRequest request = context.Request;
34
35                         builder.Append ("CachedRawResponse\n");
36                         builder.Append (file_path);
37                         builder.Append ('\n');
38                         builder.Append ("METHOD:" + request.HttpMethod);
39                         builder.Append ('\n');
40
41                         if (prms != null) {
42                                 for (int i=0; i<prms.Length; i++) {
43                                         if (request.Params [prms [i]] == null)
44                                                 continue;
45                                         builder.Append ("VP:");
46                                         builder.Append (prms [i]);
47                                         builder.Append ('=');
48                                         builder.Append (request.Params [prms [i]]);
49                                         builder.Append ('\n');
50                                 }
51                         }
52                         
53                         if (headers != null) {
54                                 for (int i=0; i<headers.Length; i++) {
55                                         builder.Append ("VH:");
56                                         builder.Append (headers [i]);
57                                         builder.Append ('=');
58                                         builder.Append (request.Headers [headers [i]]);
59                                         builder.Append ('\n');
60                                 }
61                         }
62
63                         if (custom != null) {
64                                 string s = app.GetVaryByCustomString (context, custom);
65                                 builder.Append ("VC:");
66                                 builder.Append (custom);
67                                 builder.Append ('=');
68                                 builder.Append (s != null ? s : "__null__");
69                                 builder.Append ('\n');
70                         }
71
72                         return builder.ToString ();
73                 }
74         }
75 }
76