[wcf] AJAX GET requests work now.
[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.Now;
91                         accept_channel_handle.WaitOne (timeout - (DateTime.Now - start));
92                         accept_channel_handle.Reset ();
93                         TChannel ch = CreateChannel (timeout - (DateTime.Now - start));
94                         ch.Closed += delegate {
95                                 accept_channel_handle.Set ();
96                         };
97                         return ch;
98                 }
99
100                 protected TChannel CreateChannel (TimeSpan timeout)
101                 {
102                         lock (ThisLock) {
103                                 return CreateChannelCore (timeout);
104                         }
105                 }
106
107                 TChannel CreateChannelCore (TimeSpan timeout)
108                 {
109                         if (typeof (TChannel) == typeof (IReplyChannel))
110                                 return (TChannel) (object) new HttpReplyChannel ((HttpChannelListener<IReplyChannel>) (object) this);
111
112                         throw new NotSupportedException (String.Format ("Channel type {0} is not supported", typeof (TChannel)));
113                 }
114
115                 protected override bool OnWaitForChannel (TimeSpan timeout)
116                 {
117                         throw new NotImplementedException ();
118                 }
119
120                 protected HttpListenerManager GetOrCreateListenerManager ()
121                 {
122                         var table = HttpListenerManagerTable.GetOrCreate (ChannelDispatcher != null ? ChannelDispatcher.Host : null);
123                         return table.GetOrCreateManager (Uri);
124                 }
125
126                 protected override void OnOpen (TimeSpan timeout)
127                 {
128                         listener_manager = GetOrCreateListenerManager ();
129                         Properties.Add (listener_manager);
130                         listener_manager.RegisterListener (ChannelDispatcher, timeout);
131                 }
132
133                 protected override void OnAbort ()
134                 {
135                         listener_manager.UnregisterListener (ChannelDispatcher, TimeSpan.Zero);
136                 }
137
138                 protected override void OnClose (TimeSpan timeout)
139                 {
140                         if (State == CommunicationState.Closed)
141                                 return;
142                         base.OnClose (timeout);
143                         // The channels are kept open when the creator channel listener is closed.
144                         // http://blogs.msdn.com/drnick/archive/2006/03/22/557642.aspx
145                         listener_manager.UnregisterListener (ChannelDispatcher, timeout);
146                 }
147
148                 // immediately stop accepting channel.
149                 public override bool CancelAsync (TimeSpan timeout)
150                 {
151                         try {
152                                 CurrentAsyncResult.AsyncWaitHandle.WaitOne (TimeSpan.Zero);
153                         } catch (TimeoutException) {
154                         }
155                         return true;
156                 }
157         }
158 }