Making sure mono_marshal_free is used instead of g_free in mono_string_builder_to_utf8
[mono.git] / mcs / class / referencesource / System / net / System / Net / Cache / RequestCacheManager.cs
1 /*++
2 Copyright (c) Microsoft Corporation
3
4 Module Name:
5
6     RequestCacheManager.cs
7
8 Abstract:
9     The class implements the app domain wide configuration
10     for request cache aware clients
11
12 Author:
13
14     Alexei Vopilov    21-Dec-2002
15
16 Revision History:
17
18     Jan 25 2004 - Changed the visibility of the class from public to internal, compressed unused logic.
19
20 --*/
21 namespace System.Net.Cache {
22 using System;
23 using System.Collections;
24 using System.Net.Configuration;
25 using System.Configuration;
26
27
28     /// <summary>  Specifies app domain-wide default settings for request caching </summary>
29     internal sealed class RequestCacheManager {
30         private RequestCacheManager() {}
31
32         private static volatile RequestCachingSectionInternal s_CacheConfigSettings;
33
34         private static readonly RequestCacheBinding s_BypassCacheBinding = new RequestCacheBinding (null, null, new RequestCachePolicy(RequestCacheLevel.BypassCache));
35
36         private static volatile RequestCacheBinding s_DefaultGlobalBinding;
37         private static volatile RequestCacheBinding s_DefaultHttpBinding;
38         private static volatile RequestCacheBinding s_DefaultFtpBinding;
39
40         //
41         // ATN: the caller MUST use uri.Scheme as a parameter, otheriwse cannot do schme ref comparision
42         //
43         internal static RequestCacheBinding GetBinding(string internedScheme)
44         {
45             if (internedScheme == null)
46                 throw new ArgumentNullException("uriScheme");
47
48             if (s_CacheConfigSettings == null)
49                 LoadConfigSettings();
50
51             if(s_CacheConfigSettings.DisableAllCaching)
52                 return s_BypassCacheBinding;
53
54             if (internedScheme.Length == 0)
55                 return s_DefaultGlobalBinding;
56
57             if ((object)internedScheme == (object)Uri.UriSchemeHttp || (object)internedScheme == (object)Uri.UriSchemeHttps)
58                 return s_DefaultHttpBinding;
59
60             if ((object)internedScheme == (object)Uri.UriSchemeFtp)
61                 return s_DefaultFtpBinding;
62
63             return s_BypassCacheBinding;
64         }
65
66         internal static bool IsCachingEnabled
67         {
68             get
69             {
70                 if (s_CacheConfigSettings == null)
71                     LoadConfigSettings();
72
73                 return !s_CacheConfigSettings.DisableAllCaching;
74             }
75         }
76
77         //
78         internal static void SetBinding(string uriScheme, RequestCacheBinding binding)
79         {
80             if (uriScheme == null)
81                 throw new ArgumentNullException("uriScheme");
82
83             if (s_CacheConfigSettings == null)
84                 LoadConfigSettings();
85
86             if(s_CacheConfigSettings.DisableAllCaching)
87                 return;
88
89             if (uriScheme.Length == 0)
90                 s_DefaultGlobalBinding = binding;
91             else if (uriScheme == Uri.UriSchemeHttp || uriScheme == Uri.UriSchemeHttps)
92                 s_DefaultHttpBinding = binding;
93             else if (uriScheme == Uri.UriSchemeFtp)
94                 s_DefaultFtpBinding = binding;
95         }
96         //
97         private static void LoadConfigSettings()
98         {
99             // Any concurent access shall go here and block until we've grabbed the config settings
100             lock (s_BypassCacheBinding)
101             {
102                 if (s_CacheConfigSettings == null)
103                 {
104                     RequestCachingSectionInternal settings = RequestCachingSectionInternal.GetSection();
105
106                     s_DefaultGlobalBinding = new RequestCacheBinding (settings.DefaultCache, settings.DefaultHttpValidator, settings.DefaultCachePolicy);
107                     s_DefaultHttpBinding = new RequestCacheBinding (settings.DefaultCache, settings.DefaultHttpValidator, settings.DefaultHttpCachePolicy);
108                     s_DefaultFtpBinding = new RequestCacheBinding (settings.DefaultCache, settings.DefaultFtpValidator, settings.DefaultFtpCachePolicy);
109
110                     s_CacheConfigSettings = settings;
111                 }
112             }
113         }
114     }
115
116     //
117     //
118     internal class RequestCacheBinding  {
119         private RequestCache          m_RequestCache;
120         private RequestCacheValidator m_CacheValidator;
121         private RequestCachePolicy    m_Policy;
122
123
124         internal RequestCacheBinding (RequestCache requestCache, RequestCacheValidator cacheValidator, RequestCachePolicy  policy) {
125             m_RequestCache = requestCache;
126             m_CacheValidator = cacheValidator;
127             m_Policy = policy;
128         }
129
130         internal RequestCache Cache {
131             get {return m_RequestCache;}
132         }
133
134         internal RequestCacheValidator Validator {
135             get {return m_CacheValidator;}
136         }
137
138         internal RequestCachePolicy Policy {
139             get {return m_Policy;}
140         }
141
142     }
143 }