2010-03-17 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / SvcHttpHandler.cs
1 //
2 // SvcHttpHandler.cs
3 //
4 // Author:
5 //      Ankit Jain  <jankit@novell.com>
6 //      Atsushi Enomoto <atsushi@ximian.com>
7 //
8 // Copyright (C) 2006,2009 Novell, Inc.  http://www.novell.com
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 using System;
30 using System.Collections.Generic;
31 using System.Collections.ObjectModel;
32 using System.Linq;
33 using System.Web;
34 using System.Threading;
35
36 using System.ServiceModel;
37 using System.ServiceModel.Activation;
38 using System.ServiceModel.Configuration;
39 using System.ServiceModel.Description;
40
41 namespace System.ServiceModel.Channels {
42
43         internal class WcfListenerInfo
44         {
45                 public WcfListenerInfo ()
46                 {
47                         Pending = new List<HttpContext> ();
48                         ProcessRequestHandles = new List<ManualResetEvent> ();
49                 }
50
51                 public IChannelListener Listener { get; set; }
52                 public List<ManualResetEvent> ProcessRequestHandles { get; private set; }
53                 public List<HttpContext> Pending { get; private set; }
54         }
55
56         internal class WcfListenerInfoCollection : KeyedCollection<IChannelListener,WcfListenerInfo>
57         {
58                 protected override IChannelListener GetKeyForItem (WcfListenerInfo info)
59                 {
60                         return info.Listener;
61                 }
62         }
63
64         internal class SvcHttpHandler : IHttpHandler
65         {
66                 internal static SvcHttpHandler Current;
67
68                 static object type_lock = new object ();
69
70                 Type type;
71                 Type factory_type;
72                 string path;
73                 ServiceHostBase host;
74                 WcfListenerInfoCollection listeners = new WcfListenerInfoCollection ();
75                 Dictionary<HttpContext,AutoResetEvent> wcf_wait_handles = new Dictionary<HttpContext,AutoResetEvent> ();
76                 AutoResetEvent wait_for_request_handle = new AutoResetEvent (false);
77                 int close_state;
78
79                 public SvcHttpHandler (Type type, Type factoryType, string path)
80                 {
81                         this.type = type;
82                         this.factory_type = factoryType;
83                         this.path = path;
84                 }
85
86                 public bool IsReusable 
87                 {
88                         get { return true; }
89                 }
90
91                 public ServiceHostBase Host {
92                         get { return host; }
93                 }
94
95                 public HttpContext WaitForRequest (IChannelListener listener)
96                 {
97                         if (close_state > 0)
98                                 return null;
99
100                         var wait = new ManualResetEvent (false);
101                         var info = listeners [listener];
102                         if (info.Pending.Count == 0) {
103                                 info.ProcessRequestHandles.Add (wait);
104                                 wait_for_request_handle.Set ();
105                                 wait.WaitOne ();
106                                 info.ProcessRequestHandles.Remove (wait);
107                         }
108
109                         var ctx = listeners [listener].Pending [0];
110                         listeners [listener].Pending.RemoveAt (0);
111                         return ctx;
112                 }
113
114                 IChannelListener FindBestMatchListener (HttpContext ctx)
115                 {
116                         var actx = new AspNetHttpContextInfo (ctx);
117
118                         // Select the best-match listener.
119                         IChannelListener best = null;
120                         string rel = null;
121                         foreach (var li in listeners) {
122                                 var l = li.Listener;
123                                 if (!l.GetProperty<HttpListenerManager> ().FilterHttpContext (actx))
124                                         continue;
125                                 if (l.Uri.Equals (ctx.Request.Url)) {
126                                         best = l;
127                                         break;
128                                 }
129                         }
130                         // FIXME: the matching must be better-considered.
131                         foreach (var li in listeners) {
132                                 var l = li.Listener;
133                                 if (!l.GetProperty<HttpListenerManager> ().FilterHttpContext (actx))
134                                         continue;
135                                 if (!ctx.Request.Url.ToString ().StartsWith (l.Uri.ToString (), StringComparison.Ordinal))
136                                         continue;
137                                 if (best == null)
138                                         best = l;
139                         }
140                         if (best != null)
141                                 return best;
142                         throw new InvalidOperationException (String.Format ("The argument HTTP context did not match any of the registered listener manager (could be mismatch in URL, method etc.) {0}", ctx.Request.Url));
143 /*
144                         var actx = new AspNetHttpContextInfo (ctx);
145                         foreach (var i in listeners)
146                                 if (i.Listener.GetProperty<HttpListenerManager> ().FilterHttpContext (actx))
147                                         return i.Listener;
148                         throw new InvalidOperationException ();
149 */
150                 }
151
152                 public void ProcessRequest (HttpContext context)
153                 {
154                         EnsureServiceHost ();
155
156                         var wait = new AutoResetEvent (false);
157                         var l = FindBestMatchListener (context);
158                         var i = listeners [l];
159                         lock (i) {
160                                 i.Pending.Add (context);
161                                 wcf_wait_handles [context] = wait;
162                                 if (i.ProcessRequestHandles.Count > 0)
163                                         i.ProcessRequestHandles [0].Set ();
164                         }
165
166                         wait.WaitOne ();
167                 }
168
169                 public void EndRequest (IChannelListener listener, HttpContext context)
170                 {
171                         var wait = wcf_wait_handles [context];
172                         wcf_wait_handles.Remove (context);
173                         wait.Set ();
174                 }
175
176                 // called from SvcHttpHandlerFactory's remove callback (i.e.
177                 // unloading asp.net). It closes ServiceHost, then the host
178                 // in turn closes the listener and the channels it opened.
179                 // The channel listener calls CloseServiceChannel() to stop
180                 // accepting further requests on its shutdown.
181                 public void Close ()
182                 {
183                         host.Close ();
184                         host = null;
185                 }
186
187                 public void RegisterListener (IChannelListener listener)
188                 {
189                         lock (type_lock)
190                                 listeners.Add (new WcfListenerInfo () {Listener = listener});
191                 }
192
193                 public void UnregisterListener (IChannelListener listener)
194                 {
195                         listeners.Remove (listener);
196                 }
197
198                 void EnsureServiceHost ()
199                 {
200                         lock (type_lock) {
201                                 Current = this;
202                                 try {
203                                         EnsureServiceHostCore ();
204                                 } finally {
205                                         Current = null;
206                                 }
207                         }
208                 }
209
210                 void EnsureServiceHostCore ()
211                 {
212                         if (host != null)
213                                 return;
214
215                         //ServiceHost for this not created yet
216                         var baseUri = new Uri (new Uri (HttpContext.Current.Request.Url.GetLeftPart (UriPartial.Authority)), path);
217                         if (factory_type != null) {
218                                 host = ((ServiceHostFactory) Activator.CreateInstance (factory_type)).CreateServiceHost (type, new Uri [] {baseUri});
219                         }
220                         else
221                                 host = new ServiceHost (type, baseUri);
222                         host.Extensions.Add (new VirtualPathExtension (baseUri.AbsolutePath));
223
224                         host.Open ();
225
226                         // Not precise, but it needs some wait time to have all channels start requesting. And it is somehow required.
227                         Thread.Sleep (500);
228                 }
229         }
230 }