-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathHttpCacheVaryByContentEncodings.cs
146 lines (126 loc) · 4.95 KB
/
HttpCacheVaryByContentEncodings.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
//------------------------------------------------------------------------------
// <copyright file="HttpCacheVaryByContentEncodings.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
/*
* HttpCacheVaryByContentEncodings
*
* 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>Provides a type-safe way to vary by Content-Encoding.</para>
/// </devdoc>
public sealed class HttpCacheVaryByContentEncodings {
String[] _contentEncodings;
bool _isModified;
public HttpCacheVaryByContentEncodings() {
Reset();
}
internal void Reset() {
_isModified = false;
_contentEncodings = null;
}
/// <summary>
/// Set the Content Encodings in Cache Vary
/// </summary>
/// <param name="contentEncodings"></param>
public void SetContentEncodings(string[] contentEncodings) {
Reset();
if (contentEncodings != null) {
_isModified = true;
_contentEncodings = new String[contentEncodings.Length];
for (int i = 0; i < contentEncodings.Length; i++) {
_contentEncodings[i] = contentEncodings[i];
}
}
}
// the response is not cacheable if we're varying by content encoding
// and the content-encoding header is not one of the encodings that we're
// varying by
internal bool IsCacheableEncoding(string coding) {
// return true if we are not varying by content encoding.
if (_contentEncodings == null) {
return true;
}
// return true if there is no Content-Encoding header
if (coding == null) {
return true;
}
// return true if the Content-Encoding header is listed
for (int i = 0; i < _contentEncodings.Length; i++) {
if (_contentEncodings[i] == coding) {
return true;
}
}
// return false if the Content-Encoding header is not listed
return false;
}
internal bool IsModified() {
return _isModified;
}
/// <summary>
/// Get the Content Encodings in Cache Vary
/// </summary>
/// <returns></returns>
public string[] GetContentEncodings() {
if (_contentEncodings != null) {
string[] contentEncodings = new string[_contentEncodings.Length];
_contentEncodings.CopyTo(contentEncodings, 0);
return contentEncodings;
}
return null;
}
//
// Public methods and properties
//
/// <devdoc>
/// <para> Default property.
/// Indexed property indicating that a cache should (or should not) vary according
/// to a Content-Encoding.</para>
/// </devdoc>
public bool this[String contentEncoding]
{
get {
if (String.IsNullOrEmpty(contentEncoding)) {
throw new ArgumentNullException(SR.GetString(SR.Parameter_NullOrEmpty, "contentEncoding"));
}
if (_contentEncodings == null) {
return false;
}
for(int i = 0; i < _contentEncodings.Length; i++) {
if (_contentEncodings[i] == contentEncoding) {
return true;
}
}
return false;
}
set {
if (String.IsNullOrEmpty(contentEncoding)) {
throw new ArgumentNullException(SR.GetString(SR.Parameter_NullOrEmpty, "contentEncoding"));
}
// if someone enabled it, don't allow someone else to disable it.
if (!value) {
return;
}
_isModified = true;
if (_contentEncodings != null) {
string[] contentEncodings = new String[_contentEncodings.Length + 1];
for (int i = 0; i < _contentEncodings.Length; i++) {
contentEncodings[i] = _contentEncodings[i];
}
contentEncodings[contentEncodings.Length - 1] = contentEncoding;
_contentEncodings = contentEncodings;
return;
}
_contentEncodings = new String[1];
_contentEncodings[0] = contentEncoding;
}
}
}
}