Merge pull request #146 from flutos/207ce0bccceec3ff7860ea986086fedb5a92d822
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel / BasicHttpBinding.cs
1 //
2 // BasicHttpBinding.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005-2006 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.Net;
31 using System.Net.Security;
32 using System.ServiceModel.Channels;
33 using System.ServiceModel.Description;
34 using System.Text;
35 using System.Xml;
36 using System.ServiceModel.Configuration;
37
38 namespace System.ServiceModel
39 {
40         public class BasicHttpBinding : Binding,
41                 IBindingRuntimePreferences
42         {
43                 bool allow_cookies, bypass_proxy_on_local;
44                 HostNameComparisonMode host_name_comparison_mode
45                         = HostNameComparisonMode.StrongWildcard;
46                 long max_buffer_pool_size = 0x80000;
47                 int max_buffer_size = 0x10000;
48                 long max_recv_message_size = 0x10000;
49                 WSMessageEncoding message_encoding
50                         = WSMessageEncoding.Text;
51                 Uri proxy_address;
52                 XmlDictionaryReaderQuotas reader_quotas
53                         = new XmlDictionaryReaderQuotas ();
54                 EnvelopeVersion env_version = EnvelopeVersion.Soap11;
55                 Encoding text_encoding = new UTF8Encoding ();
56                 TransferMode transfer_mode
57                          = TransferMode.Buffered;
58                 bool use_default_web_proxy = true;
59                 BasicHttpSecurity security;
60
61                 public BasicHttpBinding ()
62                         : this (BasicHttpSecurityMode.None)
63                 {
64                 }
65
66 #if !NET_2_1
67                 public BasicHttpBinding (string configurationName)
68                         : this ()
69                 {
70                         BindingsSection bindingsSection = ConfigUtil.BindingsSection;
71                         BasicHttpBindingElement el = 
72                                 bindingsSection.BasicHttpBinding.Bindings [configurationName];
73
74                         el.ApplyConfiguration (this);
75                 }
76 #endif
77
78                 public BasicHttpBinding (
79                         BasicHttpSecurityMode securityMode)
80                 {
81                         security = new BasicHttpSecurity (securityMode);
82                 }
83
84                 public bool AllowCookies {
85                         get { return allow_cookies; }
86                         set { allow_cookies = value; }
87                 }
88
89                 public bool BypassProxyOnLocal {
90                         get { return bypass_proxy_on_local; }
91                         set { bypass_proxy_on_local = value; }
92                 }
93
94 #if NET_2_1
95                 public bool EnableHttpCookieContainer { get; set; }
96 #endif
97
98                 public HostNameComparisonMode HostNameComparisonMode {
99                         get { return host_name_comparison_mode; }
100                         set { host_name_comparison_mode = value; }
101                 }
102
103                 public long MaxBufferPoolSize {
104                         get { return max_buffer_pool_size; }
105                         set {
106                                 if (value <= 0)
107                                         throw new ArgumentOutOfRangeException ();
108                                 max_buffer_pool_size = value;
109                         }
110                 }
111
112                 public int MaxBufferSize {
113                         get { return max_buffer_size; }
114                         set {
115                                 if (value <= 0)
116                                         throw new ArgumentOutOfRangeException ();
117                                 max_buffer_size = value;
118                         }
119                 }
120
121                 public long MaxReceivedMessageSize {
122                         get { return max_recv_message_size; }
123                         set {
124                                 if (value <= 0)
125                                         throw new ArgumentOutOfRangeException ();
126                                 max_recv_message_size = value;
127                         }
128                 }
129
130                 public WSMessageEncoding MessageEncoding {
131                         get { return message_encoding; }
132                         set { message_encoding = value; }
133                 }
134
135                 public Uri ProxyAddress {
136                         get { return proxy_address; }
137                         set { proxy_address = value; }
138                 }
139
140                 public XmlDictionaryReaderQuotas ReaderQuotas {
141                         get { return reader_quotas; }
142                         set { reader_quotas = value; }
143                 }
144
145                 public override string Scheme {
146                         get {
147                                 switch (Security.Mode) {
148                                 case BasicHttpSecurityMode.Transport:
149                                 case BasicHttpSecurityMode.TransportWithMessageCredential:
150                                         return Uri.UriSchemeHttps;
151                                 default:
152                                         return Uri.UriSchemeHttp;
153                                 }
154                         }
155                 }
156
157                 public BasicHttpSecurity Security {
158                         get { return security; }
159                 }
160
161                 public EnvelopeVersion EnvelopeVersion {
162                         get { return env_version; }
163                 }
164
165                 public Encoding TextEncoding {
166                         get { return text_encoding; }
167                         set { text_encoding = value; }
168                 }
169
170                 public TransferMode TransferMode {
171                         get { return transfer_mode; }
172                         set { transfer_mode = value; }
173                 }
174
175                 public bool UseDefaultWebProxy {
176                         get { return use_default_web_proxy; }
177                         set { use_default_web_proxy = value; }
178                 }
179
180                 public override BindingElementCollection
181                         CreateBindingElements ()
182                 {
183                         var list = new List<BindingElement> ();
184                         
185                         var security = CreateSecurityBindingElement ();
186                         if (security != null)
187                                 list.Add (security);
188
189 #if NET_2_1
190                         if (EnableHttpCookieContainer)
191                                 list.Add (new HttpCookieContainerBindingElement ());
192 #endif
193
194                         list.Add (BuildMessageEncodingBindingElement ());
195                         list.Add (GetTransport ());
196
197                         return new BindingElementCollection (list.ToArray ());
198                 }
199                 
200                 SecurityBindingElement CreateSecurityBindingElement () 
201                 {
202             SecurityBindingElement element;
203                         switch (Security.Mode) {
204 #if !NET_2_1
205                         case BasicHttpSecurityMode.Message:
206                                 if (Security.Message.ClientCredentialType != BasicHttpMessageCredentialType.Certificate)
207                                         throw new InvalidOperationException ("When Message security is enabled in a BasicHttpBinding, the message security credential type must be BasicHttpMessageCredentialType.Certificate.");
208                                 element = SecurityBindingElement.CreateMutualCertificateBindingElement (
209                                     MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10);
210                                 break;
211
212                         case BasicHttpSecurityMode.TransportWithMessageCredential:
213                                 if (Security.Message.ClientCredentialType != BasicHttpMessageCredentialType.Certificate)
214                                         // FIXME: pass proper security token parameters.
215                                         element = SecurityBindingElement.CreateCertificateOverTransportBindingElement ();
216                                 else
217                                         element = new AsymmetricSecurityBindingElement ();
218                                 break;
219 #endif
220                         default: 
221                                 return null;
222                         }
223
224 #if !NET_2_1
225                         element.SetKeyDerivation (false);
226                         element.SecurityHeaderLayout = SecurityHeaderLayout.Lax;
227 #endif
228                         return element;
229                 }
230
231                 MessageEncodingBindingElement BuildMessageEncodingBindingElement ()
232                 {
233                         if (MessageEncoding == WSMessageEncoding.Text) {
234                                 TextMessageEncodingBindingElement tm = new TextMessageEncodingBindingElement (
235                                         MessageVersion.CreateVersion (EnvelopeVersion, AddressingVersion.None), TextEncoding);
236 #if !NET_2_1
237                                 ReaderQuotas.CopyTo (tm.ReaderQuotas);
238 #endif
239                                 return tm;
240                         }
241                         else
242 #if NET_2_1
243                                 throw new SystemException ("INTERNAL ERROR: should not happen");
244 #else
245                                 return new MtomMessageEncodingBindingElement (
246                                         MessageVersion.CreateVersion (EnvelopeVersion, AddressingVersion.None), TextEncoding);
247 #endif
248                 }
249
250                 TransportBindingElement GetTransport ()
251                 {
252                         HttpTransportBindingElement h;
253                         switch (Security.Mode) {
254                         case BasicHttpSecurityMode.Transport:
255                         case BasicHttpSecurityMode.TransportWithMessageCredential:
256                                 h = new HttpsTransportBindingElement ();
257                                 break;
258                         default:
259                                 h = new HttpTransportBindingElement ();
260                                 break;
261                         }
262
263                         h.AllowCookies = AllowCookies;
264                         h.BypassProxyOnLocal = BypassProxyOnLocal;
265                         h.HostNameComparisonMode = HostNameComparisonMode;
266                         h.MaxBufferPoolSize = MaxBufferPoolSize;
267                         h.MaxBufferSize = MaxBufferSize;
268                         h.MaxReceivedMessageSize = MaxReceivedMessageSize;
269                         h.ProxyAddress = ProxyAddress;
270                         h.UseDefaultWebProxy = UseDefaultWebProxy;
271                         h.TransferMode = TransferMode;
272 #if NET_4_0
273                         h.ExtendedProtectionPolicy = Security.Transport.ExtendedProtectionPolicy;
274 #endif
275
276 #if !NET_2_1
277                         switch (Security.Mode) {
278                         case BasicHttpSecurityMode.Transport:
279                                 switch (Security.Transport.ClientCredentialType) {
280                                 case HttpClientCredentialType.Basic:
281                                         h.AuthenticationScheme = AuthenticationSchemes.Basic;
282                                         break;
283                                 case HttpClientCredentialType.Ntlm:
284                                         h.AuthenticationScheme = AuthenticationSchemes.Ntlm;
285                                         break;
286                                 case HttpClientCredentialType.Windows:
287                                         h.AuthenticationScheme = AuthenticationSchemes.Negotiate;
288                                         break;
289                                 case HttpClientCredentialType.Digest:
290                                         h.AuthenticationScheme = AuthenticationSchemes.Digest;
291                                         break;
292                                 case HttpClientCredentialType.Certificate:
293                                         var https = (HttpsTransportBindingElement) h;
294                                         https.RequireClientCertificate = true;
295                                         break;
296                                 }
297                                 break;
298                         case BasicHttpSecurityMode.TransportCredentialOnly:
299                                 switch (Security.Transport.ClientCredentialType) {
300                                 case HttpClientCredentialType.Basic:
301                                         h.AuthenticationScheme = AuthenticationSchemes.Basic;
302                                         break;
303                                 case HttpClientCredentialType.Ntlm:
304                                         h.AuthenticationScheme = AuthenticationSchemes.Ntlm;
305                                         break;
306                                 case HttpClientCredentialType.Windows:
307                                         h.AuthenticationScheme = AuthenticationSchemes.Negotiate;
308                                         break;
309                                 case HttpClientCredentialType.Digest:
310                                         h.AuthenticationScheme = AuthenticationSchemes.Digest;
311                                         break;
312                                 case HttpClientCredentialType.Certificate:
313                                         throw new InvalidOperationException ("Certificate-based client authentication is not supported by 'TransportCredentialOnly' mode.");
314                                 }
315                                 break;
316                         }
317 #endif
318
319                         return h;
320                 }
321
322                 // explicit interface implementations
323
324                 bool IBindingRuntimePreferences.ReceiveSynchronously {
325                         get { return false; }
326                 }
327         }
328 }