[coop] Temporarily restore MonoThreadInfo when TLS destructor runs. Fixes #43099
[mono.git] / mcs / class / referencesource / System.Web / HttpCacheVaryByContentEncodings.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="HttpCacheVaryByContentEncodings.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 //------------------------------------------------------------------------------
6
7 /*
8  * HttpCacheVaryByContentEncodings
9  * 
10  * Copyright (c) 1998 Microsoft Corporation
11  */
12
13 namespace System.Web {
14     using System.Text;
15     using System.Runtime.InteropServices;
16     using System.Web.Util;
17     using System.Security.Permissions;
18
19
20     /// <devdoc>
21     ///    <para>Provides a type-safe way to vary by Content-Encoding.</para>
22     /// </devdoc>
23     public sealed class HttpCacheVaryByContentEncodings {
24         String[]        _contentEncodings;
25         bool            _isModified;
26
27         public HttpCacheVaryByContentEncodings() {
28             Reset();
29         }
30
31         internal void Reset() {
32             _isModified = false;
33             _contentEncodings = null;
34         }
35
36         /*
37          * Reset based on content encodings.
38          */
39         internal void ResetFromContentEncodings(String[] contentEncodings) {
40             Reset();
41             if (contentEncodings != null) {
42                 _isModified = true;
43                 _contentEncodings = new String[contentEncodings.Length];
44                 for (int i = 0; i < contentEncodings.Length; i++) {
45                     _contentEncodings[i] = contentEncodings[i];
46                 }
47             }
48         }
49
50         // the response is not cacheable if we're varying by content encoding
51         // and the content-encoding header is not one of the encodings that we're
52         // varying by
53         internal bool IsCacheableEncoding(string coding) {
54             // return true if we are not varying by content encoding.
55             if (_contentEncodings == null) {
56                 return true;
57             }
58
59             // return true if there is no Content-Encoding header
60             if (coding == null) {
61                 return true;
62             }
63
64             // return true if the Content-Encoding header is listed
65             for (int i = 0; i < _contentEncodings.Length; i++) {
66                 if (_contentEncodings[i] == coding) {
67                     return true;
68                 }
69             }
70
71             // return false if the Content-Encoding header is not listed
72             return false;
73         }
74
75         internal bool IsModified() {
76             return _isModified;
77         }
78
79         internal String[] GetContentEncodings() {
80             return _contentEncodings;
81         }
82
83         //
84         // Public methods and properties
85         //
86
87
88         /// <devdoc>
89         ///    <para> Default property.
90         ///       Indexed property indicating that a cache should (or should not) vary according
91         ///       to a Content-Encoding.</para>
92         /// </devdoc>
93         public bool this[String contentEncoding]
94         {
95             get {
96                 if (String.IsNullOrEmpty(contentEncoding)) {
97                     throw new ArgumentNullException(SR.GetString(SR.Parameter_NullOrEmpty, "contentEncoding"));
98                 }
99                 if (_contentEncodings == null) {
100                     return false;
101                 }
102                 for(int i = 0; i < _contentEncodings.Length; i++) {
103                     if (_contentEncodings[i] == contentEncoding) {
104                         return true;
105                     }
106                 }
107                 return false;
108             }
109
110             set {
111                 if (String.IsNullOrEmpty(contentEncoding)) {
112                     throw new ArgumentNullException(SR.GetString(SR.Parameter_NullOrEmpty, "contentEncoding"));
113                 }
114
115                 // if someone enabled it, don't allow someone else to disable it.
116                 if (!value) {
117                     return;
118                 }
119
120                 _isModified = true;
121                 if (_contentEncodings != null) {
122                     string[] contentEncodings = new String[_contentEncodings.Length + 1];
123                     for (int i = 0; i < _contentEncodings.Length; i++) {
124                         contentEncodings[i] = _contentEncodings[i];
125                     }
126                     contentEncodings[contentEncodings.Length - 1] = contentEncoding;
127                     _contentEncodings = contentEncodings;
128                     return;
129                 }
130                 _contentEncodings = new String[1];
131                 _contentEncodings[0] = contentEncoding;
132             }
133         }
134     }
135 }