[bcl] Remove NET_2_0 defines from the class libs. This has been done using: unifdef...
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / HttpWebClientProtocol.cs
1 // 
2 // System.Web.Services.Protocols.HttpWebClientProtocol.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;
32 using System.ComponentModel;
33 using System.Net;
34 using System.Security.Cryptography.X509Certificates;
35 using System.Threading;
36 using System.Web.Services;
37 using System.Collections;
38
39 namespace System.Web.Services.Protocols {
40         [System.Runtime.InteropServices.ComVisible (true)]
41         public abstract class HttpWebClientProtocol : WebClientProtocol {
42
43                 #region Fields
44
45                 bool allowAutoRedirect, enableDecompression;
46                 X509CertificateCollection clientCertificates;
47                 CookieContainer cookieContainer;
48                 IWebProxy proxy;
49                 string userAgent;
50                 
51                 bool _unsafeAuthenticated;
52                 #endregion
53
54                 #region Constructors
55
56                 protected HttpWebClientProtocol () 
57                 {
58                         allowAutoRedirect = false;
59                         clientCertificates = null;
60                         cookieContainer = null;
61                         proxy = null; // FIXME
62                         userAgent = String.Format ("Mono Web Services Client Protocol {0}", Environment.Version);
63                 }
64                 
65                 #endregion // Constructors
66
67                 #region Properties
68
69                 [DefaultValue (false)]
70                 [WebServicesDescription ("Enable automatic handling of server redirects.")]
71                 public bool AllowAutoRedirect {
72                         get { return allowAutoRedirect; }
73                         set { allowAutoRedirect = value; }
74                 }
75
76                 [Browsable (false)]
77                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
78                 [WebServicesDescription ("The client certificates that will be sent to the server, if the server requests them.")]
79                 public X509CertificateCollection ClientCertificates {
80                         get {
81                                 if (clientCertificates == null)
82                                         clientCertificates = new X509CertificateCollection ();
83                                 return clientCertificates;
84                         }
85                 }
86
87                 [DefaultValue (null)]
88                 [WebServicesDescription ("A container for all cookies received from servers in the current session.")]
89                 public CookieContainer CookieContainer {
90                         get { return cookieContainer; }
91                         set { cookieContainer = value; }
92                 }
93
94                 [DefaultValue (false)]
95                 public bool EnableDecompression {
96                         get { return enableDecompression; }
97                         set { enableDecompression = value; }
98                 }
99
100                 [Browsable (false)]
101                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
102                 public IWebProxy Proxy {
103                         get { return proxy; }
104                         set { proxy = value; }
105                 }
106
107                 [WebServicesDescription ("Sets the user agent http header for the request.")]
108                 [Browsable (false)]
109                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
110                 public string UserAgent {
111                         get { return userAgent; }
112                         set { userAgent = value; }
113                 }
114                 
115                 [Browsable (false)]
116                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
117                 public bool UnsafeAuthenticatedConnectionSharing
118                 {
119                         get { return _unsafeAuthenticated; }
120                         set { _unsafeAuthenticated = value; }
121                 }
122
123                 #endregion // Properties
124
125                 #region Methods
126
127                 internal virtual void CheckForCookies (HttpWebResponse response)
128                 {
129                         CookieCollection cookies = response.Cookies;
130                         if (cookieContainer == null || cookies.Count == 0)
131                                 return;
132
133                         CookieCollection coll = cookieContainer.GetCookies (uri);
134                         foreach (Cookie c in cookies) {
135                                 bool add = true;
136                                 foreach (Cookie prev in coll) {
137                                         if (c.Equals (prev)) {
138                                                 add = false;
139                                                 break;
140                                         }
141                                 }
142                                 if (add)
143                                         cookieContainer.Add (c);
144                         }
145                 }
146                 
147                 protected override WebRequest GetWebRequest (Uri uri)
148                 {
149                         WebRequest req = base.GetWebRequest (uri);
150                         HttpWebRequest request = req as HttpWebRequest;
151                         if (request == null)
152                                 return req;
153                         if (enableDecompression)
154                                 request.AutomaticDecompression = DecompressionMethods.GZip;
155
156                         request.AllowAutoRedirect = allowAutoRedirect;
157                         if (clientCertificates != null)
158                                 request.ClientCertificates.AddRange (clientCertificates);
159
160                         request.CookieContainer = cookieContainer;
161                         if (proxy != null)
162                                 request.Proxy = proxy;
163
164                         request.UserAgent = userAgent;
165
166                         return request;
167                 }
168
169                 protected override WebResponse GetWebResponse (WebRequest request)
170                 {
171                         WebResponse response = base.GetWebResponse (request);
172                         HttpWebResponse wr = response as HttpWebResponse;
173                         if (wr != null)
174                                 CheckForCookies (wr);
175                                 
176                         return response;
177                 }
178
179                 protected override WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
180                 {
181                         WebResponse response = base.GetWebResponse (request, result);
182                         HttpWebResponse wr = response as HttpWebResponse;
183                         if (wr != null)
184                                 CheckForCookies (wr);
185                                 
186                         return response;
187                 }
188                 
189                 Hashtable mappings = new Hashtable ();
190                 
191                 internal void RegisterMapping (object userState, WebClientAsyncResult result)
192                 {
193                         if (userState == null)
194                                 userState = typeof (string);
195                         
196                         mappings [userState] = result;
197                 }
198
199                 internal void UnregisterMapping (object userState)
200                 {
201                         if (userState == null)
202                                 userState = typeof (string);
203                         
204                         mappings.Remove (userState);
205                 }
206                 
207                 protected void CancelAsync (object userState)
208                 {
209                         WebClientAsyncResult result = (WebClientAsyncResult) mappings [userState];
210
211                         if (result == null)
212                                 return;
213                         
214                         mappings.Remove (userState);
215                         result.Abort ();
216                 }
217
218                 [MonoTODO]
219                 public static bool GenerateXmlMappings (Type type, ArrayList mapping)
220                 {
221                         throw new NotImplementedException ();
222                 }
223
224                 [MonoTODO]
225                 public static Hashtable GenerateXmlMappings (Type[] types, ArrayList mapping)
226                 {
227                         throw new NotImplementedException ();
228                 }
229
230                 #endregion // Methods
231         }
232         
233         internal class InvokeAsyncInfo
234         {
235                 public SynchronizationContext Context;
236                 public object UserState;
237                 public SendOrPostCallback Callback;
238                 
239                 public InvokeAsyncInfo (SendOrPostCallback callback, object userState)
240                 {
241                         Callback = callback;
242                         UserState = userState;
243                         Context = SynchronizationContext.Current;
244                 }
245         }
246 }