New test.
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / SoapHttpClientProtocol.cs
1 // 
2 // System.Web.Services.Protocols.SoapHttpClientProtocol.cs
3 //
4 // Author:
5 //   Tim Coleman (tim@timcoleman.com)
6 //   Miguel de Icaza (miguel@ximian.com)
7 //   Lluis Sanchez Gual (lluis@ximian.com)
8 //
9 // Copyright (C) Tim Coleman, 2002
10 // Copyright (C) Ximian, Inc, 2003
11 //
12
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System.ComponentModel;
35 using System.IO;
36 using System.Net;
37 using System.Web;
38 using System.Xml;
39 using System.Text;
40 using System.Reflection;
41 using System.Web.Services;
42 using System.Diagnostics;
43 using System.Runtime.CompilerServices;
44 using System.Web.Services.Description;
45 using System.Web.Services.Discovery;
46 using System.Xml.Serialization;
47 using System.Xml.Schema;
48 using System.Collections;
49 using System.Threading;
50
51 namespace System.Web.Services.Protocols 
52 {
53 #if NET_2_0
54         [System.Runtime.InteropServices.ComVisible (true)]
55 #endif
56         public class SoapHttpClientProtocol : HttpWebClientProtocol 
57         {
58                 SoapTypeStubInfo type_info;
59 #if NET_2_0
60                 SoapProtocolVersion soapVersion;
61 #endif
62
63                 #region SoapWebClientAsyncResult class
64
65                 internal class SoapWebClientAsyncResult: WebClientAsyncResult
66                 {
67                         public SoapWebClientAsyncResult (WebRequest request, AsyncCallback callback, object asyncState)
68                         : base (request, callback, asyncState)
69                         {
70                         }
71                 
72                         public SoapClientMessage Message;
73                         public SoapExtension[] Extensions;
74                 }
75                 #endregion
76
77                 #region Constructors
78
79                 public SoapHttpClientProtocol () 
80                 {
81                         type_info = (SoapTypeStubInfo) TypeStubManager.GetTypeStub (this.GetType (), "Soap");
82                 }
83
84                 #endregion // Constructors
85
86                 #region Methods
87
88                 protected IAsyncResult BeginInvoke (string methodName, object[] parameters, AsyncCallback callback, object asyncState)
89                 {
90                         SoapMethodStubInfo msi = (SoapMethodStubInfo) type_info.GetMethod (methodName);
91
92                         SoapWebClientAsyncResult ainfo = null;
93                         try
94                         {
95                                 SoapClientMessage message = new SoapClientMessage (this, msi, Url, parameters);
96                                 message.CollectHeaders (this, message.MethodStubInfo.Headers, SoapHeaderDirection.In);
97                                 
98                                 WebRequest request = GetRequestForMessage (uri, message);
99                                 
100                                 ainfo = new SoapWebClientAsyncResult (request, callback, asyncState);
101                                 ainfo.Message = message;
102                                 ainfo.Extensions = SoapExtension.CreateExtensionChain (type_info.SoapExtensions[0], msi.SoapExtensions, type_info.SoapExtensions[1]);
103
104                                 ainfo.Request.BeginGetRequestStream (new AsyncCallback (AsyncGetRequestStreamDone), ainfo);
105                         }
106                         catch (Exception ex)
107                         {
108                                 if (ainfo != null)
109                                         ainfo.SetCompleted (null, ex, false);
110                         }
111
112                         return ainfo;
113                 }
114
115                 void AsyncGetRequestStreamDone (IAsyncResult ar)
116                 {
117                         SoapWebClientAsyncResult ainfo = (SoapWebClientAsyncResult) ar.AsyncState;
118                         try
119                         {
120                                 SendRequest (ainfo.Request.EndGetRequestStream (ar), ainfo.Message, ainfo.Extensions);
121                                 ainfo.Request.BeginGetResponse (new AsyncCallback (AsyncGetResponseDone), ainfo);
122                         }
123                         catch (Exception ex)
124                         {
125                                 ainfo.SetCompleted (null, ex, true);
126                         }
127                 }
128
129                 void AsyncGetResponseDone (IAsyncResult ar)
130                 {
131                         SoapWebClientAsyncResult ainfo = (SoapWebClientAsyncResult) ar.AsyncState;
132                         WebResponse response = null;
133
134                         try {
135                                 response = GetWebResponse (ainfo.Request, ar);
136                         }
137                         catch (WebException ex) {
138                                 response = ex.Response;
139                                 HttpWebResponse http_response = response as HttpWebResponse;
140                                 if (http_response == null || http_response.StatusCode != HttpStatusCode.InternalServerError) {
141                                         ainfo.SetCompleted (null, ex, true);
142                                         return;
143                                 }
144                         }
145                         catch (Exception ex) {
146                                 ainfo.SetCompleted (null, ex, true);
147                                 return;
148                         }
149
150                         try {
151                                 object[] result = ReceiveResponse (response, ainfo.Message, ainfo.Extensions);
152                                 ainfo.SetCompleted (result, null, true);
153                         }
154                         catch (Exception ex) {
155                                 ainfo.SetCompleted (null, ex, true);
156                         }
157                         finally {
158                                 response.Close();
159                         }
160                 }
161
162                 protected object[] EndInvoke (IAsyncResult asyncResult)
163                 {
164                         if (!(asyncResult is SoapWebClientAsyncResult)) throw new ArgumentException ("asyncResult is not the return value from BeginInvoke");
165
166                         SoapWebClientAsyncResult ainfo = (SoapWebClientAsyncResult) asyncResult;
167                         lock (ainfo)
168                         {
169                                 if (!ainfo.IsCompleted) ainfo.WaitForComplete ();
170                                 if (ainfo.Exception != null) throw ainfo.Exception;
171                                 else return (object[]) ainfo.Result;
172                         }
173                 }
174
175                 public void Discover ()
176                 {
177                         BindingInfo bnd = (BindingInfo) type_info.Bindings [0];
178                         
179                         DiscoveryClientProtocol discoverer = new DiscoveryClientProtocol ();
180                         discoverer.Discover (Url);
181                         
182                         foreach (object info in discoverer.AdditionalInformation)
183                         {
184                                 System.Web.Services.Discovery.SoapBinding sb = info as System.Web.Services.Discovery.SoapBinding;
185                                 if (sb != null && sb.Binding.Name == bnd.Name && sb.Binding.Namespace == bnd.Namespace) {
186                                         Url = sb.Address;
187                                         return;
188                                 }
189                         }
190                         
191                         string msg = string.Format ("The binding named '{0}' from namespace '{1}' was not found in the discovery document at '{2}'", bnd.Name, bnd.Namespace, Url);
192                         throw new Exception (msg);
193                 }
194
195                 protected override WebRequest GetWebRequest (Uri uri)
196                 {
197                         return base.GetWebRequest (uri);
198                 }
199
200                 WebRequest GetRequestForMessage (Uri uri, SoapClientMessage message)
201                 {
202                         WebRequest request = GetWebRequest (uri);
203                         request.Method = "POST";
204                         WebHeaderCollection headers = request.Headers;
205                         headers.Add ("SOAPAction", "\"" + message.Action + "\"");
206                         request.ContentType = message.ContentType + "; charset=utf-8";
207                         return request;
208                 }
209                 
210                 void SendRequest (Stream s, SoapClientMessage message, SoapExtension[] extensions)
211                 {
212                         using (s) {
213
214                                 if (extensions != null) {
215                                         s = SoapExtension.ExecuteChainStream (extensions, s);
216                                         message.SetStage (SoapMessageStage.BeforeSerialize);
217                                         SoapExtension.ExecuteProcessMessage (extensions, message, s, true);
218                                 }
219
220                                 XmlTextWriter xtw = WebServiceHelper.CreateXmlWriter (s);
221                                 
222                                 WebServiceHelper.WriteSoapMessage (xtw, message.MethodStubInfo, SoapHeaderDirection.In, message.Parameters, message.Headers);
223
224                                 if (extensions != null) {
225                                         message.SetStage (SoapMessageStage.AfterSerialize);
226                                         SoapExtension.ExecuteProcessMessage (extensions, message, s, true);
227                                 }
228
229                                 xtw.Flush ();
230                                 xtw.Close ();
231                          }
232                 }
233
234
235                 //
236                 // TODO:
237                 //    Handle other web responses (multi-output?)
238                 //    
239                 object [] ReceiveResponse (WebResponse response, SoapClientMessage message, SoapExtension[] extensions)
240                 {
241                         SoapMethodStubInfo msi = message.MethodStubInfo;
242                         HttpWebResponse http_response = response as HttpWebResponse;
243                         
244                         if (http_response != null)
245                         {
246                                 HttpStatusCode code = http_response.StatusCode;
247         
248                                 if (!(code == HttpStatusCode.Accepted || code == HttpStatusCode.OK || code == HttpStatusCode.InternalServerError)) {
249                                         string msg = "The request failed with HTTP status {0}: {1}";
250                                         msg = String.Format (msg, (int) code, code);
251                                         throw new WebException (msg, null, WebExceptionStatus.ProtocolError, http_response);
252                                 }
253                                 if (message.OneWay && response.ContentLength <= 0 && (code == HttpStatusCode.Accepted || code == HttpStatusCode.OK)) {
254                                         return new object[0];
255                                 }
256                         }
257                         
258                         //
259                         // Remove optional encoding
260                         //
261                         string ctype;
262                         Encoding encoding = WebServiceHelper.GetContentEncoding (response.ContentType, out ctype);
263                         if (ctype != "text/xml")
264                                 WebServiceHelper.InvalidOperation (
265                                         "Content is not 'text/xml' but '" + response.ContentType + "'",
266                                         response, encoding);
267
268                         message.ContentType = ctype;
269                         message.ContentEncoding = encoding.WebName;
270                         
271                         Stream stream = response.GetResponseStream ();
272
273                         if (extensions != null) {
274                                 stream = SoapExtension.ExecuteChainStream (extensions, stream);
275                                 message.SetStage (SoapMessageStage.BeforeDeserialize);
276                                 SoapExtension.ExecuteProcessMessage (extensions, message, stream, false);
277                         }
278                         
279                         // Deserialize the response
280
281                         SoapHeaderCollection headers;
282                         object content;
283
284                         using (StreamReader reader = new StreamReader (stream, encoding, false)) {
285                                 XmlTextReader xml_reader = new XmlTextReader (reader);
286
287                                 WebServiceHelper.ReadSoapMessage (xml_reader, msi, SoapHeaderDirection.Out, out content, out headers);
288                         }
289
290                         
291                         if (content is Fault)
292                         {
293                                 Fault fault = (Fault) content;
294                                 SoapException ex = new SoapException (fault.faultstring, fault.faultcode, fault.faultactor, fault.detail);
295                                 message.SetException (ex);
296                         }
297                         else
298                                 message.OutParameters = (object[]) content;
299                         
300                         message.SetHeaders (headers);
301                         message.UpdateHeaderValues (this, message.MethodStubInfo.Headers);
302
303                         if (extensions != null) {
304                                 message.SetStage (SoapMessageStage.AfterDeserialize);
305                                 SoapExtension.ExecuteProcessMessage (extensions, message, stream, false);
306                         }
307
308                         if (message.Exception == null)
309                                 return message.OutParameters;
310                         else
311                                 throw message.Exception;
312                 }
313
314                 protected object[] Invoke (string method_name, object[] parameters)
315                 {
316                         SoapMethodStubInfo msi = (SoapMethodStubInfo) type_info.GetMethod (method_name);
317                         
318                         SoapClientMessage message = new SoapClientMessage (this, msi, Url, parameters);
319                         message.CollectHeaders (this, message.MethodStubInfo.Headers, SoapHeaderDirection.In);
320
321                         SoapExtension[] extensions = SoapExtension.CreateExtensionChain (type_info.SoapExtensions[0], msi.SoapExtensions, type_info.SoapExtensions[1]);
322
323                         WebResponse response;
324                         try
325                         {
326                                 WebRequest request = GetRequestForMessage (uri, message);
327                                 SendRequest (request.GetRequestStream (), message, extensions);
328                                 response = GetWebResponse (request);
329                         }
330                         catch (WebException ex)
331                         {
332                                 response = ex.Response;
333                                 HttpWebResponse http_response = response as HttpWebResponse;
334                                 if (http_response == null || http_response.StatusCode != HttpStatusCode.InternalServerError)
335                                         throw ex;
336                         }
337
338                         try {
339                                 return ReceiveResponse (response, message, extensions);
340                         }
341                         finally {
342                                 response.Close();
343                         }
344                 }
345                 
346 #if NET_2_0
347
348                 [MonoTODO ("Do something with this")]
349                 [System.Runtime.InteropServices.ComVisible(false)]
350                 [DefaultValue (SoapProtocolVersion.Default)]
351                 public SoapProtocolVersion SoapVersion {
352                         get { return soapVersion; }
353                         set { soapVersion = value; }
354                 }
355                 
356                 protected void InvokeAsync (string methodName, object[] parameters, SendOrPostCallback callback)
357                 {
358                         InvokeAsync (methodName, parameters, callback, null);
359                 }
360
361                 protected void InvokeAsync (string methodName, object[] parameters, SendOrPostCallback callback, object userState)
362                 {
363                         InvokeAsyncInfo info = new InvokeAsyncInfo (callback, userState);
364                         BeginInvoke (methodName, parameters, new AsyncCallback (InvokeAsyncCallback), info);
365                 }
366                 
367                 void InvokeAsyncCallback (IAsyncResult ar)
368                 {
369                         InvokeAsyncInfo info = (InvokeAsyncInfo) ar.AsyncState;
370                         SoapWebClientAsyncResult sar = (SoapWebClientAsyncResult) ar;
371                         InvokeCompletedEventArgs args = new InvokeCompletedEventArgs (sar.Exception, false, info.UserState, (object[]) sar.Result);
372                         if (info.Context != null)
373                                 info.Context.Send (info.Callback, args);
374                         else
375                                 info.Callback (args);
376                 }
377
378 #endif
379
380                 #endregion // Methods
381         }
382 }
383