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.
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 Encoding text_encoding = new UTF8Encoding ();
61 TransferMode transfer_mode
62 = TransferMode.Buffered;
63 bool use_default_web_proxy = true;
64 BasicHttpSecurity security;
66 public BasicHttpBinding ()
67 : this (BasicHttpSecurityMode.None)
72 public BasicHttpBinding (string configurationName)
75 BindingsSection bindingsSection = ConfigUtil.BindingsSection;
76 BasicHttpBindingElement el =
77 bindingsSection.BasicHttpBinding.Bindings [configurationName];
79 el.ApplyConfiguration (this);
83 public BasicHttpBinding (
84 BasicHttpSecurityMode securityMode)
86 security = new BasicHttpSecurity (securityMode);
89 public bool AllowCookies {
90 get { return allow_cookies; }
91 set { allow_cookies = value; }
94 public bool BypassProxyOnLocal {
95 get { return bypass_proxy_on_local; }
96 set { bypass_proxy_on_local = value; }
100 public bool EnableHttpCookieContainer { get; set; }
103 public HostNameComparisonMode HostNameComparisonMode {
104 get { return host_name_comparison_mode; }
105 set { host_name_comparison_mode = value; }
108 public long MaxBufferPoolSize {
109 get { return max_buffer_pool_size; }
112 throw new ArgumentOutOfRangeException ();
113 max_buffer_pool_size = value;
117 public int MaxBufferSize {
118 get { return max_buffer_size; }
121 throw new ArgumentOutOfRangeException ();
122 max_buffer_size = value;
126 public long MaxReceivedMessageSize {
127 get { return max_recv_message_size; }
130 throw new ArgumentOutOfRangeException ();
131 max_recv_message_size = value;
135 public WSMessageEncoding MessageEncoding {
136 get { return message_encoding; }
137 set { message_encoding = value; }
140 public Uri ProxyAddress {
141 get { return proxy_address; }
142 set { proxy_address = value; }
145 public XmlDictionaryReaderQuotas ReaderQuotas {
146 get { return reader_quotas; }
147 set { reader_quotas = value; }
150 public override string Scheme {
152 switch (Security.Mode) {
153 case BasicHttpSecurityMode.Transport:
154 case BasicHttpSecurityMode.TransportWithMessageCredential:
155 return Uri.UriSchemeHttps;
157 return Uri.UriSchemeHttp;
162 public BasicHttpSecurity Security {
163 get { return security; }
166 public EnvelopeVersion EnvelopeVersion {
167 get { return env_version; }
170 public Encoding TextEncoding {
171 get { return text_encoding; }
172 set { text_encoding = value; }
175 public TransferMode TransferMode {
176 get { return transfer_mode; }
177 set { transfer_mode = value; }
180 public bool UseDefaultWebProxy {
181 get { return use_default_web_proxy; }
182 set { use_default_web_proxy = value; }
185 public override BindingElementCollection
186 CreateBindingElements ()
188 var list = new List<BindingElement> ();
190 var security = CreateSecurityBindingElement ();
191 if (security != null)
195 if (EnableHttpCookieContainer)
196 list.Add (new HttpCookieContainerBindingElement ());
199 list.Add (BuildMessageEncodingBindingElement ());
200 list.Add (GetTransport ());
202 return new BindingElementCollection (list.ToArray ());
205 SecurityBindingElement CreateSecurityBindingElement ()
207 SecurityBindingElement element;
208 switch (Security.Mode) {
210 case BasicHttpSecurityMode.Message:
211 if (Security.Message.ClientCredentialType != BasicHttpMessageCredentialType.Certificate)
212 throw new InvalidOperationException ("When Message security is enabled in a BasicHttpBinding, the message security credential type must be BasicHttpMessageCredentialType.Certificate.");
213 element = SecurityBindingElement.CreateMutualCertificateBindingElement (
214 MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10);
217 case BasicHttpSecurityMode.TransportWithMessageCredential:
218 if (Security.Message.ClientCredentialType != BasicHttpMessageCredentialType.Certificate)
219 // FIXME: pass proper security token parameters.
220 element = SecurityBindingElement.CreateCertificateOverTransportBindingElement ();
222 element = new AsymmetricSecurityBindingElement ();
230 element.SetKeyDerivation (false);
231 element.SecurityHeaderLayout = SecurityHeaderLayout.Lax;
236 MessageEncodingBindingElement BuildMessageEncodingBindingElement ()
238 if (MessageEncoding == WSMessageEncoding.Text) {
239 TextMessageEncodingBindingElement tm = new TextMessageEncodingBindingElement (
240 MessageVersion.CreateVersion (EnvelopeVersion, AddressingVersion.None), TextEncoding);
242 ReaderQuotas.CopyTo (tm.ReaderQuotas);
248 throw new SystemException ("INTERNAL ERROR: should not happen");
250 return new MtomMessageEncodingBindingElement (
251 MessageVersion.CreateVersion (EnvelopeVersion, AddressingVersion.None), TextEncoding);
255 TransportBindingElement GetTransport ()
257 HttpTransportBindingElement h;
258 switch (Security.Mode) {
259 case BasicHttpSecurityMode.Transport:
260 case BasicHttpSecurityMode.TransportWithMessageCredential:
261 h = new HttpsTransportBindingElement ();
264 h = new HttpTransportBindingElement ();
268 h.AllowCookies = AllowCookies;
269 h.BypassProxyOnLocal = BypassProxyOnLocal;
270 h.HostNameComparisonMode = HostNameComparisonMode;
271 h.MaxBufferPoolSize = MaxBufferPoolSize;
272 h.MaxBufferSize = MaxBufferSize;
273 h.MaxReceivedMessageSize = MaxReceivedMessageSize;
274 h.ProxyAddress = ProxyAddress;
275 h.UseDefaultWebProxy = UseDefaultWebProxy;
276 h.TransferMode = TransferMode;
278 h.ExtendedProtectionPolicy = Security.Transport.ExtendedProtectionPolicy;
281 #if !NET_2_1 || MOBILE
282 switch (Security.Transport.ClientCredentialType) {
283 case HttpClientCredentialType.Basic:
284 h.AuthenticationScheme = AuthenticationSchemes.Basic;
286 case HttpClientCredentialType.Ntlm:
287 h.AuthenticationScheme = AuthenticationSchemes.Ntlm;
289 case HttpClientCredentialType.Windows:
290 h.AuthenticationScheme = AuthenticationSchemes.Negotiate;
292 case HttpClientCredentialType.Digest:
293 h.AuthenticationScheme = AuthenticationSchemes.Digest;
295 case HttpClientCredentialType.Certificate:
296 switch (Security.Mode) {
297 case BasicHttpSecurityMode.Transport:
298 (h as HttpsTransportBindingElement).RequireClientCertificate = true;
300 case BasicHttpSecurityMode.TransportCredentialOnly:
301 throw new InvalidOperationException ("Certificate-based client authentication is not supported by 'TransportCredentialOnly' mode.");
310 // explicit interface implementations
312 bool IBindingRuntimePreferences.ReceiveSynchronously {
313 get { return false; }