New tests.
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / 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
40 namespace System.ServiceModel.Channels
41 {
42         internal class HttpSimpleChannelListener<TChannel> : HttpChannelListenerBase<TChannel>
43                 where TChannel : class, IChannel
44         {
45                 public HttpSimpleChannelListener (HttpTransportBindingElement source,
46                         BindingContext context)
47                         : base (source, context)
48                 {
49                 }
50
51                 object creator_lock = new object ();
52
53                 protected override TChannel CreateChannel (TimeSpan timeout)
54                 {
55                         lock (creator_lock) {
56                                 return CreateChannelCore (timeout);
57                         }
58                 }
59
60                 TChannel CreateChannelCore (TimeSpan timeout)
61                 {
62                         if (typeof (TChannel) == typeof (IReplyChannel))
63                                 return (TChannel) (object) new HttpSimpleReplyChannel ((HttpSimpleChannelListener<IReplyChannel>) (object) this);
64
65                         throw new NotSupportedException (String.Format ("Channel type {0} is not supported", typeof (TChannel)));
66                 }
67
68                 protected override HttpListenerManager CreateListenerManager ()
69                 {
70                         return new HttpSimpleListenerManager (this, Source, SecurityTokenManager, ChannelDispatcher);
71                 }
72         }
73
74         internal class AspNetChannelListener<TChannel> : HttpChannelListenerBase<TChannel>
75                 where TChannel : class, IChannel
76         {
77                 public AspNetChannelListener (HttpTransportBindingElement source,
78                         BindingContext context)
79                         : base (source, context)
80                 {
81                 }
82
83                 protected override TChannel CreateChannel (TimeSpan timeout)
84                 {
85                         if (typeof (TChannel) == typeof (IReplyChannel))
86                                 return (TChannel) (object) new AspNetReplyChannel ((AspNetChannelListener<IReplyChannel>) (object) this);
87
88                         throw new NotSupportedException (String.Format ("Channel type {0} is not supported", typeof (TChannel)));
89                 }
90
91                 protected override HttpListenerManager CreateListenerManager ()
92                 {
93                         return new AspNetListenerManager (this, Source, SecurityTokenManager, ChannelDispatcher);
94                 }
95         }
96
97         internal abstract class HttpChannelListenerBase<TChannel> : InternalChannelListenerBase<TChannel>
98                 where TChannel : class, IChannel
99         {
100                 List<TChannel> channels = new List<TChannel> ();
101                 MessageEncoder encoder;
102                 HttpListenerManager httpChannelManager;
103
104                 internal static HttpChannelListenerBase<TChannel>CurrentHttpChannelListener;
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.RemainingBindingElements) {
120                                 MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
121                                 if (mbe != null) {
122                                         encoder = CreateEncoder<TChannel> (mbe);
123                                         break;
124                                 }
125                         }
126                         if (encoder == null)
127                                 encoder = 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                 internal 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 MessageEncoder MessageEncoder {
142                         get { return encoder; }
143                 }
144
145                 public ServiceCredentialsSecurityTokenManager SecurityTokenManager { get; private set; }
146
147                 protected override TChannel OnAcceptChannel (TimeSpan timeout)
148                 {
149                         TChannel ch = CreateChannel (timeout);
150                         return ch;
151                 }
152
153                 protected abstract TChannel CreateChannel (TimeSpan timeout);
154
155                 protected override bool OnWaitForChannel (TimeSpan timeout)
156                 {
157                         throw new NotImplementedException ();
158                 }
159
160                 protected abstract HttpListenerManager CreateListenerManager ();
161
162                 protected override void OnOpen (TimeSpan timeout)
163                 {
164                         httpChannelManager = CreateListenerManager ();
165                         Properties.Add (httpChannelManager);
166                         httpChannelManager.Open (timeout);
167                 }
168
169                 protected override void OnAbort ()
170                 {
171                         httpChannelManager.Stop (true);
172                 }
173
174                 protected override void OnClose (TimeSpan timeout)
175                 {
176                         if (State == CommunicationState.Closed)
177                                 return;
178                         base.OnClose (timeout);
179                         // The channels are kept open when the creator channel listener is closed.
180                         // http://blogs.msdn.com/drnick/archive/2006/03/22/557642.aspx
181                         httpChannelManager.Stop (false);
182                 }
183         }
184 }