Merge branch 'patch-1' of https://github.com/ReubenBond/mono into ReubenBond-patch-1
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / HttpSimpleClientProtocol.cs
1 // 
2 // System.Web.Services.Protocols.HttpSimpleClientProtocol.cs
3 //
4 // Author:
5 //   Tim Coleman (tim@timcoleman.com)
6 //   Lluis Sanchez Gual (lluis@ximian.com)
7 //
8 // Copyright (C) Tim Coleman, 2002
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Web.Services;
33 using System.Net;
34 using System.IO;
35 using System.Threading;
36
37 namespace System.Web.Services.Protocols {
38 #if NET_2_0
39         [System.Runtime.InteropServices.ComVisible (true)]
40 #endif
41         public abstract class HttpSimpleClientProtocol : HttpWebClientProtocol {
42
43                 #region Fields
44
45                 internal HttpSimpleTypeStubInfo TypeStub;
46                 
47                 #endregion // Fields
48
49                 #region Constructors
50
51                 protected HttpSimpleClientProtocol () 
52                 {
53                 }
54                 
55                 #endregion // Constructors
56
57                 #region Methods
58
59                 protected IAsyncResult BeginInvoke (string methodName, string requestUrl, object[] parameters, AsyncCallback callback, object asyncState)
60                 {
61                         HttpSimpleMethodStubInfo method = (HttpSimpleMethodStubInfo) TypeStub.GetMethod (methodName);
62                         SimpleWebClientAsyncResult ainfo = null;
63                         
64                         try
65                         {
66                                 MimeParameterWriter parameterWriter = (MimeParameterWriter) method.ParameterWriterType.Create ();
67                                 string url = parameterWriter.GetRequestUrl (requestUrl, parameters);
68                                 WebRequest req = GetWebRequest (new Uri(url));
69                                 
70                                 ainfo = new SimpleWebClientAsyncResult (req, callback, asyncState);
71                                 ainfo.Parameters = parameters;
72                                 ainfo.ParameterWriter = parameterWriter;
73                                 ainfo.Method = method;
74                                 
75                                 ainfo.Request = req;
76                                 ainfo.Request.BeginGetRequestStream (new AsyncCallback (AsyncGetRequestStreamDone), ainfo);
77                                 RegisterMapping (asyncState, ainfo);
78                         }
79                         catch (Exception ex)
80                         {
81                                 if (ainfo != null)
82                                         ainfo.SetCompleted (null, ex, false);
83                                 else
84                                         throw ex;
85                         }
86
87                         return ainfo;
88
89                 }
90
91                 void AsyncGetRequestStreamDone (IAsyncResult ar)
92                 {
93                         SimpleWebClientAsyncResult ainfo = (SimpleWebClientAsyncResult) ar.AsyncState;
94                         try
95                         {
96                                 if (ainfo.ParameterWriter.UsesWriteRequest)
97                                 {
98                                         Stream stream = ainfo.Request.GetRequestStream ();
99                                         ainfo.ParameterWriter.WriteRequest (stream, ainfo.Parameters);
100                                         stream.Close ();
101                                 }
102
103                                 ainfo.Request.BeginGetResponse (new AsyncCallback (AsyncGetResponseDone), ainfo);
104                         }
105                         catch (Exception ex)
106                         {
107                                 ainfo.SetCompleted (null, ex, true);
108                         }
109                 }
110
111                 void AsyncGetResponseDone (IAsyncResult ar)
112                 {
113                         SimpleWebClientAsyncResult ainfo = (SimpleWebClientAsyncResult) ar.AsyncState;
114                         WebResponse response = null;
115
116                         try {
117                                 response = GetWebResponse (ainfo.Request, ar);
118                         }
119                         catch (WebException ex) {
120                                 response = ex.Response;
121                                 HttpWebResponse http_response = response as HttpWebResponse;
122                                 if (http_response == null || http_response.StatusCode != HttpStatusCode.InternalServerError) {
123                                         ainfo.SetCompleted (null, ex, true);
124                                         return;
125                                 }
126                         }
127                         catch (Exception ex) {
128                                 ainfo.SetCompleted (null, ex, true);
129                                 return;
130                         }
131
132                         try {
133                                 MimeReturnReader returnReader = (MimeReturnReader) ainfo.Method.ReturnReaderType.Create ();
134                                 object result = returnReader.Read (response, response.GetResponseStream ());
135                                 ainfo.SetCompleted (result, null, true);
136                         }
137                         catch (Exception ex) {
138                                 ainfo.SetCompleted (null, ex, true);
139                         }
140                 }
141
142
143                 protected object EndInvoke (IAsyncResult asyncResult)
144                 {
145                         if (!(asyncResult is SimpleWebClientAsyncResult))
146                                 throw new ArgumentException ("asyncResult is not the return value from BeginInvoke");
147
148                         SimpleWebClientAsyncResult ainfo = (SimpleWebClientAsyncResult) asyncResult;
149                         lock (ainfo)
150                         {
151                                 if (!ainfo.IsCompleted)
152                                         ainfo.WaitForComplete ();
153
154                                 UnregisterMapping (ainfo.AsyncState);
155                                 
156                                 if (ainfo.Exception != null)
157                                         throw ainfo.Exception;
158                                 else
159                                         return ainfo.Result;
160                         }
161                 }
162
163                 protected object Invoke (string methodName, string requestUrl, object[] parameters)
164                 {
165                         HttpSimpleMethodStubInfo method = (HttpSimpleMethodStubInfo) TypeStub.GetMethod (methodName);
166                         MimeParameterWriter parameterWriter = (MimeParameterWriter) method.ParameterWriterType.Create ();
167                         
168                         string url = parameterWriter.GetRequestUrl (requestUrl, parameters);
169                         WebRequest request = GetWebRequest (new Uri(url, true));
170                         
171                         parameterWriter.InitializeRequest (request, parameters);
172                         
173                         if (parameterWriter.UsesWriteRequest)
174                         {
175                                 Stream stream = request.GetRequestStream ();
176                                 parameterWriter.WriteRequest (stream, parameters);
177                                 stream.Close ();
178                         }
179                         
180                         WebResponse response = GetWebResponse (request);
181                         
182                         MimeReturnReader returnReader = (MimeReturnReader) method.ReturnReaderType.Create ();
183                         return returnReader.Read (response, response.GetResponseStream ());
184                 }
185                 
186 #if NET_2_0
187
188                 protected void InvokeAsync (string methodName, string requestUrl, object[] parameters, SendOrPostCallback callback)
189                 {
190                         InvokeAsync (methodName, requestUrl, parameters, callback, null);
191                 }
192
193                 protected void InvokeAsync (string methodName, string requestUrl, object[] parameters, SendOrPostCallback callback, object userState)
194                 {
195                         InvokeAsyncInfo info = new InvokeAsyncInfo (callback, userState);
196                         BeginInvoke (methodName, requestUrl, parameters, new AsyncCallback (InvokeAsyncCallback), info);
197                 }
198
199                 void InvokeAsyncCallback (IAsyncResult ar)
200                 {
201                         InvokeAsyncInfo info = (InvokeAsyncInfo) ar.AsyncState;
202                         SimpleWebClientAsyncResult sar = (SimpleWebClientAsyncResult) ar;
203                         InvokeCompletedEventArgs args = new InvokeCompletedEventArgs (sar.Exception, false, info.UserState, (object[]) sar.Result);
204                         if (info.Context != null)
205                                 info.Context.Send (info.Callback, args);
206                         else
207                                 info.Callback (args);
208                 }
209 #endif
210                 
211                 #endregion // Methods
212         }
213         
214         internal class SimpleWebClientAsyncResult : WebClientAsyncResult
215         {
216                 public SimpleWebClientAsyncResult (WebRequest request, AsyncCallback callback, object asyncState)
217                 : base (request, callback, asyncState)
218                 {
219                 }
220                 
221                 public object[] Parameters;
222                 public HttpSimpleMethodStubInfo Method;
223                 public MimeParameterWriter ParameterWriter;
224         }
225 }