Merge pull request #3142 from henricm/fix-for-win-mono_string_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 #if MONO
105                     var settings = new RequestCachingSectionInternal();
106 #else
107                     RequestCachingSectionInternal settings = RequestCachingSectionInternal.GetSection();
108
109                     s_DefaultGlobalBinding = new RequestCacheBinding (settings.DefaultCache, settings.DefaultHttpValidator, settings.DefaultCachePolicy);
110                     s_DefaultHttpBinding = new RequestCacheBinding (settings.DefaultCache, settings.DefaultHttpValidator, settings.DefaultHttpCachePolicy);
111                     s_DefaultFtpBinding = new RequestCacheBinding (settings.DefaultCache, settings.DefaultFtpValidator, settings.DefaultFtpCachePolicy);
112 #endif
113
114                     s_CacheConfigSettings = settings;
115                 }
116             }
117         }
118     }
119
120 #if MONO
121     class RequestCacheValidator
122     {
123         public object CreateValidator ()
124         {
125             throw new NotImplementedException ();
126         }
127     }
128
129     class RequestCachingSectionInternal
130     {
131         // TODO: Implement
132         public readonly bool DisableAllCaching = true;
133     }
134 #endif
135
136     //
137     //
138     internal class RequestCacheBinding  {
139         private RequestCache          m_RequestCache;
140         private RequestCacheValidator m_CacheValidator;
141         private RequestCachePolicy    m_Policy;
142
143
144         internal RequestCacheBinding (RequestCache requestCache, RequestCacheValidator cacheValidator, RequestCachePolicy  policy) {
145             m_RequestCache = requestCache;
146             m_CacheValidator = cacheValidator;
147             m_Policy = policy;
148         }
149
150         internal RequestCache Cache {
151             get {return m_RequestCache;}
152         }
153
154         internal RequestCacheValidator Validator {
155             get {return m_CacheValidator;}
156         }
157
158         internal RequestCachePolicy Policy {
159             get {return m_Policy;}
160         }
161
162     }
163 }