HttpWebClientProtocol.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. public abstract class HttpWebClientProtocol : WebClientProtocol {
  38. #region Fields
  39. bool allowAutoRedirect;
  40. X509CertificateCollection clientCertificates;
  41. CookieContainer cookieContainer;
  42. IWebProxy proxy;
  43. string userAgent;
  44. #if NET_1_1
  45. bool _unsafeAuthenticated;
  46. #endif
  47. #endregion
  48. #region Constructors
  49. protected HttpWebClientProtocol ()
  50. {
  51. allowAutoRedirect = false;
  52. clientCertificates = null;
  53. cookieContainer = null;
  54. proxy = null; // FIXME
  55. userAgent = String.Format ("Mono Web Services Client Protocol {0}", Environment.Version);
  56. }
  57. #endregion // Constructors
  58. #region Properties
  59. [DefaultValue (false)]
  60. [WebServicesDescription ("Enable automatic handling of server redirects.")]
  61. public bool AllowAutoRedirect {
  62. get { return allowAutoRedirect; }
  63. set { allowAutoRedirect = value; }
  64. }
  65. [Browsable (false)]
  66. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  67. [WebServicesDescription ("The client certificates that will be sent to the server, if the server requests them.")]
  68. public X509CertificateCollection ClientCertificates {
  69. get {
  70. if (clientCertificates == null)
  71. clientCertificates = new X509CertificateCollection ();
  72. return clientCertificates;
  73. }
  74. }
  75. [DefaultValue (null)]
  76. [WebServicesDescription ("A container for all cookies received from servers in the current session.")]
  77. public CookieContainer CookieContainer {
  78. get { return cookieContainer; }
  79. set { cookieContainer = value; }
  80. }
  81. [Browsable (false)]
  82. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  83. public IWebProxy Proxy {
  84. get { return proxy; }
  85. set { proxy = value; }
  86. }
  87. [WebServicesDescription ("Sets the user agent http header for the request.")]
  88. public string UserAgent {
  89. get { return userAgent; }
  90. set { userAgent = value; }
  91. }
  92. #if NET_1_1
  93. public bool UnsafeAuthenticatedConnectionSharing
  94. {
  95. get { return _unsafeAuthenticated; }
  96. set { _unsafeAuthenticated = value; }
  97. }
  98. #endif
  99. #endregion // Properties
  100. #region Methods
  101. internal virtual void CheckForCookies (HttpWebResponse response)
  102. {
  103. CookieCollection cookies = response.Cookies;
  104. if (cookieContainer == null || cookies.Count == 0)
  105. return;
  106. CookieCollection coll = cookieContainer.GetCookies (uri);
  107. foreach (Cookie c in cookies) {
  108. bool add = true;
  109. foreach (Cookie prev in coll) {
  110. if (c.Equals (prev)) {
  111. add = false;
  112. break;
  113. }
  114. }
  115. if (add)
  116. cookieContainer.Add (c);
  117. }
  118. }
  119. protected override WebRequest GetWebRequest (Uri uri)
  120. {
  121. WebRequest req = base.GetWebRequest (uri);
  122. HttpWebRequest request = req as HttpWebRequest;
  123. if (request == null)
  124. return req;
  125. request.AllowAutoRedirect = allowAutoRedirect;
  126. if (clientCertificates != null)
  127. request.ClientCertificates.AddRange (clientCertificates);
  128. request.CookieContainer = cookieContainer;
  129. if (proxy != null)
  130. request.Proxy = proxy;
  131. request.UserAgent = userAgent;
  132. #if NET_1_1
  133. // request.UnsafeAuthenticatedConnectionSharing = _unsafeAuthenticated;
  134. #endif
  135. return request;
  136. }
  137. protected override WebResponse GetWebResponse (WebRequest request)
  138. {
  139. WebResponse response = base.GetWebResponse (request);
  140. HttpWebResponse wr = response as HttpWebResponse;
  141. if (wr != null)
  142. CheckForCookies (wr);
  143. return response;
  144. }
  145. protected override WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
  146. {
  147. WebResponse response = base.GetWebResponse (request, result);
  148. HttpWebResponse wr = response as HttpWebResponse;
  149. if (wr != null)
  150. CheckForCookies (wr);
  151. return response;
  152. }
  153. #if NET_2_0
  154. [MonoTODO]
  155. protected void CancelAsync (object userState)
  156. {
  157. throw new NotImplementedException ();
  158. }
  159. [MonoTODO]
  160. public static bool GenerateXmlMappings (Type type, ArrayList mapping)
  161. {
  162. throw new NotImplementedException ();
  163. }
  164. [MonoTODO]
  165. public static Hashtable GenerateXmlMappings (Type[] types, ArrayList mapping)
  166. {
  167. throw new NotImplementedException ();
  168. }
  169. #endif
  170. #endregion // Methods
  171. }
  172. #if NET_2_0
  173. internal class InvokeAsyncInfo
  174. {
  175. public SynchronizationContext Context;
  176. public object UserState;
  177. public SendOrPostCallback Callback;
  178. public InvokeAsyncInfo (SendOrPostCallback callback, object userState)
  179. {
  180. Callback = callback;
  181. UserState = userState;
  182. Context = SynchronizationContext.Current;
  183. }
  184. }
  185. #endif
  186. }