HttpTransportBindingElement.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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.Channels.Http;
  34. using System.ServiceModel.Description;
  35. namespace System.ServiceModel.Channels
  36. {
  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. internal static object ListenerBuildLock = new object ();
  154. public override IChannelListener<TChannel> BuildChannelListener<TChannel> (
  155. BindingContext context)
  156. {
  157. // remaining contexts are ignored ... e.g. such binding
  158. // element that always causes an error is ignored.
  159. if (ServiceHostingEnvironment.InAspNet)
  160. return new AspNetChannelListener<TChannel> (this, context);
  161. else
  162. return new HttpStandaloneChannelListener<TChannel> (this, context);
  163. // return new HttpSimpleChannelListener<TChannel> (this, context);
  164. }
  165. #endif
  166. public override BindingElement Clone ()
  167. {
  168. return new HttpTransportBindingElement (this);
  169. }
  170. public override T GetProperty<T> (BindingContext context)
  171. {
  172. // http://blogs.msdn.com/drnick/archive/2007/04/10/interfaces-for-getproperty-part-1.aspx
  173. #if !NET_2_1
  174. if (typeof (T) == typeof (ISecurityCapabilities))
  175. return (T) (object) new HttpBindingProperties (this);
  176. if (typeof (T) == typeof (IBindingDeliveryCapabilities))
  177. return (T) (object) new HttpBindingProperties (this);
  178. #endif
  179. if (typeof (T) == typeof (TransferMode))
  180. return (T) (object) TransferMode;
  181. return base.GetProperty<T> (context);
  182. }
  183. #if !NET_2_1
  184. [MonoTODO]
  185. void IPolicyExportExtension.ExportPolicy (
  186. MetadataExporter exporter,
  187. PolicyConversionContext context)
  188. {
  189. throw new NotImplementedException ();
  190. }
  191. [MonoTODO]
  192. void IWsdlExportExtension.ExportContract (WsdlExporter exporter,
  193. WsdlContractConversionContext context)
  194. {
  195. throw new NotImplementedException ();
  196. }
  197. [MonoTODO]
  198. void IWsdlExportExtension.ExportEndpoint (WsdlExporter exporter,
  199. WsdlEndpointConversionContext context)
  200. {
  201. throw new NotImplementedException ();
  202. }
  203. #endif
  204. }
  205. #if !NET_2_1
  206. class HttpBindingProperties : ISecurityCapabilities, IBindingDeliveryCapabilities
  207. {
  208. HttpTransportBindingElement source;
  209. public HttpBindingProperties (HttpTransportBindingElement source)
  210. {
  211. this.source = source;
  212. }
  213. public bool AssuresOrderedDelivery {
  214. get { return false; }
  215. }
  216. public bool QueuedDelivery {
  217. get { return false; }
  218. }
  219. public virtual ProtectionLevel SupportedRequestProtectionLevel {
  220. get { return ProtectionLevel.None; }
  221. }
  222. public virtual ProtectionLevel SupportedResponseProtectionLevel {
  223. get { return ProtectionLevel.None; }
  224. }
  225. public virtual bool SupportsClientAuthentication {
  226. get { return source.AuthenticationScheme != AuthenticationSchemes.Anonymous; }
  227. }
  228. public virtual bool SupportsServerAuthentication {
  229. get {
  230. switch (source.AuthenticationScheme) {
  231. case AuthenticationSchemes.Negotiate:
  232. return true;
  233. default:
  234. return false;
  235. }
  236. }
  237. }
  238. public virtual bool SupportsClientWindowsIdentity {
  239. get {
  240. switch (source.AuthenticationScheme) {
  241. case AuthenticationSchemes.Basic:
  242. case AuthenticationSchemes.Digest: // hmm... why? but they return true on .NET
  243. case AuthenticationSchemes.Negotiate:
  244. case AuthenticationSchemes.Ntlm:
  245. return true;
  246. default:
  247. return false;
  248. }
  249. }
  250. }
  251. }
  252. #endif
  253. }