Merge pull request #3240 from alexanderkyte/aot_compiler_leaks
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel / BasicHttpsBinding.cs
1 //
2 // BasicHttpsBinding.cs
3 //
4 // Authors:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //      Martin Baulig <martin.baulig@xamarin.com>
7 //
8 // Copyright (C) 2005-2006 Novell, Inc.  http://www.novell.com
9 // Copyright 2011-2012 Xamarin Inc (http://www.xamarin.com).
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 using System;
31 using System.Collections.Generic;
32 using System.Net;
33 using System.Net.Security;
34 using System.ServiceModel.Channels;
35 using System.ServiceModel.Description;
36 using System.Text;
37 using System.Xml;
38 using System.ServiceModel.Configuration;
39
40 namespace System.ServiceModel
41 {
42         public class BasicHttpsBinding : HttpBindingBase,
43                 IBindingRuntimePreferences
44         {
45                 WSMessageEncoding message_encoding = WSMessageEncoding.Text;
46                 BasicHttpsSecurity security;
47
48                 public BasicHttpsBinding ()
49                         : this (BasicHttpsSecurityMode.Transport)
50                 {
51                 }
52                 
53 #if !NET_2_1 && !XAMMAC_4_5
54                 public BasicHttpsBinding (string configurationName)
55                         : this ()
56                 {
57                         BindingsSection bindingsSection = ConfigUtil.BindingsSection;
58                         BasicHttpsBindingElement el = 
59                                 bindingsSection.BasicHttpsBinding.Bindings [configurationName];
60
61                         el.ApplyConfiguration (this);
62                 }
63 #endif
64
65                 public BasicHttpsBinding (
66                         BasicHttpsSecurityMode securityMode)
67                 {
68                         security = new BasicHttpsSecurity (securityMode);
69                 }
70
71                 public WSMessageEncoding MessageEncoding {
72                         get { return message_encoding; }
73                         set { message_encoding = value; }
74                 }
75
76                 public override string Scheme {
77                         get { return Uri.UriSchemeHttps; }
78                 }
79
80                 public BasicHttpsSecurity Security {
81                         get { return security; }
82                         set { security = value; }
83                 }
84
85                 public override BindingElementCollection
86                         CreateBindingElements ()
87                 {
88                         var list = new List<BindingElement> ();
89                         
90                         var security = CreateSecurityBindingElement ();
91                         if (security != null)
92                                 list.Add (security);
93
94                         list.Add (BuildMessageEncodingBindingElement ());
95                         list.Add (GetTransport ());
96
97                         return new BindingElementCollection (list.ToArray ());
98                 }
99                 
100                 SecurityBindingElement CreateSecurityBindingElement () 
101                 {
102                         SecurityBindingElement element;
103                         switch (Security.Mode) {
104                         case BasicHttpsSecurityMode.TransportWithMessageCredential:
105 #if NET_2_1 || XAMMAC_4_5
106
107                                 throw new NotImplementedException ();
108 #else
109                                 if (Security.Message.ClientCredentialType != BasicHttpMessageCredentialType.Certificate)
110                                         // FIXME: pass proper security token parameters.
111                                         element = SecurityBindingElement.CreateCertificateOverTransportBindingElement ();
112                                 else
113                                         element = new AsymmetricSecurityBindingElement ();
114                                 break;
115 #endif
116                         default: 
117                                 return null;
118                         }
119                         
120 #if !NET_2_1 && !XAMMAC_4_5
121                         element.SetKeyDerivation (false);
122                         element.SecurityHeaderLayout = SecurityHeaderLayout.Lax;
123 #endif
124                         return element;
125                 }
126
127                 MessageEncodingBindingElement BuildMessageEncodingBindingElement ()
128                 {
129                         if (MessageEncoding == WSMessageEncoding.Text) {
130                                 TextMessageEncodingBindingElement tm = new TextMessageEncodingBindingElement (
131                                         MessageVersion.CreateVersion (EnvelopeVersion, AddressingVersion.None), TextEncoding);
132                                 ReaderQuotas.CopyTo (tm.ReaderQuotas);
133                                 return tm;
134                         } else {
135 #if NET_2_1 || XAMMAC_4_5
136                                 throw new NotImplementedException ();
137 #else
138                                 return new MtomMessageEncodingBindingElement (
139                                         MessageVersion.CreateVersion (EnvelopeVersion, AddressingVersion.None), TextEncoding);
140 #endif
141                         }
142                 }
143
144                 TransportBindingElement GetTransport ()
145                 {
146                         HttpsTransportBindingElement h = new HttpsTransportBindingElement ();
147
148                         h.AllowCookies = AllowCookies;
149                         h.BypassProxyOnLocal = BypassProxyOnLocal;
150                         h.HostNameComparisonMode = HostNameComparisonMode;
151                         h.MaxBufferPoolSize = MaxBufferPoolSize;
152                         h.MaxBufferSize = MaxBufferSize;
153                         h.MaxReceivedMessageSize = MaxReceivedMessageSize;
154                         h.ProxyAddress = ProxyAddress;
155                         h.UseDefaultWebProxy = UseDefaultWebProxy;
156                         h.TransferMode = TransferMode;
157                         
158                         h.ExtendedProtectionPolicy = Security.Transport.ExtendedProtectionPolicy;
159
160                         switch (Security.Transport.ClientCredentialType) {
161                         case HttpClientCredentialType.Basic:
162                                 h.AuthenticationScheme = AuthenticationSchemes.Basic;
163                                 break;
164                         case HttpClientCredentialType.Ntlm:
165                                 h.AuthenticationScheme = AuthenticationSchemes.Ntlm;
166                                 break;
167                         case HttpClientCredentialType.Windows:
168                                 h.AuthenticationScheme = AuthenticationSchemes.Negotiate;
169                                 break;
170                         case HttpClientCredentialType.Digest:
171                                 h.AuthenticationScheme = AuthenticationSchemes.Digest;
172                                 break;
173                         case HttpClientCredentialType.Certificate:
174                                 h.RequireClientCertificate = true;
175                                 break;
176                         }
177
178                         return h;
179                 }
180         }
181 }