5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005-2006 Novell, Inc. http://www.novell.com
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.Generic;
31 using System.Net.Security;
32 using System.ServiceModel.Channels;
33 using System.ServiceModel.Description;
36 using System.ServiceModel.Configuration;
38 namespace System.ServiceModel
40 public class BasicHttpBinding : Binding,
41 IBindingRuntimePreferences
43 bool allow_cookies, bypass_proxy_on_local;
44 HostNameComparisonMode host_name_comparison_mode
45 = HostNameComparisonMode.StrongWildcard;
46 long max_buffer_pool_size = 0x80000;
47 int max_buffer_size = 0x10000;
48 long max_recv_message_size = 0x10000;
49 WSMessageEncoding message_encoding
50 = WSMessageEncoding.Text;
52 XmlDictionaryReaderQuotas reader_quotas
53 = new XmlDictionaryReaderQuotas ();
54 EnvelopeVersion env_version = EnvelopeVersion.Soap11;
55 Encoding text_encoding = new UTF8Encoding ();
56 TransferMode transfer_mode
57 = TransferMode.Buffered;
58 bool use_default_web_proxy = true;
59 BasicHttpSecurity security;
61 public BasicHttpBinding ()
62 : this (BasicHttpSecurityMode.None)
67 public BasicHttpBinding (string configurationName)
70 BindingsSection bindingsSection = ConfigUtil.BindingsSection;
71 BasicHttpBindingElement el =
72 bindingsSection.BasicHttpBinding.Bindings [configurationName];
74 el.ApplyConfiguration (this);
78 public BasicHttpBinding (
79 BasicHttpSecurityMode securityMode)
81 security = new BasicHttpSecurity (securityMode);
84 public bool AllowCookies {
85 get { return allow_cookies; }
86 set { allow_cookies = value; }
89 public bool BypassProxyOnLocal {
90 get { return bypass_proxy_on_local; }
91 set { bypass_proxy_on_local = value; }
95 public bool EnableHttpCookieContainer { get; set; }
98 public HostNameComparisonMode HostNameComparisonMode {
99 get { return host_name_comparison_mode; }
100 set { host_name_comparison_mode = value; }
103 public long MaxBufferPoolSize {
104 get { return max_buffer_pool_size; }
107 throw new ArgumentOutOfRangeException ();
108 max_buffer_pool_size = value;
112 public int MaxBufferSize {
113 get { return max_buffer_size; }
116 throw new ArgumentOutOfRangeException ();
117 max_buffer_size = value;
121 public long MaxReceivedMessageSize {
122 get { return max_recv_message_size; }
125 throw new ArgumentOutOfRangeException ();
126 max_recv_message_size = value;
130 public WSMessageEncoding MessageEncoding {
131 get { return message_encoding; }
132 set { message_encoding = value; }
135 public Uri ProxyAddress {
136 get { return proxy_address; }
137 set { proxy_address = value; }
140 public XmlDictionaryReaderQuotas ReaderQuotas {
141 get { return reader_quotas; }
142 set { reader_quotas = value; }
145 public override string Scheme {
147 switch (Security.Mode) {
148 case BasicHttpSecurityMode.Transport:
149 case BasicHttpSecurityMode.TransportWithMessageCredential:
150 return Uri.UriSchemeHttps;
152 return Uri.UriSchemeHttp;
157 public BasicHttpSecurity Security {
158 get { return security; }
161 public EnvelopeVersion EnvelopeVersion {
162 get { return env_version; }
165 public Encoding TextEncoding {
166 get { return text_encoding; }
167 set { text_encoding = value; }
170 public TransferMode TransferMode {
171 get { return transfer_mode; }
172 set { transfer_mode = value; }
175 public bool UseDefaultWebProxy {
176 get { return use_default_web_proxy; }
177 set { use_default_web_proxy = value; }
180 public override BindingElementCollection
181 CreateBindingElements ()
183 var list = new List<BindingElement> ();
184 switch (Security.Mode) {
186 case BasicHttpSecurityMode.Message:
187 if (Security.Message.ClientCredentialType != BasicHttpMessageCredentialType.Certificate)
188 throw new InvalidOperationException ("When Message security is enabled in a BasicHttpBinding, the message security credential type must be BasicHttpMessageCredentialType.Certificate.");
189 goto case BasicHttpSecurityMode.TransportWithMessageCredential;
190 case BasicHttpSecurityMode.TransportWithMessageCredential:
191 SecurityBindingElement sec;
192 if (Security.Message.ClientCredentialType != BasicHttpMessageCredentialType.Certificate)
193 // FIXME: pass proper security token parameters.
194 sec = SecurityBindingElement.CreateCertificateOverTransportBindingElement ();
196 sec = new AsymmetricSecurityBindingElement ();
203 if (EnableHttpCookieContainer)
204 list.Add (new HttpCookieContainerBindingElement ());
207 list.Add (BuildMessageEncodingBindingElement ());
208 list.Add (GetTransport ());
210 return new BindingElementCollection (list.ToArray ());
213 MessageEncodingBindingElement BuildMessageEncodingBindingElement ()
215 if (MessageEncoding == WSMessageEncoding.Text) {
216 TextMessageEncodingBindingElement tm = new TextMessageEncodingBindingElement (
217 MessageVersion.CreateVersion (EnvelopeVersion, AddressingVersion.None), TextEncoding);
219 ReaderQuotas.CopyTo (tm.ReaderQuotas);
225 throw new SystemException ("INTERNAL ERROR: should not happen");
227 return new MtomMessageEncodingBindingElement (
228 MessageVersion.CreateVersion (EnvelopeVersion, AddressingVersion.None), TextEncoding);
232 TransportBindingElement GetTransport ()
234 HttpTransportBindingElement h;
235 switch (Security.Mode) {
236 case BasicHttpSecurityMode.Transport:
237 case BasicHttpSecurityMode.TransportWithMessageCredential:
238 h = new HttpsTransportBindingElement ();
241 h = new HttpTransportBindingElement ();
245 h.AllowCookies = AllowCookies;
246 h.BypassProxyOnLocal = BypassProxyOnLocal;
247 h.HostNameComparisonMode = HostNameComparisonMode;
248 h.MaxBufferPoolSize = MaxBufferPoolSize;
249 h.MaxBufferSize = MaxBufferSize;
250 h.MaxReceivedMessageSize = MaxReceivedMessageSize;
251 h.ProxyAddress = ProxyAddress;
252 h.UseDefaultWebProxy = UseDefaultWebProxy;
253 h.TransferMode = TransferMode;
256 switch (Security.Mode) {
257 case BasicHttpSecurityMode.Transport:
258 switch (Security.Transport.ClientCredentialType) {
259 case HttpClientCredentialType.Basic:
260 h.AuthenticationScheme = AuthenticationSchemes.Basic;
262 case HttpClientCredentialType.Ntlm:
263 h.AuthenticationScheme = AuthenticationSchemes.Ntlm;
265 case HttpClientCredentialType.Windows:
266 h.AuthenticationScheme = AuthenticationSchemes.Negotiate;
268 case HttpClientCredentialType.Digest:
269 h.AuthenticationScheme = AuthenticationSchemes.Digest;
271 case HttpClientCredentialType.Certificate:
272 var https = (HttpsTransportBindingElement) h;
273 https.RequireClientCertificate = true;
277 case BasicHttpSecurityMode.TransportCredentialOnly:
278 switch (Security.Transport.ClientCredentialType) {
279 case HttpClientCredentialType.Basic:
280 h.AuthenticationScheme = AuthenticationSchemes.Basic;
282 case HttpClientCredentialType.Ntlm:
283 h.AuthenticationScheme = AuthenticationSchemes.Ntlm;
285 case HttpClientCredentialType.Windows:
286 h.AuthenticationScheme = AuthenticationSchemes.Negotiate;
288 case HttpClientCredentialType.Digest:
289 h.AuthenticationScheme = AuthenticationSchemes.Digest;
291 case HttpClientCredentialType.Certificate:
292 throw new InvalidOperationException ("Certificate-based client authentication is not supported by 'TransportCredentialOnly' mode.");
301 // explicit interface implementations
303 bool IBindingRuntimePreferences.ReceiveSynchronously {
304 get { return false; }