5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2006 Novell, Inc. http://www.novell.com
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.Generic;
30 using System.Collections.Specialized;
33 using System.ServiceModel;
35 using System.Threading;
37 namespace System.ServiceModel.Channels
39 internal class HttpSimpleReplyChannel : HttpReplyChannel
41 HttpSimpleChannelListener<IReplyChannel> source;
42 List<HttpListenerContext> waiting = new List<HttpListenerContext> ();
43 RequestContext reqctx;
45 public HttpSimpleReplyChannel (HttpSimpleChannelListener<IReplyChannel> listener)
48 this.source = listener;
51 protected override void OnAbort ()
53 AbortConnections (TimeSpan.Zero);
54 base.OnAbort (); // FIXME: remove it. The base is wrong. But it is somehow required to not block some tests.
57 public override bool CancelAsync (TimeSpan timeout)
59 AbortConnections (timeout);
60 // FIXME: this wait is sort of hack (because it should not be required), but without it some tests are blocked.
61 // This hack even had better be moved to base.CancelAsync().
62 if (CurrentAsyncResult != null)
63 CurrentAsyncResult.AsyncWaitHandle.WaitOne (TimeSpan.FromMilliseconds (300));
64 return base.CancelAsync (timeout);
67 void SignalAsyncWait ()
76 void AbortConnections (TimeSpan timeout)
80 foreach (var ctx in waiting)
81 ctx.Response.Close ();
83 source.ListenerManager.CancelGetHttpContextAsync ();
86 protected override void OnClose (TimeSpan timeout)
88 DateTime start = DateTime.Now;
90 reqctx.Close (timeout);
92 // FIXME: consider timeout
93 AbortConnections (timeout - (DateTime.Now - start));
95 base.OnClose (timeout - (DateTime.Now - start));
98 public override bool TryReceiveRequest (TimeSpan timeout, out RequestContext context)
101 if (waiting.Count == 0 && !WaitForRequest (timeout))
103 HttpListenerContext ctx = null;
105 if (waiting.Count > 0) {
107 waiting.RemoveAt (0);
111 // Though as long as this instance is used
112 // synchronously, it should not happen.
114 if (ctx.Response.StatusCode != 200) {
115 ctx.Response.Close ();
119 // FIXME: supply maxSizeOfHeaders.
120 int maxSizeOfHeaders = 0x10000;
124 // FIXME: our HttpConnection (under HttpListener)
125 // somehow breaks when the underlying connection is
126 // reused. Remove it when it gets fixed.
127 ctx.Response.KeepAlive = false;
129 if (ctx.Request.HttpMethod == "POST") {
130 if (!Encoder.IsContentTypeSupported (ctx.Request.ContentType)) {
131 ctx.Response.StatusCode = (int) HttpStatusCode.UnsupportedMediaType;
132 ctx.Response.StatusDescription = String.Format (
133 "Expected content-type '{0}' but got '{1}'", Encoder.ContentType, ctx.Request.ContentType);
134 ctx.Response.Close ();
139 msg = Encoder.ReadMessage (
140 ctx.Request.InputStream, maxSizeOfHeaders);
142 if (MessageVersion.Envelope.Equals (EnvelopeVersion.Soap11) ||
143 MessageVersion.Addressing.Equals (AddressingVersion.None)) {
144 string action = GetHeaderItem (ctx.Request.Headers ["SOAPAction"]);
145 if (action != null) {
146 if (action.Length > 2 && action [0] == '"' && action [action.Length] == '"')
147 action = action.Substring (1, action.Length - 2);
148 msg.Headers.Action = action;
151 } else if (ctx.Request.HttpMethod == "GET") {
152 msg = Message.CreateMessage (MessageVersion, null);
154 msg.Headers.To = ctx.Request.Url;
155 msg.Properties.Add ("Via", LocalAddress.Uri);
156 msg.Properties.Add (HttpRequestMessageProperty.Name, CreateRequestProperty (ctx.Request.HttpMethod, ctx.Request.Url.Query, ctx.Request.Headers));
158 MessageBuffer buf = msg.CreateBufferedCopy (0x10000);
159 msg = buf.CreateMessage ();
160 System.Xml.XmlTextWriter w = new System.Xml.XmlTextWriter (Console.Out);
161 w.Formatting = System.Xml.Formatting.Indented;
162 buf.CreateMessage ().WriteMessage (w);
165 context = new HttpRequestContext (this, msg, ctx);
172 public override bool WaitForRequest (TimeSpan timeout)
175 throw new InvalidOperationException ("Another wait operation is in progress");
177 wait = new AutoResetEvent (false);
178 source.ListenerManager.GetHttpContextAsync (timeout, HttpContextAcquired);
179 if (wait != null) // in case callback is done before WaitOne() here.
180 return wait.WaitOne (timeout, false);
181 return waiting.Count > 0;
182 } catch (HttpListenerException e) {
183 // FIXME: does this make sense? I doubt.
184 if ((uint) e.ErrorCode == 0x80004005) // invalid handle. Happens during shutdown.
185 while (true) Thread.Sleep (1000); // thread is about to be terminated.
187 } catch (ObjectDisposedException) {
194 void HttpContextAcquired (HttpContextInfo ctx)
197 throw new InvalidOperationException ("WaitForRequest operation has not started");
198 var sctx = (HttpListenerContextInfo) ctx;
199 if (State == CommunicationState.Opened && ctx != null)
201 waiting.Add (sctx.Source);
206 internal abstract class HttpReplyChannel : InternalReplyChannelBase
208 HttpChannelListenerBase<IReplyChannel> source;
210 public HttpReplyChannel (HttpChannelListenerBase<IReplyChannel> listener)
213 this.source = listener;
216 public MessageEncoder Encoder {
217 get { return source.MessageEncoder; }
220 internal MessageVersion MessageVersion {
221 get { return source.MessageEncoder.MessageVersion; }
224 public override RequestContext ReceiveRequest (TimeSpan timeout)
227 TryReceiveRequest (timeout, out ctx);
231 protected override void OnOpen (TimeSpan timeout)
235 protected string GetHeaderItem (string raw)
237 if (raw == null || raw.Length == 0)
242 if (raw [raw.Length - 1] == raw [0])
243 return raw.Substring (1, raw.Length - 2);
244 // FIXME: is it simply an error?
250 protected HttpRequestMessageProperty CreateRequestProperty (string method, string query, NameValueCollection headers)
252 var prop = new HttpRequestMessageProperty ();
253 prop.Method = method;
254 prop.QueryString = query.StartsWith ("?") ? query.Substring (1) : query;
255 // FIXME: prop.SuppressEntityBody
256 prop.Headers.Add (headers);