2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / WebClientProtocol.cs
index f6a01973cea38e15e7b14866c707b43e1fa80aab..65f0f0852597b8e4e67d6541a1121529caa2a9e0 100644 (file)
@@ -6,10 +6,33 @@
 //\r
 // Copyright (C) Tim Coleman, 2002\r
 //\r
-\r
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+\r
+using System.Collections.Specialized;\r
 using System.ComponentModel;\r
 using System.Net;\r
 using System.Text;\r
+using System.Threading;\r
 using System.Web.Services;\r
 \r
 namespace System.Web.Services.Protocols {\r
@@ -23,11 +46,28 @@ namespace System.Web.Services.Protocols {
                Encoding requestEncoding;\r
                int timeout;\r
                string url;\r
-\r
+               bool abort;\r
+\r
+               //\r
+               // Used by SoapHttpClientProtocol, use this to avoid creating a new Uri on each invocation.\r
+               //\r
+               internal Uri uri;\r
+                       \r
+               //\r
+               // Points to the current request, so we can call Abort() on it\r
+               //\r
+               WebRequest current_request;\r
+               \r
+               static HybridDictionary cache;\r
                #endregion\r
 \r
                #region Constructors\r
 \r
+               static WebClientProtocol ()\r
+               {\r
+                       cache = new HybridDictionary ();\r
+               }\r
+\r
                protected WebClientProtocol () \r
                {\r
                        connectionGroupName = String.Empty;\r
@@ -36,6 +76,7 @@ namespace System.Web.Services.Protocols {
                        requestEncoding = null;\r
                        timeout = 100000;\r
                        url = String.Empty;\r
+                       abort = false;\r
                }\r
                \r
                #endregion // Constructors\r
@@ -81,47 +122,75 @@ namespace System.Web.Services.Protocols {
                [WebServicesDescription ("The base URL to the server to use for requests.")]\r
                public string Url {\r
                        get { return url; }\r
-                       set { url = value; }\r
+                       set {\r
+                               url = value;\r
+                               uri = new Uri (url);\r
+                       }\r
                }\r
 \r
                #endregion // Properties\r
 \r
                #region Methods\r
 \r
-               [MonoTODO]\r
                public virtual void Abort ()\r
                {\r
-                       throw new NotImplementedException ();\r
+                       if (current_request != null){\r
+                               current_request.Abort ();\r
+                               current_request = null;\r
+                       }\r
+                       abort = true;\r
                }\r
 \r
-               [MonoTODO]\r
                protected static void AddToCache (Type type, object value)\r
                {\r
-                       throw new NotImplementedException ();\r
+                       cache [type] = value;\r
                }\r
 \r
-               [MonoTODO]\r
                protected static object GetFromCache (Type type)\r
                {\r
-                       throw new NotImplementedException ();\r
+                       return cache [type];\r
                }\r
 \r
-               [MonoTODO]\r
                protected virtual WebRequest GetWebRequest (Uri uri)\r
                {\r
-                       throw new NotImplementedException ();\r
+                       if (uri == null)\r
+                               throw new InvalidOperationException ("uri is null");\r
+\r
+                       current_request = WebRequest.Create (uri);\r
+                       current_request.Timeout = timeout;\r
+                       current_request.PreAuthenticate = preAuthenticate;\r
+                       current_request.ConnectionGroupName = connectionGroupName;\r
+\r
+                       if (credentials != null)\r
+                               current_request.Credentials = credentials;\r
+\r
+                       return current_request;\r
                }\r
 \r
-               [MonoTODO]\r
                protected virtual WebResponse GetWebResponse (WebRequest request)\r
                {\r
-                       throw new NotImplementedException ();\r
+                       if (abort)\r
+                               throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);\r
+\r
+                       WebResponse response = null;\r
+                       try {\r
+                               request.Timeout = timeout;\r
+                               response = request.GetResponse ();\r
+                       } catch (WebException e) {\r
+                               response = e.Response;\r
+                               if (response == null)\r
+                                       throw;\r
+                       }\r
+\r
+                       return response;\r
                }\r
 \r
-               [MonoTODO]\r
                protected virtual WebResponse GetWebResponse (WebRequest request, IAsyncResult result)\r
                {\r
-                       throw new NotImplementedException ();\r
+                       if (abort)\r
+                               throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);\r
+\r
+                       return request.EndGetResponse (result);\r
                }\r
 \r
                #endregion // Methods\r