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 { get; set; }
104 public HostNameComparisonMode HostNameComparisonMode {
105 get { return host_name_comparison_mode; }
106 set { host_name_comparison_mode = value; }
109 public long MaxBufferPoolSize {
110 get { return max_buffer_pool_size; }
113 throw new ArgumentOutOfRangeException ();
114 max_buffer_pool_size = value;
118 public int MaxBufferSize {
119 get { return max_buffer_size; }
122 throw new ArgumentOutOfRangeException ();
123 max_buffer_size = value;
127 public long MaxReceivedMessageSize {
128 get { return max_recv_message_size; }
131 throw new ArgumentOutOfRangeException ();
132 max_recv_message_size = value;
136 public WSMessageEncoding MessageEncoding {
137 get { return message_encoding; }
138 set { message_encoding = value; }
141 public Uri ProxyAddress {
142 get { return proxy_address; }
143 set { proxy_address = value; }
146 public XmlDictionaryReaderQuotas ReaderQuotas {
147 get { return reader_quotas; }
148 set { reader_quotas = value; }
151 public override string Scheme {
153 switch (Security.Mode) {
154 case BasicHttpSecurityMode.Transport:
155 case BasicHttpSecurityMode.TransportWithMessageCredential:
156 return Uri.UriSchemeHttps;
158 return Uri.UriSchemeHttp;
163 public BasicHttpSecurity Security {
164 get { return security; }
167 public EnvelopeVersion EnvelopeVersion {
168 get { return env_version; }
171 internal static Encoding DefaultTextEncoding {
172 get { return default_text_encoding; }
175 public Encoding TextEncoding {
176 get { return text_encoding; }
177 set { text_encoding = value; }
180 public TransferMode TransferMode {
181 get { return transfer_mode; }
182 set { transfer_mode = value; }
185 public bool UseDefaultWebProxy {
186 get { return use_default_web_proxy; }
187 set { use_default_web_proxy = value; }
190 public override BindingElementCollection
191 CreateBindingElements ()
193 var list = new List<BindingElement> ();
195 var security = CreateSecurityBindingElement ();
196 if (security != null)
200 if (EnableHttpCookieContainer)
201 list.Add (new HttpCookieContainerBindingElement ());
204 list.Add (BuildMessageEncodingBindingElement ());
205 list.Add (GetTransport ());
207 return new BindingElementCollection (list.ToArray ());
210 SecurityBindingElement CreateSecurityBindingElement ()
212 SecurityBindingElement element;
213 switch (Security.Mode) {
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);
222 case BasicHttpSecurityMode.TransportWithMessageCredential:
223 if (Security.Message.ClientCredentialType != BasicHttpMessageCredentialType.Certificate)
224 // FIXME: pass proper security token parameters.
225 element = SecurityBindingElement.CreateCertificateOverTransportBindingElement ();
227 element = new AsymmetricSecurityBindingElement ();
235 element.SetKeyDerivation (false);
236 element.SecurityHeaderLayout = SecurityHeaderLayout.Lax;
241 MessageEncodingBindingElement BuildMessageEncodingBindingElement ()
243 if (MessageEncoding == WSMessageEncoding.Text) {
244 TextMessageEncodingBindingElement tm = new TextMessageEncodingBindingElement (
245 MessageVersion.CreateVersion (EnvelopeVersion, AddressingVersion.None), TextEncoding);
247 ReaderQuotas.CopyTo (tm.ReaderQuotas);
253 throw new SystemException ("INTERNAL ERROR: should not happen");
255 return new MtomMessageEncodingBindingElement (
256 MessageVersion.CreateVersion (EnvelopeVersion, AddressingVersion.None), TextEncoding);
260 TransportBindingElement GetTransport ()
262 HttpTransportBindingElement h;
263 switch (Security.Mode) {
264 case BasicHttpSecurityMode.Transport:
265 case BasicHttpSecurityMode.TransportWithMessageCredential:
266 h = new HttpsTransportBindingElement ();
269 h = new HttpTransportBindingElement ();
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;
283 h.ExtendedProtectionPolicy = Security.Transport.ExtendedProtectionPolicy;
286 #if !NET_2_1 || MOBILE
287 switch (Security.Transport.ClientCredentialType) {
288 case HttpClientCredentialType.Basic:
289 h.AuthenticationScheme = AuthenticationSchemes.Basic;
291 case HttpClientCredentialType.Ntlm:
292 h.AuthenticationScheme = AuthenticationSchemes.Ntlm;
294 case HttpClientCredentialType.Windows:
295 h.AuthenticationScheme = AuthenticationSchemes.Negotiate;
297 case HttpClientCredentialType.Digest:
298 h.AuthenticationScheme = AuthenticationSchemes.Digest;
300 case HttpClientCredentialType.Certificate:
301 switch (Security.Mode) {
302 case BasicHttpSecurityMode.Transport:
303 (h as HttpsTransportBindingElement).RequireClientCertificate = true;
305 case BasicHttpSecurityMode.TransportCredentialOnly:
306 throw new InvalidOperationException ("Certificate-based client authentication is not supported by 'TransportCredentialOnly' mode.");
315 // explicit interface implementations
317 bool IBindingRuntimePreferences.ReceiveSynchronously {
318 get { return false; }