Merge pull request #5198 from BrzVlad/fix-binprot-stats
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels.Http / HttpChannelListener.cs
1 //
2 // HttpChannelListener.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2010 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.Http
42 {
43         internal class HttpChannelListener<TChannel> : InternalChannelListenerBase<TChannel>, IChannelDispatcherBoundListener
44                 where TChannel : class, IChannel
45         {
46                 HttpListenerManager listener_manager;
47
48                 public HttpChannelListener (HttpTransportBindingElement source, BindingContext context)
49                         : base (context)
50                 {
51                         if (ServiceHostBase.CurrentServiceHostHack != null)
52                                 DispatcherBuilder.ChannelDispatcherSetter = delegate (ChannelDispatcher cd) { this.ChannelDispatcher = cd; };
53
54                         this.Source = source;
55                         // The null Uri check looks weird, but it seems the listener can be built without it.
56                         // See HttpTransportBindingElementTest.BuildChannelListenerWithoutListenUri().
57                         if (Uri != null && source.Scheme != Uri.Scheme)
58                                 throw new ArgumentException (String.Format ("Requested listen uri scheme must be {0}, but was {1}.", source.Scheme, Uri.Scheme));
59
60                         foreach (BindingElement be in context.Binding.Elements) {
61                                 MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
62                                 if (mbe != null) {
63                                         MessageEncoder = CreateEncoder<TChannel> (mbe);
64                                         break;
65                                 }
66                         }
67                         if (MessageEncoder == null)
68                                 MessageEncoder = new TextMessageEncoder (MessageVersion.Default, Encoding.UTF8);
69
70                         if (context.BindingParameters.Contains (typeof (ServiceCredentials)))
71                                 SecurityTokenManager = new ServiceCredentialsSecurityTokenManager ((ServiceCredentials) context.BindingParameters [typeof (ServiceCredentials)]);
72                 }
73
74                 public ChannelDispatcher ChannelDispatcher { get; set; }
75
76                 public HttpTransportBindingElement Source { get; private set; }
77
78                 public HttpListenerManager ListenerManager {
79                         get {  return listener_manager; }
80                 }
81
82                 public ServiceCredentialsSecurityTokenManager SecurityTokenManager { get; private set; }
83
84                 ManualResetEvent accept_channel_handle = new ManualResetEvent (true);
85
86                 protected override TChannel OnAcceptChannel (TimeSpan timeout)
87                 {
88                         // HTTP channel could be accepted while there is no incoming request yet. The reply channel waits for the actual request.
89                         // HTTP channel listeners do not accept more than one channel at a time.
90                         DateTime start = DateTime.UtcNow;
91                         TimeSpan waitTimeout;
92                         if (timeout == TimeSpan.MaxValue)
93                                 waitTimeout = TimeSpan.FromMilliseconds(int.MaxValue);
94                         else
95                                 waitTimeout = timeout - (DateTime.UtcNow - start);
96                         accept_channel_handle.WaitOne (waitTimeout);
97                         accept_channel_handle.Reset (); 
98                         TChannel ch = CreateChannel (timeout - (DateTime.UtcNow - start));
99                         ch.Closed += delegate {
100                                 accept_channel_handle.Set ();
101                         };
102                         return ch;
103                 }
104
105                 protected TChannel CreateChannel (TimeSpan timeout)
106                 {
107                         lock (ThisLock) {
108                                 return CreateChannelCore (timeout);
109                         }
110                 }
111
112                 TChannel CreateChannelCore (TimeSpan timeout)
113                 {
114                         if (typeof (TChannel) == typeof (IReplyChannel))
115                                 return (TChannel) (object) new HttpReplyChannel ((HttpChannelListener<IReplyChannel>) (object) this);
116
117                         throw new NotSupportedException (String.Format ("Channel type {0} is not supported", typeof (TChannel)));
118                 }
119
120                 protected override bool OnWaitForChannel (TimeSpan timeout)
121                 {
122                         throw new NotImplementedException ();
123                 }
124
125                 protected HttpListenerManager GetOrCreateListenerManager ()
126                 {
127                         var table = HttpListenerManagerTable.GetOrCreate (ChannelDispatcher != null ? ChannelDispatcher.Host : null);
128                         return table.GetOrCreateManager (Uri, Source);
129                 }
130
131                 protected override void OnOpen (TimeSpan timeout)
132                 {
133                         listener_manager = GetOrCreateListenerManager ();
134                         Properties.Add (listener_manager);
135                         listener_manager.RegisterListener (ChannelDispatcher, Source, timeout);
136                 }
137
138                 protected override void OnAbort ()
139                 {
140                         listener_manager.UnregisterListener (ChannelDispatcher, TimeSpan.Zero);
141                 }
142
143                 protected override void OnClose (TimeSpan timeout)
144                 {
145                         if (State == CommunicationState.Closed)
146                                 return;
147                         base.OnClose (timeout);
148                         // The channels are kept open when the creator channel listener is closed.
149                         // http://blogs.msdn.com/drnick/archive/2006/03/22/557642.aspx
150                         listener_manager.UnregisterListener (ChannelDispatcher, timeout);
151                 }
152
153                 // immediately stop accepting channel.
154                 public override bool CancelAsync (TimeSpan timeout)
155                 {
156                         try {
157                                 CurrentAsyncResult.AsyncWaitHandle.WaitOne (TimeSpan.Zero);
158                         } catch (TimeoutException) {
159                         }
160                         return true;
161                 }
162         }
163 }