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;
60 AuthenticationSchemes auth_scheme =
61 AuthenticationSchemes.Anonymous;
62 AuthenticationSchemes proxy_auth_scheme =
63 AuthenticationSchemes.Anonymous;
64 // If you add fields, do not forget them in copy constructor.
66 public HttpTransportBindingElement ()
70 protected HttpTransportBindingElement (
71 HttpTransportBindingElement other)
74 allow_cookies = other.allow_cookies;
75 bypass_proxy_on_local = other.bypass_proxy_on_local;
76 unsafe_ntlm_auth = other.unsafe_ntlm_auth;
77 use_default_proxy = other.use_default_proxy;
78 keep_alive_enabled = other.keep_alive_enabled;
79 max_buffer_size = other.max_buffer_size;
80 host_cmp_mode = other.host_cmp_mode;
81 proxy_address = other.proxy_address;
83 transfer_mode = other.transfer_mode;
84 // FIXME: it does not look safe
85 timeouts = other.timeouts;
86 auth_scheme = other.auth_scheme;
87 proxy_auth_scheme = other.proxy_auth_scheme;
90 DecompressionEnabled = other.DecompressionEnabled;
91 LegacyExtendedProtectionPolicy = other.LegacyExtendedProtectionPolicy;
92 ExtendedProtectionPolicy = other.ExtendedProtectionPolicy;
97 [DefaultValue (AuthenticationSchemes.Anonymous)]
99 public AuthenticationSchemes AuthenticationScheme {
100 get { return auth_scheme; }
101 set { auth_scheme = value; }
105 [DefaultValue (AuthenticationSchemes.Anonymous)]
107 public AuthenticationSchemes ProxyAuthenticationScheme {
108 get { return proxy_auth_scheme; }
109 set { proxy_auth_scheme = value; }
113 [DefaultValue (false)]
115 public bool AllowCookies {
116 get { return allow_cookies; }
117 set { allow_cookies = value; }
121 [DefaultValue (false)]
123 public bool BypassProxyOnLocal {
124 get { return bypass_proxy_on_local; }
125 set { bypass_proxy_on_local = value; }
129 [DefaultValue (false)]
131 public bool DecompressionEnabled { get; set; }
135 [DefaultValue (HostNameComparisonMode.StrongWildcard)]
137 public HostNameComparisonMode HostNameComparisonMode {
138 get { return host_cmp_mode; }
139 set { host_cmp_mode = value; }
143 [DefaultValue (true)]
145 public bool KeepAliveEnabled {
146 get { return keep_alive_enabled; }
147 set { keep_alive_enabled = value; }
151 [DefaultValue (0x10000)]
153 public int MaxBufferSize {
154 get { return max_buffer_size; }
155 set { max_buffer_size = value; }
159 [DefaultValue (null)]
160 [TypeConverter (typeof (UriTypeConverter))]
162 public Uri ProxyAddress {
163 get { return proxy_address; }
164 set { proxy_address = value; }
170 public string Realm {
171 get { return realm; }
172 set { realm = value; }
175 public override string Scheme {
176 get { return Uri.UriSchemeHttp; }
180 [DefaultValue (TransferMode.Buffered)]
182 public TransferMode TransferMode {
183 get { return transfer_mode; }
184 set { transfer_mode = value; }
188 [DefaultValue (false)]
190 public bool UnsafeConnectionNtlmAuthentication {
191 get { return unsafe_ntlm_auth; }
192 set { unsafe_ntlm_auth = value; }
196 [DefaultValue (true)]
198 public bool UseDefaultWebProxy {
199 get { return use_default_proxy; }
200 set { use_default_proxy = value; }
204 [Obsolete ("Use ExtendedProtectionPolicy")]
206 public object LegacyExtendedProtectionPolicy { get; set; }
209 public ExtendedProtectionPolicy ExtendedProtectionPolicy { get; set; }
212 public override bool CanBuildChannelFactory<TChannel> (
213 BindingContext context)
215 return typeof (TChannel) == typeof (IRequestChannel);
219 public override bool CanBuildChannelListener<TChannel> (
220 BindingContext context)
222 return typeof (TChannel) == typeof (IReplyChannel);
226 public override IChannelFactory<TChannel> BuildChannelFactory<TChannel> (
227 BindingContext context)
229 // remaining contexts are ignored ... e.g. such binding
230 // element that always causes an error is ignored.
231 return new HttpChannelFactory<TChannel> (this, context);
235 internal static object ListenerBuildLock = new object ();
237 public override IChannelListener<TChannel> BuildChannelListener<TChannel> (
238 BindingContext context)
240 // remaining contexts are ignored ... e.g. such binding
241 // element that always causes an error is ignored.
242 return new HttpChannelListener<TChannel> (this, context);
246 public override BindingElement Clone ()
248 return new HttpTransportBindingElement (this);
251 public override T GetProperty<T> (BindingContext context)
253 // http://blogs.msdn.com/drnick/archive/2007/04/10/interfaces-for-getproperty-part-1.aspx
254 if (typeof (T) == typeof (ISecurityCapabilities))
255 return (T) (object) new HttpBindingProperties (this);
256 if (typeof (T) == typeof (IBindingDeliveryCapabilities))
257 return (T) (object) new HttpBindingProperties (this);
258 if (typeof (T) == typeof (TransferMode))
259 return (T) (object) TransferMode;
260 return base.GetProperty<T> (context);
264 public WebSocketTransportSettings WebSocketSettings {
265 get { throw new NotImplementedException (); }
266 set { throw new NotImplementedException (); }
271 void IPolicyExportExtension.ExportPolicy (
272 MetadataExporter exporter,
273 PolicyConversionContext context)
275 if (exporter == null)
276 throw new ArgumentNullException ("exporter");
278 throw new ArgumentNullException ("context");
280 PolicyAssertionCollection assertions = context.GetBindingAssertions ();
281 XmlDocument doc = new XmlDocument ();
283 ExportAddressingPolicy (context);
285 switch (auth_scheme) {
286 case AuthenticationSchemes.Basic:
287 case AuthenticationSchemes.Digest:
288 case AuthenticationSchemes.Negotiate:
289 case AuthenticationSchemes.Ntlm:
290 assertions.Add (doc.CreateElement ("http",
291 auth_scheme.ToString () + "Authentication",
292 "http://schemas.microsoft.com/ws/06/2004/policy/http"));
296 var transportProvider = this as ITransportTokenAssertionProvider;
297 if (transportProvider != null) {
298 var token = transportProvider.GetTransportTokenAssertion ();
299 assertions.Add (CreateTransportBinding (token));
303 XmlElement CreateTransportBinding (XmlElement transportToken)
305 var doc = new XmlDocument ();
306 var transportBinding = doc.CreateElement (
307 "sp", "TransportBinding", PolicyImportHelper.SecurityPolicyNS);
309 var token = doc.CreateElement (
310 "sp", "TransportToken", PolicyImportHelper.SecurityPolicyNS);
311 PolicyImportHelper.AddWrappedPolicyElement (token, transportToken);
313 var algorithmSuite = doc.CreateElement (
314 "sp", "AlgorithmSuite", PolicyImportHelper.SecurityPolicyNS);
315 var basic256 = doc.CreateElement (
316 "sp", "Basic256", PolicyImportHelper.SecurityPolicyNS);
317 PolicyImportHelper.AddWrappedPolicyElement (algorithmSuite, basic256);
319 var layout = doc.CreateElement (
320 "sp", "Layout", PolicyImportHelper.SecurityPolicyNS);
321 var strict = doc.CreateElement (
322 "sp", "Strict", PolicyImportHelper.SecurityPolicyNS);
323 PolicyImportHelper.AddWrappedPolicyElement (layout, strict);
325 PolicyImportHelper.AddWrappedPolicyElements (
326 transportBinding, token, algorithmSuite, layout);
328 return transportBinding;
332 void IWsdlExportExtension.ExportContract (WsdlExporter exporter,
333 WsdlContractConversionContext context)
335 throw new NotImplementedException ();
339 void IWsdlExportExtension.ExportEndpoint (WsdlExporter exporter,
340 WsdlEndpointConversionContext context)
342 var soap_binding = new WS.SoapBinding ();
343 soap_binding.Transport = WS.SoapBinding.HttpTransport;
344 soap_binding.Style = WS.SoapBindingStyle.Document;
345 context.WsdlBinding.Extensions.Add (soap_binding);
347 var soap_address = new WS.SoapAddressBinding ();
348 soap_address.Location = context.Endpoint.Address.Uri.AbsoluteUri;
350 context.WsdlPort.Extensions.Add (soap_address);
355 class HttpBindingProperties : ISecurityCapabilities, IBindingDeliveryCapabilities
357 HttpTransportBindingElement source;
359 public HttpBindingProperties (HttpTransportBindingElement source)
361 this.source = source;
364 public bool AssuresOrderedDelivery {
365 get { return false; }
368 public bool QueuedDelivery {
369 get { return false; }
372 public virtual ProtectionLevel SupportedRequestProtectionLevel {
373 get { return ProtectionLevel.None; }
376 public virtual ProtectionLevel SupportedResponseProtectionLevel {
377 get { return ProtectionLevel.None; }
380 public virtual bool SupportsClientAuthentication {
381 get { return source.AuthenticationScheme != AuthenticationSchemes.Anonymous; }
384 public virtual bool SupportsServerAuthentication {
386 switch (source.AuthenticationScheme) {
387 case AuthenticationSchemes.Negotiate:
395 public virtual bool SupportsClientWindowsIdentity {
397 switch (source.AuthenticationScheme) {
398 case AuthenticationSchemes.Basic:
399 case AuthenticationSchemes.Digest: // hmm... why? but they return true on .NET
400 case AuthenticationSchemes.Negotiate:
401 case AuthenticationSchemes.Ntlm: