8eee88bb876ad8387abadae15f001784359e2f8f
[mono.git] / mcs / class / System.ServiceModel.Web / System.ServiceModel / WebHttpBinding.cs
1 //
2 // WebHttpBinding.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // Copyright (C) 2008 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.ComponentModel;
30 using System.Linq;
31 using System.ServiceModel.Channels;
32 using System.Text;
33 using System.Xml;
34 #if !NET_2_1
35 using System.Configuration;
36 using System.ServiceModel.Configuration;
37 #endif
38
39 namespace System.ServiceModel
40 {
41         public class WebHttpBinding : Binding, IBindingRuntimePreferences
42         {
43                 public WebHttpBinding ()
44                         : this (String.Empty)
45                 {
46                 }
47
48                 public WebHttpBinding (WebHttpSecurityMode mode)
49                 {
50                         Initialize (mode);
51                 }
52
53                 public WebHttpBinding (string configurationName)
54                 {
55 #if !NET_2_1 && !XAMMAC_4_5
56                         BindingsSection bindingsSection = ConfigUtil.BindingsSection;
57                         WebHttpBindingElement el = (WebHttpBindingElement) bindingsSection ["webHttpBinding"].ConfiguredBindings.FirstOrDefault (c => c.Name == configurationName);
58                         if (el != null) {
59                                 Initialize (el.Security.Mode); // to initialize Transport correctly.
60                                 el.ApplyConfiguration (this);
61                         }
62                         else if (!String.IsNullOrEmpty (configurationName))
63                                 throw new ConfigurationException (String.Format ("Specified webHttpBinding configuration '{0}' was not found", configurationName));
64                         else
65                                 Initialize (WebHttpSecurityMode.None);
66 #else
67                         Initialize (WebHttpSecurityMode.None);
68 #endif
69                 }
70
71                 void Initialize (WebHttpSecurityMode mode)
72                 {
73                         security.Mode = mode;
74                         // MSDN says that this security mode can be set only
75                         // at .ctor(), so there is no problem on depending on
76                         // this value here.
77                         t = mode == WebHttpSecurityMode.Transport ? new HttpsTransportBindingElement () : new HttpTransportBindingElement ();
78                         t.ManualAddressing = true;
79                 }
80
81                 WebHttpSecurity security = new WebHttpSecurity ();
82                 HttpTransportBindingElement t;
83                 // This can be changed only using <synchronousReceive> configuration element.
84                 bool receive_synchronously;
85                 WebMessageEncodingBindingElement msgenc = new WebMessageEncodingBindingElement ();
86
87                 public EnvelopeVersion EnvelopeVersion {
88                         get { return EnvelopeVersion.None; }
89                 }
90
91 #if !NET_2_1 && !XAMMAC_4_5
92                 [DefaultValue (false)]
93                 public bool AllowCookies {
94                         get { return t.AllowCookies; }
95                         set { t.AllowCookies = value; }
96                 }
97
98                 [DefaultValue (false)]
99                 public bool BypassProxyOnLocal {
100                         get { return t.BypassProxyOnLocal; }
101                         set { t.BypassProxyOnLocal = value; }
102                 }
103
104                 [MonoTODO]
105                 public bool CrossDomainScriptAccessEnabled { get; set; }
106
107                 public WebContentTypeMapper ContentTypeMapper {
108                         get { return msgenc.ContentTypeMapper; }
109                         set { msgenc.ContentTypeMapper = value; }
110                 }
111
112                 [DefaultValue (HostNameComparisonMode.StrongWildcard)]
113                 public HostNameComparisonMode HostNameComparisonMode {
114                         get { return t.HostNameComparisonMode; }
115                         set { t.HostNameComparisonMode = value; }
116                 }
117
118                 [DefaultValue (0x10000)]
119                 public long MaxBufferPoolSize {
120                         get { return t.MaxBufferPoolSize; }
121                         set { t.MaxBufferPoolSize = value; }
122                 }
123
124                 [DefaultValue (TransferMode.Buffered)]
125                 public TransferMode TransferMode {
126                         get { return t.TransferMode; }
127                         set { t.TransferMode = value; }
128                 }
129
130                 [DefaultValue (true)]
131                 public bool UseDefaultWebProxy {
132                         get { return t.UseDefaultWebProxy; }
133                         set { t.UseDefaultWebProxy = value; }
134                 }
135
136                 [DefaultValue (null)]
137                 public Uri ProxyAddress {
138                         get { return t.ProxyAddress; }
139                         set { t.ProxyAddress = value; }
140                 }
141 #endif
142
143                 [DefaultValue (0x80000)]
144                 public int MaxBufferSize {
145                         get { return t.MaxBufferSize; }
146                         set { t.MaxBufferSize = value; }
147                 }
148
149                 [DefaultValue (0x10000)]
150                 public long MaxReceivedMessageSize {
151                         get { return t.MaxReceivedMessageSize; }
152                         set { t.MaxReceivedMessageSize = value; }
153                 }
154
155                 public XmlDictionaryReaderQuotas ReaderQuotas {
156                         get { return msgenc.ReaderQuotas; }
157                         set { msgenc.ReaderQuotas = value; }
158                 }
159
160                 public override string Scheme {
161                         get { return Security.Mode == WebHttpSecurityMode.Transport ? Uri.UriSchemeHttps : Uri.UriSchemeHttp; }
162                 }
163
164                 public WebHttpSecurity Security {
165                         get { return security; }
166                         set {
167                                 if (value == null)
168                                         throw new ArgumentNullException ("value");
169                                 security = value;
170                         }
171                 }
172
173                 public Encoding WriteEncoding {
174                         get { return msgenc.WriteEncoding; }
175                         set {
176                                 if (value == null)
177                                         throw new ArgumentNullException ("value");
178                                 msgenc.WriteEncoding = value; 
179                         }
180                 }
181
182                 public override BindingElementCollection CreateBindingElements ()
183                 {
184                         return new BindingElementCollection (new BindingElement [] { msgenc, t.Clone () });
185                 }
186
187                 bool IBindingRuntimePreferences.ReceiveSynchronously {
188                         get { return receive_synchronously; }
189                 }
190
191                 [EditorBrowsable (EditorBrowsableState.Advanced)]
192                 public bool ShouldSerializeReaderQuotas ()
193                 {
194                         return false;
195                 }
196                 
197                 [EditorBrowsable (EditorBrowsableState.Advanced)]
198                 public bool ShouldSerializeSecurity ()
199                 {
200                         return false;
201                 }
202                 
203                 [EditorBrowsable (EditorBrowsableState.Advanced)]
204                 public bool ShouldSerializeWriteEncoding ()
205                 {
206                         return false;
207                 }
208         }
209 }