HttpTransportBindingElement.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // HttpTransportBindingElement.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005 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.Generic;
  30. using System.Net;
  31. using System.Net.Security;
  32. using System.ServiceModel.Channels;
  33. using System.ServiceModel.Description;
  34. namespace System.ServiceModel.Channels
  35. {
  36. [MonoTODO]
  37. public class HttpTransportBindingElement : TransportBindingElement,
  38. IPolicyExportExtension, IWsdlExportExtension
  39. {
  40. bool allow_cookies, bypass_proxy_on_local,
  41. unsafe_ntlm_auth;
  42. bool use_default_proxy = true, keep_alive_enabled = true;
  43. int max_buffer_size = 0x10000;
  44. HostNameComparisonMode host_cmp_mode;
  45. Uri proxy_address;
  46. string realm = String.Empty;
  47. TransferMode transfer_mode;
  48. IDefaultCommunicationTimeouts timeouts;
  49. #if !MOONLIGHT
  50. AuthenticationSchemes auth_scheme =
  51. AuthenticationSchemes.Anonymous;
  52. AuthenticationSchemes proxy_auth_scheme =
  53. AuthenticationSchemes.Anonymous;
  54. #endif
  55. // If you add fields, do not forget them in copy constructor.
  56. public HttpTransportBindingElement ()
  57. {
  58. }
  59. protected HttpTransportBindingElement (
  60. HttpTransportBindingElement other)
  61. : base (other)
  62. {
  63. allow_cookies = other.allow_cookies;
  64. bypass_proxy_on_local = other.bypass_proxy_on_local;
  65. unsafe_ntlm_auth = other.unsafe_ntlm_auth;
  66. use_default_proxy = other.use_default_proxy;
  67. keep_alive_enabled = other.keep_alive_enabled;
  68. max_buffer_size = other.max_buffer_size;
  69. host_cmp_mode = other.host_cmp_mode;
  70. proxy_address = other.proxy_address;
  71. realm = other.realm;
  72. transfer_mode = other.transfer_mode;
  73. // FIXME: it does not look safe
  74. timeouts = other.timeouts;
  75. #if !MOONLIGHT
  76. auth_scheme = other.auth_scheme;
  77. proxy_auth_scheme = other.proxy_auth_scheme;
  78. #endif
  79. }
  80. #if !MOONLIGHT
  81. public AuthenticationSchemes AuthenticationScheme {
  82. get { return auth_scheme; }
  83. set { auth_scheme = value; }
  84. }
  85. public AuthenticationSchemes ProxyAuthenticationScheme {
  86. get { return proxy_auth_scheme; }
  87. set { proxy_auth_scheme = value; }
  88. }
  89. #endif
  90. public bool AllowCookies {
  91. get { return allow_cookies; }
  92. set { allow_cookies = value; }
  93. }
  94. public bool BypassProxyOnLocal {
  95. get { return bypass_proxy_on_local; }
  96. set { bypass_proxy_on_local = value; }
  97. }
  98. public HostNameComparisonMode HostNameComparisonMode {
  99. get { return host_cmp_mode; }
  100. set { host_cmp_mode = value; }
  101. }
  102. public bool KeepAliveEnabled {
  103. get { return keep_alive_enabled; }
  104. set { keep_alive_enabled = value; }
  105. }
  106. public int MaxBufferSize {
  107. get { return max_buffer_size; }
  108. set { max_buffer_size = value; }
  109. }
  110. public Uri ProxyAddress {
  111. get { return proxy_address; }
  112. set { proxy_address = value; }
  113. }
  114. public string Realm {
  115. get { return realm; }
  116. set { realm = value; }
  117. }
  118. public override string Scheme {
  119. get { return Uri.UriSchemeHttp; }
  120. }
  121. public TransferMode TransferMode {
  122. get { return transfer_mode; }
  123. set { transfer_mode = value; }
  124. }
  125. public bool UnsafeConnectionNtlmAuthentication {
  126. get { return unsafe_ntlm_auth; }
  127. set { unsafe_ntlm_auth = value; }
  128. }
  129. public bool UseDefaultWebProxy {
  130. get { return use_default_proxy; }
  131. set { use_default_proxy = value; }
  132. }
  133. public override bool CanBuildChannelFactory<TChannel> (
  134. BindingContext context)
  135. {
  136. return typeof (TChannel) == typeof (IRequestChannel);
  137. }
  138. #if !NET_2_1
  139. public override bool CanBuildChannelListener<TChannel> (
  140. BindingContext context)
  141. {
  142. return typeof (TChannel) == typeof (IReplyChannel);
  143. }
  144. #endif
  145. public override IChannelFactory<TChannel> BuildChannelFactory<TChannel> (
  146. BindingContext context)
  147. {
  148. // remaining contexts are ignored ... e.g. such binding
  149. // element that always causes an error is ignored.
  150. return new HttpChannelFactory<TChannel> (this, context);
  151. }
  152. #if !NET_2_1
  153. public override IChannelListener<TChannel> BuildChannelListener<TChannel> (
  154. BindingContext context)
  155. {
  156. // remaining contexts are ignored ... e.g. such binding
  157. // element that always causes an error is ignored.
  158. if (ServiceHostingEnvironment.InAspNet)
  159. return new AspNetChannelListener<TChannel> (this, context);
  160. else
  161. return new HttpSimpleChannelListener<TChannel> (this, context);
  162. }
  163. #endif
  164. public override BindingElement Clone ()
  165. {
  166. return new HttpTransportBindingElement (this);
  167. }
  168. public override T GetProperty<T> (BindingContext context)
  169. {
  170. // http://blogs.msdn.com/drnick/archive/2007/04/10/interfaces-for-getproperty-part-1.aspx
  171. #if !NET_2_1
  172. if (typeof (T) == typeof (ISecurityCapabilities))
  173. return (T) (object) new HttpBindingProperties (this);
  174. if (typeof (T) == typeof (IBindingDeliveryCapabilities))
  175. return (T) (object) new HttpBindingProperties (this);
  176. #endif
  177. if (typeof (T) == typeof (TransferMode))
  178. return (T) (object) TransferMode;
  179. return base.GetProperty<T> (context);
  180. }
  181. #if !NET_2_1
  182. [MonoTODO]
  183. void IPolicyExportExtension.ExportPolicy (
  184. MetadataExporter exporter,
  185. PolicyConversionContext context)
  186. {
  187. throw new NotImplementedException ();
  188. }
  189. [MonoTODO]
  190. void IWsdlExportExtension.ExportContract (WsdlExporter exporter,
  191. WsdlContractConversionContext context)
  192. {
  193. throw new NotImplementedException ();
  194. }
  195. [MonoTODO]
  196. void IWsdlExportExtension.ExportEndpoint (WsdlExporter exporter,
  197. WsdlEndpointConversionContext context)
  198. {
  199. throw new NotImplementedException ();
  200. }
  201. #endif
  202. }
  203. #if !NET_2_1
  204. class HttpBindingProperties : ISecurityCapabilities, IBindingDeliveryCapabilities
  205. {
  206. HttpTransportBindingElement source;
  207. public HttpBindingProperties (HttpTransportBindingElement source)
  208. {
  209. this.source = source;
  210. }
  211. public bool AssuresOrderedDelivery {
  212. get { return false; }
  213. }
  214. public bool QueuedDelivery {
  215. get { return false; }
  216. }
  217. public virtual ProtectionLevel SupportedRequestProtectionLevel {
  218. get { return ProtectionLevel.None; }
  219. }
  220. public virtual ProtectionLevel SupportedResponseProtectionLevel {
  221. get { return ProtectionLevel.None; }
  222. }
  223. public virtual bool SupportsClientAuthentication {
  224. get { return source.AuthenticationScheme != AuthenticationSchemes.Anonymous; }
  225. }
  226. public virtual bool SupportsServerAuthentication {
  227. get {
  228. switch (source.AuthenticationScheme) {
  229. case AuthenticationSchemes.Negotiate:
  230. return true;
  231. default:
  232. return false;
  233. }
  234. }
  235. }
  236. public virtual bool SupportsClientWindowsIdentity {
  237. get {
  238. switch (source.AuthenticationScheme) {
  239. case AuthenticationSchemes.Basic:
  240. case AuthenticationSchemes.Digest: // hmm... why? but they return true on .NET
  241. case AuthenticationSchemes.Negotiate:
  242. case AuthenticationSchemes.Ntlm:
  243. return true;
  244. default:
  245. return false;
  246. }
  247. }
  248. }
  249. }
  250. #endif
  251. }