-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathHttpCacheParams.cs
207 lines (178 loc) · 6.25 KB
/
HttpCacheParams.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//------------------------------------------------------------------------------
// <copyright file="HttpCacheParams.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
/*
* Cache Vary class. Wraps Vary header
*
* Copyright (c) 1998 Microsoft Corporation
*/
namespace System.Web {
using System.Text;
using System.Runtime.InteropServices;
using System.Web.Util;
using System.Security.Permissions;
/// <devdoc>
/// <para>Indicates that a cache should contain multiple
/// representations for a particular Uri. This class is an encapsulation that
/// provides a rich, type-safe way to set the Vary header.</para>
/// </devdoc>
public sealed class HttpCacheVaryByParams {
HttpDictionary _parameters;
int _ignoreParams;
bool _isModified;
bool _paramsStar;
public HttpCacheVaryByParams() {
Reset();
}
internal void Reset() {
_isModified = false;
_paramsStar = false;
_parameters = null;
_ignoreParams = -1;
}
/// <summary>
/// Set the Parameters in Cache Vary
/// </summary>
/// <param name="parameters"></param>
public void SetParams(string[] parameters) {
int i, n;
Reset();
if (parameters != null) {
_isModified = true;
if (parameters[0].Length == 0) {
Debug.Assert(parameters.Length == 1, "parameters.Length == 1");
IgnoreParams = true;
}
else if (parameters[0].Equals("*")) {
Debug.Assert(parameters.Length == 1, "parameters.Length == 1");
_paramsStar = true;
}
else {
_parameters = new HttpDictionary();
for (i = 0, n = parameters.Length; i < n; i++) {
_parameters.SetValue(parameters[i], parameters[i]);
}
}
}
}
internal bool IsModified() {
return _isModified;
}
internal bool AcceptsParams() {
return _ignoreParams == 1 || _paramsStar || _parameters != null;
}
/// <summary>
/// Get the Parameters in Cache Vary
/// </summary>
/// <returns></returns>
public string[] GetParams() {
string[] s = null;
Object item;
int i, j, c, n;
if (_ignoreParams == 1) {
s = new string[1] {string.Empty};
}
else if (_paramsStar) {
s = new string[1] {"*"};
}
else if (_parameters != null) {
n = _parameters.Size;
c = 0;
for (i = 0; i < n; i++) {
item = _parameters.GetValue(i);
if (item != null) {
c++;
}
}
if (c > 0) {
s = new string[c];
j = 0;
for (i = 0; i < n; i++) {
item = _parameters.GetValue(i);
if (item != null) {
s[j] = (string) item;
j++;
}
}
Debug.Assert(j == c, "j == c");
}
}
return s;
}
//
// Public methods and properties
//
/// <devdoc>
/// <para> Default property.
/// Indexed property indicating that a cache should (or should not) vary according
/// to a custom header.</para>
/// </devdoc>
public bool this[String header]
{
get {
if (header == null) {
throw new ArgumentNullException("header");
}
if (header.Length == 0) {
return _ignoreParams == 1;
}
else {
return _paramsStar ||
(_parameters != null && _parameters.GetValue(header) != null);
}
}
set {
if (header == null) {
throw new ArgumentNullException("header");
}
if (header.Length == 0) {
IgnoreParams = value;
}
/*
* Since adding a Vary parameter is more restrictive, we don't
* want components to be able to set a Vary parameter to false
* if another component has set it to true.
*/
else if (value) {
_isModified = true;
_ignoreParams = 0;
if (header.Equals("*")) {
_paramsStar = true;
_parameters = null;
}
else {
// set value to header if true or null if false
if (!_paramsStar) {
if (_parameters == null) {
_parameters = new HttpDictionary();
}
_parameters.SetValue(header, header);
}
}
}
}
}
public bool IgnoreParams {
get {
return _ignoreParams == 1;
}
set {
// Don't ignore if params have been added
if (_paramsStar || _parameters != null) {
return;
}
if (_ignoreParams == -1 || _ignoreParams == 1) {
_ignoreParams = value ? 1 : 0;
_isModified = true;
}
}
}
internal bool IsVaryByStar {
get {
return _paramsStar;
}
}
}
}