2 // HttpRequestContext.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2006 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.
31 using System.Threading;
33 namespace System.ServiceModel.Channels
35 internal class HttpRequestContext : HttpRequestContextBase
37 HttpListenerContext ctx;
39 public HttpRequestContext (
40 HttpReplyChannel channel,
41 Message msg, HttpListenerContext ctx)
45 throw new ArgumentNullException ("ctx");
49 public override void Abort ()
51 ctx.Response.Abort ();
54 protected override void ProcessReply (Message msg, TimeSpan timeout)
57 throw new ArgumentNullException ("msg");
59 // FIXME: should this be done here?
60 if (channel.MessageVersion.Addressing.Equals (AddressingVersion.None))
61 msg.Headers.Action = null; // prohibited
63 MemoryStream ms = new MemoryStream ();
64 channel.Encoder.WriteMessage (msg, ms);
65 ctx.Response.ContentType = channel.Encoder.ContentType;
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;
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 ();
88 public override void Close (TimeSpan timeout)
90 ctx.Response.Close ();
94 internal abstract class HttpRequestContextBase : RequestContext
97 internal HttpReplyChannel channel;
99 public HttpRequestContextBase (
100 HttpReplyChannel channel,
104 throw new ArgumentNullException ("channel");
106 throw new ArgumentNullException ("request");
107 this.channel = channel;
108 this.request = request;
111 public override Message RequestMessage {
112 get { return request; }
115 public HttpReplyChannel Channel {
116 get { return channel; }
119 protected abstract void ProcessReply (Message msg, TimeSpan timeout);
121 public override IAsyncResult BeginReply (
122 Message msg, AsyncCallback callback, object state)
124 return BeginReply (msg,
125 channel.DefaultSendTimeout,
129 Action<Message,TimeSpan> reply_delegate;
131 public override IAsyncResult BeginReply (
132 Message msg, TimeSpan timeout,
133 AsyncCallback callback, object state)
135 if (reply_delegate == null)
136 reply_delegate = new Action<Message,TimeSpan> (Reply);
137 return reply_delegate.BeginInvoke (msg, timeout, callback, state);
140 public override void EndReply (IAsyncResult result)
143 throw new ArgumentNullException ("result");
144 if (reply_delegate == null)
145 throw new InvalidOperationException ("reply operation has not started");
146 reply_delegate.EndInvoke (result);
149 public override void Reply (Message msg)
151 Reply (msg, channel.DefaultSendTimeout);
154 public override void Reply (Message msg, TimeSpan timeout)
156 ProcessReply (msg, timeout);
159 public override void Close ()
161 Close (channel.DefaultSendTimeout);