2010-07-02 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels.Http / HttpStandaloneRequestContext.cs
1 //
2 // HttpRequestContext.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
29 using System.IO;
30 using System.Net;
31 using System.Threading;
32
33 namespace System.ServiceModel.Channels.Http
34 {
35         internal class HttpStandaloneRequestContext : HttpRequestContextBase
36         {
37                 HttpListenerContext ctx;
38
39                 public HttpStandaloneRequestContext (HttpReplyChannel channel, Message msg, HttpListenerContext ctx)
40                         : base (channel, msg)
41                 {
42                         if (ctx == null)
43                                 throw new ArgumentNullException ("ctx");
44                         this.ctx = ctx;
45                 }
46
47                 public override void Abort ()
48                 {
49                         ctx.Response.Abort ();
50                 }
51
52                 protected override void ProcessReply (Message msg, TimeSpan timeout)
53                 {
54                         if (msg == null)
55                                 throw new ArgumentNullException ("msg");
56
57                         // FIXME: probably in WebHttpBinding land, there should 
58                         // be some additional code (probably IErrorHandler) that
59                         // treats DestinationUnreachable (and possibly any other)
60                         // errors as HTTP 400 or something appropriate. 
61                         // I originally rewrote the HTTP status here, but it 
62                         // was wrong.
63
64                         // FIXME: should this be done here?
65                         if (Channel.MessageVersion.Addressing.Equals (AddressingVersion.None))
66                                 msg.Headers.Action = null; // prohibited
67
68                         MemoryStream ms = new MemoryStream ();
69                         Channel.Encoder.WriteMessage (msg, ms);
70                         ctx.Response.ContentType = Channel.Encoder.ContentType;
71
72                         string pname = HttpResponseMessageProperty.Name;
73                         bool suppressEntityBody = false;
74                         if (msg.Properties.ContainsKey (pname)) {
75                                 HttpResponseMessageProperty hp = (HttpResponseMessageProperty) msg.Properties [pname];
76                                 string contentType = hp.Headers ["Content-Type"];
77                                 if (contentType != null)
78                                         ctx.Response.ContentType = contentType;
79                                 ctx.Response.Headers.Add (hp.Headers);
80                                 if (hp.StatusCode != default (HttpStatusCode))
81                                         ctx.Response.StatusCode = (int) hp.StatusCode;
82                                 ctx.Response.StatusDescription = hp.StatusDescription;
83                                 if (hp.SuppressEntityBody)
84                                         suppressEntityBody = true;
85                         }
86                         if (msg.IsFault)
87                                 ctx.Response.StatusCode = 500;
88                         if (!suppressEntityBody) {
89                                 ctx.Response.ContentLength64 = ms.Length;
90                                 ctx.Response.OutputStream.Write (ms.GetBuffer (), 0, (int) ms.Length);
91                                 ctx.Response.OutputStream.Flush ();
92                         }
93                 }
94
95                 public override void Close (TimeSpan timeout)
96                 {
97                         ctx.Response.Close ();
98                 }
99         }
100
101         internal class AspNetRequestContext : HttpRequestContextBase
102         {
103                 public AspNetRequestContext (HttpReplyChannel channel, Message request)
104                         : base (channel, request)
105                 {
106                         throw new NotImplementedException ();
107                 }
108
109                 public override void Abort ()
110                 {
111                         throw new NotImplementedException ();
112                 }
113
114                 protected override void ProcessReply (Message msg, TimeSpan timeout)
115                 {
116                         throw new NotImplementedException ();
117                 }
118
119                 public override void Close (TimeSpan timeout)
120                 {
121                         throw new NotImplementedException ();
122                 }
123         }
124
125         internal abstract class HttpRequestContextBase : RequestContext
126         {
127                 Message request;
128                 HttpReplyChannel channel;
129
130                 public HttpRequestContextBase (
131                         HttpReplyChannel channel,
132                         Message request)
133                 {
134                         if (channel == null)
135                                 throw new ArgumentNullException ("channel");
136                         if (request == null)
137                                 throw new ArgumentNullException ("request");
138                         this.channel = channel;
139                         this.request = request;
140                 }
141
142                 public override Message RequestMessage {
143                         get { return request; }
144                 }
145
146                 public HttpReplyChannel Channel {
147                         get { return channel; }
148                 }
149
150                 protected abstract void ProcessReply (Message msg, TimeSpan timeout);
151
152                 public override IAsyncResult BeginReply (
153                         Message msg, AsyncCallback callback, object state)
154                 {
155                         return BeginReply (msg,
156                                 Channel.DefaultSendTimeout,
157                                 callback, state);
158                 }
159
160                 Action<Message,TimeSpan> reply_delegate;
161
162                 public override IAsyncResult BeginReply (
163                         Message msg, TimeSpan timeout,
164                         AsyncCallback callback, object state)
165                 {
166                         if (reply_delegate == null)
167                                 reply_delegate = new Action<Message,TimeSpan> (Reply);
168                         return reply_delegate.BeginInvoke (msg, timeout, callback, state);
169                 }
170
171                 public override void EndReply (IAsyncResult result)
172                 {
173                         if (result == null)
174                                 throw new ArgumentNullException ("result");
175                         if (reply_delegate == null)
176                                 throw new InvalidOperationException ("reply operation has not started");
177                         reply_delegate.EndInvoke (result);
178                 }
179
180                 public override void Reply (Message msg)
181                 {
182                         Reply (msg, Channel.DefaultSendTimeout);
183                 }
184
185                 public override void Reply (Message msg, TimeSpan timeout)
186                 {
187                         ProcessReply (msg, timeout);
188                 }
189
190                 public override void Close ()
191                 {
192                         Close (Channel.DefaultSendTimeout);
193                 }
194         }
195 }