| | | 1 | | using System.ComponentModel; |
| | | 2 | | using System.Text; |
| | | 3 | | using System.Xml; |
| | | 4 | | using CoreWCF.Channels; |
| | | 5 | | |
| | | 6 | | namespace CoreWCF |
| | | 7 | | { |
| | | 8 | | public class WebHttpBinding : Binding |
| | | 9 | | { |
| | | 10 | | private HttpsTransportBindingElement _httpsTransportBindingElement; |
| | | 11 | | // private BindingElements |
| | | 12 | | private HttpTransportBindingElement _httpTransportBindingElement; |
| | 39 | 13 | | private WebHttpSecurity _security = new WebHttpSecurity(); |
| | | 14 | | private WebMessageEncodingBindingElement _webMessageEncodingBindingElement; |
| | | 15 | | |
| | 36 | 16 | | public WebHttpBinding() : base() |
| | | 17 | | { |
| | 36 | 18 | | Initialize(); |
| | 36 | 19 | | } |
| | | 20 | | |
| | 3 | 21 | | public WebHttpBinding(WebHttpSecurityMode securityMode) : base() |
| | | 22 | | { |
| | 3 | 23 | | Initialize(); |
| | 3 | 24 | | _security.Mode = securityMode; |
| | 3 | 25 | | } |
| | | 26 | | |
| | | 27 | | // This needs an update in the HttpTransportBindingElement to work. |
| | | 28 | | //[DefaultValue(false)] |
| | | 29 | | //public bool AllowCookies |
| | | 30 | | //{ |
| | | 31 | | // get { throw new PlatformNotSupportedException(); } |
| | | 32 | | // set { throw new PlatformNotSupportedException(); } |
| | | 33 | | //} |
| | | 34 | | |
| | 0 | 35 | | public EnvelopeVersion EnvelopeVersion => EnvelopeVersion.None; |
| | | 36 | | |
| | | 37 | | // This needs an update in the HttpTransportBindingElement to work. |
| | | 38 | | //[DefaultValue(HostNameComparisonMode.StrongWildcard)] |
| | | 39 | | //public HostNameComparisonMode HostNameComparisonMode |
| | | 40 | | //{ |
| | | 41 | | // get { throw new PlatformNotSupportedException(); } |
| | | 42 | | // set { throw new PlatformNotSupportedException(); } |
| | | 43 | | //} |
| | | 44 | | |
| | | 45 | | [DefaultValue(TransportDefaults.MaxBufferPoolSize)] |
| | | 46 | | public long MaxBufferPoolSize |
| | | 47 | | { |
| | 0 | 48 | | get { return _httpTransportBindingElement.MaxBufferPoolSize; } |
| | | 49 | | set |
| | | 50 | | { |
| | 2 | 51 | | _httpTransportBindingElement.MaxBufferPoolSize = value; |
| | 2 | 52 | | _httpsTransportBindingElement.MaxBufferPoolSize = value; |
| | 2 | 53 | | } |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | [DefaultValue(TransportDefaults.MaxBufferSize)] |
| | | 57 | | public int MaxBufferSize |
| | | 58 | | { |
| | 2 | 59 | | get { return _httpTransportBindingElement.MaxBufferSize; } |
| | | 60 | | set |
| | | 61 | | { |
| | 2 | 62 | | _httpTransportBindingElement.MaxBufferSize = value; |
| | 2 | 63 | | _httpsTransportBindingElement.MaxBufferSize = value; |
| | 2 | 64 | | } |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | [DefaultValue(TransportDefaults.MaxReceivedMessageSize)] |
| | | 68 | | public long MaxReceivedMessageSize |
| | | 69 | | { |
| | 2 | 70 | | get { return _httpTransportBindingElement.MaxReceivedMessageSize; } |
| | | 71 | | set |
| | | 72 | | { |
| | 2 | 73 | | _httpTransportBindingElement.MaxReceivedMessageSize = value; |
| | 2 | 74 | | _httpsTransportBindingElement.MaxReceivedMessageSize = value; |
| | 2 | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | public XmlDictionaryReaderQuotas ReaderQuotas |
| | | 79 | | { |
| | 1 | 80 | | get { return _webMessageEncodingBindingElement.ReaderQuotas; } |
| | | 81 | | set |
| | | 82 | | { |
| | 2 | 83 | | if (value == null) |
| | | 84 | | { |
| | 0 | 85 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value)); |
| | | 86 | | } |
| | | 87 | | |
| | 2 | 88 | | value.CopyTo(_webMessageEncodingBindingElement.ReaderQuotas); |
| | 2 | 89 | | } |
| | | 90 | | } |
| | | 91 | | |
| | 150 | 92 | | public override string Scheme => GetTransport().Scheme; |
| | | 93 | | |
| | | 94 | | public WebHttpSecurity Security |
| | | 95 | | { |
| | 4 | 96 | | get { return _security; } |
| | | 97 | | set |
| | | 98 | | { |
| | 6 | 99 | | if (value == null) |
| | 0 | 100 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value)); |
| | | 101 | | |
| | 6 | 102 | | _security = value; |
| | 6 | 103 | | } |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | [DefaultValue(HttpTransportDefaults.TransferMode)] |
| | | 107 | | public TransferMode TransferMode |
| | | 108 | | { |
| | 2 | 109 | | get { return _httpTransportBindingElement.TransferMode; } |
| | | 110 | | set |
| | | 111 | | { |
| | 2 | 112 | | _httpTransportBindingElement.TransferMode = value; |
| | 2 | 113 | | _httpsTransportBindingElement.TransferMode = value; |
| | 2 | 114 | | } |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | [TypeConverter(typeof(EncodingConverter))] |
| | | 118 | | public Encoding WriteEncoding |
| | | 119 | | { |
| | 0 | 120 | | get { return _webMessageEncodingBindingElement.WriteEncoding; } |
| | | 121 | | set |
| | | 122 | | { |
| | 2 | 123 | | _webMessageEncodingBindingElement.WriteEncoding = value; |
| | 2 | 124 | | } |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | public WebContentTypeMapper ContentTypeMapper |
| | | 128 | | { |
| | 0 | 129 | | get { return _webMessageEncodingBindingElement.ContentTypeMapper; } |
| | | 130 | | set |
| | | 131 | | { |
| | 2 | 132 | | _webMessageEncodingBindingElement.ContentTypeMapper = value; |
| | 2 | 133 | | } |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | public bool CrossDomainScriptAccessEnabled |
| | | 137 | | { |
| | 0 | 138 | | get { return _webMessageEncodingBindingElement.CrossDomainScriptAccessEnabled; } |
| | | 139 | | set |
| | | 140 | | { |
| | 0 | 141 | | _webMessageEncodingBindingElement.CrossDomainScriptAccessEnabled = value; |
| | 0 | 142 | | } |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | public override BindingElementCollection CreateBindingElements() |
| | | 146 | | { |
| | | 147 | | // return collection of BindingElements |
| | 627 | 148 | | BindingElementCollection bindingElements = new BindingElementCollection(); |
| | | 149 | | // order of BindingElements is important |
| | | 150 | | // add encoding |
| | 627 | 151 | | bindingElements.Add(_webMessageEncodingBindingElement); |
| | | 152 | | // add transport (http or https) |
| | 627 | 153 | | bindingElements.Add(GetTransport()); |
| | | 154 | | |
| | 627 | 155 | | return bindingElements.Clone(); |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | private TransportBindingElement GetTransport() |
| | | 159 | | { |
| | 777 | 160 | | if (_security.Mode == WebHttpSecurityMode.Transport) |
| | | 161 | | { |
| | 22 | 162 | | _security.EnableTransportSecurity(_httpsTransportBindingElement); |
| | 22 | 163 | | _security.ApplyAuthorizationPolicySupport(_httpsTransportBindingElement); |
| | 22 | 164 | | return _httpsTransportBindingElement; |
| | | 165 | | } |
| | 755 | 166 | | else if (_security.Mode == WebHttpSecurityMode.TransportCredentialOnly) |
| | | 167 | | { |
| | 124 | 168 | | _security.EnableTransportAuthentication(_httpTransportBindingElement); |
| | 124 | 169 | | _security.ApplyAuthorizationPolicySupport(_httpTransportBindingElement); |
| | 124 | 170 | | return _httpTransportBindingElement; |
| | | 171 | | } |
| | | 172 | | else |
| | | 173 | | { |
| | | 174 | | // ensure that there is no transport security |
| | 631 | 175 | | _security.DisableTransportAuthentication(_httpTransportBindingElement); |
| | 631 | 176 | | _security.ApplyAuthorizationPolicySupport(_httpTransportBindingElement); |
| | 631 | 177 | | return _httpTransportBindingElement; |
| | | 178 | | } |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | private void Initialize() |
| | | 182 | | { |
| | 39 | 183 | | _httpTransportBindingElement = new HttpTransportBindingElement |
| | 39 | 184 | | { |
| | 39 | 185 | | ManualAddressing = true |
| | 39 | 186 | | }; |
| | | 187 | | |
| | 39 | 188 | | _httpsTransportBindingElement = new HttpsTransportBindingElement |
| | 39 | 189 | | { |
| | 39 | 190 | | ManualAddressing = true |
| | 39 | 191 | | }; |
| | | 192 | | |
| | 39 | 193 | | _webMessageEncodingBindingElement = new WebMessageEncodingBindingElement |
| | 39 | 194 | | { |
| | 39 | 195 | | MessageVersion = MessageVersion.None |
| | 39 | 196 | | }; |
| | 39 | 197 | | } |
| | | 198 | | } |
| | | 199 | | } |