HttpWebClientProtocol.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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;
  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. [Browsable (false)]
  85. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  86. public IWebProxy Proxy {
  87. get { return proxy; }
  88. set { proxy = value; }
  89. }
  90. [WebServicesDescription ("Sets the user agent http header for the request.")]
  91. #if NET_2_0
  92. [Browsable (false)]
  93. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  94. #endif
  95. public string UserAgent {
  96. get { return userAgent; }
  97. set { userAgent = value; }
  98. }
  99. #if NET_1_1
  100. #if NET_2_0
  101. [Browsable (false)]
  102. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  103. #endif
  104. public bool UnsafeAuthenticatedConnectionSharing
  105. {
  106. get { return _unsafeAuthenticated; }
  107. set { _unsafeAuthenticated = value; }
  108. }
  109. #endif
  110. #endregion // Properties
  111. #region Methods
  112. internal virtual void CheckForCookies (HttpWebResponse response)
  113. {
  114. CookieCollection cookies = response.Cookies;
  115. if (cookieContainer == null || cookies.Count == 0)
  116. return;
  117. CookieCollection coll = cookieContainer.GetCookies (uri);
  118. foreach (Cookie c in cookies) {
  119. bool add = true;
  120. foreach (Cookie prev in coll) {
  121. if (c.Equals (prev)) {
  122. add = false;
  123. break;
  124. }
  125. }
  126. if (add)
  127. cookieContainer.Add (c);
  128. }
  129. }
  130. protected override WebRequest GetWebRequest (Uri uri)
  131. {
  132. WebRequest req = base.GetWebRequest (uri);
  133. HttpWebRequest request = req as HttpWebRequest;
  134. if (request == null)
  135. return req;
  136. request.AllowAutoRedirect = allowAutoRedirect;
  137. if (clientCertificates != null)
  138. request.ClientCertificates.AddRange (clientCertificates);
  139. request.CookieContainer = cookieContainer;
  140. if (proxy != null)
  141. request.Proxy = proxy;
  142. request.UserAgent = userAgent;
  143. #if NET_1_1
  144. // request.UnsafeAuthenticatedConnectionSharing = _unsafeAuthenticated;
  145. #endif
  146. return request;
  147. }
  148. protected override WebResponse GetWebResponse (WebRequest request)
  149. {
  150. WebResponse response = base.GetWebResponse (request);
  151. HttpWebResponse wr = response as HttpWebResponse;
  152. if (wr != null)
  153. CheckForCookies (wr);
  154. return response;
  155. }
  156. protected override WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
  157. {
  158. WebResponse response = base.GetWebResponse (request, result);
  159. HttpWebResponse wr = response as HttpWebResponse;
  160. if (wr != null)
  161. CheckForCookies (wr);
  162. return response;
  163. }
  164. #if NET_2_0
  165. [MonoTODO]
  166. protected void CancelAsync (object userState)
  167. {
  168. throw new NotImplementedException ();
  169. }
  170. [MonoTODO]
  171. public static bool GenerateXmlMappings (Type type, ArrayList mapping)
  172. {
  173. throw new NotImplementedException ();
  174. }
  175. [MonoTODO]
  176. public static Hashtable GenerateXmlMappings (Type[] types, ArrayList mapping)
  177. {
  178. throw new NotImplementedException ();
  179. }
  180. #endif
  181. #endregion // Methods
  182. }
  183. #if NET_2_0
  184. internal class InvokeAsyncInfo
  185. {
  186. public SynchronizationContext Context;
  187. public object UserState;
  188. public SendOrPostCallback Callback;
  189. public InvokeAsyncInfo (SendOrPostCallback callback, object userState)
  190. {
  191. Callback = callback;
  192. UserState = userState;
  193. Context = SynchronizationContext.Current;
  194. }
  195. }
  196. #endif
  197. }