move to from olive to mcs
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / AspNetReplyChannel.cs
1 //
2 // AspNetReplyChannel.cs
3 //
4 // Author:
5 //      Ankit Jain  <jankit@novell.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.IO;
31 using System.Net;
32 using System.ServiceModel;
33 using System.Text;
34 using System.Threading;
35 using System.Web;
36
37 namespace System.ServiceModel.Channels
38 {
39         internal class AspNetReplyChannel : HttpReplyChannel
40         {
41                 HttpContext http_context;
42                 Uri uri;
43
44                 public AspNetReplyChannel (HttpChannelListener<IReplyChannel> listener,
45                         TimeSpan timeout)
46                         : base (listener, timeout)
47                 {
48                         uri = listener.Uri;
49                 }
50
51                 public override bool TryReceiveRequest (TimeSpan timeout, out RequestContext context)
52                 {
53 #if false
54                         context = null;
55                         if (waiting.Count == 0 && !WaitForRequest (timeout))
56                                 return false;
57                         HttpListenerContext ctx = null;
58                         lock (waiting) {
59                                 if (waiting.Count > 0) {
60                                         ctx = waiting [0];
61                                         waiting.RemoveAt (0);
62                                 }
63                         }
64                         if (ctx == null) 
65                                 // Though as long as this instance is used
66                                 // synchronously, it should not happen.
67                                 return false;
68 #endif
69
70                         Message msg;
71                         if (http_context.Request.HttpMethod == "GET") {
72                                 if (http_context.Request.QueryString ["wsdl"] != null) {
73                                         msg = Message.CreateMessage (Encoder.MessageVersion,
74                                                 "http://schemas.xmlsoap.org/ws/2004/09/transfer/Get");
75                                         msg.Headers.To = http_context.Request.Url;
76                                 } else {
77                                         msg = Message.CreateMessage (Encoder.MessageVersion, null);
78                                         msg.Headers.To = http_context.Request.Url;
79                                 }
80                         } else {
81                                 //FIXME: Do above stuff for HttpContext ?
82                                 int maxSizeOfHeaders = 0x10000;
83
84                                 msg = Encoder.ReadMessage (
85                                         http_context.Request.InputStream, maxSizeOfHeaders);
86
87                                 if (Encoder.MessageVersion.Envelope == EnvelopeVersion.Soap11) {
88                                         string action = GetHeaderItem (http_context.Request.Headers ["SOAPAction"]);
89                                         if (action != null)
90                                                 msg.Headers.Action = action;
91                                 }
92                         }
93                         context = new AspNetRequestContext (this, msg, http_context);
94
95                         return true;
96                 }
97
98                 public HttpContext Context {
99                         get { return http_context; }
100                         set { http_context = value; }
101                 }
102
103                 public override bool WaitForRequest (TimeSpan timeout)
104                 {
105                         // FIXME: we might want to take other approaches.
106                         if (timeout.Ticks > int.MaxValue)
107                                 timeout = TimeSpan.FromDays (20);
108
109                         SvcHttpHandler h = SvcHttpHandlerFactory.GetHandler (uri.OriginalString.Replace ("file://", ""));
110                         return h.WaitForRequest (this, timeout);
111
112 #if false
113                         AutoResetEvent wait = new AutoResetEvent (false);
114                         source.Http.BeginGetContext (HttpContextReceived, wait);
115                         // FIXME: we might want to take other approaches.
116                         if (timeout.Ticks > int.MaxValue)
117                                 timeout = TimeSpan.FromDays (20);
118                         return wait.WaitOne (timeout, false);
119 #endif
120                 }
121
122                 void HttpContextReceived (IAsyncResult result)
123                 {
124                         throw new NotImplementedException ();
125 #if false       
126                         waiting.Add (source.Http.EndGetContext (result));
127                         AutoResetEvent wait = (AutoResetEvent) result.AsyncState;
128                         wait.Set ();
129 #endif
130                 }
131         }
132 }