b3df15d6f1a703a462e834bb3dfd8d7c271c61e8
[mono.git] / mcs / class / referencesource / System.ServiceModel / System / ServiceModel / Configuration / HttpTransportElement.cs
1 //------------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //------------------------------------------------------------------------------
4
5 namespace System.ServiceModel.Configuration
6 {
7     using System.Configuration;
8     using System.Diagnostics.CodeAnalysis;
9     using System.ServiceModel;
10     using System.Globalization;
11     using System.Net;
12     using System.Net.Security;
13     using System.Runtime;
14     using System.Security.Authentication.ExtendedProtection.Configuration;
15     using System.Security.Principal;
16     using System.ServiceModel.Channels;
17     using System.ComponentModel;
18
19     [SuppressMessage(FxCop.Category.Configuration, FxCop.Rule.ConfigurationValidatorAttributeRule,
20             Justification = "The ExtendedProtectionPolicyElement configuration object and the configuration validation is owned by the NCL team.")]
21     public partial class HttpTransportElement : TransportElement
22     {
23         public HttpTransportElement()
24         {
25         }
26
27         [ConfigurationProperty(ConfigurationStrings.AllowCookies, DefaultValue = HttpTransportDefaults.AllowCookies)]
28         public bool AllowCookies
29         {
30             get { return (bool)base[ConfigurationStrings.AllowCookies]; }
31             set { base[ConfigurationStrings.AllowCookies] = value; }
32         }
33
34         [ConfigurationProperty(ConfigurationStrings.RequestInitializationTimeout, DefaultValue = HttpTransportDefaults.RequestInitializationTimeoutString)]
35         [TypeConverter(typeof(TimeSpanOrInfiniteConverter))]
36         [ServiceModelTimeSpanValidator(MinValueString = ConfigurationStrings.TimeSpanZero)]
37         public TimeSpan RequestInitializationTimeout
38         {
39             get { return (TimeSpan)base[ConfigurationStrings.RequestInitializationTimeout]; }
40             set { base[ConfigurationStrings.RequestInitializationTimeout] = value; }
41         }
42
43         [ConfigurationProperty(ConfigurationStrings.AuthenticationScheme, DefaultValue = HttpTransportDefaults.AuthenticationScheme)]
44         [StandardRuntimeFlagEnumValidator(typeof(AuthenticationSchemes))]
45         public AuthenticationSchemes AuthenticationScheme
46         {
47             get { return (AuthenticationSchemes)base[ConfigurationStrings.AuthenticationScheme]; }
48             set { base[ConfigurationStrings.AuthenticationScheme] = value; }
49         }
50
51         public override Type BindingElementType
52         {
53             get { return typeof(HttpTransportBindingElement); }
54         }
55
56         [ConfigurationProperty(ConfigurationStrings.BypassProxyOnLocal, DefaultValue = HttpTransportDefaults.BypassProxyOnLocal)]
57         public bool BypassProxyOnLocal
58         {
59             get { return (bool)base[ConfigurationStrings.BypassProxyOnLocal]; }
60             set { base[ConfigurationStrings.BypassProxyOnLocal] = value; }
61         }
62
63         [ConfigurationProperty(ConfigurationStrings.DecompressionEnabled, DefaultValue = HttpTransportDefaults.DecompressionEnabled)]
64         public bool DecompressionEnabled
65         {
66             get { return (bool)base[ConfigurationStrings.DecompressionEnabled]; }
67             set { base[ConfigurationStrings.DecompressionEnabled] = value; }
68         }
69
70         [ConfigurationProperty(ConfigurationStrings.HostNameComparisonMode, DefaultValue = HttpTransportDefaults.HostNameComparisonMode)]
71         [ServiceModelEnumValidator(typeof(HostNameComparisonModeHelper))]
72         public HostNameComparisonMode HostNameComparisonMode
73         {
74             get { return (HostNameComparisonMode)base[ConfigurationStrings.HostNameComparisonMode]; }
75             set { base[ConfigurationStrings.HostNameComparisonMode] = value; }
76         }
77
78         [ConfigurationProperty(ConfigurationStrings.KeepAliveEnabled, DefaultValue = HttpTransportDefaults.KeepAliveEnabled)]
79         public bool KeepAliveEnabled
80         {
81             get { return (bool)base[ConfigurationStrings.KeepAliveEnabled]; }
82             set { base[ConfigurationStrings.KeepAliveEnabled] = value; }
83         }
84
85         [ConfigurationProperty(ConfigurationStrings.MaxBufferSize, DefaultValue = TransportDefaults.MaxBufferSize)]
86         [IntegerValidator(MinValue = 1)]
87         public int MaxBufferSize
88         {
89             get { return (int)base[ConfigurationStrings.MaxBufferSize]; }
90             set { base[ConfigurationStrings.MaxBufferSize] = value; }
91         }
92
93         [ConfigurationProperty(ConfigurationStrings.MaxPendingAccepts, DefaultValue = HttpTransportDefaults.DefaultMaxPendingAccepts)]
94         [IntegerValidator(MinValue = 0, MaxValue = HttpTransportDefaults.MaxPendingAcceptsUpperLimit)]
95         public int MaxPendingAccepts
96         {
97             get { return (int)base[ConfigurationStrings.MaxPendingAccepts]; }
98             set { base[ConfigurationStrings.MaxPendingAccepts] = value; }
99         }
100
101         [ConfigurationProperty(ConfigurationStrings.MessageHandlerFactory, DefaultValue = HttpTransportDefaults.MessageHandlerFactory)]
102         [HttpMessageHandlerFactoryValidator]
103         public HttpMessageHandlerFactoryElement MessageHandlerFactory
104         {
105             get { return (HttpMessageHandlerFactoryElement)this[ConfigurationStrings.MessageHandlerFactory]; }
106             set { base[ConfigurationStrings.MessageHandlerFactory] = value; }
107         }
108
109         [ConfigurationProperty(ConfigurationStrings.ProxyAddress, DefaultValue = HttpTransportDefaults.ProxyAddress)]
110         public Uri ProxyAddress
111         {
112             get { return (Uri)base[ConfigurationStrings.ProxyAddress]; }
113             set { base[ConfigurationStrings.ProxyAddress] = value; }
114         }
115
116         [ConfigurationProperty(ConfigurationStrings.ProxyAuthenticationScheme, DefaultValue = HttpTransportDefaults.ProxyAuthenticationScheme)]
117         [StandardRuntimeEnumValidator(typeof(AuthenticationSchemes))]
118         public AuthenticationSchemes ProxyAuthenticationScheme
119         {
120             get { return (AuthenticationSchemes)base[ConfigurationStrings.ProxyAuthenticationScheme]; }
121             set { base[ConfigurationStrings.ProxyAuthenticationScheme] = value; }
122         }
123
124         [ConfigurationProperty(ConfigurationStrings.Realm, DefaultValue = HttpTransportDefaults.Realm)]
125         [StringValidator(MinLength = 0)]
126         public string Realm
127         {
128             get { return (string)base[ConfigurationStrings.Realm]; }
129             set
130             {
131                 if (String.IsNullOrEmpty(value))
132                 {
133                     value = String.Empty;
134                 }
135                 base[ConfigurationStrings.Realm] = value;
136             }
137         }
138
139         [ConfigurationProperty(ConfigurationStrings.TransferMode, DefaultValue = HttpTransportDefaults.TransferMode)]
140         [ServiceModelEnumValidator(typeof(TransferModeHelper))]
141         public TransferMode TransferMode
142         {
143             get { return (TransferMode)base[ConfigurationStrings.TransferMode]; }
144             set { base[ConfigurationStrings.TransferMode] = value; }
145         }
146
147         [ConfigurationProperty(ConfigurationStrings.UnsafeConnectionNtlmAuthentication, DefaultValue = HttpTransportDefaults.UnsafeConnectionNtlmAuthentication)]
148         public bool UnsafeConnectionNtlmAuthentication
149         {
150             get { return (bool)base[ConfigurationStrings.UnsafeConnectionNtlmAuthentication]; }
151             set { base[ConfigurationStrings.UnsafeConnectionNtlmAuthentication] = value; }
152         }
153
154         [ConfigurationProperty(ConfigurationStrings.UseDefaultWebProxy, DefaultValue = HttpTransportDefaults.UseDefaultWebProxy)]
155         public bool UseDefaultWebProxy
156         {
157             get { return (bool)base[ConfigurationStrings.UseDefaultWebProxy]; }
158             set { base[ConfigurationStrings.UseDefaultWebProxy] = value; }
159         }
160
161
162         [ConfigurationProperty(ConfigurationStrings.ExtendedProtectionPolicy)]
163         public ExtendedProtectionPolicyElement ExtendedProtectionPolicy
164         {
165             get { return (ExtendedProtectionPolicyElement)base[ConfigurationStrings.ExtendedProtectionPolicy]; }
166             private set { base[ConfigurationStrings.ExtendedProtectionPolicy] = value; }
167         }
168
169         [System.Diagnostics.CodeAnalysis.SuppressMessage(FxCop.Category.Configuration, "Configuration104")]
170         [ConfigurationProperty(ConfigurationStrings.WebSocketSettingsSectionName)]
171         public WebSocketTransportSettingsElement WebSocketSettings
172         {
173             get { return (WebSocketTransportSettingsElement)base[ConfigurationStrings.WebSocketSettingsSectionName]; }
174             set { base[ConfigurationStrings.WebSocketSettingsSectionName] = value; }
175         }
176
177         public override void ApplyConfiguration(BindingElement bindingElement)
178         {
179             base.ApplyConfiguration(bindingElement);
180             HttpTransportBindingElement binding = (HttpTransportBindingElement)bindingElement;
181
182             binding.AllowCookies = this.AllowCookies;
183             binding.AuthenticationScheme = this.AuthenticationScheme;
184             binding.BypassProxyOnLocal = this.BypassProxyOnLocal;
185             binding.DecompressionEnabled = this.DecompressionEnabled;
186             binding.KeepAliveEnabled = this.KeepAliveEnabled;
187             binding.HostNameComparisonMode = this.HostNameComparisonMode;
188             PropertyInformationCollection propertyInfo = this.ElementInformation.Properties;
189             if (propertyInfo[ConfigurationStrings.MaxBufferSize].ValueOrigin != PropertyValueOrigin.Default)
190             {
191                 binding.MaxBufferSize = this.MaxBufferSize;
192             }
193             binding.MaxPendingAccepts = this.MaxPendingAccepts;
194             binding.ProxyAddress = this.ProxyAddress;
195             binding.ProxyAuthenticationScheme = this.ProxyAuthenticationScheme;
196             binding.Realm = this.Realm;
197             binding.RequestInitializationTimeout = this.RequestInitializationTimeout;
198             binding.TransferMode = this.TransferMode;
199             binding.UnsafeConnectionNtlmAuthentication = this.UnsafeConnectionNtlmAuthentication;
200             binding.UseDefaultWebProxy = this.UseDefaultWebProxy;
201             binding.ExtendedProtectionPolicy = ChannelBindingUtility.BuildPolicy(this.ExtendedProtectionPolicy);
202             this.WebSocketSettings.ApplyConfiguration(binding.WebSocketSettings);
203             if (this.MessageHandlerFactory != null)
204             {
205                 binding.MessageHandlerFactory = HttpMessageHandlerFactory.CreateFromConfigurationElement(this.MessageHandlerFactory);
206             }
207         }
208
209         public override void CopyFrom(ServiceModelExtensionElement from)
210         {
211             base.CopyFrom(from);
212
213             HttpTransportElement source = (HttpTransportElement)from;
214 #pragma warning suppress 56506 // Microsoft, base.CopyFrom() validates the argument
215             this.AllowCookies = source.AllowCookies;
216             this.RequestInitializationTimeout = source.RequestInitializationTimeout;
217             this.AuthenticationScheme = source.AuthenticationScheme;
218             this.BypassProxyOnLocal = source.BypassProxyOnLocal;
219             this.DecompressionEnabled = source.DecompressionEnabled;
220             this.KeepAliveEnabled = source.KeepAliveEnabled;
221             this.HostNameComparisonMode = source.HostNameComparisonMode;
222             this.MaxBufferSize = source.MaxBufferSize;
223             this.MaxPendingAccepts = source.MaxPendingAccepts;
224             this.ProxyAddress = source.ProxyAddress;
225             this.ProxyAuthenticationScheme = source.ProxyAuthenticationScheme;
226             this.Realm = source.Realm;
227             this.TransferMode = source.TransferMode;
228             this.UnsafeConnectionNtlmAuthentication = source.UnsafeConnectionNtlmAuthentication;
229             this.UseDefaultWebProxy = source.UseDefaultWebProxy;
230             this.WebSocketSettings = source.WebSocketSettings;
231             this.MessageHandlerFactory = source.MessageHandlerFactory;
232             ChannelBindingUtility.CopyFrom(source.ExtendedProtectionPolicy, this.ExtendedProtectionPolicy);
233         }
234
235         protected override TransportBindingElement CreateDefaultBindingElement()
236         {
237             return new HttpTransportBindingElement();
238         }
239
240         protected internal override void InitializeFrom(BindingElement bindingElement)
241         {
242             base.InitializeFrom(bindingElement);
243             HttpTransportBindingElement source = (HttpTransportBindingElement)bindingElement;
244             SetPropertyValueIfNotDefaultValue(ConfigurationStrings.AllowCookies, source.AllowCookies);
245             SetPropertyValueIfNotDefaultValue(ConfigurationStrings.AuthenticationScheme, source.AuthenticationScheme);
246             SetPropertyValueIfNotDefaultValue(ConfigurationStrings.DecompressionEnabled, source.DecompressionEnabled);
247             SetPropertyValueIfNotDefaultValue(ConfigurationStrings.BypassProxyOnLocal, source.BypassProxyOnLocal);
248             SetPropertyValueIfNotDefaultValue(ConfigurationStrings.KeepAliveEnabled, source.KeepAliveEnabled);
249             SetPropertyValueIfNotDefaultValue(ConfigurationStrings.HostNameComparisonMode, source.HostNameComparisonMode);
250             SetPropertyValueIfNotDefaultValue(ConfigurationStrings.MaxBufferSize, source.MaxBufferSize);
251             SetPropertyValueIfNotDefaultValue(ConfigurationStrings.MaxPendingAccepts, source.MaxPendingAccepts);
252             SetPropertyValueIfNotDefaultValue(ConfigurationStrings.ProxyAddress, source.ProxyAddress);
253             SetPropertyValueIfNotDefaultValue(ConfigurationStrings.ProxyAuthenticationScheme, source.ProxyAuthenticationScheme);
254             SetPropertyValueIfNotDefaultValue(ConfigurationStrings.Realm, source.Realm);
255             SetPropertyValueIfNotDefaultValue(ConfigurationStrings.RequestInitializationTimeout, source.RequestInitializationTimeout);
256             SetPropertyValueIfNotDefaultValue(ConfigurationStrings.TransferMode, source.TransferMode);
257             SetPropertyValueIfNotDefaultValue(ConfigurationStrings.UnsafeConnectionNtlmAuthentication, source.UnsafeConnectionNtlmAuthentication);
258             SetPropertyValueIfNotDefaultValue(ConfigurationStrings.UseDefaultWebProxy, source.UseDefaultWebProxy);
259             this.WebSocketSettings.InitializeFrom(source.WebSocketSettings);
260             if (source.MessageHandlerFactory != null)
261             {
262                 this.MessageHandlerFactory = source.MessageHandlerFactory.GenerateConfigurationElement();
263             }
264
265             ChannelBindingUtility.InitializeFrom(source.ExtendedProtectionPolicy, this.ExtendedProtectionPolicy);
266         }
267     }
268 }