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;
33 using System.Security.Authentication.ExtendedProtection;
34 using System.ServiceModel.Channels;
36 using System.ServiceModel.Channels.Http;
38 using System.ServiceModel.Description;
40 using WS = System.Web.Services.Description;
44 namespace System.ServiceModel.Channels
46 public class HttpTransportBindingElement : TransportBindingElement,
47 IPolicyExportExtension, IWsdlExportExtension
49 bool allow_cookies, bypass_proxy_on_local,
51 bool use_default_proxy = true, keep_alive_enabled = true;
52 int max_buffer_size = 0x10000;
53 HostNameComparisonMode host_cmp_mode;
55 string realm = String.Empty;
56 TransferMode transfer_mode;
57 IDefaultCommunicationTimeouts timeouts;
58 AuthenticationSchemes auth_scheme =
59 AuthenticationSchemes.Anonymous;
60 AuthenticationSchemes proxy_auth_scheme =
61 AuthenticationSchemes.Anonymous;
62 // If you add fields, do not forget them in copy constructor.
63 HttpCookieContainerManager cookie_manager;
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;
85 auth_scheme = other.auth_scheme;
86 proxy_auth_scheme = other.proxy_auth_scheme;
88 DecompressionEnabled = other.DecompressionEnabled;
89 LegacyExtendedProtectionPolicy = other.LegacyExtendedProtectionPolicy;
90 ExtendedProtectionPolicy = other.ExtendedProtectionPolicy;
91 cookie_manager = other.cookie_manager;
94 [DefaultValue (AuthenticationSchemes.Anonymous)]
95 public AuthenticationSchemes AuthenticationScheme {
96 get { return auth_scheme; }
97 set { auth_scheme = value; }
100 [DefaultValue (AuthenticationSchemes.Anonymous)]
101 public AuthenticationSchemes ProxyAuthenticationScheme {
102 get { return proxy_auth_scheme; }
103 set { proxy_auth_scheme = value; }
106 [DefaultValue (false)]
107 public bool AllowCookies {
108 get { return allow_cookies; }
109 set { allow_cookies = value; }
112 [DefaultValue (false)]
113 public bool BypassProxyOnLocal {
114 get { return bypass_proxy_on_local; }
115 set { bypass_proxy_on_local = value; }
118 [DefaultValue (false)]
120 public bool DecompressionEnabled { get; set; }
122 [DefaultValue (HostNameComparisonMode.StrongWildcard)]
123 public HostNameComparisonMode HostNameComparisonMode {
124 get { return host_cmp_mode; }
125 set { host_cmp_mode = value; }
128 [DefaultValue (true)]
129 public bool KeepAliveEnabled {
130 get { return keep_alive_enabled; }
131 set { keep_alive_enabled = value; }
134 [DefaultValue (0x10000)]
135 public int MaxBufferSize {
136 get { return max_buffer_size; }
137 set { max_buffer_size = value; }
140 [DefaultValue (null)]
141 [TypeConverter (typeof (UriTypeConverter))]
142 public Uri ProxyAddress {
143 get { return proxy_address; }
144 set { proxy_address = value; }
148 public string Realm {
149 get { return realm; }
150 set { realm = value; }
153 public override string Scheme {
154 get { return Uri.UriSchemeHttp; }
157 [DefaultValue (TransferMode.Buffered)]
158 public TransferMode TransferMode {
159 get { return transfer_mode; }
160 set { transfer_mode = value; }
163 [DefaultValue (false)]
164 public bool UnsafeConnectionNtlmAuthentication {
165 get { return unsafe_ntlm_auth; }
166 set { unsafe_ntlm_auth = value; }
169 [DefaultValue (true)]
170 public bool UseDefaultWebProxy {
171 get { return use_default_proxy; }
172 set { use_default_proxy = value; }
175 [Obsolete ("Use ExtendedProtectionPolicy")]
177 public object LegacyExtendedProtectionPolicy { get; set; }
180 public ExtendedProtectionPolicy ExtendedProtectionPolicy { get; set; }
182 public override bool CanBuildChannelFactory<TChannel> (
183 BindingContext context)
185 return typeof (TChannel) == typeof (IRequestChannel);
188 #if !NET_2_1 && !XAMMAC_4_5
189 public override bool CanBuildChannelListener<TChannel> (
190 BindingContext context)
192 return typeof (TChannel) == typeof (IReplyChannel);
196 public override IChannelFactory<TChannel> BuildChannelFactory<TChannel> (
197 BindingContext context)
199 // remaining contexts are ignored ... e.g. such binding
200 // element that always causes an error is ignored.
201 return new HttpChannelFactory<TChannel> (this, context);
204 #if !NET_2_1 && !XAMMAC_4_5
205 internal static object ListenerBuildLock = new object ();
207 public override IChannelListener<TChannel> BuildChannelListener<TChannel> (
208 BindingContext context)
210 // remaining contexts are ignored ... e.g. such binding
211 // element that always causes an error is ignored.
212 return new HttpChannelListener<TChannel> (this, context);
216 public override BindingElement Clone ()
218 return new HttpTransportBindingElement (this);
221 public override T GetProperty<T> (BindingContext context)
223 // http://blogs.msdn.com/drnick/archive/2007/04/10/interfaces-for-getproperty-part-1.aspx
224 if (typeof (T) == typeof (ISecurityCapabilities))
225 return (T) (object) new HttpBindingProperties (this);
226 if (typeof (T) == typeof (IBindingDeliveryCapabilities))
227 return (T) (object) new HttpBindingProperties (this);
228 if (typeof (T) == typeof (TransferMode))
229 return (T) (object) TransferMode;
230 if (typeof(T) == typeof(IHttpCookieContainerManager)) {
233 if (cookie_manager == null)
234 cookie_manager = new HttpCookieContainerManager ();
235 return (T) (object) cookie_manager;
237 return base.GetProperty<T> (context);
240 public WebSocketTransportSettings WebSocketSettings {
241 get { throw new NotImplementedException (); }
242 set { throw new NotImplementedException (); }
245 #if !NET_2_1 && !XAMMAC_4_5
246 void IPolicyExportExtension.ExportPolicy (
247 MetadataExporter exporter,
248 PolicyConversionContext context)
250 if (exporter == null)
251 throw new ArgumentNullException ("exporter");
253 throw new ArgumentNullException ("context");
255 PolicyAssertionCollection assertions = context.GetBindingAssertions ();
256 XmlDocument doc = new XmlDocument ();
258 ExportAddressingPolicy (context);
260 switch (auth_scheme) {
261 case AuthenticationSchemes.Basic:
262 case AuthenticationSchemes.Digest:
263 case AuthenticationSchemes.Negotiate:
264 case AuthenticationSchemes.Ntlm:
265 assertions.Add (doc.CreateElement ("http",
266 auth_scheme.ToString () + "Authentication",
267 "http://schemas.microsoft.com/ws/06/2004/policy/http"));
271 var transportProvider = this as ITransportTokenAssertionProvider;
272 if (transportProvider != null) {
273 var token = transportProvider.GetTransportTokenAssertion ();
274 assertions.Add (CreateTransportBinding (token));
278 XmlElement CreateTransportBinding (XmlElement transportToken)
280 var doc = new XmlDocument ();
281 var transportBinding = doc.CreateElement (
282 "sp", "TransportBinding", PolicyImportHelper.SecurityPolicyNS);
284 var token = doc.CreateElement (
285 "sp", "TransportToken", PolicyImportHelper.SecurityPolicyNS);
286 PolicyImportHelper.AddWrappedPolicyElement (token, transportToken);
288 var algorithmSuite = doc.CreateElement (
289 "sp", "AlgorithmSuite", PolicyImportHelper.SecurityPolicyNS);
290 var basic256 = doc.CreateElement (
291 "sp", "Basic256", PolicyImportHelper.SecurityPolicyNS);
292 PolicyImportHelper.AddWrappedPolicyElement (algorithmSuite, basic256);
294 var layout = doc.CreateElement (
295 "sp", "Layout", PolicyImportHelper.SecurityPolicyNS);
296 var strict = doc.CreateElement (
297 "sp", "Strict", PolicyImportHelper.SecurityPolicyNS);
298 PolicyImportHelper.AddWrappedPolicyElement (layout, strict);
300 PolicyImportHelper.AddWrappedPolicyElements (
301 transportBinding, token, algorithmSuite, layout);
303 return transportBinding;
307 void IWsdlExportExtension.ExportContract (WsdlExporter exporter,
308 WsdlContractConversionContext context)
310 throw new NotImplementedException ();
314 void IWsdlExportExtension.ExportEndpoint (WsdlExporter exporter,
315 WsdlEndpointConversionContext context)
317 var soap_binding = new WS.SoapBinding ();
318 soap_binding.Transport = WS.SoapBinding.HttpTransport;
319 soap_binding.Style = WS.SoapBindingStyle.Document;
320 context.WsdlBinding.Extensions.Add (soap_binding);
322 var soap_address = new WS.SoapAddressBinding ();
323 soap_address.Location = context.Endpoint.Address.Uri.AbsoluteUri;
325 context.WsdlPort.Extensions.Add (soap_address);
330 class HttpBindingProperties : ISecurityCapabilities, IBindingDeliveryCapabilities
332 HttpTransportBindingElement source;
334 public HttpBindingProperties (HttpTransportBindingElement source)
336 this.source = source;
339 public bool AssuresOrderedDelivery {
340 get { return false; }
343 public bool QueuedDelivery {
344 get { return false; }
347 public virtual ProtectionLevel SupportedRequestProtectionLevel {
348 get { return ProtectionLevel.None; }
351 public virtual ProtectionLevel SupportedResponseProtectionLevel {
352 get { return ProtectionLevel.None; }
355 public virtual bool SupportsClientAuthentication {
356 get { return source.AuthenticationScheme != AuthenticationSchemes.Anonymous; }
359 public virtual bool SupportsServerAuthentication {
361 switch (source.AuthenticationScheme) {
362 case AuthenticationSchemes.Negotiate:
370 public virtual bool SupportsClientWindowsIdentity {
372 switch (source.AuthenticationScheme) {
373 case AuthenticationSchemes.Basic:
374 case AuthenticationSchemes.Digest: // hmm... why? but they return true on .NET
375 case AuthenticationSchemes.Negotiate:
376 case AuthenticationSchemes.Ntlm: