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