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.Http
35 internal class HttpStandaloneRequestContext : HttpRequestContextBase
37 HttpListenerContext ctx;
39 public HttpStandaloneRequestContext (HttpReplyChannel channel, Message msg, HttpListenerContext ctx)
43 throw new ArgumentNullException ("ctx");
47 public override void Abort ()
49 ctx.Response.Abort ();
52 protected override void ProcessReply (Message msg, TimeSpan timeout)
55 throw new ArgumentNullException ("msg");
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
64 // FIXME: should this be done here?
65 if (Channel.MessageVersion.Addressing.Equals (AddressingVersion.None))
66 msg.Headers.Action = null; // prohibited
68 MemoryStream ms = new MemoryStream ();
69 Channel.Encoder.WriteMessage (msg, ms);
70 ctx.Response.ContentType = Channel.Encoder.ContentType;
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;
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 ();
95 public override void Close (TimeSpan timeout)
97 ctx.Response.Close ();
101 internal class AspNetRequestContext : HttpRequestContextBase
103 public AspNetRequestContext (HttpReplyChannel channel, Message request)
104 : base (channel, request)
106 throw new NotImplementedException ();
109 public override void Abort ()
111 throw new NotImplementedException ();
114 protected override void ProcessReply (Message msg, TimeSpan timeout)
116 throw new NotImplementedException ();
119 public override void Close (TimeSpan timeout)
121 throw new NotImplementedException ();
125 internal abstract class HttpRequestContextBase : RequestContext
128 HttpReplyChannel channel;
130 public HttpRequestContextBase (
131 HttpReplyChannel channel,
135 throw new ArgumentNullException ("channel");
137 throw new ArgumentNullException ("request");
138 this.channel = channel;
139 this.request = request;
142 public override Message RequestMessage {
143 get { return request; }
146 public HttpReplyChannel Channel {
147 get { return channel; }
150 protected abstract void ProcessReply (Message msg, TimeSpan timeout);
152 public override IAsyncResult BeginReply (
153 Message msg, AsyncCallback callback, object state)
155 return BeginReply (msg,
156 Channel.DefaultSendTimeout,
160 Action<Message,TimeSpan> reply_delegate;
162 public override IAsyncResult BeginReply (
163 Message msg, TimeSpan timeout,
164 AsyncCallback callback, object state)
166 if (reply_delegate == null)
167 reply_delegate = new Action<Message,TimeSpan> (Reply);
168 return reply_delegate.BeginInvoke (msg, timeout, callback, state);
171 public override void EndReply (IAsyncResult result)
174 throw new ArgumentNullException ("result");
175 if (reply_delegate == null)
176 throw new InvalidOperationException ("reply operation has not started");
177 reply_delegate.EndInvoke (result);
180 public override void Reply (Message msg)
182 Reply (msg, Channel.DefaultSendTimeout);
185 public override void Reply (Message msg, TimeSpan timeout)
187 ProcessReply (msg, timeout);
190 public override void Close ()
192 Close (Channel.DefaultSendTimeout);