[runtime] Free rgctx_lazy_fetch_trampoline info
[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                 }
83
84                 public override BindingElementCollection
85                         CreateBindingElements ()
86                 {
87                         var list = new List<BindingElement> ();
88                         
89                         var security = CreateSecurityBindingElement ();
90                         if (security != null)
91                                 list.Add (security);
92
93                         list.Add (BuildMessageEncodingBindingElement ());
94                         list.Add (GetTransport ());
95
96                         return new BindingElementCollection (list.ToArray ());
97                 }
98                 
99                 SecurityBindingElement CreateSecurityBindingElement () 
100                 {
101                         SecurityBindingElement element;
102                         switch (Security.Mode) {
103                         case BasicHttpsSecurityMode.TransportWithMessageCredential:
104 #if NET_2_1 || XAMMAC_4_5
105
106                                 throw new NotImplementedException ();
107 #else
108                                 if (Security.Message.ClientCredentialType != BasicHttpMessageCredentialType.Certificate)
109                                         // FIXME: pass proper security token parameters.
110                                         element = SecurityBindingElement.CreateCertificateOverTransportBindingElement ();
111                                 else
112                                         element = new AsymmetricSecurityBindingElement ();
113                                 break;
114 #endif
115                         default: 
116                                 return null;
117                         }
118                         
119 #if !NET_2_1 && !XAMMAC_4_5
120                         element.SetKeyDerivation (false);
121                         element.SecurityHeaderLayout = SecurityHeaderLayout.Lax;
122 #endif
123                         return element;
124                 }
125
126                 MessageEncodingBindingElement BuildMessageEncodingBindingElement ()
127                 {
128                         if (MessageEncoding == WSMessageEncoding.Text) {
129                                 TextMessageEncodingBindingElement tm = new TextMessageEncodingBindingElement (
130                                         MessageVersion.CreateVersion (EnvelopeVersion, AddressingVersion.None), TextEncoding);
131                                 ReaderQuotas.CopyTo (tm.ReaderQuotas);
132                                 return tm;
133                         } else {
134 #if NET_2_1 || XAMMAC_4_5
135                                 throw new NotImplementedException ();
136 #else
137                                 return new MtomMessageEncodingBindingElement (
138                                         MessageVersion.CreateVersion (EnvelopeVersion, AddressingVersion.None), TextEncoding);
139 #endif
140                         }
141                 }
142
143                 TransportBindingElement GetTransport ()
144                 {
145                         HttpsTransportBindingElement h = new HttpsTransportBindingElement ();
146
147                         h.AllowCookies = AllowCookies;
148                         h.BypassProxyOnLocal = BypassProxyOnLocal;
149                         h.HostNameComparisonMode = HostNameComparisonMode;
150                         h.MaxBufferPoolSize = MaxBufferPoolSize;
151                         h.MaxBufferSize = MaxBufferSize;
152                         h.MaxReceivedMessageSize = MaxReceivedMessageSize;
153                         h.ProxyAddress = ProxyAddress;
154                         h.UseDefaultWebProxy = UseDefaultWebProxy;
155                         h.TransferMode = TransferMode;
156                         
157                         h.ExtendedProtectionPolicy = Security.Transport.ExtendedProtectionPolicy;
158
159                         switch (Security.Transport.ClientCredentialType) {
160                         case HttpClientCredentialType.Basic:
161                                 h.AuthenticationScheme = AuthenticationSchemes.Basic;
162                                 break;
163                         case HttpClientCredentialType.Ntlm:
164                                 h.AuthenticationScheme = AuthenticationSchemes.Ntlm;
165                                 break;
166                         case HttpClientCredentialType.Windows:
167                                 h.AuthenticationScheme = AuthenticationSchemes.Negotiate;
168                                 break;
169                         case HttpClientCredentialType.Digest:
170                                 h.AuthenticationScheme = AuthenticationSchemes.Digest;
171                                 break;
172                         case HttpClientCredentialType.Certificate:
173                                 h.RequireClientCertificate = true;
174                                 break;
175                         }
176
177                         return h;
178                 }
179         }
180 }