Merge pull request #475 from pruiz/xamarin-bug-7408
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel / BasicHttpBinding.cs
1 //
2 // BasicHttpBinding.cs
3 //
4 // See BasicHttpBinding_4_5.cs and HttpBindingBase.cs for the .NET 4.5
5 // version of this class.
6 //
7 // Author:
8 //      Atsushi Enomoto <atsushi@ximian.com>
9 //
10 // Copyright (C) 2005-2006 Novell, Inc.  http://www.novell.com
11 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32 #if !NET_4_5 && !MOBILE
33 using System;
34 using System.Collections.Generic;
35 using System.Net;
36 using System.Net.Security;
37 using System.ServiceModel.Channels;
38 using System.ServiceModel.Description;
39 using System.Text;
40 using System.Xml;
41 using System.ServiceModel.Configuration;
42
43 namespace System.ServiceModel
44 {
45         public class BasicHttpBinding : Binding,
46                 IBindingRuntimePreferences
47         {
48                 bool allow_cookies, bypass_proxy_on_local;
49                 HostNameComparisonMode host_name_comparison_mode
50                         = HostNameComparisonMode.StrongWildcard;
51                 long max_buffer_pool_size = 0x80000;
52                 int max_buffer_size = 0x10000;
53                 long max_recv_message_size = 0x10000;
54                 WSMessageEncoding message_encoding
55                         = WSMessageEncoding.Text;
56                 Uri proxy_address;
57                 XmlDictionaryReaderQuotas reader_quotas
58                         = new XmlDictionaryReaderQuotas ();
59                 EnvelopeVersion env_version = EnvelopeVersion.Soap11;
60                 static readonly Encoding default_text_encoding = new UTF8Encoding ();
61                 Encoding text_encoding = default_text_encoding;
62                 TransferMode transfer_mode
63                          = TransferMode.Buffered;
64                 bool use_default_web_proxy = true;
65                 BasicHttpSecurity security;
66
67                 public BasicHttpBinding ()
68                         : this (BasicHttpSecurityMode.None)
69                 {
70                 }
71
72 #if !NET_2_1
73                 public BasicHttpBinding (string configurationName)
74                         : this ()
75                 {
76                         BindingsSection bindingsSection = ConfigUtil.BindingsSection;
77                         BasicHttpBindingElement el = 
78                                 bindingsSection.BasicHttpBinding.Bindings [configurationName];
79
80                         el.ApplyConfiguration (this);
81                 }
82 #endif
83
84                 public BasicHttpBinding (
85                         BasicHttpSecurityMode securityMode)
86                 {
87                         security = new BasicHttpSecurity (securityMode);
88                 }
89
90                 public bool AllowCookies {
91                         get { return allow_cookies; }
92                         set { allow_cookies = value; }
93                 }
94
95                 public bool BypassProxyOnLocal {
96                         get { return bypass_proxy_on_local; }
97                         set { bypass_proxy_on_local = value; }
98                 }
99
100 #if NET_2_1
101                 public bool EnableHttpCookieContainer { get; set; }
102 #endif
103
104                 public HostNameComparisonMode HostNameComparisonMode {
105                         get { return host_name_comparison_mode; }
106                         set { host_name_comparison_mode = value; }
107                 }
108
109                 public long MaxBufferPoolSize {
110                         get { return max_buffer_pool_size; }
111                         set {
112                                 if (value <= 0)
113                                         throw new ArgumentOutOfRangeException ();
114                                 max_buffer_pool_size = value;
115                         }
116                 }
117
118                 public int MaxBufferSize {
119                         get { return max_buffer_size; }
120                         set {
121                                 if (value <= 0)
122                                         throw new ArgumentOutOfRangeException ();
123                                 max_buffer_size = value;
124                         }
125                 }
126
127                 public long MaxReceivedMessageSize {
128                         get { return max_recv_message_size; }
129                         set {
130                                 if (value <= 0)
131                                         throw new ArgumentOutOfRangeException ();
132                                 max_recv_message_size = value;
133                         }
134                 }
135
136                 public WSMessageEncoding MessageEncoding {
137                         get { return message_encoding; }
138                         set { message_encoding = value; }
139                 }
140
141                 public Uri ProxyAddress {
142                         get { return proxy_address; }
143                         set { proxy_address = value; }
144                 }
145
146                 public XmlDictionaryReaderQuotas ReaderQuotas {
147                         get { return reader_quotas; }
148                         set { reader_quotas = value; }
149                 }
150
151                 public override string Scheme {
152                         get {
153                                 switch (Security.Mode) {
154                                 case BasicHttpSecurityMode.Transport:
155                                 case BasicHttpSecurityMode.TransportWithMessageCredential:
156                                         return Uri.UriSchemeHttps;
157                                 default:
158                                         return Uri.UriSchemeHttp;
159                                 }
160                         }
161                 }
162
163                 public BasicHttpSecurity Security {
164                         get { return security; }
165                 }
166
167                 public EnvelopeVersion EnvelopeVersion {
168                         get { return env_version; }
169                 }
170
171                 internal static Encoding DefaultTextEncoding {
172                         get { return default_text_encoding; }
173                 }
174                 
175                 public Encoding TextEncoding {
176                         get { return text_encoding; }
177                         set { text_encoding = value; }
178                 }
179
180                 public TransferMode TransferMode {
181                         get { return transfer_mode; }
182                         set { transfer_mode = value; }
183                 }
184
185                 public bool UseDefaultWebProxy {
186                         get { return use_default_web_proxy; }
187                         set { use_default_web_proxy = value; }
188                 }
189
190                 public override BindingElementCollection
191                         CreateBindingElements ()
192                 {
193                         var list = new List<BindingElement> ();
194                         
195                         var security = CreateSecurityBindingElement ();
196                         if (security != null)
197                                 list.Add (security);
198
199 #if NET_2_1
200                         if (EnableHttpCookieContainer)
201                                 list.Add (new HttpCookieContainerBindingElement ());
202 #endif
203
204                         list.Add (BuildMessageEncodingBindingElement ());
205                         list.Add (GetTransport ());
206
207                         return new BindingElementCollection (list.ToArray ());
208                 }
209                 
210                 SecurityBindingElement CreateSecurityBindingElement () 
211                 {
212             SecurityBindingElement element;
213                         switch (Security.Mode) {
214 #if !NET_2_1
215                         case BasicHttpSecurityMode.Message:
216                                 if (Security.Message.ClientCredentialType != BasicHttpMessageCredentialType.Certificate)
217                                         throw new InvalidOperationException ("When Message security is enabled in a BasicHttpBinding, the message security credential type must be BasicHttpMessageCredentialType.Certificate.");
218                                 element = SecurityBindingElement.CreateMutualCertificateBindingElement (
219                                     MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10);
220                                 break;
221
222                         case BasicHttpSecurityMode.TransportWithMessageCredential:
223                                 if (Security.Message.ClientCredentialType != BasicHttpMessageCredentialType.Certificate)
224                                         // FIXME: pass proper security token parameters.
225                                         element = SecurityBindingElement.CreateCertificateOverTransportBindingElement ();
226                                 else
227                                         element = new AsymmetricSecurityBindingElement ();
228                                 break;
229 #endif
230                         default: 
231                                 return null;
232                         }
233
234 #if !NET_2_1
235                         element.SetKeyDerivation (false);
236                         element.SecurityHeaderLayout = SecurityHeaderLayout.Lax;
237 #endif
238                         return element;
239                 }
240
241                 MessageEncodingBindingElement BuildMessageEncodingBindingElement ()
242                 {
243                         if (MessageEncoding == WSMessageEncoding.Text) {
244                                 TextMessageEncodingBindingElement tm = new TextMessageEncodingBindingElement (
245                                         MessageVersion.CreateVersion (EnvelopeVersion, AddressingVersion.None), TextEncoding);
246 #if !NET_2_1
247                                 ReaderQuotas.CopyTo (tm.ReaderQuotas);
248 #endif
249                                 return tm;
250                         }
251                         else
252 #if NET_2_1
253                                 throw new SystemException ("INTERNAL ERROR: should not happen");
254 #else
255                                 return new MtomMessageEncodingBindingElement (
256                                         MessageVersion.CreateVersion (EnvelopeVersion, AddressingVersion.None), TextEncoding);
257 #endif
258                 }
259
260                 TransportBindingElement GetTransport ()
261                 {
262                         HttpTransportBindingElement h;
263                         switch (Security.Mode) {
264                         case BasicHttpSecurityMode.Transport:
265                         case BasicHttpSecurityMode.TransportWithMessageCredential:
266                                 h = new HttpsTransportBindingElement ();
267                                 break;
268                         default:
269                                 h = new HttpTransportBindingElement ();
270                                 break;
271                         }
272
273                         h.AllowCookies = AllowCookies;
274                         h.BypassProxyOnLocal = BypassProxyOnLocal;
275                         h.HostNameComparisonMode = HostNameComparisonMode;
276                         h.MaxBufferPoolSize = MaxBufferPoolSize;
277                         h.MaxBufferSize = MaxBufferSize;
278                         h.MaxReceivedMessageSize = MaxReceivedMessageSize;
279                         h.ProxyAddress = ProxyAddress;
280                         h.UseDefaultWebProxy = UseDefaultWebProxy;
281                         h.TransferMode = TransferMode;
282 #if NET_4_0
283                         h.ExtendedProtectionPolicy = Security.Transport.ExtendedProtectionPolicy;
284 #endif
285
286 #if !NET_2_1 || MOBILE
287                         switch (Security.Transport.ClientCredentialType) {
288                         case HttpClientCredentialType.Basic:
289                                 h.AuthenticationScheme = AuthenticationSchemes.Basic;
290                                 break;
291                         case HttpClientCredentialType.Ntlm:
292                                 h.AuthenticationScheme = AuthenticationSchemes.Ntlm;
293                                 break;
294                         case HttpClientCredentialType.Windows:
295                                 h.AuthenticationScheme = AuthenticationSchemes.Negotiate;
296                                 break;
297                         case HttpClientCredentialType.Digest:
298                                 h.AuthenticationScheme = AuthenticationSchemes.Digest;
299                                 break;
300                         case HttpClientCredentialType.Certificate:
301                                 switch (Security.Mode) {
302                                 case BasicHttpSecurityMode.Transport:
303                                         (h as HttpsTransportBindingElement).RequireClientCertificate = true;
304                                         break;
305                                 case BasicHttpSecurityMode.TransportCredentialOnly:
306                                         throw new InvalidOperationException ("Certificate-based client authentication is not supported by 'TransportCredentialOnly' mode.");
307                                 }
308                                 break;
309                         }
310 #endif
311
312                         return h;
313                 }
314
315                 // explicit interface implementations
316
317                 bool IBindingRuntimePreferences.ReceiveSynchronously {
318                         get { return false; }
319                 }
320         }
321 }
322 #endif