File: WebHttpBinding.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (299 lines) | stat: -rw-r--r-- 10,844 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel
{
    using System;
    using System.Configuration;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Configuration;
    using System.ServiceModel.Description;
    using System.Text;
    using System.Xml;
    using System.ComponentModel;

    public class WebHttpBinding : Binding, IBindingRuntimePreferences
    {
        HttpsTransportBindingElement httpsTransportBindingElement;
        // private BindingElements
        HttpTransportBindingElement httpTransportBindingElement;
        WebHttpSecurity security = new WebHttpSecurity();
        WebMessageEncodingBindingElement webMessageEncodingBindingElement;

        public WebHttpBinding() : base()
        {
            Initialize();
        }

        public WebHttpBinding(string configurationName) : this()
        {
            ApplyConfiguration(configurationName);
        }

        public WebHttpBinding(WebHttpSecurityMode securityMode) : base()
        {
            Initialize();
            this.security.Mode = securityMode;
        }

        [DefaultValue(HttpTransportDefaults.AllowCookies)]
        public bool AllowCookies
        {
            get { return httpTransportBindingElement.AllowCookies; }
            set
            {
                httpTransportBindingElement.AllowCookies = value;
                httpsTransportBindingElement.AllowCookies = value;
            }
        }

        [DefaultValue(HttpTransportDefaults.BypassProxyOnLocal)]
        public bool BypassProxyOnLocal
        {
            get { return httpTransportBindingElement.BypassProxyOnLocal; }
            set
            {
                httpTransportBindingElement.BypassProxyOnLocal = value;
                httpsTransportBindingElement.BypassProxyOnLocal = value;
            }
        }

        public EnvelopeVersion EnvelopeVersion
        {
            get { return EnvelopeVersion.None; }
        }

        [DefaultValue(HttpTransportDefaults.HostNameComparisonMode)]
        public HostNameComparisonMode HostNameComparisonMode
        {
            get { return httpTransportBindingElement.HostNameComparisonMode; }
            set
            {
                httpTransportBindingElement.HostNameComparisonMode = value;
                httpsTransportBindingElement.HostNameComparisonMode = value;
            }
        }

        [DefaultValue(TransportDefaults.MaxBufferPoolSize)]
        public long MaxBufferPoolSize
        {
            get { return httpTransportBindingElement.MaxBufferPoolSize; }
            set
            {
                httpTransportBindingElement.MaxBufferPoolSize = value;
                httpsTransportBindingElement.MaxBufferPoolSize = value;
            }
        }

        [DefaultValue(TransportDefaults.MaxBufferSize)]
        public int MaxBufferSize
        {
            get { return httpTransportBindingElement.MaxBufferSize; }
            set
            {
                httpTransportBindingElement.MaxBufferSize = value;
                httpsTransportBindingElement.MaxBufferSize = value;
            }
        }

        [DefaultValue(TransportDefaults.MaxReceivedMessageSize)]
        public long MaxReceivedMessageSize
        {
            get { return httpTransportBindingElement.MaxReceivedMessageSize; }
            set
            {
                httpTransportBindingElement.MaxReceivedMessageSize = value;
                httpsTransportBindingElement.MaxReceivedMessageSize = value;
            }
        }

        [DefaultValue(HttpTransportDefaults.ProxyAddress)]
        public Uri ProxyAddress
        {
            get { return httpTransportBindingElement.ProxyAddress; }
            set
            {
                httpTransportBindingElement.ProxyAddress = value;
                httpsTransportBindingElement.ProxyAddress = value;
            }
        }

        public XmlDictionaryReaderQuotas ReaderQuotas
        {
            get { return webMessageEncodingBindingElement.ReaderQuotas; }
            set
            {
                if (value == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
                }
                value.CopyTo(webMessageEncodingBindingElement.ReaderQuotas);
            }
        }

        public override string Scheme
        { get { return GetTransport().Scheme; } }

        public WebHttpSecurity Security
        {
            get { return this.security; }
            set
            {
                if (value == null)
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
                this.security = value;
            }
        }

        [DefaultValue(HttpTransportDefaults.TransferMode)]
        public TransferMode TransferMode
        {
            get { return httpTransportBindingElement.TransferMode; }
            set
            {
                httpTransportBindingElement.TransferMode = value;
                httpsTransportBindingElement.TransferMode = value;
            }
        }

        [DefaultValue(HttpTransportDefaults.UseDefaultWebProxy)]
        public bool UseDefaultWebProxy
        {
            get { return httpTransportBindingElement.UseDefaultWebProxy; }
            set
            {
                httpTransportBindingElement.UseDefaultWebProxy = value;
                httpsTransportBindingElement.UseDefaultWebProxy = value;
            }
        }

        [TypeConverter(typeof(EncodingConverter))]
        public Encoding WriteEncoding
        {
            get { return webMessageEncodingBindingElement.WriteEncoding; }
            set
            {
                webMessageEncodingBindingElement.WriteEncoding = value;
            }
        }

        public WebContentTypeMapper ContentTypeMapper
        {
            get { return webMessageEncodingBindingElement.ContentTypeMapper; }
            set
            {
                webMessageEncodingBindingElement.ContentTypeMapper = value;
            }
        }

        public bool CrossDomainScriptAccessEnabled
        {
            get { return webMessageEncodingBindingElement.CrossDomainScriptAccessEnabled; }
            set
            {
                webMessageEncodingBindingElement.CrossDomainScriptAccessEnabled = value;
            }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")] // [....], This is the pattern we use on the standard bindings in Indigo V1
        bool IBindingRuntimePreferences.ReceiveSynchronously
        {
            get { return false; }
        }

        public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingParameterCollection parameters)
        {
            if ((security.Mode == WebHttpSecurityMode.Transport ||
                security.Mode == WebHttpSecurityMode.TransportCredentialOnly) &&
                security.Transport.ClientCredentialType == HttpClientCredentialType.InheritedFromHost)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.HttpClientCredentialTypeInvalid, security.Transport.ClientCredentialType)));
            }

            return base.BuildChannelFactory<TChannel>(parameters);
        }

        public override BindingElementCollection CreateBindingElements()
        {
            // return collection of BindingElements
            BindingElementCollection bindingElements = new BindingElementCollection();
            // order of BindingElements is important
            // add encoding 
            bindingElements.Add(webMessageEncodingBindingElement);
            // add transport (http or https)
            bindingElements.Add(GetTransport());

            return bindingElements.Clone();
        }

        void ApplyConfiguration(string configurationName)
        {
            WebHttpBindingCollectionElement section = WebHttpBindingCollectionElement.GetBindingCollectionElement();
            WebHttpBindingElement element = section.Bindings[configurationName];
            if (element == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(
                    SR2.GetString(SR2.ConfigInvalidBindingConfigurationName,
                    configurationName,
                    WebHttpBindingConfigurationStrings.WebHttpBindingCollectionElementName)));
            }
            else
            {
                element.ApplyConfiguration(this);
            }
        }

        TransportBindingElement GetTransport()
        {
            if (security.Mode == WebHttpSecurityMode.Transport)
            {
                security.EnableTransportSecurity(httpsTransportBindingElement);
                return httpsTransportBindingElement;
            }
            else if (security.Mode == WebHttpSecurityMode.TransportCredentialOnly)
            {
                security.EnableTransportAuthentication(httpTransportBindingElement);
                return httpTransportBindingElement;
            }
            else
            {
                // ensure that there is no transport security
                security.DisableTransportAuthentication(httpTransportBindingElement);
                return httpTransportBindingElement;
            }
        }

        void Initialize()
        {
            httpTransportBindingElement = new HttpTransportBindingElement();
            httpsTransportBindingElement = new HttpsTransportBindingElement();
            httpTransportBindingElement.ManualAddressing = true;
            httpsTransportBindingElement.ManualAddressing = true;
            webMessageEncodingBindingElement = new WebMessageEncodingBindingElement();
            webMessageEncodingBindingElement.MessageVersion = MessageVersion.None;
        }

        internal static class WebHttpBindingConfigurationStrings
        {
            internal const string WebHttpBindingCollectionElementName = "webHttpBinding";
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        public bool ShouldSerializeReaderQuotas()
        {
            return (!EncoderDefaults.IsDefaultReaderQuotas(this.ReaderQuotas));
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        public bool ShouldSerializeWriteEncoding()
        {
            return (this.WriteEncoding != TextEncoderDefaults.Encoding);
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        public bool ShouldSerializeSecurity()
        {
            return Security.InternalShouldSerialize();
        }
    }
}