2009-09-06 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / HttpRequestContext.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
34 {
35         internal class HttpRequestContext : HttpRequestContextBase
36         {
37                 HttpListenerContext ctx;
38
39                 public HttpRequestContext (
40                         HttpReplyChannel channel,
41                         Message msg, HttpListenerContext ctx)
42                         : base (channel, msg)
43                 {
44                         if (ctx == null)
45                                 throw new ArgumentNullException ("ctx");
46                         this.ctx = ctx;
47                 }
48
49                 public override void Abort ()
50                 {
51                         ctx.Response.Abort ();
52                 }
53
54                 protected override void ProcessReply (Message msg, TimeSpan timeout)
55                 {
56                         if (msg == null)
57                                 throw new ArgumentNullException ("msg");
58
59                         // FIXME: should this be done here?
60                         if (channel.MessageVersion.Addressing.Equals (AddressingVersion.None))
61                                 msg.Headers.Action = null; // prohibited
62
63                         MemoryStream ms = new MemoryStream ();
64                         channel.Encoder.WriteMessage (msg, ms);
65                         ctx.Response.ContentType = channel.Encoder.ContentType;
66
67                         string pname = HttpResponseMessageProperty.Name;
68                         bool suppressEntityBody = false;
69                         if (msg.Properties.ContainsKey (pname)) {
70                                 HttpResponseMessageProperty hp = (HttpResponseMessageProperty) msg.Properties [pname];
71                                 string contentType = hp.Headers ["Content-Type"];
72                                 if (contentType != null)
73                                         ctx.Response.ContentType = contentType;
74                                 ctx.Response.Headers.Add (hp.Headers);
75                                 if (hp.StatusCode != default (HttpStatusCode))
76                                         ctx.Response.StatusCode = (int) hp.StatusCode;
77                                 ctx.Response.StatusDescription = hp.StatusDescription;
78                                 if (hp.SuppressEntityBody)
79                                         suppressEntityBody = true;
80                         }
81                         if (!suppressEntityBody) {
82                                 ctx.Response.ContentLength64 = ms.Length;
83                                 ctx.Response.OutputStream.Write (ms.GetBuffer (), 0, (int) ms.Length);
84                                 ctx.Response.OutputStream.Flush ();
85                         }
86                 }
87
88                 public override void Close (TimeSpan timeout)
89                 {
90                         ctx.Response.Close ();
91                 }
92         }
93
94         internal abstract class HttpRequestContextBase : RequestContext
95         {
96                 Message request;
97                 internal HttpReplyChannel channel;
98
99                 public HttpRequestContextBase (
100                         HttpReplyChannel channel,
101                         Message request)
102                 {
103                         if (channel == null)
104                                 throw new ArgumentNullException ("channel");
105                         if (request == null)
106                                 throw new ArgumentNullException ("request");
107                         this.channel = channel;
108                         this.request = request;
109                 }
110
111                 public override Message RequestMessage {
112                         get { return request; }
113                 }
114
115                 public HttpReplyChannel Channel {
116                         get { return channel; }
117                 }
118
119                 protected abstract void ProcessReply (Message msg, TimeSpan timeout);
120
121                 public override IAsyncResult BeginReply (
122                         Message msg, AsyncCallback callback, object state)
123                 {
124                         return BeginReply (msg,
125                                 channel.DefaultSendTimeout,
126                                 callback, state);
127                 }
128
129                 Action<Message,TimeSpan> reply_delegate;
130
131                 public override IAsyncResult BeginReply (
132                         Message msg, TimeSpan timeout,
133                         AsyncCallback callback, object state)
134                 {
135                         if (reply_delegate == null)
136                                 reply_delegate = new Action<Message,TimeSpan> (Reply);
137                         return reply_delegate.BeginInvoke (msg, timeout, callback, state);
138                 }
139
140                 public override void EndReply (IAsyncResult result)
141                 {
142                         if (result == null)
143                                 throw new ArgumentNullException ("result");
144                         if (reply_delegate == null)
145                                 throw new InvalidOperationException ("reply operation has not started");
146                         reply_delegate.EndInvoke (result);
147                 }
148
149                 public override void Reply (Message msg)
150                 {
151                         Reply (msg, channel.DefaultSendTimeout);
152                 }
153
154                 public override void Reply (Message msg, TimeSpan timeout)
155                 {
156                         ProcessReply (msg, timeout);
157                 }
158
159                 public override void Close ()
160                 {
161                         Close (channel.DefaultSendTimeout);
162                 }
163         }
164 }