Merge pull request #498 from Unroll-Me/master
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Configuration / BasicHttpBindingElement.cs
1 //
2 // BasicHttpBindingElement.cs
3 //
4 // See BasicHttpBindingElement_4_5.cs and HttpBindingBaseElement.cs
5 // for the .NET 4.5 version of this class, where most of the code has
6 // been moved into the new abstract HttpBindingBaseElement class.
7 //
8 //
9 // Author:
10 //      Atsushi Enomoto <atsushi@ximian.com>
11 //
12 // Copyright (C) 2006 Novell, Inc.  http://www.novell.com
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 #if !NET_4_5
35 using System;
36 using System.Collections;
37 using System.Collections.Generic;
38 using System.Collections.ObjectModel;
39 using System.ComponentModel;
40 using System.Configuration;
41 using System.Net;
42 using System.Net.Security;
43 using System.Reflection;
44 using System.Security.Cryptography.X509Certificates;
45 using System.Security.Principal;
46 using System.IdentityModel.Claims;
47 using System.IdentityModel.Policy;
48 using System.IdentityModel.Tokens;
49 using System.ServiceModel;
50 using System.ServiceModel.Channels;
51 using System.ServiceModel.Description;
52 using System.ServiceModel.Diagnostics;
53 using System.ServiceModel.Dispatcher;
54 using System.ServiceModel.MsmqIntegration;
55 using System.ServiceModel.PeerResolvers;
56 using System.ServiceModel.Security;
57 using System.Runtime.Serialization;
58 using System.Text;
59 using System.Xml;
60
61 namespace System.ServiceModel.Configuration
62 {
63         public class BasicHttpBindingElement
64                  : StandardBindingElement,  IBindingConfigurationElement
65         {
66                 ConfigurationPropertyCollection _properties;
67
68                 public BasicHttpBindingElement ()
69                 {
70                 }
71
72                 public BasicHttpBindingElement (string name) : base (name) { }
73
74                 // Properties
75
76                 [ConfigurationProperty ("allowCookies",
77                         DefaultValue = false,
78                          Options = ConfigurationPropertyOptions.None)]
79                 public bool AllowCookies {
80                         get { return (bool) this ["allowCookies"]; }
81                         set { this ["allowCookies"] = value; }
82                 }
83
84                 protected override Type BindingElementType {
85                         get { return typeof (BasicHttpBinding); }
86                 }
87
88                 [ConfigurationProperty ("bypassProxyOnLocal",
89                         DefaultValue = false,
90                          Options = ConfigurationPropertyOptions.None)]
91                 public bool BypassProxyOnLocal {
92                         get { return (bool) this ["bypassProxyOnLocal"]; }
93                         set { this ["bypassProxyOnLocal"] = value; }
94                 }
95
96                 [ConfigurationProperty ("hostNameComparisonMode",
97                          DefaultValue = "StrongWildcard",
98                          Options = ConfigurationPropertyOptions.None)]
99                 public HostNameComparisonMode HostNameComparisonMode {
100                         get { return (HostNameComparisonMode) this ["hostNameComparisonMode"]; }
101                         set { this ["hostNameComparisonMode"] = value; }
102                 }
103
104                 [LongValidator ( MinValue = 0,
105                          MaxValue = 9223372036854775807,
106                         ExcludeRange = false)]
107                 [ConfigurationProperty ("maxBufferPoolSize",
108                          DefaultValue = "524288",
109                          Options = ConfigurationPropertyOptions.None)]
110                 public long MaxBufferPoolSize {
111                         get { return (long) this ["maxBufferPoolSize"]; }
112                         set { this ["maxBufferPoolSize"] = value; }
113                 }
114
115                 [IntegerValidator ( MinValue = 1,
116                         MaxValue = int.MaxValue,
117                         ExcludeRange = false)]
118                 [ConfigurationProperty ("maxBufferSize",
119                          DefaultValue = "65536",
120                          Options = ConfigurationPropertyOptions.None)]
121                 public int MaxBufferSize {
122                         get { return (int) this ["maxBufferSize"]; }
123                         set { this ["maxBufferSize"] = value; }
124                 }
125
126                 [LongValidator ( MinValue = 1,
127                          MaxValue = 9223372036854775807,
128                         ExcludeRange = false)]
129                 [ConfigurationProperty ("maxReceivedMessageSize",
130                          DefaultValue = "65536",
131                          Options = ConfigurationPropertyOptions.None)]
132                 public long MaxReceivedMessageSize {
133                         get { return (long) this ["maxReceivedMessageSize"]; }
134                         set { this ["maxReceivedMessageSize"] = value; }
135                 }
136
137                 [ConfigurationProperty ("messageEncoding",
138                          DefaultValue = "Text",
139                          Options = ConfigurationPropertyOptions.None)]
140                 public WSMessageEncoding MessageEncoding {
141                         get { return (WSMessageEncoding) this ["messageEncoding"]; }
142                         set { this ["messageEncoding"] = value; }
143                 }
144
145                 protected override ConfigurationPropertyCollection Properties {
146                         get {
147                                 if (_properties == null) {
148                                         _properties = base.Properties;
149                                         _properties.Add (new ConfigurationProperty ("allowCookies", typeof (bool), "false", null, null, ConfigurationPropertyOptions.None));
150                                         _properties.Add (new ConfigurationProperty ("bypassProxyOnLocal", typeof (bool), "false", null, null, ConfigurationPropertyOptions.None));
151                                         _properties.Add (new ConfigurationProperty ("hostNameComparisonMode", typeof (HostNameComparisonMode), "StrongWildcard", null, null, ConfigurationPropertyOptions.None));
152                                         _properties.Add (new ConfigurationProperty ("maxBufferPoolSize", typeof (long), "524288", null, new LongValidator (0, 9223372036854775807, false), ConfigurationPropertyOptions.None));
153                                         _properties.Add (new ConfigurationProperty ("maxBufferSize", typeof (int), "65536", null, new IntegerValidator (1, int.MaxValue, false), ConfigurationPropertyOptions.None));
154                                         _properties.Add (new ConfigurationProperty ("maxReceivedMessageSize", typeof (long), "65536", null, new LongValidator (1, 9223372036854775807, false), ConfigurationPropertyOptions.None));
155                                         _properties.Add (new ConfigurationProperty ("messageEncoding", typeof (WSMessageEncoding), "Text", null, null, ConfigurationPropertyOptions.None));
156                                         _properties.Add (new ConfigurationProperty ("proxyAddress", typeof (Uri), null, new UriTypeConverter (), null, ConfigurationPropertyOptions.None));
157                                         _properties.Add (new ConfigurationProperty ("readerQuotas", typeof (XmlDictionaryReaderQuotasElement), null, null, null, ConfigurationPropertyOptions.None));
158                                         _properties.Add (new ConfigurationProperty ("security", typeof (BasicHttpSecurityElement), null, null, null, ConfigurationPropertyOptions.None));
159                                         _properties.Add (new ConfigurationProperty ("textEncoding", typeof (Encoding), "utf-8", EncodingConverter.Instance, null, ConfigurationPropertyOptions.None));
160                                         _properties.Add (new ConfigurationProperty ("transferMode", typeof (TransferMode), "Buffered", null, null, ConfigurationPropertyOptions.None));
161                                         _properties.Add (new ConfigurationProperty ("useDefaultWebProxy", typeof (bool), "true", new BooleanConverter (), null, ConfigurationPropertyOptions.None));
162                                 }
163                                 return _properties;
164                         }
165                 }
166
167                 [ConfigurationProperty ("proxyAddress",
168                          DefaultValue = null,
169                          Options = ConfigurationPropertyOptions.None)]
170                 public Uri ProxyAddress {
171                         get { return (Uri) this ["proxyAddress"]; }
172                         set { this ["proxyAddress"] = value; }
173                 }
174
175                 [ConfigurationProperty ("readerQuotas",
176                          Options = ConfigurationPropertyOptions.None)]
177                 public XmlDictionaryReaderQuotasElement ReaderQuotas {
178                         get { return (XmlDictionaryReaderQuotasElement) this ["readerQuotas"]; }
179                 }
180
181                 [ConfigurationProperty ("security",
182                          Options = ConfigurationPropertyOptions.None)]
183                 public BasicHttpSecurityElement Security {
184                         get { return (BasicHttpSecurityElement) this ["security"]; }
185                 }
186
187                 [TypeConverter (typeof (EncodingConverter))]
188                 [ConfigurationProperty ("textEncoding",
189                          DefaultValue = "utf-8",
190                          Options = ConfigurationPropertyOptions.None)]
191                 public Encoding TextEncoding {
192                         get { return (Encoding) this ["textEncoding"]; }
193                         set { this ["textEncoding"] = value; }
194                 }
195
196                 [ConfigurationProperty ("transferMode",
197                          DefaultValue = "Buffered",
198                          Options = ConfigurationPropertyOptions.None)]
199                 public TransferMode TransferMode {
200                         get { return (TransferMode) this ["transferMode"]; }
201                         set { this ["transferMode"] = value; }
202                 }
203
204                 [ConfigurationProperty ("useDefaultWebProxy",
205                         DefaultValue = true,
206                          Options = ConfigurationPropertyOptions.None)]
207                 public bool UseDefaultWebProxy {
208                         get { return (bool) this ["useDefaultWebProxy"]; }
209                         set { this ["useDefaultWebProxy"] = value; }
210                 }
211
212                 protected override void OnApplyConfiguration (Binding binding)
213                 {
214                         BasicHttpBinding basicHttpBinding = (BasicHttpBinding) binding;
215                         
216                         basicHttpBinding.AllowCookies = AllowCookies;
217                         basicHttpBinding.BypassProxyOnLocal = BypassProxyOnLocal;
218                         basicHttpBinding.HostNameComparisonMode = HostNameComparisonMode;
219                         basicHttpBinding.MaxBufferPoolSize = MaxBufferPoolSize;
220                         basicHttpBinding.MaxBufferSize = MaxBufferSize;
221                         basicHttpBinding.MaxReceivedMessageSize = MaxReceivedMessageSize;
222                         basicHttpBinding.MessageEncoding = MessageEncoding;
223                         basicHttpBinding.ProxyAddress = ProxyAddress;
224
225                         ReaderQuotas.ApplyConfiguration (basicHttpBinding.ReaderQuotas);
226
227                         basicHttpBinding.Security.Mode = Security.Mode;
228                         Security.Transport.ApplyConfiguration (basicHttpBinding.Security.Transport);
229                         basicHttpBinding.TextEncoding = TextEncoding;
230                         basicHttpBinding.TransferMode = TransferMode;
231                         basicHttpBinding.UseDefaultWebProxy = UseDefaultWebProxy;
232                 }
233
234                 protected internal override void InitializeFrom (Binding binding)
235                 {
236                         BasicHttpBinding b = (BasicHttpBinding) binding;
237                         
238                         base.InitializeFrom (binding);
239                         AllowCookies = b.AllowCookies;
240                         BypassProxyOnLocal = b.BypassProxyOnLocal;
241                         HostNameComparisonMode = b.HostNameComparisonMode;
242                         MaxBufferPoolSize = b.MaxBufferPoolSize;
243                         MaxBufferSize = b.MaxBufferSize;
244                         MaxReceivedMessageSize = b.MaxReceivedMessageSize;
245                         MessageEncoding = b.MessageEncoding;
246                         ProxyAddress = b.ProxyAddress;
247
248                         ReaderQuotas.ApplyConfiguration (b.ReaderQuotas);
249
250                         Security.Mode = b.Security.Mode;
251                         Security.Transport.ApplyConfiguration (b.Security.Transport);
252                         TextEncoding = b.TextEncoding;
253                         TransferMode = b.TransferMode;
254                         UseDefaultWebProxy = b.UseDefaultWebProxy;
255                 }
256         }
257
258 }
259 #endif