PageRenderTime 21ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.ServiceModel/System.ServiceModel.Channels/HttpChannelListener.cs

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 176 lines | 121 code | 24 blank | 31 comment | 14 complexity | cb167161c40c2953b9dedd449a776843 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1
  1. //
  2. // HttpChannelListener.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <atsushi@ximian.com>
  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.Collections.ObjectModel;
  31. using System.Linq;
  32. using System.Net;
  33. using System.Net.Security;
  34. using System.ServiceModel;
  35. using System.ServiceModel.Description;
  36. using System.ServiceModel.Security;
  37. using System.Text;
  38. namespace System.ServiceModel.Channels
  39. {
  40. internal class HttpSimpleChannelListener<TChannel> : HttpChannelListenerBase<TChannel>
  41. where TChannel : class, IChannel
  42. {
  43. public HttpSimpleChannelListener (HttpTransportBindingElement source,
  44. BindingContext context)
  45. : base (source, context)
  46. {
  47. }
  48. object creator_lock = new object ();
  49. protected override TChannel CreateChannel (TimeSpan timeout)
  50. {
  51. lock (creator_lock) {
  52. return CreateChannelCore (timeout);
  53. }
  54. }
  55. TChannel CreateChannelCore (TimeSpan timeout)
  56. {
  57. if (typeof (TChannel) == typeof (IReplyChannel))
  58. return (TChannel) (object) new HttpSimpleReplyChannel ((HttpSimpleChannelListener<IReplyChannel>) (object) this);
  59. throw new NotSupportedException (String.Format ("Channel type {0} is not supported", typeof (TChannel)));
  60. }
  61. protected override HttpListenerManager CreateListenerManager ()
  62. {
  63. return new HttpSimpleListenerManager (this, Source);
  64. }
  65. }
  66. internal class AspNetChannelListener<TChannel> : HttpChannelListenerBase<TChannel>
  67. where TChannel : class, IChannel
  68. {
  69. public AspNetChannelListener (HttpTransportBindingElement source,
  70. BindingContext context)
  71. : base (source, context)
  72. {
  73. }
  74. internal SvcHttpHandler HttpHandler {
  75. get { return ((AspNetListenerManager) ListenerManager).Source; }
  76. }
  77. protected override TChannel CreateChannel (TimeSpan timeout)
  78. {
  79. if (typeof (TChannel) == typeof (IReplyChannel))
  80. return (TChannel) (object) new AspNetReplyChannel ((AspNetChannelListener<IReplyChannel>) (object) this);
  81. throw new NotSupportedException (String.Format ("Channel type {0} is not supported", typeof (TChannel)));
  82. }
  83. protected override HttpListenerManager CreateListenerManager ()
  84. {
  85. return new AspNetListenerManager (this, Source);
  86. }
  87. }
  88. internal abstract class HttpChannelListenerBase<TChannel> : InternalChannelListenerBase<TChannel>
  89. where TChannel : class, IChannel
  90. {
  91. BindingContext context;
  92. List<TChannel> channels = new List<TChannel> ();
  93. MessageEncoder encoder;
  94. HttpListenerManager httpChannelManager;
  95. public HttpChannelListenerBase (HttpTransportBindingElement source,
  96. BindingContext context)
  97. : base (context)
  98. {
  99. this.Source = source;
  100. // The null Uri check looks weird, but it seems the listener can be built without it.
  101. // See HttpTransportBindingElementTest.BuildChannelListenerWithoutListenUri().
  102. if (Uri != null && source.Scheme != Uri.Scheme)
  103. throw new ArgumentException (String.Format ("Requested listen uri scheme must be {0}, but was {1}.", source.Scheme, Uri.Scheme));
  104. foreach (BindingElement be in context.RemainingBindingElements) {
  105. MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
  106. if (mbe != null) {
  107. encoder = CreateEncoder<TChannel> (mbe);
  108. break;
  109. }
  110. }
  111. if (encoder == null)
  112. encoder = new TextMessageEncoder (MessageVersion.Default, Encoding.UTF8);
  113. }
  114. public HttpTransportBindingElement Source { get; private set; }
  115. public HttpListenerManager ListenerManager {
  116. get { return httpChannelManager; }
  117. }
  118. public MessageEncoder MessageEncoder {
  119. get { return encoder; }
  120. }
  121. protected override TChannel OnAcceptChannel (TimeSpan timeout)
  122. {
  123. TChannel ch = CreateChannel (timeout);
  124. return ch;
  125. }
  126. protected abstract TChannel CreateChannel (TimeSpan timeout);
  127. protected override bool OnWaitForChannel (TimeSpan timeout)
  128. {
  129. throw new NotImplementedException ();
  130. }
  131. protected abstract HttpListenerManager CreateListenerManager ();
  132. protected override void OnOpen (TimeSpan timeout)
  133. {
  134. httpChannelManager = CreateListenerManager ();
  135. Properties.Add (httpChannelManager);
  136. httpChannelManager.Open (timeout);
  137. }
  138. protected override void OnAbort ()
  139. {
  140. httpChannelManager.Stop (true);
  141. }
  142. protected override void OnClose (TimeSpan timeout)
  143. {
  144. if (State == CommunicationState.Closed)
  145. return;
  146. base.OnClose (timeout);
  147. // The channels are kept open when the creator channel listener is closed.
  148. // http://blogs.msdn.com/drnick/archive/2006/03/22/557642.aspx
  149. httpChannelManager.Stop (false);
  150. }
  151. }
  152. }