e805b0fc0a63f73f12957ea3a10e97e57f7eda79
[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                 public HostNameComparisonMode HostNameComparisonMode {
95                         get { return host_name_comparison_mode; }
96                         set { host_name_comparison_mode = value; }
97                 }
98
99                 public long MaxBufferPoolSize {
100                         get { return max_buffer_pool_size; }
101                         set {
102                                 if (value <= 0)
103                                         throw new ArgumentOutOfRangeException ();
104                                 max_buffer_pool_size = value;
105                         }
106                 }
107
108                 public int MaxBufferSize {
109                         get { return max_buffer_size; }
110                         set {
111                                 if (value <= 0)
112                                         throw new ArgumentOutOfRangeException ();
113                                 max_buffer_size = value;
114                         }
115                 }
116
117                 public long MaxReceivedMessageSize {
118                         get { return max_recv_message_size; }
119                         set {
120                                 if (value <= 0)
121                                         throw new ArgumentOutOfRangeException ();
122                                 max_recv_message_size = value;
123                         }
124                 }
125
126                 public WSMessageEncoding MessageEncoding {
127                         get { return message_encoding; }
128                         set { message_encoding = value; }
129                 }
130
131                 public Uri ProxyAddress {
132                         get { return proxy_address; }
133                         set { proxy_address = value; }
134                 }
135
136                 public XmlDictionaryReaderQuotas ReaderQuotas {
137                         get { return reader_quotas; }
138                         set { reader_quotas = value; }
139                 }
140
141                 public override string Scheme {
142                         get {
143                                 switch (Security.Mode) {
144                                 case BasicHttpSecurityMode.Transport:
145                                 case BasicHttpSecurityMode.TransportWithMessageCredential:
146                                         return Uri.UriSchemeHttps;
147                                 default:
148                                         return Uri.UriSchemeHttp;
149                                 }
150                         }
151                 }
152
153                 public BasicHttpSecurity Security {
154                         get { return security; }
155                 }
156
157                 public EnvelopeVersion EnvelopeVersion {
158                         get { return env_version; }
159                 }
160
161                 public Encoding TextEncoding {
162                         get { return text_encoding; }
163                         set { text_encoding = value; }
164                 }
165
166                 public TransferMode TransferMode {
167                         get { return transfer_mode; }
168                         set { transfer_mode = value; }
169                 }
170
171                 public bool UseDefaultWebProxy {
172                         get { return use_default_web_proxy; }
173                         set { use_default_web_proxy = value; }
174                 }
175
176                 public override BindingElementCollection
177                         CreateBindingElements ()
178                 {
179                         switch (Security.Mode) {
180 #if !NET_2_1
181                         case BasicHttpSecurityMode.Message:
182                                 if (Security.Message.ClientCredentialType != BasicHttpMessageCredentialType.Certificate)
183                                         throw new InvalidOperationException ("When Message security is enabled in a BasicHttpBinding, the message security credential type must be BasicHttpMessageCredentialType.Certificate.");
184                                 goto case BasicHttpSecurityMode.TransportWithMessageCredential;
185                         case BasicHttpSecurityMode.TransportWithMessageCredential:
186                                 SecurityBindingElement sec;
187                                 if (Security.Message.ClientCredentialType != BasicHttpMessageCredentialType.Certificate)
188                                         // FIXME: pass proper security token parameters.
189                                         sec = SecurityBindingElement.CreateCertificateOverTransportBindingElement ();
190                                 else
191                                         sec = new AsymmetricSecurityBindingElement ();
192                                 return new BindingElementCollection (new BindingElement [] {sec, BuildMessageEncodingBindingElement (), GetTransport ()});
193 #endif
194                         default:
195                                 return new BindingElementCollection (new BindingElement [] {
196                                         BuildMessageEncodingBindingElement (),
197                                         GetTransport ()});
198                         }
199
200                 }
201
202                 MessageEncodingBindingElement BuildMessageEncodingBindingElement ()
203                 {
204                         if (MessageEncoding == WSMessageEncoding.Text) {
205                                 TextMessageEncodingBindingElement tm = new TextMessageEncodingBindingElement (
206                                         MessageVersion.CreateVersion (EnvelopeVersion, AddressingVersion.None), TextEncoding);
207 #if !NET_2_1
208                                 ReaderQuotas.CopyTo (tm.ReaderQuotas);
209 #endif
210                                 return tm;
211                         }
212                         else
213 #if NET_2_1
214                                 throw new SystemException ("INTERNAL ERROR: should not happen");
215 #else
216                                 return new MtomMessageEncodingBindingElement (
217                                         MessageVersion.CreateVersion (EnvelopeVersion, AddressingVersion.None), TextEncoding);
218 #endif
219                 }
220
221                 TransportBindingElement GetTransport ()
222                 {
223                         HttpTransportBindingElement h;
224                         switch (Security.Mode) {
225                         case BasicHttpSecurityMode.Transport:
226                         case BasicHttpSecurityMode.TransportWithMessageCredential:
227                                 h = new HttpsTransportBindingElement ();
228                                 break;
229                         default:
230                                 h = new HttpTransportBindingElement ();
231                                 break;
232                         }
233
234                         h.AllowCookies = AllowCookies;
235                         h.BypassProxyOnLocal = BypassProxyOnLocal;
236                         h.HostNameComparisonMode = HostNameComparisonMode;
237                         h.MaxBufferPoolSize = MaxBufferPoolSize;
238                         h.MaxBufferSize = MaxBufferSize;
239                         h.MaxReceivedMessageSize = MaxReceivedMessageSize;
240                         h.ProxyAddress = ProxyAddress;
241                         h.UseDefaultWebProxy = UseDefaultWebProxy;
242                         h.TransferMode = TransferMode;
243
244 #if !NET_2_1
245                         switch (Security.Mode) {
246                         case BasicHttpSecurityMode.Transport:
247                                 switch (Security.Transport.ClientCredentialType) {
248                                 case HttpClientCredentialType.Certificate:
249                                         var https = (HttpsTransportBindingElement) h;
250                                         https.RequireClientCertificate = true;
251                                         break;
252                                 }
253                                 break;
254                         case BasicHttpSecurityMode.TransportCredentialOnly:
255                                 switch (Security.Transport.ClientCredentialType) {
256                                 case HttpClientCredentialType.Basic:
257                                         h.AuthenticationScheme = AuthenticationSchemes.Basic;
258                                         break;
259                                 case HttpClientCredentialType.Ntlm:
260                                         h.AuthenticationScheme = AuthenticationSchemes.Ntlm;
261                                         break;
262                                 case HttpClientCredentialType.Windows:
263                                         h.AuthenticationScheme = AuthenticationSchemes.Negotiate;
264                                         break;
265                                 case HttpClientCredentialType.Digest:
266                                         h.AuthenticationScheme = AuthenticationSchemes.Digest;
267                                         break;
268                                 case HttpClientCredentialType.Certificate:
269                                         throw new InvalidOperationException ("Certificate-based client authentication is not supported by 'TransportCredentialOnly' mode.");
270                                 }
271                                 break;
272                         }
273 #endif
274
275                         return h;
276                 }
277
278                 // explicit interface implementations
279
280                 bool IBindingRuntimePreferences.ReceiveSynchronously {
281                         get { return false; }
282                 }
283         }
284 }