Merge pull request #5198 from BrzVlad/fix-binprot-stats
[mono.git] / mcs / class / System.ServiceModel / old-code / HttpChannelListener.cs
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.Dispatcher;
37 using System.ServiceModel.Security;
38 using System.Text;
39 using System.Threading;
40
41 namespace System.ServiceModel.Channels
42 {
43         internal class HttpSimpleChannelListener<TChannel> : HttpChannelListenerBase<TChannel>
44                 where TChannel : class, IChannel
45         {
46                 public HttpSimpleChannelListener (HttpTransportBindingElement source,
47                         BindingContext context)
48                         : base (source, context)
49                 {
50                 }
51
52                 protected override TChannel CreateChannel (TimeSpan timeout)
53                 {
54                         lock (ThisLock) {
55                                 return CreateChannelCore (timeout);
56                         }
57                 }
58
59                 TChannel CreateChannelCore (TimeSpan timeout)
60                 {
61                         if (typeof (TChannel) == typeof (IReplyChannel))
62                                 return (TChannel) (object) new HttpSimpleReplyChannel ((HttpSimpleChannelListener<IReplyChannel>) (object) this);
63
64                         throw new NotSupportedException (String.Format ("Channel type {0} is not supported", typeof (TChannel)));
65                 }
66
67                 protected override HttpListenerManager CreateListenerManager ()
68                 {
69                         return new HttpSimpleListenerManager (this, Source, SecurityTokenManager, ChannelDispatcher);
70                 }
71         }
72
73         internal class AspNetChannelListener<TChannel> : HttpChannelListenerBase<TChannel>
74                 where TChannel : class, IChannel
75         {
76                 public AspNetChannelListener (HttpTransportBindingElement source,
77                         BindingContext context)
78                         : base (source, context)
79                 {
80                 }
81
82                 protected override TChannel CreateChannel (TimeSpan timeout)
83                 {
84                         if (typeof (TChannel) == typeof (IReplyChannel))
85                                 return (TChannel) (object) new AspNetReplyChannel ((AspNetChannelListener<IReplyChannel>) (object) this);
86
87                         throw new NotSupportedException (String.Format ("Channel type {0} is not supported", typeof (TChannel)));
88                 }
89
90                 protected override HttpListenerManager CreateListenerManager ()
91                 {
92                         return new AspNetListenerManager (this, Source, SecurityTokenManager, ChannelDispatcher);
93                 }
94         }
95
96         internal interface IChannelDispatcherBoundListener
97         {
98                 ChannelDispatcher ChannelDispatcher { get; set; }
99         }
100
101         internal abstract class HttpChannelListenerBase<TChannel> : InternalChannelListenerBase<TChannel>, IChannelDispatcherBoundListener
102                 where TChannel : class, IChannel
103         {
104                 HttpListenerManager httpChannelManager;
105
106                 public HttpChannelListenerBase (HttpTransportBindingElement source,
107                         BindingContext context)
108                         : base (context)
109                 {
110                         if (ServiceHostBase.CurrentServiceHostHack != null)
111                                 DispatcherBuilder.ChannelDispatcherSetter = delegate (ChannelDispatcher cd) { this.ChannelDispatcher = cd; };
112
113                         this.Source = source;
114                         // The null Uri check looks weird, but it seems the listener can be built without it.
115                         // See HttpTransportBindingElementTest.BuildChannelListenerWithoutListenUri().
116                         if (Uri != null && source.Scheme != Uri.Scheme)
117                                 throw new ArgumentException (String.Format ("Requested listen uri scheme must be {0}, but was {1}.", source.Scheme, Uri.Scheme));
118
119                         foreach (BindingElement be in context.Binding.Elements) {
120                                 MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
121                                 if (mbe != null) {
122                                         MessageEncoder = CreateEncoder<TChannel> (mbe);
123                                         break;
124                                 }
125                         }
126                         if (MessageEncoder == null)
127                                 MessageEncoder = new TextMessageEncoder (MessageVersion.Default, Encoding.UTF8);
128
129                         if (context.BindingParameters.Contains (typeof (ServiceCredentials)))
130                                 SecurityTokenManager = new ServiceCredentialsSecurityTokenManager ((ServiceCredentials) context.BindingParameters [typeof (ServiceCredentials)]);
131                 }
132
133                 public ChannelDispatcher ChannelDispatcher { get; set; }
134
135                 public HttpTransportBindingElement Source { get; private set; }
136
137                 public HttpListenerManager ListenerManager {
138                         get {  return httpChannelManager; }
139                 }
140
141                 public ServiceCredentialsSecurityTokenManager SecurityTokenManager { get; private set; }
142
143                 ManualResetEvent accept_channel_handle = new ManualResetEvent (true);
144
145                 protected override TChannel OnAcceptChannel (TimeSpan timeout)
146                 {
147                         // HTTP channel listeners do not accept more than one channel at a time.
148                         DateTime start = DateTime.UtcNow;
149                         accept_channel_handle.WaitOne (timeout - (DateTime.UtcNow - start));
150                         accept_channel_handle.Reset ();
151                         TChannel ch = CreateChannel (timeout - (DateTime.UtcNow - start));
152                         ch.Closed += delegate {
153                                 accept_channel_handle.Set ();
154                         };
155                         return ch;
156                 }
157
158                 protected abstract TChannel CreateChannel (TimeSpan timeout);
159
160                 protected override bool OnWaitForChannel (TimeSpan timeout)
161                 {
162                         throw new NotImplementedException ();
163                 }
164
165                 protected abstract HttpListenerManager CreateListenerManager ();
166
167                 protected override void OnOpen (TimeSpan timeout)
168                 {
169                         httpChannelManager = CreateListenerManager ();
170                         Properties.Add (httpChannelManager);
171                         httpChannelManager.Open (timeout);
172                 }
173
174                 protected override void OnAbort ()
175                 {
176                         httpChannelManager.Stop (true);
177                 }
178
179                 protected override void OnClose (TimeSpan timeout)
180                 {
181                         if (State == CommunicationState.Closed)
182                                 return;
183                         base.OnClose (timeout);
184                         // The channels are kept open when the creator channel listener is closed.
185                         // http://blogs.msdn.com/drnick/archive/2006/03/22/557642.aspx
186                         httpChannelManager.Stop (false);
187                 }
188
189                 // immediately stop accepting channel.
190                 public override bool CancelAsync (TimeSpan timeout)
191                 {
192                         try {
193                                 CurrentAsyncResult.AsyncWaitHandle.WaitOne (TimeSpan.Zero);
194                         } catch (TimeoutException) {
195                         }
196                         return true;
197                 }
198         }
199 }