2
0

HttpTransportManager.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.Collections.Generic;
  7. using System.ServiceModel;
  8. using System.ServiceModel.Diagnostics;
  9. using System.Runtime;
  10. using System.ServiceModel.Diagnostics.Application;
  11. using System.Runtime.Diagnostics;
  12. abstract class HttpTransportManager : TransportManager, ITransportManagerRegistration
  13. {
  14. volatile Dictionary<string, UriPrefixTable<HttpChannelListener>> addressTables;
  15. readonly HostNameComparisonMode hostNameComparisonMode;
  16. readonly Uri listenUri;
  17. readonly string realm;
  18. internal HttpTransportManager()
  19. {
  20. this.addressTables = new Dictionary<string, UriPrefixTable<HttpChannelListener>>();
  21. }
  22. internal HttpTransportManager(Uri listenUri, HostNameComparisonMode hostNameComparisonMode)
  23. : this()
  24. {
  25. this.hostNameComparisonMode = hostNameComparisonMode;
  26. this.listenUri = listenUri;
  27. }
  28. internal HttpTransportManager(Uri listenUri, HostNameComparisonMode hostNameComparisonMode, string realm)
  29. : this(listenUri, hostNameComparisonMode)
  30. {
  31. this.realm = realm;
  32. }
  33. internal string Realm
  34. {
  35. get
  36. {
  37. return this.realm;
  38. }
  39. }
  40. public HostNameComparisonMode HostNameComparisonMode
  41. {
  42. get
  43. {
  44. return this.hostNameComparisonMode;
  45. }
  46. }
  47. // are we hosted in Asp.Net? Default is false.
  48. internal bool IsHosted
  49. {
  50. get;
  51. set;
  52. }
  53. internal override string Scheme
  54. {
  55. get
  56. {
  57. return Uri.UriSchemeHttp;
  58. }
  59. }
  60. internal virtual UriPrefixTable<ITransportManagerRegistration> TransportManagerTable
  61. {
  62. get
  63. {
  64. return HttpChannelListener.StaticTransportManagerTable;
  65. }
  66. }
  67. public Uri ListenUri
  68. {
  69. get
  70. {
  71. return this.listenUri;
  72. }
  73. }
  74. protected void Fault(Exception exception)
  75. {
  76. lock (ThisLock)
  77. {
  78. foreach (KeyValuePair<string, UriPrefixTable<HttpChannelListener>> pair in this.addressTables)
  79. {
  80. this.Fault(pair.Value, exception);
  81. }
  82. }
  83. }
  84. internal virtual bool IsCompatible(HttpChannelListener listener)
  85. {
  86. return (
  87. (this.hostNameComparisonMode == listener.HostNameComparisonMode) &&
  88. (this.realm == listener.Realm)
  89. );
  90. }
  91. internal override void OnClose(TimeSpan timeout)
  92. {
  93. Cleanup();
  94. }
  95. internal override void OnAbort()
  96. {
  97. Cleanup();
  98. base.OnAbort();
  99. }
  100. void Cleanup()
  101. {
  102. this.TransportManagerTable.UnregisterUri(this.ListenUri, this.HostNameComparisonMode);
  103. }
  104. protected void StartReceiveBytesActivity(ServiceModelActivity activity, Uri requestUri)
  105. {
  106. Fx.Assert(DiagnosticUtility.ShouldUseActivity, "should only call this if we're using SM Activities");
  107. ServiceModelActivity.Start(activity, SR.GetString(SR.ActivityReceiveBytes, requestUri.ToString()), ActivityType.ReceiveBytes);
  108. }
  109. protected void TraceMessageReceived(EventTraceActivity eventTraceActivity, Uri listenUri)
  110. {
  111. if (TD.HttpMessageReceiveStartIsEnabled())
  112. {
  113. TD.HttpMessageReceiveStart(eventTraceActivity);
  114. }
  115. }
  116. protected bool TryLookupUri(Uri requestUri, string requestMethod,
  117. HostNameComparisonMode hostNameComparisonMode, bool isWebSocketRequest, out HttpChannelListener listener)
  118. {
  119. listener = null;
  120. if (isWebSocketRequest)
  121. {
  122. Fx.Assert(StringComparer.OrdinalIgnoreCase.Compare(requestMethod, "GET") == 0, "The requestMethod must be GET in WebSocket case.");
  123. requestMethod = WebSocketTransportSettings.WebSocketMethod;
  124. }
  125. if (requestMethod == null)
  126. {
  127. requestMethod = string.Empty;
  128. }
  129. UriPrefixTable<HttpChannelListener> addressTable;
  130. Dictionary<string, UriPrefixTable<HttpChannelListener>> localAddressTables = addressTables;
  131. // check for a method match if necessary
  132. HttpChannelListener methodListener = null;
  133. if (requestMethod.Length > 0)
  134. {
  135. if (localAddressTables.TryGetValue(requestMethod, out addressTable))
  136. {
  137. if (addressTable.TryLookupUri(requestUri, hostNameComparisonMode, out methodListener)
  138. && string.Compare(requestUri.AbsolutePath, methodListener.Uri.AbsolutePath, StringComparison.OrdinalIgnoreCase) != 0)
  139. {
  140. methodListener = null;
  141. }
  142. }
  143. }
  144. // and also check the wildcard bucket
  145. if (localAddressTables.TryGetValue(string.Empty, out addressTable)
  146. && addressTable.TryLookupUri(requestUri, hostNameComparisonMode, out listener))
  147. {
  148. if (methodListener != null && methodListener.Uri.AbsoluteUri.Length >= listener.Uri.AbsoluteUri.Length)
  149. {
  150. listener = methodListener;
  151. }
  152. }
  153. else
  154. {
  155. listener = methodListener;
  156. }
  157. return (listener != null);
  158. }
  159. internal override void Register(TransportChannelListener channelListener)
  160. {
  161. string method = ((HttpChannelListener)channelListener).Method;
  162. UriPrefixTable<HttpChannelListener> addressTable;
  163. if (!addressTables.TryGetValue(method, out addressTable))
  164. {
  165. lock (ThisLock)
  166. {
  167. if (!addressTables.TryGetValue(method, out addressTable))
  168. {
  169. Dictionary<string, UriPrefixTable<HttpChannelListener>> newAddressTables =
  170. new Dictionary<string, UriPrefixTable<HttpChannelListener>>(addressTables);
  171. addressTable = new UriPrefixTable<HttpChannelListener>();
  172. newAddressTables[method] = addressTable;
  173. addressTables = newAddressTables;
  174. }
  175. }
  176. }
  177. addressTable.RegisterUri(channelListener.Uri,
  178. channelListener.InheritBaseAddressSettings ? hostNameComparisonMode : channelListener.HostNameComparisonModeInternal,
  179. (HttpChannelListener)channelListener);
  180. }
  181. IList<TransportManager> ITransportManagerRegistration.Select(TransportChannelListener channelListener)
  182. {
  183. IList<TransportManager> result = null;
  184. if (this.IsCompatible((HttpChannelListener)channelListener))
  185. {
  186. result = new List<TransportManager>();
  187. result.Add(this);
  188. }
  189. return result;
  190. }
  191. internal override void Unregister(TransportChannelListener channelListener)
  192. {
  193. UriPrefixTable<HttpChannelListener> addressTable;
  194. if (!addressTables.TryGetValue(((HttpChannelListener)channelListener).Method, out addressTable))
  195. {
  196. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(
  197. SR.ListenerFactoryNotRegistered, channelListener.Uri)));
  198. }
  199. HostNameComparisonMode registeredMode = channelListener.InheritBaseAddressSettings ? hostNameComparisonMode : channelListener.HostNameComparisonModeInternal;
  200. EnsureRegistered(addressTable, (HttpChannelListener)channelListener, registeredMode);
  201. addressTable.UnregisterUri(channelListener.Uri, registeredMode);
  202. }
  203. protected class ActivityHolder : IDisposable
  204. {
  205. internal HttpRequestContext context;
  206. internal ServiceModelActivity activity;
  207. public ActivityHolder(ServiceModelActivity activity, HttpRequestContext requestContext)
  208. {
  209. Fx.Assert(requestContext != null, "requestContext cannot be null.");
  210. this.activity = activity;
  211. this.context = requestContext;
  212. }
  213. public void Dispose()
  214. {
  215. if (this.activity != null)
  216. {
  217. this.activity.Dispose();
  218. }
  219. }
  220. }
  221. }
  222. }