2
0

WebHttpBinding.cs 5.9 KB

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