New tests, updates
[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.Net;
32 using System.Net.Security;
33 using System.ServiceModel;
34 using System.ServiceModel.Description;
35 using System.ServiceModel.Security;
36 using System.Text;
37
38 namespace System.ServiceModel.Channels
39 {
40         internal class HttpChannelListener<TChannel> : ChannelListenerBase<TChannel>
41                 where TChannel : class, IChannel
42         {
43                 HttpTransportBindingElement source;
44                 BindingContext context;
45                 Uri listen_uri;
46                 List<IChannel> channels = new List<IChannel> ();
47                 MessageEncoder encoder;
48                 HttpChannelManager<TChannel> httpChannelManager;
49
50                 public HttpChannelListener (HttpTransportBindingElement source,
51                         BindingContext context)
52                         : base (context.Binding)
53                 {
54                         // FIXME: consider ListenUriMode
55                         // FIXME: there should be some way to post-provide Uri in case of null listenerUri in context.
56                         listen_uri = context.ListenUriBaseAddress != null ?
57                                 new Uri (context.ListenUriBaseAddress, context.ListenUriRelativeAddress) : null;
58                         foreach (BindingElement be in context.RemainingBindingElements) {
59                                 MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
60                                 if (mbe != null) {
61                                         encoder = CreateEncoder<TChannel> (mbe);
62                                         break;
63                                 }
64                         }
65                         if (encoder == null)
66                                 encoder = new TextMessageEncoder (MessageVersion.Default, Encoding.UTF8);
67                 }
68
69                 public HttpListener Http {
70                         get {  return httpChannelManager.HttpListener; }
71                 }
72
73                 public MessageEncoder MessageEncoder {
74                         get { return encoder; }
75                 }
76
77                 public override Uri Uri {
78                         get { return listen_uri; }
79                 }
80
81                 protected override TChannel OnAcceptChannel (TimeSpan timeout)
82                 {
83                         TChannel ch = PopulateChannel (timeout);
84                         channels.Add (ch);
85                         return ch;
86                 }
87
88                 TChannel PopulateChannel (TimeSpan timeout)
89                 {
90                         if (typeof (TChannel) == typeof (IReplyChannel)) {
91                                 if (ServiceHostingEnvironment.InAspNet)
92                                         return (TChannel) (object) new AspNetReplyChannel (
93                                                 (HttpChannelListener<IReplyChannel>) (object) this, timeout);
94                                 else
95                                         return (TChannel) (object) new HttpReplyChannel (
96                                                 (HttpChannelListener<IReplyChannel>) (object) this, timeout);
97                         }
98
99                         // FIXME: implement more
100
101                         throw new NotImplementedException ();
102                 }
103
104                 protected override IAsyncResult OnBeginAcceptChannel (
105                         TimeSpan timeout, AsyncCallback callback,
106                         object asyncState)
107                 {
108                         throw new NotImplementedException ();
109                 }
110
111                 protected override TChannel OnEndAcceptChannel (IAsyncResult result)
112                 {
113                         throw new NotImplementedException ();
114                 }
115
116                 protected override IAsyncResult OnBeginWaitForChannel (
117                         TimeSpan timeout, AsyncCallback callback, object state)
118                 {
119                         throw new NotImplementedException ();
120                 }
121
122                 protected override bool OnEndWaitForChannel (IAsyncResult result)
123                 {
124                         throw new NotImplementedException ();
125                 }
126
127                 protected override bool OnWaitForChannel (TimeSpan timeout)
128                 {
129                         throw new NotImplementedException ();
130                 }
131                 
132                 void StartListening (TimeSpan timeout)
133                 {
134                         httpChannelManager = new HttpChannelManager<TChannel> (this);
135                         httpChannelManager.Open (timeout);
136                 }
137
138                 protected override void OnOpen (TimeSpan timeout)
139                 {
140                         if (ServiceHostingEnvironment.InAspNet)
141                                 return;
142
143                         StartListening (timeout);
144                 }
145
146                 protected override IAsyncResult OnBeginOpen (TimeSpan timeout,
147                         AsyncCallback callback, object state)
148                 {
149                         throw new NotImplementedException ();
150                 }
151
152                 protected override void OnEndOpen (IAsyncResult result)
153                 {
154                         throw new NotImplementedException ();
155                 }
156
157                 protected override void OnClose (TimeSpan timeout)
158                 {
159                         if (ServiceHostingEnvironment.InAspNet)
160                                 return;
161
162                         foreach (TChannel ch in channels)
163                                 ch.Close(timeout);
164                         httpChannelManager.Stop ();
165                 }
166
167                 [MonoTODO]
168                 protected override IAsyncResult OnBeginClose (TimeSpan timeout,
169                         AsyncCallback callback, object state)
170                 {
171                         throw new NotImplementedException ();
172                 }
173
174                 [MonoTODO]
175                 protected override void OnEndClose (IAsyncResult result)
176                 {
177                         throw new NotImplementedException ();
178                 }
179
180                 [MonoTODO ("find out what to do here.")]
181                 protected override void OnAbort ()
182                 {
183                 }
184         }
185 }