[bcl] Rename aot_hybrid profile to testing_aot_hybrid
[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         [System.Runtime.InteropServices.ComVisible (true)]
39         public abstract class HttpSimpleClientProtocol : HttpWebClientProtocol {
40
41                 #region Fields
42
43                 internal HttpSimpleTypeStubInfo TypeStub;
44                 
45                 #endregion // Fields
46
47                 #region Constructors
48
49                 protected HttpSimpleClientProtocol () 
50                 {
51                 }
52                 
53                 #endregion // Constructors
54
55                 #region Methods
56
57                 protected IAsyncResult BeginInvoke (string methodName, string requestUrl, object[] parameters, AsyncCallback callback, object asyncState)
58                 {
59                         HttpSimpleMethodStubInfo method = (HttpSimpleMethodStubInfo) TypeStub.GetMethod (methodName);
60                         SimpleWebClientAsyncResult ainfo = null;
61                         
62                         try
63                         {
64                                 MimeParameterWriter parameterWriter = (MimeParameterWriter) method.ParameterWriterType.Create ();
65                                 string url = parameterWriter.GetRequestUrl (requestUrl, parameters);
66                                 WebRequest req = GetWebRequest (new Uri(url));
67                                 
68                                 ainfo = new SimpleWebClientAsyncResult (req, callback, asyncState);
69                                 ainfo.Parameters = parameters;
70                                 ainfo.ParameterWriter = parameterWriter;
71                                 ainfo.Method = method;
72                                 
73                                 ainfo.Request = req;
74                                 ainfo.Request.BeginGetRequestStream (new AsyncCallback (AsyncGetRequestStreamDone), ainfo);
75                                 RegisterMapping (asyncState, ainfo);
76                         }
77                         catch (Exception ex)
78                         {
79                                 if (ainfo != null)
80                                         ainfo.SetCompleted (null, ex, false);
81                                 else
82                                         throw ex;
83                         }
84
85                         return ainfo;
86
87                 }
88
89                 void AsyncGetRequestStreamDone (IAsyncResult ar)
90                 {
91                         SimpleWebClientAsyncResult ainfo = (SimpleWebClientAsyncResult) ar.AsyncState;
92                         try
93                         {
94                                 if (ainfo.ParameterWriter.UsesWriteRequest)
95                                 {
96                                         Stream stream = ainfo.Request.GetRequestStream ();
97                                         ainfo.ParameterWriter.WriteRequest (stream, ainfo.Parameters);
98                                         stream.Close ();
99                                 }
100
101                                 ainfo.Request.BeginGetResponse (new AsyncCallback (AsyncGetResponseDone), ainfo);
102                         }
103                         catch (Exception ex)
104                         {
105                                 ainfo.SetCompleted (null, ex, true);
106                         }
107                 }
108
109                 void AsyncGetResponseDone (IAsyncResult ar)
110                 {
111                         SimpleWebClientAsyncResult ainfo = (SimpleWebClientAsyncResult) ar.AsyncState;
112                         WebResponse response = null;
113
114                         try {
115                                 response = GetWebResponse (ainfo.Request, ar);
116                         }
117                         catch (WebException ex) {
118                                 response = ex.Response;
119                                 HttpWebResponse http_response = response as HttpWebResponse;
120                                 if (http_response == null || http_response.StatusCode != HttpStatusCode.InternalServerError) {
121                                         ainfo.SetCompleted (null, ex, true);
122                                         return;
123                                 }
124                         }
125                         catch (Exception ex) {
126                                 ainfo.SetCompleted (null, ex, true);
127                                 return;
128                         }
129
130                         try {
131                                 MimeReturnReader returnReader = (MimeReturnReader) ainfo.Method.ReturnReaderType.Create ();
132                                 object result = returnReader.Read (response, response.GetResponseStream ());
133                                 ainfo.SetCompleted (result, null, true);
134                         }
135                         catch (Exception ex) {
136                                 ainfo.SetCompleted (null, ex, true);
137                         }
138                 }
139
140
141                 protected object EndInvoke (IAsyncResult asyncResult)
142                 {
143                         if (!(asyncResult is SimpleWebClientAsyncResult))
144                                 throw new ArgumentException ("asyncResult is not the return value from BeginInvoke");
145
146                         SimpleWebClientAsyncResult ainfo = (SimpleWebClientAsyncResult) asyncResult;
147                         lock (ainfo)
148                         {
149                                 if (!ainfo.IsCompleted)
150                                         ainfo.WaitForComplete ();
151
152                                 UnregisterMapping (ainfo.AsyncState);
153                                 
154                                 if (ainfo.Exception != null)
155                                         throw ainfo.Exception;
156                                 else
157                                         return ainfo.Result;
158                         }
159                 }
160
161                 protected object Invoke (string methodName, string requestUrl, object[] parameters)
162                 {
163                         HttpSimpleMethodStubInfo method = (HttpSimpleMethodStubInfo) TypeStub.GetMethod (methodName);
164                         MimeParameterWriter parameterWriter = (MimeParameterWriter) method.ParameterWriterType.Create ();
165                         
166                         string url = parameterWriter.GetRequestUrl (requestUrl, parameters);
167                         WebRequest request = GetWebRequest (new Uri(url, true));
168                         
169                         parameterWriter.InitializeRequest (request, parameters);
170                         
171                         if (parameterWriter.UsesWriteRequest)
172                         {
173                                 Stream stream = request.GetRequestStream ();
174                                 parameterWriter.WriteRequest (stream, parameters);
175                                 stream.Close ();
176                         }
177                         
178                         WebResponse response = GetWebResponse (request);
179                         
180                         MimeReturnReader returnReader = (MimeReturnReader) method.ReturnReaderType.Create ();
181                         return returnReader.Read (response, response.GetResponseStream ());
182                 }
183                 
184
185                 protected void InvokeAsync (string methodName, string requestUrl, object[] parameters, SendOrPostCallback callback)
186                 {
187                         InvokeAsync (methodName, requestUrl, parameters, callback, null);
188                 }
189
190                 protected void InvokeAsync (string methodName, string requestUrl, object[] parameters, SendOrPostCallback callback, object userState)
191                 {
192                         InvokeAsyncInfo info = new InvokeAsyncInfo (callback, userState);
193                         BeginInvoke (methodName, requestUrl, parameters, new AsyncCallback (InvokeAsyncCallback), info);
194                 }
195
196                 void InvokeAsyncCallback (IAsyncResult ar)
197                 {
198                         InvokeAsyncInfo info = (InvokeAsyncInfo) ar.AsyncState;
199                         SimpleWebClientAsyncResult sar = (SimpleWebClientAsyncResult) ar;
200                         InvokeCompletedEventArgs args = new InvokeCompletedEventArgs (sar.Exception, false, info.UserState, (object[]) sar.Result);
201                         if (info.Context != null)
202                                 info.Context.Send (info.Callback, args);
203                         else
204                                 info.Callback (args);
205                 }
206                 
207                 #endregion // Methods
208         }
209         
210         internal class SimpleWebClientAsyncResult : WebClientAsyncResult
211         {
212                 public SimpleWebClientAsyncResult (WebRequest request, AsyncCallback callback, object asyncState)
213                 : base (request, callback, asyncState)
214                 {
215                 }
216                 
217                 public object[] Parameters;
218                 public HttpSimpleMethodStubInfo Method;
219                 public MimeParameterWriter ParameterWriter;
220         }
221 }