2010-07-02 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels.Http / HttpStandaloneChannelListener.cs
1 //
2 // HttpStandaloneChannelListener.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 interface IChannelDispatcherBoundListener
44         {
45                 ChannelDispatcher ChannelDispatcher { get; set; }
46         }
47
48         internal class HttpStandaloneChannelListener<TChannel> : InternalChannelListenerBase<TChannel>, IChannelDispatcherBoundListener
49                 where TChannel : class, IChannel
50         {
51                 HttpListenerManager listener_manager;
52
53                 public HttpStandaloneChannelListener (HttpTransportBindingElement source, BindingContext context)
54                         : base (context)
55                 {
56                         if (ServiceHostBase.CurrentServiceHostHack != null)
57                                 DispatcherBuilder.ChannelDispatcherSetter = delegate (ChannelDispatcher cd) { this.ChannelDispatcher = cd; };
58
59                         this.Source = source;
60                         // The null Uri check looks weird, but it seems the listener can be built without it.
61                         // See HttpTransportBindingElementTest.BuildChannelListenerWithoutListenUri().
62                         if (Uri != null && source.Scheme != Uri.Scheme)
63                                 throw new ArgumentException (String.Format ("Requested listen uri scheme must be {0}, but was {1}.", source.Scheme, Uri.Scheme));
64
65                         foreach (BindingElement be in context.Binding.Elements) {
66                                 MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
67                                 if (mbe != null) {
68                                         MessageEncoder = CreateEncoder<TChannel> (mbe);
69                                         break;
70                                 }
71                         }
72                         if (MessageEncoder == null)
73                                 MessageEncoder = new TextMessageEncoder (MessageVersion.Default, Encoding.UTF8);
74
75                         if (context.BindingParameters.Contains (typeof (ServiceCredentials)))
76                                 SecurityTokenManager = new ServiceCredentialsSecurityTokenManager ((ServiceCredentials) context.BindingParameters [typeof (ServiceCredentials)]);
77                 }
78
79                 public ChannelDispatcher ChannelDispatcher { get; set; }
80
81                 public HttpTransportBindingElement Source { get; private set; }
82
83                 public HttpListenerManager ListenerManager {
84                         get {  return listener_manager; }
85                 }
86
87                 public ServiceCredentialsSecurityTokenManager SecurityTokenManager { get; private set; }
88
89                 ManualResetEvent accept_channel_handle = new ManualResetEvent (true);
90
91                 protected override TChannel OnAcceptChannel (TimeSpan timeout)
92                 {
93                         // HTTP channel could be accepted while there is no incoming request yet. The reply channel waits for the actual request.
94                         // HTTP channel listeners do not accept more than one channel at a time.
95                         DateTime start = DateTime.Now;
96                         accept_channel_handle.WaitOne (timeout - (DateTime.Now - start));
97                         accept_channel_handle.Reset ();
98                         TChannel ch = CreateChannel (timeout - (DateTime.Now - 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 HttpStandaloneReplyChannel ((HttpStandaloneChannelListener<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);
129                 }
130
131                 protected override void OnOpen (TimeSpan timeout)
132                 {
133                         listener_manager = GetOrCreateListenerManager ();
134                         Properties.Add (listener_manager);
135                         listener_manager.RegisterListener (ChannelDispatcher, 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 }