ServiceClientImpl.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Copyright (C) Alibaba Cloud Computing
  3. * All rights reserved.
  4. *
  5. * 版权所有 (C)阿里云计算有限公司
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using System.Diagnostics.CodeAnalysis;
  11. using System.IO;
  12. using System.Net;
  13. using System.Reflection;
  14. using System.Security.Cryptography.X509Certificates;
  15. using System.Net.Security;
  16. using System.Linq;
  17. using Aliyun.OSS.Util;
  18. namespace Aliyun.OSS.Common.Communication
  19. {
  20. /// <summary>
  21. /// The default implementation of <see cref="ServiceClient"/> that
  22. /// used to communicate with Aliyun OSS via HTTP protocol.
  23. /// </summary>
  24. internal class ServiceClientImpl : ServiceClient
  25. {
  26. #region Embeded Classes
  27. /// <summary>
  28. /// Represents the async operation of requests in <see cref="ServiceClientImpl"/>.
  29. /// </summary>
  30. public class HttpAsyncResult : AsyncResult<ServiceResponse>
  31. {
  32. public HttpWebRequest WebRequest { get; set; }
  33. public ExecutionContext Context { get; set; }
  34. public HttpAsyncResult(AsyncCallback callback, object state)
  35. : base(callback, state)
  36. { }
  37. }
  38. /// <summary>
  39. /// Represents the response data of <see cref="ServiceClientImpl"/> requests.
  40. /// </summary>
  41. private class ResponseImpl : ServiceResponse
  42. {
  43. private bool _disposed;
  44. private HttpWebResponse _response;
  45. private readonly Exception _failure;
  46. private IDictionary<string, string> _headers;
  47. public override HttpStatusCode StatusCode
  48. {
  49. get { return _response.StatusCode; }
  50. }
  51. public override Exception Failure
  52. {
  53. get { return _failure; }
  54. }
  55. public override IDictionary<string, string> Headers
  56. {
  57. get
  58. {
  59. ThrowIfObjectDisposed();
  60. return _headers ?? (_headers = GetResponseHeaders(_response));
  61. }
  62. }
  63. public override Stream Content
  64. {
  65. get
  66. {
  67. ThrowIfObjectDisposed();
  68. try
  69. {
  70. return (_response != null) ? _response.GetResponseStream() : null;
  71. }
  72. catch (ProtocolViolationException ex)
  73. {
  74. throw new InvalidOperationException(ex.Message, ex);
  75. }
  76. }
  77. }
  78. public ResponseImpl(HttpWebResponse httpWebResponse)
  79. {
  80. _response = httpWebResponse;
  81. }
  82. public ResponseImpl(WebException failure)
  83. {
  84. var httpWebResponse = failure.Response as HttpWebResponse;
  85. _failure = failure;
  86. _response = httpWebResponse;
  87. }
  88. private static IDictionary<string, string> GetResponseHeaders(HttpWebResponse response)
  89. {
  90. var headers = response.Headers;
  91. var result = new Dictionary<string, string>(headers.Count);
  92. for (var i = 0; i < headers.Count; i++)
  93. {
  94. var key = headers.Keys[i];
  95. var value = headers.Get(key);
  96. result.Add(key, HttpUtils.Reencode(value, HttpUtils.Iso88591Charset, HttpUtils.Utf8Charset));
  97. }
  98. return result;
  99. }
  100. protected override void Dispose(bool disposing)
  101. {
  102. base.Dispose(disposing);
  103. if (_disposed)
  104. return;
  105. if (disposing)
  106. {
  107. if (_response != null)
  108. {
  109. _response.Close();
  110. _response = null;
  111. }
  112. _disposed = true;
  113. }
  114. }
  115. private void ThrowIfObjectDisposed()
  116. {
  117. if (_disposed)
  118. throw new ObjectDisposedException(GetType().Name);
  119. }
  120. }
  121. #endregion
  122. #region Constructors
  123. public ServiceClientImpl(ClientConfiguration configuration)
  124. : base(configuration)
  125. {
  126. }
  127. #endregion
  128. #region Implementations
  129. protected override ServiceResponse SendCore(ServiceRequest serviceRequest,
  130. ExecutionContext context)
  131. {
  132. var request = HttpFactory.CreateWebRequest(serviceRequest, Configuration);
  133. SetRequestContent(request, serviceRequest, false, null);
  134. try
  135. {
  136. var response = request.GetResponse() as HttpWebResponse;
  137. if (response.Server.ToLower() == "aliyunoss" || response.Headers.AllKeys.Any(s => s.ToLower().Contains("x-oss-")))
  138. {
  139. return new ResponseImpl(response);
  140. }
  141. else
  142. {
  143. return HandleException(new WebException("文件不存在."));
  144. }
  145. }
  146. catch (WebException ex)
  147. {
  148. return HandleException(ex);
  149. }
  150. }
  151. protected override IAsyncResult BeginSendCore(ServiceRequest serviceRequest,
  152. ExecutionContext context,
  153. AsyncCallback callback, object state)
  154. {
  155. var request = HttpFactory.CreateWebRequest(serviceRequest, Configuration);
  156. var asyncResult = new HttpAsyncResult(callback, state)
  157. {
  158. WebRequest = request,
  159. Context = context
  160. };
  161. SetRequestContent(request, serviceRequest, true,
  162. () => request.BeginGetResponse(OnGetResponseCompleted, asyncResult));
  163. return asyncResult;
  164. }
  165. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes",
  166. Justification = "Catch the exception and set it to async result.")]
  167. private void OnGetResponseCompleted(IAsyncResult ar)
  168. {
  169. var asyncResult = ar.AsyncState as HttpAsyncResult;
  170. Debug.Assert(asyncResult != null && asyncResult.WebRequest != null);
  171. try
  172. {
  173. var response = asyncResult.WebRequest.EndGetResponse(ar) as HttpWebResponse;
  174. ServiceResponse res = new ResponseImpl(response);
  175. HandleResponse(res, asyncResult.Context.ResponseHandlers);
  176. asyncResult.Complete(res);
  177. }
  178. catch (WebException we)
  179. {
  180. try
  181. {
  182. var res = HandleException(we);
  183. HandleResponse(res, asyncResult.Context.ResponseHandlers);
  184. asyncResult.WebRequest.Abort();
  185. asyncResult.Complete(res);
  186. }
  187. catch (Exception ie)
  188. {
  189. asyncResult.WebRequest.Abort();
  190. asyncResult.Complete(ie);
  191. }
  192. }
  193. catch (Exception oe)
  194. {
  195. asyncResult.WebRequest.Abort();
  196. asyncResult.Complete(oe);
  197. }
  198. }
  199. /// <summary>
  200. /// 为了兼容.NET2.0,定义了OssAction,功能等价于.NET4.0中的System.Action
  201. /// </summary>
  202. private delegate void OssAction();
  203. private static void SetRequestContent(HttpWebRequest webRequest, ServiceRequest serviceRequest,
  204. bool async, OssAction asyncCallback)
  205. {
  206. var data = serviceRequest.BuildRequestContent();
  207. if (data == null ||
  208. (serviceRequest.Method != HttpMethod.Put &&
  209. serviceRequest.Method != HttpMethod.Post))
  210. {
  211. // Skip setting content body in this case.
  212. if (async)
  213. asyncCallback();
  214. return;
  215. }
  216. // Write data to the request stream.
  217. long userSetContentLength = -1;
  218. if (serviceRequest.Headers.ContainsKey(HttpHeaders.ContentLength))
  219. userSetContentLength = long.Parse(serviceRequest.Headers[HttpHeaders.ContentLength]);
  220. long streamLength = data.Length - data.Position;
  221. webRequest.ContentLength = (userSetContentLength >= 0 &&
  222. userSetContentLength <= streamLength) ? userSetContentLength : streamLength;
  223. if (async)
  224. {
  225. webRequest.BeginGetRequestStream(
  226. (ar) =>
  227. {
  228. using (var requestStream = webRequest.EndGetRequestStream(ar))
  229. {
  230. IoUtils.WriteTo(data, requestStream, webRequest.ContentLength);
  231. }
  232. asyncCallback();
  233. }, null);
  234. }
  235. else
  236. {
  237. using (var requestStream = webRequest.GetRequestStream())
  238. {
  239. IoUtils.WriteTo(data, requestStream, webRequest.ContentLength);
  240. }
  241. }
  242. }
  243. private static ServiceResponse HandleException(WebException ex)
  244. {
  245. var response = ex.Response as HttpWebResponse;
  246. if (response == null)
  247. throw ex;
  248. else
  249. return new ResponseImpl(ex);
  250. }
  251. #endregion
  252. }
  253. internal static class HttpFactory
  254. {
  255. public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
  256. {
  257. return true;
  258. }
  259. internal static HttpWebRequest CreateWebRequest(ServiceRequest serviceRequest, ClientConfiguration configuration)
  260. {
  261. var webRequest = WebRequest.Create(serviceRequest.BuildRequestUri()) as HttpWebRequest;
  262. SetRequestHeaders(webRequest, serviceRequest, configuration);
  263. SetRequestProxy(webRequest, configuration);
  264. if (webRequest.RequestUri.Scheme == "https")
  265. {
  266. ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
  267. }
  268. return webRequest;
  269. }
  270. // Set request headers
  271. private static void SetRequestHeaders(HttpWebRequest webRequest, ServiceRequest serviceRequest,
  272. ClientConfiguration configuration)
  273. {
  274. webRequest.Timeout = configuration.ConnectionTimeout;
  275. webRequest.Method = serviceRequest.Method.ToString().ToUpperInvariant();
  276. // Because it is not allowed to set common headers
  277. // with the WebHeaderCollection.Add method,
  278. // we have to call an internal method to skip validation.
  279. foreach (var h in serviceRequest.Headers)
  280. HttpExtensions.AddInternal(webRequest.Headers, h.Key, h.Value);
  281. // Set user-agent
  282. if (!string.IsNullOrEmpty(configuration.UserAgent))
  283. webRequest.UserAgent = configuration.UserAgent;
  284. }
  285. // Set proxy
  286. private static void SetRequestProxy(HttpWebRequest webRequest, ClientConfiguration configuration)
  287. {
  288. // Perf Improvement:
  289. // If HttpWebRequest.Proxy is not set to null explicitly,
  290. // it will try to load the IE proxy settings including auto proxy detection,
  291. // which is quite time consuming.
  292. webRequest.Proxy = null;
  293. // Set proxy if proxy settings are specified.
  294. if (!string.IsNullOrEmpty(configuration.ProxyHost))
  295. {
  296. if (configuration.ProxyPort < 0)
  297. webRequest.Proxy = new WebProxy(configuration.ProxyHost);
  298. else
  299. webRequest.Proxy = new WebProxy(configuration.ProxyHost, configuration.ProxyPort);
  300. if (!string.IsNullOrEmpty(configuration.ProxyUserName))
  301. {
  302. webRequest.Proxy.Credentials = String.IsNullOrEmpty(configuration.ProxyDomain) ?
  303. new NetworkCredential(configuration.ProxyUserName, configuration.ProxyPassword ?? string.Empty) :
  304. new NetworkCredential(configuration.ProxyUserName, configuration.ProxyPassword ?? string.Empty,
  305. configuration.ProxyDomain);
  306. }
  307. }
  308. }
  309. }
  310. internal static class HttpExtensions
  311. {
  312. private static MethodInfo _addInternalMethod;
  313. private static readonly ICollection<PlatformID> MonoPlatforms =
  314. new List<PlatformID> { PlatformID.MacOSX, PlatformID.Unix };
  315. private static bool? _isMonoPlatform;
  316. internal static void AddInternal(WebHeaderCollection headers, string key, string value)
  317. {
  318. if (_isMonoPlatform == null)
  319. _isMonoPlatform = MonoPlatforms.Contains(Environment.OSVersion.Platform);
  320. // HTTP headers should be encoded to iso-8859-1,
  321. // however it will be encoded automatically by HttpWebRequest in mono.
  322. if (_isMonoPlatform == false)
  323. // Encode headers for win platforms.
  324. value = HttpUtils.Reencode(value, HttpUtils.Utf8Charset, HttpUtils.Iso88591Charset);
  325. if (_addInternalMethod == null)
  326. {
  327. // Specify the internal method name for adding headers
  328. // mono: AddWithoutValidate
  329. // win: AddInternal
  330. var internalMethodName = (_isMonoPlatform == true) ? "AddWithoutValidate" : "AddInternal";
  331. var mi = typeof(WebHeaderCollection).GetMethod(
  332. internalMethodName,
  333. BindingFlags.NonPublic | BindingFlags.Instance,
  334. null,
  335. new Type[] { typeof(string), typeof(string) },
  336. null);
  337. _addInternalMethod = mi;
  338. }
  339. _addInternalMethod.Invoke(headers, new object[] { key, value });
  340. }
  341. }
  342. }