Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / System.ServiceModel.Web / System.ServiceModel / WebHttpBinding.cs
1 //
2 // WebHttpBinding.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.ComponentModel;
30 using System.Linq;
31 using System.ServiceModel.Channels;
32 using System.Text;
33 using System.Xml;
34 #if !NET_2_1
35 using System.Configuration;
36 using System.ServiceModel.Configuration;
37 #endif
38
39 namespace System.ServiceModel
40 {
41         public class WebHttpBinding : Binding, IBindingRuntimePreferences
42         {
43                 public WebHttpBinding ()
44                         : this (String.Empty)
45                 {
46                 }
47
48                 public WebHttpBinding (WebHttpSecurityMode mode)
49                 {
50                         Initialize (mode);
51                 }
52
53                 public WebHttpBinding (string configurationName)
54                 {
55 #if !NET_2_1
56                         BindingsSection bindingsSection = ConfigUtil.BindingsSection;
57                         WebHttpBindingElement el = (WebHttpBindingElement) bindingsSection ["webHttpBinding"].ConfiguredBindings.FirstOrDefault (c => c.Name == configurationName);
58                         if (el != null) {
59                                 Initialize (el.Security.Mode); // to initialize Transport correctly.
60                                 el.ApplyConfiguration (this);
61                         }
62                         else if (!String.IsNullOrEmpty (configurationName))
63                                 throw new ConfigurationException (String.Format ("Specified webHttpBinding configuration '{0}' was not found", configurationName));
64                         else
65                                 Initialize (WebHttpSecurityMode.None);
66 #else
67                         Initialize (WebHttpSecurityMode.None);
68 #endif
69                 }
70
71                 void Initialize (WebHttpSecurityMode mode)
72                 {
73                         security.Mode = mode;
74                         // MSDN says that this security mode can be set only
75                         // at .ctor(), so there is no problem on depending on
76                         // this value here.
77                         t = mode == WebHttpSecurityMode.Transport ? new HttpsTransportBindingElement () : new HttpTransportBindingElement ();
78                         t.ManualAddressing = true;
79                 }
80
81                 WebHttpSecurity security = new WebHttpSecurity ();
82                 HttpTransportBindingElement t;
83                 // This can be changed only using <synchronousReceive> configuration element.
84                 bool receive_synchronously;
85                 WebMessageEncodingBindingElement msgenc = new WebMessageEncodingBindingElement ();
86
87                 public EnvelopeVersion EnvelopeVersion {
88                         get { return EnvelopeVersion.None; }
89                 }
90
91 #if !NET_2_1
92 #if NET_4_0
93                 [DefaultValue (false)]
94 #endif
95                 public bool AllowCookies {
96                         get { return t.AllowCookies; }
97                         set { t.AllowCookies = value; }
98                 }
99
100 #if NET_4_0
101                 [DefaultValue (false)]
102 #endif
103                 public bool BypassProxyOnLocal {
104                         get { return t.BypassProxyOnLocal; }
105                         set { t.BypassProxyOnLocal = value; }
106                 }
107
108 #if NET_4_0
109                 [MonoTODO]
110                 public bool CrossDomainScriptAccessEnabled { get; set; }
111
112                 public WebContentTypeMapper ContentTypeMapper {
113                         get { return msgenc.ContentTypeMapper; }
114                         set { msgenc.ContentTypeMapper = value; }
115                 }
116 #endif
117
118 #if NET_4_0
119                 [DefaultValue (HostNameComparisonMode.StrongWildcard)]
120 #endif
121                 public HostNameComparisonMode HostNameComparisonMode {
122                         get { return t.HostNameComparisonMode; }
123                         set { t.HostNameComparisonMode = value; }
124                 }
125
126 #if NET_4_0
127                 [DefaultValue (0x10000)]
128 #endif
129                 public long MaxBufferPoolSize {
130                         get { return t.MaxBufferPoolSize; }
131                         set { t.MaxBufferPoolSize = value; }
132                 }
133
134 #if NET_4_0
135                 [DefaultValue (TransferMode.Buffered)]
136 #endif
137                 public TransferMode TransferMode {
138                         get { return t.TransferMode; }
139                         set { t.TransferMode = value; }
140                 }
141
142 #if NET_4_0
143                 [DefaultValue (true)]
144 #endif
145                 public bool UseDefaultWebProxy {
146                         get { return t.UseDefaultWebProxy; }
147                         set { t.UseDefaultWebProxy = value; }
148                 }
149
150 #if NET_4_0
151                 [DefaultValue (null)]
152 #endif
153                 public Uri ProxyAddress {
154                         get { return t.ProxyAddress; }
155                         set { t.ProxyAddress = value; }
156                 }
157 #endif
158
159 #if NET_4_0
160                 [DefaultValue (0x80000)]
161 #endif
162                 public int MaxBufferSize {
163                         get { return t.MaxBufferSize; }
164                         set { t.MaxBufferSize = value; }
165                 }
166
167 #if NET_4_0
168                 [DefaultValue (0x10000)]
169 #endif
170                 public long MaxReceivedMessageSize {
171                         get { return t.MaxReceivedMessageSize; }
172                         set { t.MaxReceivedMessageSize = value; }
173                 }
174
175                 public XmlDictionaryReaderQuotas ReaderQuotas {
176                         get { return msgenc.ReaderQuotas; }
177                         set { msgenc.ReaderQuotas = value; }
178                 }
179
180                 public override string Scheme {
181                         get { return Security.Mode != WebHttpSecurityMode.None ? Uri.UriSchemeHttps : Uri.UriSchemeHttp; }
182                 }
183
184                 public WebHttpSecurity Security {
185                         get { return security; }
186 #if NET_4_0
187                         set {
188                                 if (value == null)
189                                         throw new ArgumentNullException ("value");
190                                 security = value;
191                         }
192 #endif
193                 }
194
195                 public Encoding WriteEncoding {
196                         get { return msgenc.WriteEncoding; }
197                         set {
198                                 if (value == null)
199                                         throw new ArgumentNullException ("value");
200                                 msgenc.WriteEncoding = value; 
201                         }
202                 }
203
204                 public override BindingElementCollection CreateBindingElements ()
205                 {
206                         return new BindingElementCollection (new BindingElement [] { msgenc, t.Clone () });
207                 }
208
209                 bool IBindingRuntimePreferences.ReceiveSynchronously {
210                         get { return receive_synchronously; }
211                 }
212
213 #if NET_4_0
214                 [EditorBrowsable (EditorBrowsableState.Advanced)]
215                 public bool ShouldSerializeReaderQuotas ()
216                 {
217                         return false;
218                 }
219                 
220                 [EditorBrowsable (EditorBrowsableState.Advanced)]
221                 public bool ShouldSerializeSecurity ()
222                 {
223                         return false;
224                 }
225                 
226                 [EditorBrowsable (EditorBrowsableState.Advanced)]
227                 public bool ShouldSerializeWriteEncoding ()
228                 {
229                         return false;
230                 }
231 #endif
232         }
233 }