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