HttpWebClientProtocol.cs 6.0 KB

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