WebHttpBindingElement.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //
  2. // WebHttpBindingElement.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.Collections;
  30. using System.Collections.Generic;
  31. using System.Collections.ObjectModel;
  32. using System.ComponentModel;
  33. using System.Configuration;
  34. using System.Net;
  35. using System.Net.Security;
  36. using System.Reflection;
  37. using System.ServiceModel;
  38. using System.ServiceModel.Channels;
  39. using System.ServiceModel.Description;
  40. using System.ServiceModel.Diagnostics;
  41. using System.ServiceModel.Dispatcher;
  42. using System.Runtime.Serialization;
  43. using System.Text;
  44. using System.Xml;
  45. namespace System.ServiceModel.Configuration
  46. {
  47. public partial class WebHttpBindingElement
  48. : StandardBindingElement, IBindingConfigurationElement
  49. {
  50. // Static Fields
  51. static bool base_merged;
  52. static ConfigurationPropertyCollection properties;
  53. static ConfigurationProperty allow_cookies;
  54. static ConfigurationProperty bypass_proxy_on_local;
  55. static ConfigurationProperty host_name_comparison_mode;
  56. static ConfigurationProperty max_buffer_pool_size;
  57. static ConfigurationProperty max_buffer_size;
  58. static ConfigurationProperty max_received_message_size;
  59. static ConfigurationProperty proxy_address;
  60. static ConfigurationProperty reader_quotas;
  61. static ConfigurationProperty security;
  62. static ConfigurationProperty write_encoding;
  63. static ConfigurationProperty transfer_mode;
  64. static ConfigurationProperty use_default_web_proxy;
  65. static WebHttpBindingElement ()
  66. {
  67. properties = new ConfigurationPropertyCollection ();
  68. allow_cookies = new ConfigurationProperty ("allowCookies",
  69. typeof (bool), "false", new BooleanConverter (), null,
  70. ConfigurationPropertyOptions.None);
  71. bypass_proxy_on_local = new ConfigurationProperty ("bypassProxyOnLocal",
  72. typeof (bool), "false", new BooleanConverter (), null,
  73. ConfigurationPropertyOptions.None);
  74. host_name_comparison_mode = new ConfigurationProperty ("hostNameComparisonMode",
  75. typeof (HostNameComparisonMode), "StrongWildcard", null/* FIXME: get converter for HostNameComparisonMode*/, null,
  76. ConfigurationPropertyOptions.None);
  77. max_buffer_pool_size = new ConfigurationProperty ("maxBufferPoolSize",
  78. typeof (long), "524288", new Int64Converter (), null,
  79. ConfigurationPropertyOptions.None);
  80. max_buffer_size = new ConfigurationProperty ("maxBufferSize",
  81. typeof (int), "65536", new Int32Converter (), null,
  82. ConfigurationPropertyOptions.None);
  83. max_received_message_size = new ConfigurationProperty ("maxReceivedMessageSize",
  84. typeof (long), "65536", new Int64Converter (), null,
  85. ConfigurationPropertyOptions.None);
  86. proxy_address = new ConfigurationProperty ("proxyAddress",
  87. typeof (Uri), null, new UriTypeConverter (), null,
  88. ConfigurationPropertyOptions.None);
  89. reader_quotas = new ConfigurationProperty ("readerQuotas",
  90. typeof (XmlDictionaryReaderQuotasElement), null, null/* FIXME: get converter for XmlDictionaryReaderQuotasElement*/, null,
  91. ConfigurationPropertyOptions.None);
  92. security = new ConfigurationProperty ("security",
  93. typeof (WebHttpSecurityElement), null, null/* FIXME: get converter for WebHttpSecurityElement*/, null,
  94. ConfigurationPropertyOptions.None);
  95. write_encoding = new ConfigurationProperty ("writeEncoding",
  96. typeof (Encoding), "utf-8", new EncodingConverter (), null,
  97. ConfigurationPropertyOptions.None);
  98. transfer_mode = new ConfigurationProperty ("transferMode",
  99. typeof (TransferMode), "Buffered", null/* FIXME: get converter for TransferMode*/, null,
  100. ConfigurationPropertyOptions.None);
  101. use_default_web_proxy = new ConfigurationProperty ("useDefaultWebProxy",
  102. typeof (bool), "true", new BooleanConverter (), null,
  103. ConfigurationPropertyOptions.None);
  104. properties.Add (allow_cookies);
  105. properties.Add (bypass_proxy_on_local);
  106. properties.Add (host_name_comparison_mode);
  107. properties.Add (max_buffer_pool_size);
  108. properties.Add (max_buffer_size);
  109. properties.Add (max_received_message_size);
  110. properties.Add (proxy_address);
  111. properties.Add (reader_quotas);
  112. properties.Add (security);
  113. properties.Add (write_encoding);
  114. properties.Add (transfer_mode);
  115. properties.Add (use_default_web_proxy);
  116. }
  117. public WebHttpBindingElement ()
  118. {
  119. }
  120. public WebHttpBindingElement (string name)
  121. {
  122. this.Name = name;
  123. }
  124. // Properties
  125. [ConfigurationProperty ("allowCookies",
  126. DefaultValue = false,
  127. Options = ConfigurationPropertyOptions.None)]
  128. public bool AllowCookies {
  129. get { return (bool) base [allow_cookies]; }
  130. set { base [allow_cookies] = value; }
  131. }
  132. protected override Type BindingElementType {
  133. get { return typeof (WebHttpBinding); }
  134. }
  135. [ConfigurationProperty ("bypassProxyOnLocal",
  136. DefaultValue = false,
  137. Options = ConfigurationPropertyOptions.None)]
  138. public bool BypassProxyOnLocal {
  139. get { return (bool) base [bypass_proxy_on_local]; }
  140. set { base [bypass_proxy_on_local] = value; }
  141. }
  142. [ConfigurationProperty ("hostNameComparisonMode",
  143. DefaultValue = "StrongWildcard",
  144. Options = ConfigurationPropertyOptions.None)]
  145. public HostNameComparisonMode HostNameComparisonMode {
  146. get { return (HostNameComparisonMode) base [host_name_comparison_mode]; }
  147. set { base [host_name_comparison_mode] = value; }
  148. }
  149. [LongValidator ( MinValue = 0,
  150. MaxValue = 9223372036854775807,
  151. ExcludeRange = false)]
  152. [ConfigurationProperty ("maxBufferPoolSize",
  153. DefaultValue = "524288",
  154. Options = ConfigurationPropertyOptions.None)]
  155. public long MaxBufferPoolSize {
  156. get { return (long) base [max_buffer_pool_size]; }
  157. set { base [max_buffer_pool_size] = value; }
  158. }
  159. [IntegerValidator ( MinValue = 1,
  160. MaxValue = int.MaxValue,
  161. ExcludeRange = false)]
  162. [ConfigurationProperty ("maxBufferSize",
  163. DefaultValue = "65536",
  164. Options = ConfigurationPropertyOptions.None)]
  165. public int MaxBufferSize {
  166. get { return (int) base [max_buffer_size]; }
  167. set { base [max_buffer_size] = value; }
  168. }
  169. [LongValidator ( MinValue = 1,
  170. MaxValue = 9223372036854775807,
  171. ExcludeRange = false)]
  172. [ConfigurationProperty ("maxReceivedMessageSize",
  173. DefaultValue = "65536",
  174. Options = ConfigurationPropertyOptions.None)]
  175. public long MaxReceivedMessageSize {
  176. get { return (long) base [max_received_message_size]; }
  177. set { base [max_received_message_size] = value; }
  178. }
  179. protected override ConfigurationPropertyCollection Properties {
  180. get {
  181. if (!base_merged) {
  182. lock (properties) {
  183. base_merged = true;
  184. foreach (ConfigurationProperty p in base.Properties)
  185. properties.Add (p);
  186. }
  187. }
  188. return properties;
  189. }
  190. }
  191. [ConfigurationProperty ("proxyAddress",
  192. DefaultValue = null,
  193. Options = ConfigurationPropertyOptions.None)]
  194. public Uri ProxyAddress {
  195. get { return (Uri) base [proxy_address]; }
  196. set { base [proxy_address] = value; }
  197. }
  198. [ConfigurationProperty ("readerQuotas",
  199. Options = ConfigurationPropertyOptions.None)]
  200. public XmlDictionaryReaderQuotasElement ReaderQuotas {
  201. get { return (XmlDictionaryReaderQuotasElement) base [reader_quotas]; }
  202. }
  203. [ConfigurationProperty ("security",
  204. Options = ConfigurationPropertyOptions.None)]
  205. public WebHttpSecurityElement Security {
  206. get { return (WebHttpSecurityElement) base [security]; }
  207. }
  208. [TypeConverter ()]
  209. [ConfigurationProperty ("writeEncoding",
  210. DefaultValue = "utf-8",
  211. Options = ConfigurationPropertyOptions.None)]
  212. public Encoding WriteEncoding {
  213. get { return (Encoding) base [write_encoding]; }
  214. set { base [write_encoding] = value; }
  215. }
  216. [ConfigurationProperty ("transferMode",
  217. DefaultValue = "Buffered",
  218. Options = ConfigurationPropertyOptions.None)]
  219. public TransferMode TransferMode {
  220. get { return (TransferMode) base [transfer_mode]; }
  221. set { base [transfer_mode] = value; }
  222. }
  223. [ConfigurationProperty ("useDefaultWebProxy",
  224. DefaultValue = true,
  225. Options = ConfigurationPropertyOptions.None)]
  226. public bool UseDefaultWebProxy {
  227. get { return (bool) base [use_default_web_proxy]; }
  228. set { base [use_default_web_proxy] = value; }
  229. }
  230. protected override void OnApplyConfiguration (Binding binding)
  231. {
  232. WebHttpBinding webBinding = (WebHttpBinding)binding;
  233. webBinding.AllowCookies = AllowCookies;
  234. webBinding.BypassProxyOnLocal = BypassProxyOnLocal;
  235. webBinding.HostNameComparisonMode = HostNameComparisonMode;
  236. webBinding.MaxBufferPoolSize = MaxBufferPoolSize;
  237. webBinding.MaxBufferSize = MaxBufferSize;
  238. webBinding.MaxReceivedMessageSize = MaxReceivedMessageSize;
  239. if(ProxyAddress != null)
  240. webBinding.ProxyAddress = ProxyAddress;
  241. webBinding.TransferMode = TransferMode;
  242. webBinding.UseDefaultWebProxy = UseDefaultWebProxy;
  243. webBinding.WriteEncoding = WriteEncoding;
  244. Security.ApplyConfiguration (webBinding.Security);
  245. }
  246. protected internal override void InitializeFrom (Binding binding)
  247. {
  248. WebHttpBinding b = (WebHttpBinding)binding;
  249. AllowCookies = b.AllowCookies;
  250. BypassProxyOnLocal = b.BypassProxyOnLocal;
  251. HostNameComparisonMode = b.HostNameComparisonMode;
  252. MaxBufferPoolSize = b.MaxBufferPoolSize;
  253. MaxBufferSize = b.MaxBufferSize;
  254. MaxReceivedMessageSize = b.MaxReceivedMessageSize;
  255. if(ProxyAddress != null)
  256. ProxyAddress = b.ProxyAddress;
  257. TransferMode = b.TransferMode;
  258. UseDefaultWebProxy = b.UseDefaultWebProxy;
  259. WriteEncoding = b.WriteEncoding;
  260. Security.InitializeFrom (b.Security);
  261. }
  262. }
  263. }