HttpClientHandler.Mono.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Net.Security;
  6. using System.Security.Authentication;
  7. using System.Security.Cryptography.X509Certificates;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace System.Net.Http
  11. {
  12. public partial class HttpClientHandler : HttpMessageHandler
  13. {
  14. // Only one of these two handlers will be initialized.
  15. private readonly IMonoHttpClientHandler _monoHandler;
  16. private readonly SocketsHttpHandler _socketsHttpHandler;
  17. private ClientCertificateOption _clientCertificateOptions;
  18. public HttpClientHandler () : this (null) { }
  19. internal HttpClientHandler (IMonoHttpClientHandler monoHandler)
  20. {
  21. if (monoHandler != null) {
  22. _monoHandler = monoHandler;
  23. } else {
  24. _socketsHttpHandler = new SocketsHttpHandler ();
  25. ClientCertificateOptions = ClientCertificateOption.Manual;
  26. }
  27. }
  28. protected override void Dispose (bool disposing)
  29. {
  30. if (disposing) {
  31. ((IDisposable)_monoHandler ?? _socketsHttpHandler).Dispose ();
  32. }
  33. base.Dispose (disposing);
  34. }
  35. public virtual bool SupportsAutomaticDecompression => _monoHandler == null || _monoHandler.SupportsAutomaticDecompression;
  36. public virtual bool SupportsProxy => true;
  37. public virtual bool SupportsRedirectConfiguration => true;
  38. public bool UseCookies {
  39. get => _monoHandler != null ? _monoHandler.UseCookies : _socketsHttpHandler.UseCookies;
  40. set {
  41. if (_monoHandler != null) {
  42. _monoHandler.UseCookies = value;
  43. } else {
  44. _socketsHttpHandler.UseCookies = value;
  45. }
  46. }
  47. }
  48. public CookieContainer CookieContainer {
  49. get => _monoHandler != null ? _monoHandler.CookieContainer : _socketsHttpHandler.CookieContainer;
  50. set {
  51. if (_monoHandler != null) {
  52. _monoHandler.CookieContainer = value;
  53. } else {
  54. _socketsHttpHandler.CookieContainer = value;
  55. }
  56. }
  57. }
  58. public ClientCertificateOption ClientCertificateOptions {
  59. get {
  60. if (_monoHandler != null) {
  61. return _monoHandler.ClientCertificateOptions;
  62. } else {
  63. return _clientCertificateOptions;
  64. }
  65. }
  66. set {
  67. if (_monoHandler != null) {
  68. _monoHandler.ClientCertificateOptions = value;
  69. } else {
  70. switch (value) {
  71. case ClientCertificateOption.Manual:
  72. ThrowForModifiedManagedSslOptionsIfStarted ();
  73. _clientCertificateOptions = value;
  74. _socketsHttpHandler.SslOptions.LocalCertificateSelectionCallback = (sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) => CertificateHelper.GetEligibleClientCertificate (ClientCertificates);
  75. break;
  76. case ClientCertificateOption.Automatic:
  77. ThrowForModifiedManagedSslOptionsIfStarted ();
  78. _clientCertificateOptions = value;
  79. _socketsHttpHandler.SslOptions.LocalCertificateSelectionCallback = (sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) => CertificateHelper.GetEligibleClientCertificate ();
  80. break;
  81. default:
  82. throw new ArgumentOutOfRangeException (nameof (value));
  83. }
  84. }
  85. }
  86. }
  87. public X509CertificateCollection ClientCertificates {
  88. get {
  89. if (_monoHandler != null) {
  90. return _monoHandler.ClientCertificates;
  91. } else {
  92. if (ClientCertificateOptions != ClientCertificateOption.Manual) {
  93. throw new InvalidOperationException (SR.Format (SR.net_http_invalid_enable_first, nameof (ClientCertificateOptions), nameof (ClientCertificateOption.Manual)));
  94. }
  95. return _socketsHttpHandler.SslOptions.ClientCertificates ??
  96. (_socketsHttpHandler.SslOptions.ClientCertificates = new X509CertificateCollection ());
  97. }
  98. }
  99. }
  100. public Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool> ServerCertificateCustomValidationCallback {
  101. get {
  102. return _monoHandler != null ?
  103. _monoHandler.ServerCertificateCustomValidationCallback :
  104. (_socketsHttpHandler.SslOptions.RemoteCertificateValidationCallback?.Target as ConnectHelper.CertificateCallbackMapper)?.FromHttpClientHandler;
  105. }
  106. set {
  107. if (_monoHandler != null) {
  108. _monoHandler.ServerCertificateCustomValidationCallback = value;
  109. } else {
  110. ThrowForModifiedManagedSslOptionsIfStarted ();
  111. _socketsHttpHandler.SslOptions.RemoteCertificateValidationCallback = value != null ?
  112. new ConnectHelper.CertificateCallbackMapper (value).ForSocketsHttpHandler :
  113. null;
  114. }
  115. }
  116. }
  117. public bool CheckCertificateRevocationList {
  118. get => _monoHandler != null ? _monoHandler.CheckCertificateRevocationList : _socketsHttpHandler.SslOptions.CertificateRevocationCheckMode == X509RevocationMode.Online;
  119. set {
  120. if (_monoHandler != null) {
  121. _monoHandler.CheckCertificateRevocationList = value;
  122. } else {
  123. ThrowForModifiedManagedSslOptionsIfStarted ();
  124. _socketsHttpHandler.SslOptions.CertificateRevocationCheckMode = value ? X509RevocationMode.Online : X509RevocationMode.NoCheck;
  125. }
  126. }
  127. }
  128. public SslProtocols SslProtocols {
  129. get => _monoHandler != null ? _monoHandler.SslProtocols : _socketsHttpHandler.SslOptions.EnabledSslProtocols;
  130. set {
  131. if (_monoHandler != null) {
  132. _monoHandler.SslProtocols = value;
  133. } else {
  134. ThrowForModifiedManagedSslOptionsIfStarted ();
  135. _socketsHttpHandler.SslOptions.EnabledSslProtocols = value;
  136. }
  137. }
  138. }
  139. public DecompressionMethods AutomaticDecompression {
  140. get => _monoHandler != null ? _monoHandler.AutomaticDecompression : _socketsHttpHandler.AutomaticDecompression;
  141. set {
  142. if (_monoHandler != null) {
  143. _monoHandler.AutomaticDecompression = value;
  144. } else {
  145. _socketsHttpHandler.AutomaticDecompression = value;
  146. }
  147. }
  148. }
  149. public bool UseProxy {
  150. get => _monoHandler != null ? _monoHandler.UseProxy : _socketsHttpHandler.UseProxy;
  151. set {
  152. if (_monoHandler != null) {
  153. _monoHandler.UseProxy = value;
  154. } else {
  155. _socketsHttpHandler.UseProxy = value;
  156. }
  157. }
  158. }
  159. public IWebProxy Proxy {
  160. get => _monoHandler != null ? _monoHandler.Proxy : _socketsHttpHandler.Proxy;
  161. set {
  162. if (_monoHandler != null) {
  163. _monoHandler.Proxy = value;
  164. } else {
  165. _socketsHttpHandler.Proxy = value;
  166. }
  167. }
  168. }
  169. public ICredentials DefaultProxyCredentials {
  170. get => _monoHandler != null ? _monoHandler.DefaultProxyCredentials : _socketsHttpHandler.DefaultProxyCredentials;
  171. set {
  172. if (_monoHandler != null) {
  173. _monoHandler.DefaultProxyCredentials = value;
  174. } else {
  175. _socketsHttpHandler.DefaultProxyCredentials = value;
  176. }
  177. }
  178. }
  179. public bool PreAuthenticate {
  180. get => _monoHandler != null ? _monoHandler.PreAuthenticate : _socketsHttpHandler.PreAuthenticate;
  181. set {
  182. if (_monoHandler != null) {
  183. _monoHandler.PreAuthenticate = value;
  184. } else {
  185. _socketsHttpHandler.PreAuthenticate = value;
  186. }
  187. }
  188. }
  189. public bool UseDefaultCredentials {
  190. // Either read variable from curlHandler or compare .Credentials as socketsHttpHandler does not have separate prop.
  191. get => _monoHandler != null ? _monoHandler.UseDefaultCredentials : _socketsHttpHandler.Credentials == CredentialCache.DefaultCredentials;
  192. set {
  193. if (_monoHandler != null) {
  194. _monoHandler.UseDefaultCredentials = value;
  195. } else {
  196. if (value) {
  197. _socketsHttpHandler.Credentials = CredentialCache.DefaultCredentials;
  198. } else {
  199. if (_socketsHttpHandler.Credentials == CredentialCache.DefaultCredentials) {
  200. // Only clear out the Credentials property if it was a DefaultCredentials.
  201. _socketsHttpHandler.Credentials = null;
  202. }
  203. }
  204. }
  205. }
  206. }
  207. public ICredentials Credentials {
  208. get => _monoHandler != null ? _monoHandler.Credentials : _socketsHttpHandler.Credentials;
  209. set {
  210. if (_monoHandler != null) {
  211. _monoHandler.Credentials = value;
  212. } else {
  213. _socketsHttpHandler.Credentials = value;
  214. }
  215. }
  216. }
  217. public bool AllowAutoRedirect {
  218. get => _monoHandler != null ? _monoHandler.AllowAutoRedirect : _socketsHttpHandler.AllowAutoRedirect;
  219. set {
  220. if (_monoHandler != null) {
  221. _monoHandler.AllowAutoRedirect = value;
  222. } else {
  223. _socketsHttpHandler.AllowAutoRedirect = value;
  224. }
  225. }
  226. }
  227. public int MaxAutomaticRedirections {
  228. get => _monoHandler != null ? _monoHandler.MaxAutomaticRedirections : _socketsHttpHandler.MaxAutomaticRedirections;
  229. set {
  230. if (_monoHandler != null) {
  231. _monoHandler.MaxAutomaticRedirections = value;
  232. } else {
  233. _socketsHttpHandler.MaxAutomaticRedirections = value;
  234. }
  235. }
  236. }
  237. public int MaxConnectionsPerServer {
  238. get => _monoHandler != null ? _monoHandler.MaxConnectionsPerServer : _socketsHttpHandler.MaxConnectionsPerServer;
  239. set {
  240. if (_monoHandler != null) {
  241. _monoHandler.MaxConnectionsPerServer = value;
  242. } else {
  243. _socketsHttpHandler.MaxConnectionsPerServer = value;
  244. }
  245. }
  246. }
  247. public int MaxResponseHeadersLength {
  248. get => _monoHandler != null ? _monoHandler.MaxResponseHeadersLength : _socketsHttpHandler.MaxResponseHeadersLength;
  249. set {
  250. if (_monoHandler != null) {
  251. _monoHandler.MaxResponseHeadersLength = value;
  252. } else {
  253. _socketsHttpHandler.MaxResponseHeadersLength = value;
  254. }
  255. }
  256. }
  257. public IDictionary<string, object> Properties => _monoHandler != null ?
  258. _monoHandler.Properties :
  259. _socketsHttpHandler.Properties;
  260. protected internal override Task<HttpResponseMessage> SendAsync (HttpRequestMessage request, CancellationToken cancellationToken) =>
  261. _monoHandler != null ? _monoHandler.SendAsync (request, cancellationToken) :
  262. _socketsHttpHandler.SendAsync (request, cancellationToken);
  263. }
  264. }