2 // HttpTransportBindingElement.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005-2010 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;
30 using System.ComponentModel;
32 using System.Net.Security;
34 using System.Security.Authentication.ExtendedProtection;
36 using System.ServiceModel.Channels;
38 using System.ServiceModel.Channels.Http;
40 using System.ServiceModel.Description;
42 using WS = System.Web.Services.Description;
46 namespace System.ServiceModel.Channels
48 public class HttpTransportBindingElement : TransportBindingElement,
49 IPolicyExportExtension, IWsdlExportExtension
51 bool allow_cookies, bypass_proxy_on_local,
53 bool use_default_proxy = true, keep_alive_enabled = true;
54 int max_buffer_size = 0x10000;
55 HostNameComparisonMode host_cmp_mode;
57 string realm = String.Empty;
58 TransferMode transfer_mode;
59 IDefaultCommunicationTimeouts timeouts;
61 AuthenticationSchemes auth_scheme =
62 AuthenticationSchemes.Anonymous;
63 AuthenticationSchemes proxy_auth_scheme =
64 AuthenticationSchemes.Anonymous;
66 // If you add fields, do not forget them in copy constructor.
68 public HttpTransportBindingElement ()
72 protected HttpTransportBindingElement (
73 HttpTransportBindingElement other)
76 allow_cookies = other.allow_cookies;
77 bypass_proxy_on_local = other.bypass_proxy_on_local;
78 unsafe_ntlm_auth = other.unsafe_ntlm_auth;
79 use_default_proxy = other.use_default_proxy;
80 keep_alive_enabled = other.keep_alive_enabled;
81 max_buffer_size = other.max_buffer_size;
82 host_cmp_mode = other.host_cmp_mode;
83 proxy_address = other.proxy_address;
85 transfer_mode = other.transfer_mode;
86 // FIXME: it does not look safe
87 timeouts = other.timeouts;
89 auth_scheme = other.auth_scheme;
90 proxy_auth_scheme = other.proxy_auth_scheme;
94 DecompressionEnabled = other.DecompressionEnabled;
95 LegacyExtendedProtectionPolicy = other.LegacyExtendedProtectionPolicy;
96 ExtendedProtectionPolicy = other.ExtendedProtectionPolicy;
102 [DefaultValue (AuthenticationSchemes.Anonymous)]
104 public AuthenticationSchemes AuthenticationScheme {
105 get { return auth_scheme; }
106 set { auth_scheme = value; }
110 [DefaultValue (AuthenticationSchemes.Anonymous)]
112 public AuthenticationSchemes ProxyAuthenticationScheme {
113 get { return proxy_auth_scheme; }
114 set { proxy_auth_scheme = value; }
119 [DefaultValue (false)]
121 public bool AllowCookies {
122 get { return allow_cookies; }
123 set { allow_cookies = value; }
127 [DefaultValue (false)]
129 public bool BypassProxyOnLocal {
130 get { return bypass_proxy_on_local; }
131 set { bypass_proxy_on_local = value; }
135 [DefaultValue (false)]
137 public bool DecompressionEnabled { get; set; }
141 [DefaultValue (HostNameComparisonMode.StrongWildcard)]
143 public HostNameComparisonMode HostNameComparisonMode {
144 get { return host_cmp_mode; }
145 set { host_cmp_mode = value; }
149 [DefaultValue (true)]
151 public bool KeepAliveEnabled {
152 get { return keep_alive_enabled; }
153 set { keep_alive_enabled = value; }
157 [DefaultValue (0x10000)]
159 public int MaxBufferSize {
160 get { return max_buffer_size; }
161 set { max_buffer_size = value; }
165 [DefaultValue (null)]
166 [TypeConverter (typeof (UriTypeConverter))]
168 public Uri ProxyAddress {
169 get { return proxy_address; }
170 set { proxy_address = value; }
176 public string Realm {
177 get { return realm; }
178 set { realm = value; }
181 public override string Scheme {
182 get { return Uri.UriSchemeHttp; }
186 [DefaultValue (TransferMode.Buffered)]
188 public TransferMode TransferMode {
189 get { return transfer_mode; }
190 set { transfer_mode = value; }
194 [DefaultValue (false)]
196 public bool UnsafeConnectionNtlmAuthentication {
197 get { return unsafe_ntlm_auth; }
198 set { unsafe_ntlm_auth = value; }
202 [DefaultValue (true)]
204 public bool UseDefaultWebProxy {
205 get { return use_default_proxy; }
206 set { use_default_proxy = value; }
210 [Obsolete ("Use ExtendedProtectionPolicy")]
212 public object LegacyExtendedProtectionPolicy { get; set; }
215 public ExtendedProtectionPolicy ExtendedProtectionPolicy { get; set; }
218 public override bool CanBuildChannelFactory<TChannel> (
219 BindingContext context)
221 return typeof (TChannel) == typeof (IRequestChannel);
225 public override bool CanBuildChannelListener<TChannel> (
226 BindingContext context)
228 return typeof (TChannel) == typeof (IReplyChannel);
232 public override IChannelFactory<TChannel> BuildChannelFactory<TChannel> (
233 BindingContext context)
235 // remaining contexts are ignored ... e.g. such binding
236 // element that always causes an error is ignored.
237 return new HttpChannelFactory<TChannel> (this, context);
241 internal static object ListenerBuildLock = new object ();
243 public override IChannelListener<TChannel> BuildChannelListener<TChannel> (
244 BindingContext context)
246 // remaining contexts are ignored ... e.g. such binding
247 // element that always causes an error is ignored.
248 return new HttpChannelListener<TChannel> (this, context);
252 public override BindingElement Clone ()
254 return new HttpTransportBindingElement (this);
257 public override T GetProperty<T> (BindingContext context)
259 // http://blogs.msdn.com/drnick/archive/2007/04/10/interfaces-for-getproperty-part-1.aspx
261 if (typeof (T) == typeof (ISecurityCapabilities))
262 return (T) (object) new HttpBindingProperties (this);
263 if (typeof (T) == typeof (IBindingDeliveryCapabilities))
264 return (T) (object) new HttpBindingProperties (this);
266 if (typeof (T) == typeof (TransferMode))
267 return (T) (object) TransferMode;
268 return base.GetProperty<T> (context);
272 void IPolicyExportExtension.ExportPolicy (
273 MetadataExporter exporter,
274 PolicyConversionContext context)
276 if (exporter == null)
277 throw new ArgumentNullException ("exporter");
279 throw new ArgumentNullException ("context");
281 PolicyAssertionCollection assertions = context.GetBindingAssertions ();
282 XmlDocument doc = new XmlDocument ();
284 ExportAddressingPolicy (context);
286 switch (auth_scheme) {
287 case AuthenticationSchemes.Basic:
288 case AuthenticationSchemes.Digest:
289 case AuthenticationSchemes.Negotiate:
290 case AuthenticationSchemes.Ntlm:
291 assertions.Add (doc.CreateElement ("http",
292 auth_scheme.ToString () + "Authentication",
293 "http://schemas.microsoft.com/ws/06/2004/policy/http"));
297 var transportProvider = this as ITransportTokenAssertionProvider;
298 if (transportProvider != null) {
299 var token = transportProvider.GetTransportTokenAssertion ();
300 assertions.Add (CreateTransportBinding (token));
304 XmlElement CreateTransportBinding (XmlElement transportToken)
306 var doc = new XmlDocument ();
307 var transportBinding = doc.CreateElement (
308 "sp", "TransportBinding", PolicyImportHelper.SecurityPolicyNS);
310 var token = doc.CreateElement (
311 "sp", "TransportToken", PolicyImportHelper.SecurityPolicyNS);
312 PolicyImportHelper.AddWrappedPolicyElement (token, transportToken);
314 var algorithmSuite = doc.CreateElement (
315 "sp", "AlgorithmSuite", PolicyImportHelper.SecurityPolicyNS);
316 var basic256 = doc.CreateElement (
317 "sp", "Basic256", PolicyImportHelper.SecurityPolicyNS);
318 PolicyImportHelper.AddWrappedPolicyElement (algorithmSuite, basic256);
320 var layout = doc.CreateElement (
321 "sp", "Layout", PolicyImportHelper.SecurityPolicyNS);
322 var strict = doc.CreateElement (
323 "sp", "Strict", PolicyImportHelper.SecurityPolicyNS);
324 PolicyImportHelper.AddWrappedPolicyElement (layout, strict);
326 PolicyImportHelper.AddWrappedPolicyElements (
327 transportBinding, token, algorithmSuite, layout);
329 return transportBinding;
333 void IWsdlExportExtension.ExportContract (WsdlExporter exporter,
334 WsdlContractConversionContext context)
336 throw new NotImplementedException ();
340 void IWsdlExportExtension.ExportEndpoint (WsdlExporter exporter,
341 WsdlEndpointConversionContext context)
343 var soap_binding = new WS.SoapBinding ();
344 soap_binding.Transport = WS.SoapBinding.HttpTransport;
345 soap_binding.Style = WS.SoapBindingStyle.Document;
346 context.WsdlBinding.Extensions.Add (soap_binding);
348 var soap_address = new WS.SoapAddressBinding ();
349 soap_address.Location = context.Endpoint.Address.Uri.AbsoluteUri;
351 context.WsdlPort.Extensions.Add (soap_address);
357 class HttpBindingProperties : ISecurityCapabilities, IBindingDeliveryCapabilities
359 HttpTransportBindingElement source;
361 public HttpBindingProperties (HttpTransportBindingElement source)
363 this.source = source;
366 public bool AssuresOrderedDelivery {
367 get { return false; }
370 public bool QueuedDelivery {
371 get { return false; }
374 public virtual ProtectionLevel SupportedRequestProtectionLevel {
375 get { return ProtectionLevel.None; }
378 public virtual ProtectionLevel SupportedResponseProtectionLevel {
379 get { return ProtectionLevel.None; }
382 public virtual bool SupportsClientAuthentication {
383 get { return source.AuthenticationScheme != AuthenticationSchemes.Anonymous; }
386 public virtual bool SupportsServerAuthentication {
388 switch (source.AuthenticationScheme) {
389 case AuthenticationSchemes.Negotiate:
397 public virtual bool SupportsClientWindowsIdentity {
399 switch (source.AuthenticationScheme) {
400 case AuthenticationSchemes.Basic:
401 case AuthenticationSchemes.Digest: // hmm... why? but they return true on .NET
402 case AuthenticationSchemes.Negotiate:
403 case AuthenticationSchemes.Ntlm: