Merge pull request #5198 from BrzVlad/fix-binprot-stats
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels.Http / HttpReplyChannel.cs
1 //
2 // HttpReplyChannel.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 using System;
29 using System.Collections.Generic;
30 using System.Collections.Specialized;
31 using System.IdentityModel.Selectors;
32 using System.IdentityModel.Tokens;
33 using System.IO;
34 using System.Net;
35 using System.ServiceModel;
36 using System.ServiceModel.Security;
37 using System.Text;
38 using System.Threading;
39
40 namespace System.ServiceModel.Channels.Http
41 {
42         internal class HttpReplyChannel : InternalReplyChannelBase
43         {
44                 HttpChannelListener<IReplyChannel> source;
45                 RequestContext reqctx;
46                 SecurityTokenAuthenticator security_token_authenticator;
47                 SecurityTokenResolver security_token_resolver;
48
49                 public HttpReplyChannel (HttpChannelListener<IReplyChannel> listener)
50                         : base (listener)
51                 {
52                         this.source = listener;
53
54                         if (listener.SecurityTokenManager != null) {
55                                 var str = new SecurityTokenRequirement () { TokenType = SecurityTokenTypes.UserName };
56                                 security_token_authenticator = listener.SecurityTokenManager.CreateSecurityTokenAuthenticator (str, out security_token_resolver);
57                         }
58                 }
59
60                 internal HttpChannelListener<IReplyChannel> Source {
61                         get { return source; }
62                 }
63
64                 public MessageEncoder Encoder {
65                         get { return source.MessageEncoder; }
66                 }
67
68                 internal MessageVersion MessageVersion {
69                         get { return source.MessageEncoder.MessageVersion; }
70                 }
71
72                 public override RequestContext ReceiveRequest (TimeSpan timeout)
73                 {
74                         RequestContext ctx;
75                         if (!TryReceiveRequest (timeout, out ctx))
76                                 throw new TimeoutException ();
77                         return ctx;
78                 }
79
80                 protected override void OnOpen (TimeSpan timeout)
81                 {
82                 }
83
84                 protected override void OnAbort ()
85                 {
86                         AbortConnections (TimeSpan.Zero);
87                         base.OnAbort (); // FIXME: remove it. The base is wrong. But it is somehow required to not block some tests.
88                 }
89
90                 public override bool CancelAsync (TimeSpan timeout)
91                 {
92                         AbortConnections (timeout);
93                         // FIXME: this wait is sort of hack (because it should not be required), but without it some tests are blocked.
94                         // This hack even had better be moved to base.CancelAsync().
95 //                      if (CurrentAsyncResult != null)
96 //                              CurrentAsyncResult.AsyncWaitHandle.WaitOne (TimeSpan.FromMilliseconds (300));
97                         return base.CancelAsync (timeout);
98                 }
99
100                 void AbortConnections (TimeSpan timeout)
101                 {
102                         if (reqctx != null)
103                                 reqctx.Close (timeout);
104                 }
105
106                 bool close_started;
107                 object close_lock = new object ();
108
109                 protected override void OnClose (TimeSpan timeout)
110                 {
111                         lock (close_lock) {
112                                 if (close_started)
113                                         return;
114                                 close_started = true;
115                         }
116                         DateTime start = DateTime.UtcNow;
117
118                         // FIXME: consider timeout
119                         AbortConnections (timeout - (DateTime.UtcNow - start));
120
121                         base.OnClose (timeout - (DateTime.UtcNow - start));
122                 }
123
124                 protected string GetHeaderItem (string raw)
125                 {
126                         if (raw == null || raw.Length == 0)
127                                 return raw;
128                         switch (raw [0]) {
129                         case '\'':
130                         case '"':
131                                 if (raw [raw.Length - 1] == raw [0])
132                                         return raw.Substring (1, raw.Length - 2);
133                                 // FIXME: is it simply an error?
134                                 break;
135                         }
136                         return raw;
137                 }
138
139                 protected HttpRequestMessageProperty CreateRequestProperty (HttpContextInfo ctxi)
140                 {
141                         var query = ctxi.Request.Url.Query;
142                         var prop = new HttpRequestMessageProperty ();
143                         prop.Method = ctxi.Request.HttpMethod;
144                         prop.QueryString = query.StartsWith ("?") ? query.Substring (1) : query;
145                         // FIXME: prop.SuppressEntityBody
146                         prop.Headers.Add (ctxi.Request.Headers);
147                         return prop;
148                 }
149
150                 public override bool TryReceiveRequest (TimeSpan timeout, out RequestContext context)
151                 {
152                         context = null;
153                         HttpContextInfo ctxi;
154                         if (!source.ListenerManager.TryDequeueRequest (source.ChannelDispatcher, timeout, out ctxi))
155                                 return false;
156                         if (ctxi == null)
157                                 return true; // returning true, yet context is null. This happens at closing phase.
158
159                         if (source.Source.AuthenticationScheme != AuthenticationSchemes.Anonymous) {
160                                 if (security_token_authenticator != null)
161                                         // FIXME: use return value?
162                                         try {
163                                                 security_token_authenticator.ValidateToken (new UserNameSecurityToken (ctxi.User, ctxi.Password));
164                                         } catch (Exception) {
165                                                 ctxi.ReturnUnauthorized ();
166                                         }
167                                 else {
168                                         ctxi.ReturnUnauthorized ();
169                                 }
170                         }
171
172                         Message msg = null;
173
174                         if (ctxi.Request.HttpMethod == "POST")
175                                 msg = CreatePostMessage (ctxi);
176                         else if (ctxi.Request.HttpMethod == "GET")
177                                 msg = Message.CreateMessage (MessageVersion.None, null); // HTTP GET-based request
178
179                         if (msg == null)
180                                 return false;
181
182                         if (msg.Headers.To == null)
183                                 msg.Headers.To = ctxi.Request.Url;
184                         msg.Properties.Add ("Via", LocalAddress.Uri);
185                         msg.Properties.Add (HttpRequestMessageProperty.Name, CreateRequestProperty (ctxi));
186
187                         Logger.LogMessage (MessageLogSourceKind.TransportReceive, ref msg, source.Source.MaxReceivedMessageSize);
188
189                         context = new HttpRequestContext (this, ctxi, msg);
190                         reqctx = context;
191                         return true;
192                 }
193
194                 protected Message CreatePostMessage (HttpContextInfo ctxi)
195                 {
196                         if (ctxi.Response.StatusCode != 200) { // it's already invalid.
197                                 ctxi.Close ();
198                                 return null;
199                         }
200
201                         if (!Encoder.IsContentTypeSupported (ctxi.Request.ContentType)) {
202                                 ctxi.Response.StatusCode = (int) HttpStatusCode.UnsupportedMediaType;
203                                 ctxi.Response.StatusDescription = String.Format (
204                                                 "Expected content-type '{0}' but got '{1}'", Encoder.ContentType, ctxi.Request.ContentType);
205                                 ctxi.Close ();
206
207                                 return null;
208                         }
209
210                         // FIXME: supply maxSizeOfHeaders.
211                         int maxSizeOfHeaders = 0x10000;
212
213 #if false // FIXME: enable it, once duplex callback test gets passed.
214                         Stream stream = ctxi.Request.InputStream;
215                         if (source.Source.TransferMode == TransferMode.Buffered) {
216                                 if (ctxi.Request.ContentLength64 <= 0)
217                                         throw new ArgumentException ("This HTTP channel is configured to use buffered mode, and thus expects Content-Length sent to the listener");
218                                 long size = 0;
219                                 var ms = new MemoryStream ();
220                                 var buf = new byte [0x1000];
221                                 while (size < ctxi.Request.ContentLength64) {
222                                         if ((size += stream.Read (buf, 0, 0x1000)) > source.Source.MaxBufferSize)
223                                                 throw new QuotaExceededException ("Message quota exceeded");
224                                         ms.Write (buf, 0, (int) (size - ms.Length));
225                                 }
226                                 ms.Position = 0;
227                                 stream = ms;
228                         }
229
230                         var msg = Encoder.ReadMessage (
231                                 stream, maxSizeOfHeaders, ctxi.Request.ContentType);
232 #else
233                         var msg = Encoder.ReadMessage (
234                                 ctxi.Request.InputStream, maxSizeOfHeaders, ctxi.Request.ContentType);
235 #endif
236
237                         if (MessageVersion.Envelope.Equals (EnvelopeVersion.Soap11) ||
238                             MessageVersion.Addressing.Equals (AddressingVersion.None)) {
239                                 string action = GetHeaderItem (ctxi.Request.Headers ["SOAPAction"]);
240                                 if (action != null) {
241                                         if (action.Length > 2 && action [0] == '"' && action [action.Length] == '"')
242                                                 action = action.Substring (1, action.Length - 2);
243                                         msg.Headers.Action = action;
244                                 }
245                         }
246                         msg.Properties.Add (RemoteEndpointMessageProperty.Name, new RemoteEndpointMessageProperty (ctxi.Request.ClientIPAddress, ctxi.Request.ClientPort));
247
248                         return msg;
249                 }
250
251                 public override bool WaitForRequest (TimeSpan timeout)
252                 {
253                         throw new NotImplementedException ();
254                 }
255         }
256 }