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;
43 namespace System.ServiceModel.Channels
45 public class HttpTransportBindingElement : TransportBindingElement,
46 IPolicyExportExtension, IWsdlExportExtension
48 bool allow_cookies, bypass_proxy_on_local,
50 bool use_default_proxy = true, keep_alive_enabled = true;
51 int max_buffer_size = 0x10000;
52 HostNameComparisonMode host_cmp_mode;
54 string realm = String.Empty;
55 TransferMode transfer_mode;
56 IDefaultCommunicationTimeouts timeouts;
58 AuthenticationSchemes auth_scheme =
59 AuthenticationSchemes.Anonymous;
60 AuthenticationSchemes proxy_auth_scheme =
61 AuthenticationSchemes.Anonymous;
63 // If you add fields, do not forget them in copy constructor.
65 public HttpTransportBindingElement ()
69 protected HttpTransportBindingElement (
70 HttpTransportBindingElement other)
73 allow_cookies = other.allow_cookies;
74 bypass_proxy_on_local = other.bypass_proxy_on_local;
75 unsafe_ntlm_auth = other.unsafe_ntlm_auth;
76 use_default_proxy = other.use_default_proxy;
77 keep_alive_enabled = other.keep_alive_enabled;
78 max_buffer_size = other.max_buffer_size;
79 host_cmp_mode = other.host_cmp_mode;
80 proxy_address = other.proxy_address;
82 transfer_mode = other.transfer_mode;
83 // FIXME: it does not look safe
84 timeouts = other.timeouts;
86 auth_scheme = other.auth_scheme;
87 proxy_auth_scheme = other.proxy_auth_scheme;
91 DecompressionEnabled = other.DecompressionEnabled;
92 LegacyExtendedProtectionPolicy = other.LegacyExtendedProtectionPolicy;
93 ExtendedProtectionPolicy = other.ExtendedProtectionPolicy;
99 [DefaultValue (AuthenticationSchemes.Anonymous)]
101 public AuthenticationSchemes AuthenticationScheme {
102 get { return auth_scheme; }
103 set { auth_scheme = value; }
107 [DefaultValue (AuthenticationSchemes.Anonymous)]
109 public AuthenticationSchemes ProxyAuthenticationScheme {
110 get { return proxy_auth_scheme; }
111 set { proxy_auth_scheme = value; }
116 [DefaultValue (false)]
118 public bool AllowCookies {
119 get { return allow_cookies; }
120 set { allow_cookies = value; }
124 [DefaultValue (false)]
126 public bool BypassProxyOnLocal {
127 get { return bypass_proxy_on_local; }
128 set { bypass_proxy_on_local = value; }
132 [DefaultValue (false)]
134 public bool DecompressionEnabled { get; set; }
138 [DefaultValue (HostNameComparisonMode.StrongWildcard)]
140 public HostNameComparisonMode HostNameComparisonMode {
141 get { return host_cmp_mode; }
142 set { host_cmp_mode = value; }
146 [DefaultValue (true)]
148 public bool KeepAliveEnabled {
149 get { return keep_alive_enabled; }
150 set { keep_alive_enabled = value; }
154 [DefaultValue (0x10000)]
156 public int MaxBufferSize {
157 get { return max_buffer_size; }
158 set { max_buffer_size = value; }
162 [DefaultValue (null)]
163 [TypeConverter (typeof (UriTypeConverter))]
165 public Uri ProxyAddress {
166 get { return proxy_address; }
167 set { proxy_address = value; }
173 public string Realm {
174 get { return realm; }
175 set { realm = value; }
178 public override string Scheme {
179 get { return Uri.UriSchemeHttp; }
183 [DefaultValue (TransferMode.Buffered)]
185 public TransferMode TransferMode {
186 get { return transfer_mode; }
187 set { transfer_mode = value; }
191 [DefaultValue (false)]
193 public bool UnsafeConnectionNtlmAuthentication {
194 get { return unsafe_ntlm_auth; }
195 set { unsafe_ntlm_auth = value; }
199 [DefaultValue (true)]
201 public bool UseDefaultWebProxy {
202 get { return use_default_proxy; }
203 set { use_default_proxy = value; }
207 [Obsolete ("Use ExtendedProtectionPolicy")]
209 public object LegacyExtendedProtectionPolicy { get; set; }
212 public ExtendedProtectionPolicy ExtendedProtectionPolicy { get; set; }
215 public override bool CanBuildChannelFactory<TChannel> (
216 BindingContext context)
218 return typeof (TChannel) == typeof (IRequestChannel);
222 public override bool CanBuildChannelListener<TChannel> (
223 BindingContext context)
225 return typeof (TChannel) == typeof (IReplyChannel);
229 public override IChannelFactory<TChannel> BuildChannelFactory<TChannel> (
230 BindingContext context)
232 // remaining contexts are ignored ... e.g. such binding
233 // element that always causes an error is ignored.
234 return new HttpChannelFactory<TChannel> (this, context);
238 internal static object ListenerBuildLock = new object ();
240 public override IChannelListener<TChannel> BuildChannelListener<TChannel> (
241 BindingContext context)
243 // remaining contexts are ignored ... e.g. such binding
244 // element that always causes an error is ignored.
245 return new HttpChannelListener<TChannel> (this, context);
249 public override BindingElement Clone ()
251 return new HttpTransportBindingElement (this);
254 public override T GetProperty<T> (BindingContext context)
256 // http://blogs.msdn.com/drnick/archive/2007/04/10/interfaces-for-getproperty-part-1.aspx
258 if (typeof (T) == typeof (ISecurityCapabilities))
259 return (T) (object) new HttpBindingProperties (this);
260 if (typeof (T) == typeof (IBindingDeliveryCapabilities))
261 return (T) (object) new HttpBindingProperties (this);
263 if (typeof (T) == typeof (TransferMode))
264 return (T) (object) TransferMode;
265 return base.GetProperty<T> (context);
269 void IPolicyExportExtension.ExportPolicy (
270 MetadataExporter exporter,
271 PolicyConversionContext context)
273 if (exporter == null)
274 throw new ArgumentNullException ("exporter");
276 throw new ArgumentNullException ("context");
278 PolicyAssertionCollection assertions = context.GetBindingAssertions ();
279 XmlDocument doc = new XmlDocument ();
281 assertions.Add (doc.CreateElement ("wsaw", "UsingAddressing", "http://www.w3.org/2006/05/addressing/wsdl"));
283 switch (auth_scheme) {
284 case AuthenticationSchemes.Basic:
285 case AuthenticationSchemes.Digest:
286 case AuthenticationSchemes.Negotiate:
287 case AuthenticationSchemes.Ntlm:
288 assertions.Add (doc.CreateElement ("http",
289 auth_scheme.ToString () + "Authentication",
290 "http://schemas.microsoft.com/ws/06/2004/policy/http"));
296 void IWsdlExportExtension.ExportContract (WsdlExporter exporter,
297 WsdlContractConversionContext context)
299 throw new NotImplementedException ();
303 void IWsdlExportExtension.ExportEndpoint (WsdlExporter exporter,
304 WsdlEndpointConversionContext context)
306 throw new NotImplementedException ();
312 class HttpBindingProperties : ISecurityCapabilities, IBindingDeliveryCapabilities
314 HttpTransportBindingElement source;
316 public HttpBindingProperties (HttpTransportBindingElement source)
318 this.source = source;
321 public bool AssuresOrderedDelivery {
322 get { return false; }
325 public bool QueuedDelivery {
326 get { return false; }
329 public virtual ProtectionLevel SupportedRequestProtectionLevel {
330 get { return ProtectionLevel.None; }
333 public virtual ProtectionLevel SupportedResponseProtectionLevel {
334 get { return ProtectionLevel.None; }
337 public virtual bool SupportsClientAuthentication {
338 get { return source.AuthenticationScheme != AuthenticationSchemes.Anonymous; }
341 public virtual bool SupportsServerAuthentication {
343 switch (source.AuthenticationScheme) {
344 case AuthenticationSchemes.Negotiate:
352 public virtual bool SupportsClientWindowsIdentity {
354 switch (source.AuthenticationScheme) {
355 case AuthenticationSchemes.Basic:
356 case AuthenticationSchemes.Digest: // hmm... why? but they return true on .NET
357 case AuthenticationSchemes.Negotiate:
358 case AuthenticationSchemes.Ntlm: