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 {
41 [System.Runtime.InteropServices.ComVisible (true)]
43 public abstract class HttpWebClientProtocol : WebClientProtocol {
47 bool allowAutoRedirect, enableDecompression;
48 X509CertificateCollection clientCertificates;
49 CookieContainer cookieContainer;
53 bool _unsafeAuthenticated;
58 protected HttpWebClientProtocol ()
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);
67 #endregion // Constructors
71 [DefaultValue (false)]
72 [WebServicesDescription ("Enable automatic handling of server redirects.")]
73 public bool AllowAutoRedirect {
74 get { return allowAutoRedirect; }
75 set { allowAutoRedirect = value; }
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 {
83 if (clientCertificates == null)
84 clientCertificates = new X509CertificateCollection ();
85 return clientCertificates;
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; }
97 [DefaultValue (false)]
98 public bool EnableDecompression {
99 get { return enableDecompression; }
100 set { enableDecompression = value; }
105 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
106 public IWebProxy Proxy {
107 get { return proxy; }
108 set { proxy = value; }
111 [WebServicesDescription ("Sets the user agent http header for the request.")]
113 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
114 public string UserAgent {
115 get { return userAgent; }
116 set { userAgent = value; }
120 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
121 public bool UnsafeAuthenticatedConnectionSharing
123 get { return _unsafeAuthenticated; }
124 set { _unsafeAuthenticated = value; }
127 #endregion // Properties
131 internal virtual void CheckForCookies (HttpWebResponse response)
133 CookieCollection cookies = response.Cookies;
134 if (cookieContainer == null || cookies.Count == 0)
137 CookieCollection coll = cookieContainer.GetCookies (uri);
138 foreach (Cookie c in cookies) {
140 foreach (Cookie prev in coll) {
141 if (c.Equals (prev)) {
147 cookieContainer.Add (c);
151 protected override WebRequest GetWebRequest (Uri uri)
153 WebRequest req = base.GetWebRequest (uri);
154 HttpWebRequest request = req as HttpWebRequest;
157 if (enableDecompression)
158 request.AutomaticDecompression = DecompressionMethods.GZip;
160 request.AllowAutoRedirect = allowAutoRedirect;
161 if (clientCertificates != null)
162 request.ClientCertificates.AddRange (clientCertificates);
164 request.CookieContainer = cookieContainer;
166 request.Proxy = proxy;
168 request.UserAgent = userAgent;
173 protected override WebResponse GetWebResponse (WebRequest request)
175 WebResponse response = base.GetWebResponse (request);
176 HttpWebResponse wr = response as HttpWebResponse;
178 CheckForCookies (wr);
183 protected override WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
185 WebResponse response = base.GetWebResponse (request, result);
186 HttpWebResponse wr = response as HttpWebResponse;
188 CheckForCookies (wr);
194 Hashtable mappings = new Hashtable ();
196 internal void RegisterMapping (object userState, WebClientAsyncResult result)
198 if (userState == null)
199 userState = typeof (string);
201 mappings [userState] = result;
204 internal void UnregisterMapping (object userState)
206 if (userState == null)
207 userState = typeof (string);
209 mappings.Remove (userState);
212 protected void CancelAsync (object userState)
214 WebClientAsyncResult result = (WebClientAsyncResult) mappings [userState];
219 mappings.Remove (userState);
224 public static bool GenerateXmlMappings (Type type, ArrayList mapping)
226 throw new NotImplementedException ();
230 public static Hashtable GenerateXmlMappings (Type[] types, ArrayList mapping)
232 throw new NotImplementedException ();
235 internal void UnregisterMapping (object userState)
239 internal void RegisterMapping (object userState, WebClientAsyncResult result)
244 #endregion // Methods
248 internal class InvokeAsyncInfo
250 public SynchronizationContext Context;
251 public object UserState;
252 public SendOrPostCallback Callback;
254 public InvokeAsyncInfo (SendOrPostCallback callback, object userState)
257 UserState = userState;
258 Context = SynchronizationContext.Current;