Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / WebClientProtocol.cs
1 // 
2 // System.Web.Services.Protocols.WebClientProtocol.cs
3 //
4 // Author:
5 //   Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2002
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Collections.Specialized;
32 using System.ComponentModel;
33 using System.Net;
34 using System.Text;
35 using System.Threading;
36 using System.Web.Services;
37
38 namespace System.Web.Services.Protocols {
39         [System.Runtime.InteropServices.ComVisible (true)]
40         public abstract class WebClientProtocol : Component {
41
42                 #region Fields
43
44                 string connectionGroupName;
45                 ICredentials credentials;
46                 bool preAuthenticate;
47                 Encoding requestEncoding;
48                 int timeout;
49
50                 //
51                 // Used by SoapHttpClientProtocol, use this to avoid creating a new Uri on each invocation.
52                 //
53                 internal Uri uri;
54                         
55                 //
56                 // Points to the current request, so we can call Abort() on it
57                 //
58                 WebRequest current_request;
59                 
60                 static HybridDictionary cache;
61                 #endregion
62
63                 #region Constructors
64
65                 static WebClientProtocol ()
66                 {
67                         cache = new HybridDictionary ();
68                 }
69
70                 protected WebClientProtocol () 
71                 {
72                         connectionGroupName = String.Empty;
73                         credentials = null;
74                         preAuthenticate = false;
75                         requestEncoding = null;
76                         timeout = 100000;
77                 }
78                 
79                 #endregion // Constructors
80
81                 #region Properties
82
83                 [DefaultValue ("")]
84                 public string ConnectionGroupName {
85                         get { return connectionGroupName; }
86                         set { connectionGroupName = value; }
87                 }
88
89                 public ICredentials Credentials {
90                         get { return credentials; }
91                         set { credentials = value; }
92                 }
93
94                 [DefaultValue (false)]
95                 [WebServicesDescription ("Enables pre authentication of the request.")]
96                 public bool PreAuthenticate {
97                         get { return preAuthenticate; }
98                         set { preAuthenticate = value; }
99                 }
100
101                 [DefaultValue (null)]
102                 [RecommendedAsConfigurable (true)]
103                 [WebServicesDescription ("The encoding to use for requests.")]
104                 public Encoding RequestEncoding {
105                         get { return requestEncoding; }
106                         set { requestEncoding = value; }
107                 }
108
109                 [DefaultValue (100000)]
110                 [RecommendedAsConfigurable (true)]
111                 [WebServicesDescription ("Sets the timeout in milliseconds to be used for synchronous calls.  The default of -1 means infinite.")]
112                 public int Timeout {
113                         get { return timeout; }
114                         set { timeout = value; }
115                 }
116
117                 [DefaultValue ("")]
118                 [RecommendedAsConfigurable (true)]
119                 [WebServicesDescription ("The base URL to the server to use for requests.")]
120                 public string Url {
121                         get { return uri == null ? String.Empty : uri.AbsoluteUri; }
122                         set { uri = new Uri (value); }
123                 }
124                 public bool UseDefaultCredentials {
125                         get { return CredentialCache.DefaultCredentials == Credentials; }
126                         set { Credentials = value ? CredentialCache.DefaultCredentials : null; }
127                 }
128
129                 #endregion // Properties
130
131                 #region Methods
132
133                 public virtual void Abort ()
134                 {
135                         WebRequest request = current_request;
136                         current_request = null;
137                         if (request != null) 
138                                 request.Abort ();
139                 }
140
141                 protected static void AddToCache (Type type, object value)
142                 {
143                         cache [type] = value;
144                 }
145
146                 protected static object GetFromCache (Type type)
147                 {
148                         return cache [type];
149                 }
150
151                 protected virtual WebRequest GetWebRequest (Uri uri)
152                 {
153                         if (uri == null)
154                                 throw new InvalidOperationException ("uri is null");
155
156                         WebRequest request = WebRequest.Create (uri);
157                         request.Timeout = timeout;
158                         request.PreAuthenticate = preAuthenticate;
159                         request.ConnectionGroupName = connectionGroupName;
160
161                         if (credentials != null)
162                                 request.Credentials = credentials;
163
164                         current_request = request;
165                         return request;
166                 }
167
168                 protected virtual WebResponse GetWebResponse (WebRequest request)
169                 {
170                         WebResponse response = null;
171                         try {
172                                 request.Timeout = timeout;
173                                 response = request.GetResponse ();
174                         } catch (WebException e) {
175                                 response = e.Response;
176                                 if (response == null)
177                                         throw;
178                         }
179
180                         return response;
181                 }
182
183                 protected virtual WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
184                 {
185                         return request.EndGetResponse (result);
186                 }
187
188                 #endregion // Methods
189         }
190 }