2 // System.Web.Services.Protocols.HttpWebClientProtocol.cs
5 // Tim Coleman (tim@timcoleman.com)
7 // Copyright (C) Tim Coleman, 2002
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.ComponentModel;
34 using System.Security.Cryptography.X509Certificates;
35 using System.Threading;
36 using System.Web.Services;
37 using System.Collections;
39 namespace System.Web.Services.Protocols {
40 [System.Runtime.InteropServices.ComVisible (true)]
41 public abstract class HttpWebClientProtocol : WebClientProtocol {
45 bool allowAutoRedirect, enableDecompression;
46 X509CertificateCollection clientCertificates;
47 CookieContainer cookieContainer;
51 bool _unsafeAuthenticated;
56 protected HttpWebClientProtocol ()
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);
65 #endregion // Constructors
69 [DefaultValue (false)]
70 [WebServicesDescription ("Enable automatic handling of server redirects.")]
71 public bool AllowAutoRedirect {
72 get { return allowAutoRedirect; }
73 set { allowAutoRedirect = value; }
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 {
81 if (clientCertificates == null)
82 clientCertificates = new X509CertificateCollection ();
83 return clientCertificates;
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; }
94 [DefaultValue (false)]
95 public bool EnableDecompression {
96 get { return enableDecompression; }
97 set { enableDecompression = value; }
101 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
102 public IWebProxy Proxy {
103 get { return proxy; }
104 set { proxy = value; }
107 [WebServicesDescription ("Sets the user agent http header for the request.")]
109 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
110 public string UserAgent {
111 get { return userAgent; }
112 set { userAgent = value; }
116 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
117 public bool UnsafeAuthenticatedConnectionSharing
119 get { return _unsafeAuthenticated; }
120 set { _unsafeAuthenticated = value; }
123 #endregion // Properties
127 internal virtual void CheckForCookies (HttpWebResponse response)
129 CookieCollection cookies = response.Cookies;
130 if (cookieContainer == null || cookies.Count == 0)
133 CookieCollection coll = cookieContainer.GetCookies (uri);
134 foreach (Cookie c in cookies) {
136 foreach (Cookie prev in coll) {
137 if (c.Equals (prev)) {
143 cookieContainer.Add (c);
147 protected override WebRequest GetWebRequest (Uri uri)
149 WebRequest req = base.GetWebRequest (uri);
150 HttpWebRequest request = req as HttpWebRequest;
153 if (enableDecompression)
154 request.AutomaticDecompression = DecompressionMethods.GZip;
156 request.AllowAutoRedirect = allowAutoRedirect;
157 if (clientCertificates != null)
158 request.ClientCertificates.AddRange (clientCertificates);
160 request.CookieContainer = cookieContainer;
162 request.Proxy = proxy;
164 request.UserAgent = userAgent;
169 protected override WebResponse GetWebResponse (WebRequest request)
171 WebResponse response = base.GetWebResponse (request);
172 HttpWebResponse wr = response as HttpWebResponse;
174 CheckForCookies (wr);
179 protected override WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
181 WebResponse response = base.GetWebResponse (request, result);
182 HttpWebResponse wr = response as HttpWebResponse;
184 CheckForCookies (wr);
189 Hashtable mappings = new Hashtable ();
191 internal void RegisterMapping (object userState, WebClientAsyncResult result)
193 if (userState == null)
194 userState = typeof (string);
196 mappings [userState] = result;
199 internal void UnregisterMapping (object userState)
201 if (userState == null)
202 userState = typeof (string);
204 mappings.Remove (userState);
207 protected void CancelAsync (object userState)
209 WebClientAsyncResult result = (WebClientAsyncResult) mappings [userState];
214 mappings.Remove (userState);
219 public static bool GenerateXmlMappings (Type type, ArrayList mapping)
221 throw new NotImplementedException ();
225 public static Hashtable GenerateXmlMappings (Type[] types, ArrayList mapping)
227 throw new NotImplementedException ();
230 #endregion // Methods
233 internal class InvokeAsyncInfo
235 public SynchronizationContext Context;
236 public object UserState;
237 public SendOrPostCallback Callback;
239 public InvokeAsyncInfo (SendOrPostCallback callback, object userState)
242 UserState = userState;
243 Context = SynchronizationContext.Current;