0dbb5a48e1b16a3ff4d140ea937bab39f207f189
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / HttpTransportBindingElement.cs
1 //
2 // HttpTransportBindingElement.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005-2010 Novell, Inc.  http://www.novell.com
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28 using System;
29 using System.Collections.Generic;
30 using System.ComponentModel;
31 using System.Net;
32 using System.Net.Security;
33 #if NET_4_0
34 using System.Security.Authentication.ExtendedProtection;
35 #endif
36 using System.ServiceModel.Channels;
37 #if !NET_2_1
38 using System.ServiceModel.Channels.Http;
39 #endif
40 using System.ServiceModel.Description;
41 #if !MOBILE
42 using WS = System.Web.Services.Description;
43 #endif
44 using System.Xml;
45
46 namespace System.ServiceModel.Channels
47 {
48         public class HttpTransportBindingElement : TransportBindingElement,
49                 IPolicyExportExtension, IWsdlExportExtension
50         {
51                 bool allow_cookies, bypass_proxy_on_local,
52                         unsafe_ntlm_auth;
53                 bool use_default_proxy = true, keep_alive_enabled = true;
54                 int max_buffer_size = 0x10000;
55                 HostNameComparisonMode host_cmp_mode;
56                 Uri proxy_address;
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.
65
66                 public HttpTransportBindingElement ()
67                 {
68                 }
69
70                 protected HttpTransportBindingElement (
71                         HttpTransportBindingElement other)
72                         : base (other)
73                 {
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;
82                         realm = other.realm;
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;
88
89 #if NET_4_0
90                         DecompressionEnabled = other.DecompressionEnabled;
91                         LegacyExtendedProtectionPolicy = other.LegacyExtendedProtectionPolicy;
92                         ExtendedProtectionPolicy = other.ExtendedProtectionPolicy;
93 #endif
94                 }
95
96 #if NET_4_0
97                 [DefaultValue (AuthenticationSchemes.Anonymous)]
98 #endif
99                 public AuthenticationSchemes AuthenticationScheme {
100                         get { return auth_scheme; }
101                         set { auth_scheme = value; }
102                 }
103
104 #if NET_4_0
105                 [DefaultValue (AuthenticationSchemes.Anonymous)]
106 #endif
107                 public AuthenticationSchemes ProxyAuthenticationScheme {
108                         get { return proxy_auth_scheme; }
109                         set { proxy_auth_scheme = value; }
110                 }
111
112 #if NET_4_0
113                 [DefaultValue (false)]
114 #endif
115                 public bool AllowCookies {
116                         get { return allow_cookies; }
117                         set { allow_cookies = value; }
118                 }
119
120 #if NET_4_0
121                 [DefaultValue (false)]
122 #endif
123                 public bool BypassProxyOnLocal {
124                         get { return bypass_proxy_on_local; }
125                         set { bypass_proxy_on_local = value; }
126                 }
127
128 #if NET_4_0
129                 [DefaultValue (false)]
130                 [MonoTODO]
131                 public bool DecompressionEnabled { get; set; }
132 #endif
133
134 #if NET_4_0
135                 [DefaultValue (HostNameComparisonMode.StrongWildcard)]
136 #endif
137                 public HostNameComparisonMode HostNameComparisonMode {
138                         get { return host_cmp_mode; }
139                         set { host_cmp_mode = value; }
140                 }
141
142 #if NET_4_0
143                 [DefaultValue (true)]
144 #endif
145                 public bool KeepAliveEnabled {
146                         get { return keep_alive_enabled; }
147                         set { keep_alive_enabled = value; }
148                 }
149
150 #if NET_4_0
151                 [DefaultValue (0x10000)]
152 #endif
153                 public int MaxBufferSize {
154                         get { return max_buffer_size; }
155                         set { max_buffer_size = value; }
156                 }
157
158 #if NET_4_0
159                 [DefaultValue (null)]
160                 [TypeConverter (typeof (UriTypeConverter))]
161 #endif
162                 public Uri ProxyAddress {
163                         get { return proxy_address; }
164                         set { proxy_address = value; }
165                 }
166
167 #if NET_4_0
168                 [DefaultValue ("")]
169 #endif
170                 public string Realm {
171                         get { return realm; }
172                         set { realm = value; }
173                 }
174
175                 public override string Scheme {
176                         get { return Uri.UriSchemeHttp; }
177                 }
178
179 #if NET_4_0
180                 [DefaultValue (TransferMode.Buffered)]
181 #endif
182                 public TransferMode TransferMode {
183                         get { return transfer_mode; }
184                         set { transfer_mode = value; }
185                 }
186
187 #if NET_4_0
188                 [DefaultValue (false)]
189 #endif
190                 public bool UnsafeConnectionNtlmAuthentication {
191                         get { return unsafe_ntlm_auth; }
192                         set { unsafe_ntlm_auth = value; }
193                 }
194
195 #if NET_4_0
196                 [DefaultValue (true)]
197 #endif
198                 public bool UseDefaultWebProxy {
199                         get { return use_default_proxy; }
200                         set { use_default_proxy = value; }
201                 }
202
203 #if NET_4_0
204                 [Obsolete ("Use ExtendedProtectionPolicy")]
205                 [MonoTODO]
206                 public object LegacyExtendedProtectionPolicy { get; set; }
207
208                 [MonoTODO]
209                 public ExtendedProtectionPolicy ExtendedProtectionPolicy { get; set; }
210 #endif
211
212                 public override bool CanBuildChannelFactory<TChannel> (
213                         BindingContext context)
214                 {
215                         return typeof (TChannel) == typeof (IRequestChannel);
216                 }
217
218 #if !NET_2_1
219                 public override bool CanBuildChannelListener<TChannel> (
220                         BindingContext context)
221                 {
222                         return typeof (TChannel) == typeof (IReplyChannel);
223                 }
224 #endif
225
226                 public override IChannelFactory<TChannel> BuildChannelFactory<TChannel> (
227                         BindingContext context)
228                 {
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);
232                 }
233
234 #if !NET_2_1
235                 internal static object ListenerBuildLock = new object ();
236
237                 public override IChannelListener<TChannel> BuildChannelListener<TChannel> (
238                         BindingContext context)
239                 {
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);
243                 }
244 #endif
245
246                 public override BindingElement Clone ()
247                 {
248                         return new HttpTransportBindingElement (this);
249                 }
250
251                 public override T GetProperty<T> (BindingContext context)
252                 {
253                         // http://blogs.msdn.com/drnick/archive/2007/04/10/interfaces-for-getproperty-part-1.aspx
254 #if !NET_2_1
255                         if (typeof (T) == typeof (ISecurityCapabilities))
256                                 return (T) (object) new HttpBindingProperties (this);
257                         if (typeof (T) == typeof (IBindingDeliveryCapabilities))
258                                 return (T) (object) new HttpBindingProperties (this);
259 #endif
260                         if (typeof (T) == typeof (TransferMode))
261                                 return (T) (object) TransferMode;
262                         return base.GetProperty<T> (context);
263                 }
264
265 #if !NET_2_1
266                 void IPolicyExportExtension.ExportPolicy (
267                         MetadataExporter exporter,
268                         PolicyConversionContext context)
269                 {
270                         if (exporter == null)
271                                 throw new ArgumentNullException ("exporter");
272                         if (context == null)
273                                 throw new ArgumentNullException ("context");
274
275                         PolicyAssertionCollection assertions = context.GetBindingAssertions ();
276                         XmlDocument doc = new XmlDocument ();
277
278                         ExportAddressingPolicy (context);
279
280                         switch (auth_scheme) {
281                         case AuthenticationSchemes.Basic:
282                         case AuthenticationSchemes.Digest:
283                         case AuthenticationSchemes.Negotiate:
284                         case AuthenticationSchemes.Ntlm:
285                                 assertions.Add (doc.CreateElement ("http", 
286                                                 auth_scheme.ToString () + "Authentication", 
287                                                 "http://schemas.microsoft.com/ws/06/2004/policy/http"));
288                                 break;
289                         }
290
291                         var transportProvider = this as ITransportTokenAssertionProvider;
292                         if (transportProvider != null) {
293                                 var token = transportProvider.GetTransportTokenAssertion ();
294                                 assertions.Add (CreateTransportBinding (token));
295                         }
296                 }
297
298                 XmlElement CreateTransportBinding (XmlElement transportToken)
299                 {
300                         var doc = new XmlDocument ();
301                         var transportBinding = doc.CreateElement (
302                                 "sp", "TransportBinding", PolicyImportHelper.SecurityPolicyNS);
303
304                         var token = doc.CreateElement (
305                                 "sp", "TransportToken", PolicyImportHelper.SecurityPolicyNS);
306                         PolicyImportHelper.AddWrappedPolicyElement (token, transportToken);
307
308                         var algorithmSuite = doc.CreateElement (
309                                 "sp", "AlgorithmSuite", PolicyImportHelper.SecurityPolicyNS);
310                         var basic256 = doc.CreateElement (
311                                 "sp", "Basic256", PolicyImportHelper.SecurityPolicyNS);
312                         PolicyImportHelper.AddWrappedPolicyElement (algorithmSuite, basic256);
313
314                         var layout = doc.CreateElement (
315                                 "sp", "Layout", PolicyImportHelper.SecurityPolicyNS);
316                         var strict = doc.CreateElement (
317                                 "sp", "Strict", PolicyImportHelper.SecurityPolicyNS);
318                         PolicyImportHelper.AddWrappedPolicyElement (layout, strict);
319
320                         PolicyImportHelper.AddWrappedPolicyElements (
321                                 transportBinding, token, algorithmSuite, layout);
322
323                         return transportBinding;
324                 }
325
326                 [MonoTODO]
327                 void IWsdlExportExtension.ExportContract (WsdlExporter exporter,
328                         WsdlContractConversionContext context)
329                 {
330                         throw new NotImplementedException ();
331                 }
332
333                 [MonoTODO]
334                 void IWsdlExportExtension.ExportEndpoint (WsdlExporter exporter,
335                         WsdlEndpointConversionContext context)
336                 {
337                         var soap_binding = new WS.SoapBinding ();
338                         soap_binding.Transport = WS.SoapBinding.HttpTransport;
339                         soap_binding.Style = WS.SoapBindingStyle.Document;
340                         context.WsdlBinding.Extensions.Add (soap_binding);
341
342                         var soap_address = new WS.SoapAddressBinding ();
343                         soap_address.Location = context.Endpoint.Address.Uri.AbsoluteUri;
344                         
345                         context.WsdlPort.Extensions.Add (soap_address);
346                 }
347 #endif
348         }
349
350 #if !NET_2_1
351         class HttpBindingProperties : ISecurityCapabilities, IBindingDeliveryCapabilities
352         {
353                 HttpTransportBindingElement source;
354
355                 public HttpBindingProperties (HttpTransportBindingElement source)
356                 {
357                         this.source = source;
358                 }
359
360                 public bool AssuresOrderedDelivery {
361                         get { return false; }
362                 }
363
364                 public bool QueuedDelivery {
365                         get { return false; }
366                 }
367
368                 public virtual ProtectionLevel SupportedRequestProtectionLevel {
369                         get { return ProtectionLevel.None; }
370                 }
371
372                 public virtual ProtectionLevel SupportedResponseProtectionLevel {
373                         get { return ProtectionLevel.None; }
374                 }
375
376                 public virtual bool SupportsClientAuthentication {
377                         get { return source.AuthenticationScheme != AuthenticationSchemes.Anonymous; }
378                 }
379
380                 public virtual bool SupportsServerAuthentication {
381                         get {
382                                 switch (source.AuthenticationScheme) {
383                                 case AuthenticationSchemes.Negotiate:
384                                         return true;
385                                 default:
386                                         return false;
387                                 }
388                         }
389                 }
390
391                 public virtual bool SupportsClientWindowsIdentity {
392                         get {
393                                 switch (source.AuthenticationScheme) {
394                                 case AuthenticationSchemes.Basic:
395                                 case AuthenticationSchemes.Digest: // hmm... why? but they return true on .NET
396                                 case AuthenticationSchemes.Negotiate:
397                                 case AuthenticationSchemes.Ntlm:
398                                         return true;
399                                 default:
400                                         return false;
401                                 }
402                         }
403                 }
404         }
405 #endif
406 }