Merge pull request #495 from nicolas-raoul/fix-for-issue2907-with-no-formatting-changes
[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 #if !MOONLIGHT
61                 AuthenticationSchemes auth_scheme =
62                         AuthenticationSchemes.Anonymous;
63                 AuthenticationSchemes proxy_auth_scheme =
64                         AuthenticationSchemes.Anonymous;
65 #endif
66                 // If you add fields, do not forget them in copy constructor.
67
68                 public HttpTransportBindingElement ()
69                 {
70                 }
71
72                 protected HttpTransportBindingElement (
73                         HttpTransportBindingElement other)
74                         : base (other)
75                 {
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;
84                         realm = other.realm;
85                         transfer_mode = other.transfer_mode;
86                         // FIXME: it does not look safe
87                         timeouts = other.timeouts;
88 #if !MOONLIGHT
89                         auth_scheme = other.auth_scheme;
90                         proxy_auth_scheme = other.proxy_auth_scheme;
91 #endif
92
93 #if NET_4_0
94                         DecompressionEnabled = other.DecompressionEnabled;
95                         LegacyExtendedProtectionPolicy = other.LegacyExtendedProtectionPolicy;
96                         ExtendedProtectionPolicy = other.ExtendedProtectionPolicy;
97 #endif
98                 }
99
100 #if !MOONLIGHT
101 #if NET_4_0
102                 [DefaultValue (AuthenticationSchemes.Anonymous)]
103 #endif
104                 public AuthenticationSchemes AuthenticationScheme {
105                         get { return auth_scheme; }
106                         set { auth_scheme = value; }
107                 }
108
109 #if NET_4_0
110                 [DefaultValue (AuthenticationSchemes.Anonymous)]
111 #endif
112                 public AuthenticationSchemes ProxyAuthenticationScheme {
113                         get { return proxy_auth_scheme; }
114                         set { proxy_auth_scheme = value; }
115                 }
116 #endif
117
118 #if NET_4_0
119                 [DefaultValue (false)]
120 #endif
121                 public bool AllowCookies {
122                         get { return allow_cookies; }
123                         set { allow_cookies = value; }
124                 }
125
126 #if NET_4_0
127                 [DefaultValue (false)]
128 #endif
129                 public bool BypassProxyOnLocal {
130                         get { return bypass_proxy_on_local; }
131                         set { bypass_proxy_on_local = value; }
132                 }
133
134 #if NET_4_0
135                 [DefaultValue (false)]
136                 [MonoTODO]
137                 public bool DecompressionEnabled { get; set; }
138 #endif
139
140 #if NET_4_0
141                 [DefaultValue (HostNameComparisonMode.StrongWildcard)]
142 #endif
143                 public HostNameComparisonMode HostNameComparisonMode {
144                         get { return host_cmp_mode; }
145                         set { host_cmp_mode = value; }
146                 }
147
148 #if NET_4_0
149                 [DefaultValue (true)]
150 #endif
151                 public bool KeepAliveEnabled {
152                         get { return keep_alive_enabled; }
153                         set { keep_alive_enabled = value; }
154                 }
155
156 #if NET_4_0
157                 [DefaultValue (0x10000)]
158 #endif
159                 public int MaxBufferSize {
160                         get { return max_buffer_size; }
161                         set { max_buffer_size = value; }
162                 }
163
164 #if NET_4_0
165                 [DefaultValue (null)]
166                 [TypeConverter (typeof (UriTypeConverter))]
167 #endif
168                 public Uri ProxyAddress {
169                         get { return proxy_address; }
170                         set { proxy_address = value; }
171                 }
172
173 #if NET_4_0
174                 [DefaultValue ("")]
175 #endif
176                 public string Realm {
177                         get { return realm; }
178                         set { realm = value; }
179                 }
180
181                 public override string Scheme {
182                         get { return Uri.UriSchemeHttp; }
183                 }
184
185 #if NET_4_0
186                 [DefaultValue (TransferMode.Buffered)]
187 #endif
188                 public TransferMode TransferMode {
189                         get { return transfer_mode; }
190                         set { transfer_mode = value; }
191                 }
192
193 #if NET_4_0
194                 [DefaultValue (false)]
195 #endif
196                 public bool UnsafeConnectionNtlmAuthentication {
197                         get { return unsafe_ntlm_auth; }
198                         set { unsafe_ntlm_auth = value; }
199                 }
200
201 #if NET_4_0
202                 [DefaultValue (true)]
203 #endif
204                 public bool UseDefaultWebProxy {
205                         get { return use_default_proxy; }
206                         set { use_default_proxy = value; }
207                 }
208
209 #if NET_4_0
210                 [Obsolete ("Use ExtendedProtectionPolicy")]
211                 [MonoTODO]
212                 public object LegacyExtendedProtectionPolicy { get; set; }
213
214                 [MonoTODO]
215                 public ExtendedProtectionPolicy ExtendedProtectionPolicy { get; set; }
216 #endif
217
218                 public override bool CanBuildChannelFactory<TChannel> (
219                         BindingContext context)
220                 {
221                         return typeof (TChannel) == typeof (IRequestChannel);
222                 }
223
224 #if !NET_2_1
225                 public override bool CanBuildChannelListener<TChannel> (
226                         BindingContext context)
227                 {
228                         return typeof (TChannel) == typeof (IReplyChannel);
229                 }
230 #endif
231
232                 public override IChannelFactory<TChannel> BuildChannelFactory<TChannel> (
233                         BindingContext context)
234                 {
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);
238                 }
239
240 #if !NET_2_1
241                 internal static object ListenerBuildLock = new object ();
242
243                 public override IChannelListener<TChannel> BuildChannelListener<TChannel> (
244                         BindingContext context)
245                 {
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);
249                 }
250 #endif
251
252                 public override BindingElement Clone ()
253                 {
254                         return new HttpTransportBindingElement (this);
255                 }
256
257                 public override T GetProperty<T> (BindingContext context)
258                 {
259                         // http://blogs.msdn.com/drnick/archive/2007/04/10/interfaces-for-getproperty-part-1.aspx
260 #if !NET_2_1
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);
265 #endif
266                         if (typeof (T) == typeof (TransferMode))
267                                 return (T) (object) TransferMode;
268                         return base.GetProperty<T> (context);
269                 }
270
271 #if !NET_2_1
272                 void IPolicyExportExtension.ExportPolicy (
273                         MetadataExporter exporter,
274                         PolicyConversionContext context)
275                 {
276                         if (exporter == null)
277                                 throw new ArgumentNullException ("exporter");
278                         if (context == null)
279                                 throw new ArgumentNullException ("context");
280
281                         PolicyAssertionCollection assertions = context.GetBindingAssertions ();
282                         XmlDocument doc = new XmlDocument ();
283
284                         ExportAddressingPolicy (context);
285
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"));
294                                 break;
295                         }
296
297                         var transportProvider = this as ITransportTokenAssertionProvider;
298                         if (transportProvider != null) {
299                                 var token = transportProvider.GetTransportTokenAssertion ();
300                                 assertions.Add (CreateTransportBinding (token));
301                         }
302                 }
303
304                 XmlElement CreateTransportBinding (XmlElement transportToken)
305                 {
306                         var doc = new XmlDocument ();
307                         var transportBinding = doc.CreateElement (
308                                 "sp", "TransportBinding", PolicyImportHelper.SecurityPolicyNS);
309
310                         var token = doc.CreateElement (
311                                 "sp", "TransportToken", PolicyImportHelper.SecurityPolicyNS);
312                         PolicyImportHelper.AddWrappedPolicyElement (token, transportToken);
313
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);
319
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);
325
326                         PolicyImportHelper.AddWrappedPolicyElements (
327                                 transportBinding, token, algorithmSuite, layout);
328
329                         return transportBinding;
330                 }
331
332                 [MonoTODO]
333                 void IWsdlExportExtension.ExportContract (WsdlExporter exporter,
334                         WsdlContractConversionContext context)
335                 {
336                         throw new NotImplementedException ();
337                 }
338
339                 [MonoTODO]
340                 void IWsdlExportExtension.ExportEndpoint (WsdlExporter exporter,
341                         WsdlEndpointConversionContext context)
342                 {
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);
347
348                         var soap_address = new WS.SoapAddressBinding ();
349                         soap_address.Location = context.Endpoint.Address.Uri.AbsoluteUri;
350                         
351                         context.WsdlPort.Extensions.Add (soap_address);
352                 }
353 #endif
354         }
355
356 #if !NET_2_1
357         class HttpBindingProperties : ISecurityCapabilities, IBindingDeliveryCapabilities
358         {
359                 HttpTransportBindingElement source;
360
361                 public HttpBindingProperties (HttpTransportBindingElement source)
362                 {
363                         this.source = source;
364                 }
365
366                 public bool AssuresOrderedDelivery {
367                         get { return false; }
368                 }
369
370                 public bool QueuedDelivery {
371                         get { return false; }
372                 }
373
374                 public virtual ProtectionLevel SupportedRequestProtectionLevel {
375                         get { return ProtectionLevel.None; }
376                 }
377
378                 public virtual ProtectionLevel SupportedResponseProtectionLevel {
379                         get { return ProtectionLevel.None; }
380                 }
381
382                 public virtual bool SupportsClientAuthentication {
383                         get { return source.AuthenticationScheme != AuthenticationSchemes.Anonymous; }
384                 }
385
386                 public virtual bool SupportsServerAuthentication {
387                         get {
388                                 switch (source.AuthenticationScheme) {
389                                 case AuthenticationSchemes.Negotiate:
390                                         return true;
391                                 default:
392                                         return false;
393                                 }
394                         }
395                 }
396
397                 public virtual bool SupportsClientWindowsIdentity {
398                         get {
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:
404                                         return true;
405                                 default:
406                                         return false;
407                                 }
408                         }
409                 }
410         }
411 #endif
412 }