4 // See BasicHttpBinding_4_5.cs and HttpBindingBase.cs for the .NET 4.5
5 // version of this class.
8 // Atsushi Enomoto <atsushi@ximian.com>
10 // Copyright (C) 2005-2006 Novell, Inc. http://www.novell.com
11 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
32 #if !NET_4_5 && !MOBILE
34 using System.Collections.Generic;
36 using System.Net.Security;
37 using System.ServiceModel.Channels;
38 using System.ServiceModel.Description;
41 using System.ServiceModel.Configuration;
43 namespace System.ServiceModel
45 public class BasicHttpBinding : Binding,
46 IBindingRuntimePreferences
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;
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;
67 public BasicHttpBinding ()
68 : this (BasicHttpSecurityMode.None)
73 public BasicHttpBinding (string configurationName)
76 BindingsSection bindingsSection = ConfigUtil.BindingsSection;
77 BasicHttpBindingElement el =
78 bindingsSection.BasicHttpBinding.Bindings [configurationName];
80 el.ApplyConfiguration (this);
84 public BasicHttpBinding (
85 BasicHttpSecurityMode securityMode)
87 security = new BasicHttpSecurity (securityMode);
90 public bool AllowCookies {
91 get { return allow_cookies; }
92 set { allow_cookies = value; }
95 public bool BypassProxyOnLocal {
96 get { return bypass_proxy_on_local; }
97 set { bypass_proxy_on_local = value; }
101 public bool EnableHttpCookieContainer {
105 [Obsolete ("Use AllowCookies.")]
106 public bool EnableHttpCookieContainer {
107 get { return AllowCookies; }
108 set { AllowCookies = value; }
112 public HostNameComparisonMode HostNameComparisonMode {
113 get { return host_name_comparison_mode; }
114 set { host_name_comparison_mode = value; }
117 public long MaxBufferPoolSize {
118 get { return max_buffer_pool_size; }
121 throw new ArgumentOutOfRangeException ();
122 max_buffer_pool_size = value;
126 public int MaxBufferSize {
127 get { return max_buffer_size; }
130 throw new ArgumentOutOfRangeException ();
131 max_buffer_size = value;
135 public long MaxReceivedMessageSize {
136 get { return max_recv_message_size; }
139 throw new ArgumentOutOfRangeException ();
140 max_recv_message_size = value;
144 public WSMessageEncoding MessageEncoding {
145 get { return message_encoding; }
146 set { message_encoding = value; }
149 public Uri ProxyAddress {
150 get { return proxy_address; }
151 set { proxy_address = value; }
154 public XmlDictionaryReaderQuotas ReaderQuotas {
155 get { return reader_quotas; }
156 set { reader_quotas = value; }
159 public override string Scheme {
161 switch (Security.Mode) {
162 case BasicHttpSecurityMode.Transport:
163 case BasicHttpSecurityMode.TransportWithMessageCredential:
164 return Uri.UriSchemeHttps;
166 return Uri.UriSchemeHttp;
171 public BasicHttpSecurity Security {
172 get { return security; }
175 public EnvelopeVersion EnvelopeVersion {
176 get { return env_version; }
179 internal static Encoding DefaultTextEncoding {
180 get { return default_text_encoding; }
183 public Encoding TextEncoding {
184 get { return text_encoding; }
185 set { text_encoding = value; }
188 public TransferMode TransferMode {
189 get { return transfer_mode; }
190 set { transfer_mode = value; }
193 public bool UseDefaultWebProxy {
194 get { return use_default_web_proxy; }
195 set { use_default_web_proxy = value; }
198 public override BindingElementCollection
199 CreateBindingElements ()
201 var list = new List<BindingElement> ();
203 var security = CreateSecurityBindingElement ();
204 if (security != null)
208 if (EnableHttpCookieContainer)
209 list.Add (new HttpCookieContainerBindingElement ());
212 list.Add (BuildMessageEncodingBindingElement ());
213 list.Add (GetTransport ());
215 return new BindingElementCollection (list.ToArray ());
218 SecurityBindingElement CreateSecurityBindingElement ()
220 SecurityBindingElement element;
221 switch (Security.Mode) {
223 case BasicHttpSecurityMode.Message:
224 if (Security.Message.ClientCredentialType != BasicHttpMessageCredentialType.Certificate)
225 throw new InvalidOperationException ("When Message security is enabled in a BasicHttpBinding, the message security credential type must be BasicHttpMessageCredentialType.Certificate.");
226 element = SecurityBindingElement.CreateMutualCertificateBindingElement (
227 MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10);
230 case BasicHttpSecurityMode.TransportWithMessageCredential:
231 if (Security.Message.ClientCredentialType != BasicHttpMessageCredentialType.Certificate)
232 // FIXME: pass proper security token parameters.
233 element = SecurityBindingElement.CreateCertificateOverTransportBindingElement ();
235 element = new AsymmetricSecurityBindingElement ();
243 element.SetKeyDerivation (false);
244 element.SecurityHeaderLayout = SecurityHeaderLayout.Lax;
249 MessageEncodingBindingElement BuildMessageEncodingBindingElement ()
251 if (MessageEncoding == WSMessageEncoding.Text) {
252 TextMessageEncodingBindingElement tm = new TextMessageEncodingBindingElement (
253 MessageVersion.CreateVersion (EnvelopeVersion, AddressingVersion.None), TextEncoding);
255 ReaderQuotas.CopyTo (tm.ReaderQuotas);
261 throw new SystemException ("INTERNAL ERROR: should not happen");
263 return new MtomMessageEncodingBindingElement (
264 MessageVersion.CreateVersion (EnvelopeVersion, AddressingVersion.None), TextEncoding);
268 TransportBindingElement GetTransport ()
270 HttpTransportBindingElement h;
271 switch (Security.Mode) {
272 case BasicHttpSecurityMode.Transport:
273 case BasicHttpSecurityMode.TransportWithMessageCredential:
274 h = new HttpsTransportBindingElement ();
277 h = new HttpTransportBindingElement ();
281 h.AllowCookies = AllowCookies;
282 h.BypassProxyOnLocal = BypassProxyOnLocal;
283 h.HostNameComparisonMode = HostNameComparisonMode;
284 h.MaxBufferPoolSize = MaxBufferPoolSize;
285 h.MaxBufferSize = MaxBufferSize;
286 h.MaxReceivedMessageSize = MaxReceivedMessageSize;
287 h.ProxyAddress = ProxyAddress;
288 h.UseDefaultWebProxy = UseDefaultWebProxy;
289 h.TransferMode = TransferMode;
291 h.ExtendedProtectionPolicy = Security.Transport.ExtendedProtectionPolicy;
294 #if !NET_2_1 || MOBILE
295 switch (Security.Transport.ClientCredentialType) {
296 case HttpClientCredentialType.Basic:
297 h.AuthenticationScheme = AuthenticationSchemes.Basic;
299 case HttpClientCredentialType.Ntlm:
300 h.AuthenticationScheme = AuthenticationSchemes.Ntlm;
302 case HttpClientCredentialType.Windows:
303 h.AuthenticationScheme = AuthenticationSchemes.Negotiate;
305 case HttpClientCredentialType.Digest:
306 h.AuthenticationScheme = AuthenticationSchemes.Digest;
308 case HttpClientCredentialType.Certificate:
309 switch (Security.Mode) {
310 case BasicHttpSecurityMode.Transport:
311 (h as HttpsTransportBindingElement).RequireClientCertificate = true;
313 case BasicHttpSecurityMode.TransportCredentialOnly:
314 throw new InvalidOperationException ("Certificate-based client authentication is not supported by 'TransportCredentialOnly' mode.");
323 // explicit interface implementations
325 bool IBindingRuntimePreferences.ReceiveSynchronously {
326 get { return false; }