5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2010 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.Http
39 internal class HttpReplyChannel : InternalReplyChannelBase
41 HttpChannelListener<IReplyChannel> source;
42 RequestContext reqctx;
44 public HttpReplyChannel (HttpChannelListener<IReplyChannel> listener)
47 this.source = listener;
50 public MessageEncoder Encoder {
51 get { return source.MessageEncoder; }
54 internal MessageVersion MessageVersion {
55 get { return source.MessageEncoder.MessageVersion; }
58 public override RequestContext ReceiveRequest (TimeSpan timeout)
61 if (!TryReceiveRequest (timeout, out ctx))
62 throw new TimeoutException ();
66 protected override void OnOpen (TimeSpan timeout)
70 protected override void OnAbort ()
72 AbortConnections (TimeSpan.Zero);
73 base.OnAbort (); // FIXME: remove it. The base is wrong. But it is somehow required to not block some tests.
76 public override bool CancelAsync (TimeSpan timeout)
78 AbortConnections (timeout);
79 // FIXME: this wait is sort of hack (because it should not be required), but without it some tests are blocked.
80 // This hack even had better be moved to base.CancelAsync().
81 if (CurrentAsyncResult != null)
82 CurrentAsyncResult.AsyncWaitHandle.WaitOne (TimeSpan.FromMilliseconds (300));
83 return base.CancelAsync (timeout);
86 void AbortConnections (TimeSpan timeout)
89 reqctx.Close (timeout);
94 protected override void OnClose (TimeSpan timeout)
99 DateTime start = DateTime.Now;
101 // FIXME: consider timeout
102 AbortConnections (timeout - (DateTime.Now - start));
104 base.OnClose (timeout - (DateTime.Now - start));
107 protected string GetHeaderItem (string raw)
109 if (raw == null || raw.Length == 0)
114 if (raw [raw.Length - 1] == raw [0])
115 return raw.Substring (1, raw.Length - 2);
116 // FIXME: is it simply an error?
122 protected HttpRequestMessageProperty CreateRequestProperty (HttpContextInfo ctxi)
124 var query = ctxi.Request.Url.Query;
125 var prop = new HttpRequestMessageProperty ();
126 prop.Method = ctxi.Request.HttpMethod;
127 prop.QueryString = query.StartsWith ("?") ? query.Substring (1) : query;
128 // FIXME: prop.SuppressEntityBody
129 prop.Headers.Add (ctxi.Request.Headers);
133 public override bool TryReceiveRequest (TimeSpan timeout, out RequestContext context)
136 HttpContextInfo ctxi;
137 if (!source.ListenerManager.TryDequeueRequest (source.ChannelDispatcher, timeout, out ctxi))
140 return true; // returning true, yet context is null. This happens at closing phase.
144 if (ctxi.Request.HttpMethod == "POST") {
145 msg = CreatePostMessage (ctxi);
148 } else if (ctxi.Request.HttpMethod == "GET")
149 msg = Message.CreateMessage (MessageVersion.None, null); // HTTP GET-based request
151 if (msg.Headers.To == null)
152 msg.Headers.To = ctxi.Request.Url;
153 msg.Properties.Add ("Via", LocalAddress.Uri);
154 msg.Properties.Add (HttpRequestMessageProperty.Name, CreateRequestProperty (ctxi));
156 context = new HttpRequestContext (this, ctxi, msg);
161 protected Message CreatePostMessage (HttpContextInfo ctxi)
163 if (ctxi.Response.StatusCode != 200) { // it's already invalid.
168 if (!Encoder.IsContentTypeSupported (ctxi.Request.ContentType)) {
169 ctxi.Response.StatusCode = (int) HttpStatusCode.UnsupportedMediaType;
170 ctxi.Response.StatusDescription = String.Format (
171 "Expected content-type '{0}' but got '{1}'", Encoder.ContentType, ctxi.Request.ContentType);
177 // FIXME: supply maxSizeOfHeaders.
178 int maxSizeOfHeaders = 0x10000;
180 var msg = Encoder.ReadMessage (
181 ctxi.Request.InputStream, maxSizeOfHeaders, ctxi.Request.ContentType);
183 if (MessageVersion.Envelope.Equals (EnvelopeVersion.Soap11) ||
184 MessageVersion.Addressing.Equals (AddressingVersion.None)) {
185 string action = GetHeaderItem (ctxi.Request.Headers ["SOAPAction"]);
186 if (action != null) {
187 if (action.Length > 2 && action [0] == '"' && action [action.Length] == '"')
188 action = action.Substring (1, action.Length - 2);
189 msg.Headers.Action = action;
196 public override bool WaitForRequest (TimeSpan timeout)
198 throw new NotImplementedException ();