2010-01-20 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / HttpReplyChannel.cs
1 //
2 // HttpReplyChannel.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2006 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.Specialized;
31 using System.IO;
32 using System.Net;
33 using System.ServiceModel;
34 using System.Text;
35 using System.Threading;
36
37 namespace System.ServiceModel.Channels
38 {
39         internal class HttpSimpleReplyChannel : HttpReplyChannel
40         {
41                 HttpSimpleChannelListener<IReplyChannel> source;
42                 List<HttpListenerContext> waiting = new List<HttpListenerContext> ();
43                 RequestContext reqctx;
44
45                 public HttpSimpleReplyChannel (HttpSimpleChannelListener<IReplyChannel> listener)
46                         : base (listener)
47                 {
48                         this.source = listener;
49                 }
50
51                 protected override void OnAbort ()
52                 {
53                         lock (waiting)
54                                 foreach (HttpListenerContext ctx in waiting)
55                                         ctx.Response.Abort ();
56                         base.OnAbort (); // FIXME: remove it. The base is wrong.
57                 }
58
59                 protected override void OnClose (TimeSpan timeout)
60                 {
61                         DateTime start = DateTime.Now;
62                         if (reqctx != null)
63                                 reqctx.Close (timeout);
64
65                         // FIXME: consider timeout
66                         lock (waiting)
67                                 foreach (HttpListenerContext ctx in waiting)
68                                         ctx.Response.Close ();
69
70                         base.OnClose (timeout - (DateTime.Now - start));
71                 }
72
73                 public override bool TryReceiveRequest (TimeSpan timeout, out RequestContext context)
74                 {
75                         context = null;
76                         if (waiting.Count == 0 && !WaitForRequest (timeout))
77                                 return false;
78                         HttpListenerContext ctx = null;
79                         lock (waiting) {
80                                 if (waiting.Count > 0) {
81                                         ctx = waiting [0];
82                                         waiting.RemoveAt (0);
83                                 }
84                         }
85                         if (ctx == null) 
86                                 // Though as long as this instance is used
87                                 // synchronously, it should not happen.
88                                 return false;
89
90                         // FIXME: supply maxSizeOfHeaders.
91                         int maxSizeOfHeaders = 0x10000;
92
93                         Message msg = null;
94
95                         // FIXME: our HttpConnection (under HttpListener) 
96                         // somehow breaks when the underlying connection is
97                         // reused. Remove it when it gets fixed.
98                         ctx.Response.KeepAlive = false;
99
100                         if (ctx.Request.HttpMethod == "POST") {
101                                 if (!Encoder.IsContentTypeSupported (ctx.Request.ContentType)) {
102                                         ctx.Response.StatusCode = (int) HttpStatusCode.UnsupportedMediaType;
103                                         ctx.Response.StatusDescription = String.Format (
104                                                         "Expected content-type '{0}' but got '{1}'", Encoder.ContentType, ctx.Request.ContentType);
105                                         ctx.Response.Close ();
106
107                                         return false;
108                                 }
109
110                                 msg = Encoder.ReadMessage (
111                                         ctx.Request.InputStream, maxSizeOfHeaders);
112
113                                 if (MessageVersion.Envelope.Equals (EnvelopeVersion.Soap11) ||
114                                     MessageVersion.Addressing.Equals (AddressingVersion.None)) {
115                                         string action = GetHeaderItem (ctx.Request.Headers ["SOAPAction"]);
116                                         if (action != null) {
117                                                 if (action.Length > 2 && action [0] == '"' && action [action.Length] == '"')
118                                                         action = action.Substring (1, action.Length - 2);
119                                                 msg.Headers.Action = action;
120                                         }
121                                 }
122                         } else if (ctx.Request.HttpMethod == "GET") {
123                                 msg = Message.CreateMessage (MessageVersion, null);
124                         }
125                         msg.Headers.To = ctx.Request.Url;
126                         msg.Properties.Add ("Via", LocalAddress.Uri);
127                         msg.Properties.Add (HttpRequestMessageProperty.Name, CreateRequestProperty (ctx.Request.HttpMethod, ctx.Request.Url.Query, ctx.Request.Headers));
128 /*
129 MessageBuffer buf = msg.CreateBufferedCopy (0x10000);
130 msg = buf.CreateMessage ();
131 System.Xml.XmlTextWriter w = new System.Xml.XmlTextWriter (Console.Out);
132 w.Formatting = System.Xml.Formatting.Indented;
133 buf.CreateMessage ().WriteMessage (w);
134 w.Close ();
135 */
136                         context = new HttpRequestContext (this, msg, ctx);
137                         reqctx = context;
138                         return true;
139                 }
140
141                 AutoResetEvent wait;
142
143                 public override bool WaitForRequest (TimeSpan timeout)
144                 {
145                         if (wait != null)
146                                 throw new InvalidOperationException ("Another wait operation is in progress");
147                         try {
148                                 wait = new AutoResetEvent (false);
149                                 source.ListenerManager.GetHttpContextAsync (timeout, HttpContextAcquired);
150                                 if (wait != null) // in case callback is done before WaitOne() here.
151                                         return wait.WaitOne (timeout, false);
152                                 return waiting.Count > 0;
153                         } catch (HttpListenerException e) {
154                                 // FIXME: does this make sense? I doubt.
155                                 if ((uint) e.ErrorCode == 0x80004005) // invalid handle. Happens during shutdown.
156                                         while (true) Thread.Sleep (1000); // thread is about to be terminated.
157                                 throw;
158                         } catch (ObjectDisposedException) {
159                                 return false;
160                         } finally {
161                                 wait = null;
162                         }
163                 }
164
165                 void HttpContextAcquired (HttpContextInfo ctx)
166                 {
167                         if (wait == null)
168                                 throw new InvalidOperationException ("WaitForRequest operation has not started");
169                         var sctx = (HttpListenerContextInfo) ctx;
170                         if (State == CommunicationState.Opened && ctx != null)
171                                 waiting.Add (sctx.Source);
172                         var wait_ = wait;
173                         wait = null;
174                         wait_.Set ();
175                 }
176         }
177
178         internal abstract class HttpReplyChannel : InternalReplyChannelBase
179         {
180                 HttpChannelListenerBase<IReplyChannel> source;
181
182                 public HttpReplyChannel (HttpChannelListenerBase<IReplyChannel> listener)
183                         : base (listener)
184                 {
185                         this.source = listener;
186                 }
187
188                 public MessageEncoder Encoder {
189                         get { return source.MessageEncoder; }
190                 }
191
192                 internal MessageVersion MessageVersion {
193                         get { return source.MessageEncoder.MessageVersion; }
194                 }
195
196                 public override RequestContext ReceiveRequest (TimeSpan timeout)
197                 {
198                         RequestContext ctx;
199                         TryReceiveRequest (timeout, out ctx);
200                         return ctx;
201                 }
202
203                 protected override void OnOpen (TimeSpan timeout)
204                 {
205                 }
206
207                 protected string GetHeaderItem (string raw)
208                 {
209                         if (raw == null || raw.Length == 0)
210                                 return raw;
211                         switch (raw [0]) {
212                         case '\'':
213                         case '"':
214                                 if (raw [raw.Length - 1] == raw [0])
215                                         return raw.Substring (1, raw.Length - 2);
216                                 // FIXME: is it simply an error?
217                                 break;
218                         }
219                         return raw;
220                 }
221
222                 protected HttpRequestMessageProperty CreateRequestProperty (string method, string query, NameValueCollection headers)
223                 {
224                         var prop = new HttpRequestMessageProperty ();
225                         prop.Method = method;
226                         prop.QueryString = query.StartsWith ("?") ? query.Substring (1) : query;
227                         // FIXME: prop.SuppressEntityBody
228                         prop.Headers.Add (headers);
229                         return prop;
230                 }
231         }
232 }