HttpWebClientProtocol.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. //
  2. // System.Web.Services.Protocols.HttpWebClientProtocol.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2002
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.ComponentModel;
  31. using System.Net;
  32. using System.Security.Cryptography.X509Certificates;
  33. using System.Threading;
  34. using System.Web.Services;
  35. using System.Collections;
  36. namespace System.Web.Services.Protocols {
  37. #if NET_2_0
  38. [System.Runtime.InteropServices.ComVisible (true)]
  39. #endif
  40. public abstract class HttpWebClientProtocol : WebClientProtocol {
  41. #region Fields
  42. bool allowAutoRedirect, enableDecompression;
  43. X509CertificateCollection clientCertificates;
  44. CookieContainer cookieContainer;
  45. IWebProxy proxy;
  46. string userAgent;
  47. #if NET_1_1
  48. bool _unsafeAuthenticated;
  49. #endif
  50. #endregion
  51. #region Constructors
  52. protected HttpWebClientProtocol ()
  53. {
  54. allowAutoRedirect = false;
  55. clientCertificates = null;
  56. cookieContainer = null;
  57. proxy = null; // FIXME
  58. userAgent = String.Format ("Mono Web Services Client Protocol {0}", Environment.Version);
  59. }
  60. #endregion // Constructors
  61. #region Properties
  62. [DefaultValue (false)]
  63. [WebServicesDescription ("Enable automatic handling of server redirects.")]
  64. public bool AllowAutoRedirect {
  65. get { return allowAutoRedirect; }
  66. set { allowAutoRedirect = value; }
  67. }
  68. [Browsable (false)]
  69. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  70. [WebServicesDescription ("The client certificates that will be sent to the server, if the server requests them.")]
  71. public X509CertificateCollection ClientCertificates {
  72. get {
  73. if (clientCertificates == null)
  74. clientCertificates = new X509CertificateCollection ();
  75. return clientCertificates;
  76. }
  77. }
  78. [DefaultValue (null)]
  79. [WebServicesDescription ("A container for all cookies received from servers in the current session.")]
  80. public CookieContainer CookieContainer {
  81. get { return cookieContainer; }
  82. set { cookieContainer = value; }
  83. }
  84. #if NET_2_0
  85. [DefaultValue (false)]
  86. public bool EnableDecompression {
  87. get { return enableDecompression; }
  88. set { enableDecompression = value; }
  89. }
  90. #endif
  91. [Browsable (false)]
  92. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  93. public IWebProxy Proxy {
  94. get { return proxy; }
  95. set { proxy = value; }
  96. }
  97. [WebServicesDescription ("Sets the user agent http header for the request.")]
  98. #if NET_2_0
  99. [Browsable (false)]
  100. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  101. #endif
  102. public string UserAgent {
  103. get { return userAgent; }
  104. set { userAgent = value; }
  105. }
  106. #if NET_1_1
  107. #if NET_2_0
  108. [Browsable (false)]
  109. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  110. #endif
  111. public bool UnsafeAuthenticatedConnectionSharing
  112. {
  113. get { return _unsafeAuthenticated; }
  114. set { _unsafeAuthenticated = value; }
  115. }
  116. #endif
  117. #endregion // Properties
  118. #region Methods
  119. internal virtual void CheckForCookies (HttpWebResponse response)
  120. {
  121. CookieCollection cookies = response.Cookies;
  122. if (cookieContainer == null || cookies.Count == 0)
  123. return;
  124. CookieCollection coll = cookieContainer.GetCookies (uri);
  125. foreach (Cookie c in cookies) {
  126. bool add = true;
  127. foreach (Cookie prev in coll) {
  128. if (c.Equals (prev)) {
  129. add = false;
  130. break;
  131. }
  132. }
  133. if (add)
  134. cookieContainer.Add (c);
  135. }
  136. }
  137. protected override WebRequest GetWebRequest (Uri uri)
  138. {
  139. WebRequest req = base.GetWebRequest (uri);
  140. HttpWebRequest request = req as HttpWebRequest;
  141. if (request == null)
  142. return req;
  143. #if NET_2_0
  144. if (enableDecompression)
  145. request.AutomaticDecompression = DecompressionMethods.GZip;
  146. #endif
  147. request.AllowAutoRedirect = allowAutoRedirect;
  148. if (clientCertificates != null)
  149. request.ClientCertificates.AddRange (clientCertificates);
  150. request.CookieContainer = cookieContainer;
  151. if (proxy != null)
  152. request.Proxy = proxy;
  153. request.UserAgent = userAgent;
  154. #if NET_1_1
  155. // request.UnsafeAuthenticatedConnectionSharing = _unsafeAuthenticated;
  156. #endif
  157. return request;
  158. }
  159. protected override WebResponse GetWebResponse (WebRequest request)
  160. {
  161. WebResponse response = base.GetWebResponse (request);
  162. HttpWebResponse wr = response as HttpWebResponse;
  163. if (wr != null)
  164. CheckForCookies (wr);
  165. return response;
  166. }
  167. protected override WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
  168. {
  169. WebResponse response = base.GetWebResponse (request, result);
  170. HttpWebResponse wr = response as HttpWebResponse;
  171. if (wr != null)
  172. CheckForCookies (wr);
  173. return response;
  174. }
  175. #if NET_2_0
  176. Hashtable mappings = new Hashtable ();
  177. internal void RegisterMapping (object userState, WebClientAsyncResult result)
  178. {
  179. if (userState == null)
  180. userState = typeof (string);
  181. mappings [userState] = result;
  182. }
  183. internal void UnregisterMapping (object userState)
  184. {
  185. if (userState == null)
  186. userState = typeof (string);
  187. mappings.Remove (userState);
  188. }
  189. protected void CancelAsync (object userState)
  190. {
  191. WebClientAsyncResult result = (WebClientAsyncResult) mappings [userState];
  192. if (result == null)
  193. return;
  194. mappings.Remove (userState);
  195. result.Abort ();
  196. }
  197. [MonoTODO]
  198. public static bool GenerateXmlMappings (Type type, ArrayList mapping)
  199. {
  200. throw new NotImplementedException ();
  201. }
  202. [MonoTODO]
  203. public static Hashtable GenerateXmlMappings (Type[] types, ArrayList mapping)
  204. {
  205. throw new NotImplementedException ();
  206. }
  207. #else
  208. internal void UnregisterMapping (object userState)
  209. {
  210. }
  211. internal void RegisterMapping (object userState, WebClientAsyncResult result)
  212. {
  213. }
  214. #endif
  215. #endregion // Methods
  216. }
  217. #if NET_2_0
  218. internal class InvokeAsyncInfo
  219. {
  220. public SynchronizationContext Context;
  221. public object UserState;
  222. public SendOrPostCallback Callback;
  223. public InvokeAsyncInfo (SendOrPostCallback callback, object userState)
  224. {
  225. Callback = callback;
  226. UserState = userState;
  227. Context = SynchronizationContext.Current;
  228. }
  229. }
  230. #endif
  231. }