CommandTask.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.Net.Security;
  6. using System.Security.Cryptography.X509Certificates;
  7. using COSXML.Log;
  8. using System.Reflection;
  9. using System.IO;
  10. using System.Net.Cache;
  11. namespace COSXML.Network
  12. {
  13. /// <summary>
  14. /// network request and response.
  15. /// <list type="bullet">
  16. /// <item><term>type1: command request</term></item>
  17. /// <item><term>type2: upload file</term></item>
  18. /// <item><term>type3: download file</term></item>
  19. /// </list>
  20. /// </summary>
  21. public sealed class CommandTask
  22. {
  23. public const string TAG = "CommandTask";
  24. public static int MaxRetries = 3;
  25. public static HttpClientConfig config;
  26. /// <summary>
  27. /// init connectionLimit and statueCode = 100 action
  28. /// </summary>
  29. /// <param name="config"></param>
  30. public static void Init(HttpClientConfig config)
  31. {
  32. ServicePointManager.Expect100Continue = false;
  33. ServicePointManager.DefaultConnectionLimit = config.ConnectionLimit;
  34. CommandTask.MaxRetries = config.MaxRetry;
  35. CommandTask.config = config;
  36. }
  37. /// <summary>
  38. /// sync excute
  39. /// </summary>
  40. /// <param name="request"></param>
  41. /// <param name="response"></param>
  42. /// <param name="config"></param>
  43. /// <returns></returns>
  44. public static void Excute(Request request, Response response, HttpClientConfig config)
  45. {
  46. HttpWebRequest httpWebRequest = null;
  47. HttpWebResponse httpWebResponse = null;
  48. try
  49. {
  50. //step1: create HttpWebRequest by request.url
  51. httpWebRequest = HttpWebRequest.Create(request.RequestUrlString) as HttpWebRequest;
  52. httpWebRequest.AllowWriteStreamBuffering = false;
  53. //bind webRequest
  54. request.BindHttpWebRequest(httpWebRequest);
  55. // handler request
  56. HandleHttpWebRequest(httpWebRequest, request, config);
  57. //final: get response
  58. httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
  59. //notify has been got response
  60. request.onNotifyGetResponse();
  61. //handle response for [100, 300)
  62. HandleHttpWebResponse(httpWebResponse, response);
  63. }
  64. catch (WebException webEx)
  65. {
  66. if (webEx.Response != null && webEx.Response is HttpWebResponse)
  67. {
  68. //notify has been got response
  69. request.onNotifyGetResponse();
  70. httpWebResponse = (HttpWebResponse)webEx.Response;
  71. //handle response for [400, 500]
  72. HandleHttpWebResponse(httpWebResponse, response);
  73. }
  74. else
  75. {
  76. //QLog.E(TAG, webEx.Message, webEx);
  77. throw;
  78. }
  79. }
  80. catch (Exception)
  81. {
  82. //QLog.E(TAG, ex.Message, ex);
  83. throw;
  84. }
  85. finally
  86. {
  87. if (httpWebResponse != null)
  88. {
  89. // print log
  90. PrintResponseInfo(httpWebResponse);
  91. httpWebResponse.Close();
  92. //QLog.D("XIAO", "response close");
  93. }
  94. if (httpWebRequest != null)
  95. {
  96. httpWebRequest.Abort();
  97. //QLog.D("XIAO", "request close");
  98. }
  99. QLog.Debug(TAG, "close");
  100. }
  101. }
  102. /// <summary>
  103. /// handle request
  104. /// </summary>
  105. /// <param name="httpWebRequest"></param>
  106. /// <param name="request"></param>
  107. /// <param name="config"></param>
  108. private static void HandleHttpWebRequest(HttpWebRequest httpWebRequest, Request request, HttpClientConfig config)
  109. {
  110. HandleHttpWebRequestHeaders(request, httpWebRequest, config);
  111. //setp5: send request content: body
  112. if (request.Body != null)
  113. {
  114. httpWebRequest.ContentLength = request.Body.ContentLength;
  115. request.Body.OnWrite(httpWebRequest.GetRequestStream());
  116. }
  117. //print request start log
  118. PrintReqeustInfo(httpWebRequest);
  119. }
  120. /// <summary>
  121. /// handle response
  122. /// </summary>
  123. /// <param name="httpWebResponse"></param>
  124. /// <param name="response"></param>
  125. private static void HandleHttpWebResponse(HttpWebResponse httpWebResponse, Response response)
  126. {
  127. HandleHttpWebResponseHeaders(response, httpWebResponse);
  128. //handle body
  129. response.Body.HandleResponseBody(httpWebResponse.GetResponseStream());
  130. response.OnFinish(response.Code >= 200 && response.Code < 300, null);
  131. // close
  132. //httpWebResponse.Close();
  133. }
  134. /// <summary>
  135. /// async to excute
  136. /// </summary>
  137. /// <param name="request"></param>
  138. /// <param name="response"></param>
  139. /// <param name="config"></param>
  140. public static void Schedue(Request request, Response response, HttpClientConfig config, int retryIndex = 0)
  141. {
  142. HttpWebRequest httpWebRequest = null;
  143. RequestState requestState = new RequestState();
  144. try
  145. {
  146. requestState.request = request;
  147. requestState.response = response;
  148. httpWebRequest = WebRequest.Create(request.RequestUrlString) as HttpWebRequest;
  149. httpWebRequest.AllowWriteStreamBuffering = false;
  150. //bind webRequest
  151. request.BindHttpWebRequest(httpWebRequest);
  152. //handle request header
  153. HandleHttpWebRequestHeaders(request, httpWebRequest, config);
  154. requestState.httpWebRequest = httpWebRequest;
  155. requestState.retryIndex = retryIndex;
  156. //handle request body
  157. if (request.Body != null)
  158. {
  159. httpWebRequest.ContentLength = request.Body.ContentLength;
  160. httpWebRequest.BeginGetRequestStream(new AsyncCallback(AsyncRequestCallback), requestState);
  161. }
  162. else
  163. {
  164. //wait for response
  165. httpWebRequest.BeginGetResponse(new AsyncCallback(AsyncResponseCallback), requestState);
  166. }
  167. //print log
  168. PrintReqeustInfo(httpWebRequest);
  169. }
  170. catch (WebException webEx)
  171. {
  172. response.OnFinish(false, webEx);
  173. //abort
  174. requestState.Clear();
  175. QLog.Debug(TAG, webEx.Message, webEx);
  176. }
  177. catch (Exception ex)
  178. {
  179. response.OnFinish(false, ex);
  180. //abort
  181. requestState.Clear();
  182. QLog.Error(TAG, ex.Message, ex);
  183. }
  184. }
  185. public static void AsyncRequestCallback(IAsyncResult ar)
  186. {
  187. RequestState requestState = ar.AsyncState as RequestState;
  188. Stream requestStream = null;
  189. try
  190. {
  191. HttpWebRequest httpWebRequest = requestState.httpWebRequest;
  192. requestStream = httpWebRequest.EndGetRequestStream(ar);
  193. ////开始写入数据
  194. //requestState.request.Body.OnWrite(requestStream);
  195. ////wait for response
  196. //httpWebRequest.BeginGetResponse(AsyncResponseCallback, requestState);
  197. requestState.request.Body.StartHandleRequestBody(requestStream, delegate (Exception exception)
  198. {
  199. if (exception != null)
  200. {
  201. // handle request body throw exception
  202. if (requestState.retryIndex < MaxRetries)
  203. {
  204. QLog.Error(TAG, exception.Message, exception);
  205. Schedue(requestState.request, requestState.response, config, requestState.retryIndex + 1);
  206. return;
  207. }
  208. requestState.response.OnFinish(false, exception);
  209. //abort
  210. requestState.Clear();
  211. QLog.Error(TAG, exception.Message, exception);
  212. }
  213. else
  214. {
  215. //wait for response
  216. httpWebRequest.BeginGetResponse(new AsyncCallback(AsyncResponseCallback), requestState);
  217. }
  218. });
  219. }
  220. catch (Exception ex)
  221. {
  222. if (requestState.retryIndex < MaxRetries)
  223. {
  224. QLog.Error(TAG, ex.Message, ex);
  225. Schedue(requestState.request, requestState.response, config, requestState.retryIndex + 1);
  226. return;
  227. }
  228. requestState.response.OnFinish(false, ex);
  229. //abort
  230. requestState.Clear();
  231. QLog.Error(TAG, ex.Message, ex);
  232. }
  233. }
  234. public static void AsyncResponseCallback(IAsyncResult ar)
  235. {
  236. RequestState requestState = ar.AsyncState as RequestState;
  237. HttpWebResponse httpWebResponse = null;
  238. try
  239. {
  240. HttpWebRequest httpWebRequest = requestState.httpWebRequest;
  241. httpWebResponse = (HttpWebResponse)httpWebRequest.EndGetResponse(ar);
  242. //nofity get response
  243. requestState.request.onNotifyGetResponse();
  244. requestState.httpWebResponse = httpWebResponse;
  245. //handle response headers
  246. HandleHttpWebResponseHeaders(requestState.response, httpWebResponse);
  247. Stream responseStream = httpWebResponse.GetResponseStream();
  248. requestState.response.Body.StartHandleResponseBody(responseStream, delegate (bool isSuccess, Exception ex)
  249. {
  250. PrintResponseInfo(httpWebResponse);
  251. requestState.response.OnFinish(isSuccess, ex);
  252. requestState.Clear();
  253. });
  254. }
  255. catch (WebException webEx)
  256. {
  257. if (requestState.retryIndex < MaxRetries)
  258. {
  259. Schedue(requestState.request, requestState.response, config, requestState.retryIndex + 1);
  260. return;
  261. }
  262. if (webEx.Response != null && webEx.Response is HttpWebResponse)
  263. {
  264. //nofity get response
  265. requestState.request.onNotifyGetResponse();
  266. //handle response for [400, 500]
  267. httpWebResponse = (HttpWebResponse)webEx.Response;
  268. requestState.httpWebResponse = httpWebResponse;
  269. //handle response headers
  270. HandleHttpWebResponseHeaders(requestState.response, httpWebResponse);
  271. Stream responseStream = httpWebResponse.GetResponseStream();
  272. requestState.response.Body.StartHandleResponseBody(responseStream, delegate (bool isSuccess, Exception ex)
  273. {
  274. PrintResponseInfo(httpWebResponse);
  275. requestState.response.OnFinish(isSuccess, ex);
  276. requestState.Clear();
  277. });
  278. }
  279. else
  280. {
  281. requestState.response.OnFinish(false, webEx);
  282. //abort
  283. requestState.Clear();
  284. QLog.Error(TAG, webEx.Message, webEx);
  285. }
  286. }
  287. catch (Exception ex)
  288. {
  289. if (requestState.retryIndex < MaxRetries)
  290. {
  291. Schedue(requestState.request, requestState.response, config, requestState.retryIndex + 1);
  292. return;
  293. }
  294. requestState.response.OnFinish(false, ex);
  295. //abort
  296. requestState.Clear();
  297. QLog.Error(TAG, ex.Message, ex);
  298. }
  299. }
  300. /// <summary>
  301. /// handle request headers
  302. /// </summary>
  303. /// <param name="request"></param>
  304. /// <param name="httpWebRequest"></param>
  305. /// <param name="config"></param>
  306. private static void HandleHttpWebRequestHeaders(Request request, HttpWebRequest httpWebRequest, HttpClientConfig config)
  307. {
  308. // set connect timeout
  309. httpWebRequest.Timeout = config.ConnectionTimeoutMs;
  310. //set read write timeout
  311. httpWebRequest.ReadWriteTimeout = config.ReadWriteTimeoutMs;
  312. // set request method
  313. httpWebRequest.Method = request.Method.ToUpperInvariant();
  314. // set user-agent
  315. httpWebRequest.UserAgent = request.UserAgent;
  316. //set host, net2.0 cannot set;
  317. // set allow auto redirect
  318. httpWebRequest.AllowAutoRedirect = config.AllowAutoRedirect;
  319. // set connection
  320. httpWebRequest.KeepAlive = config.KeepAlive;
  321. // notice: it is not allowed to set common headers with the WebHeaderCollection.Accept
  322. // such as: Connection,Content-Length,Content-Type,Date,Expect. Host,If-Modified-Since,Range, Referer,Transfer-Encoding,User-Agent,Proxy-Connection
  323. //step2: set header and connection properity by request.heders
  324. foreach (KeyValuePair<string, string> pair in request.Headers)
  325. {
  326. HttpHeaderHandle.AddHeader(httpWebRequest.Headers, pair.Key, pair.Value);
  327. }
  328. //step3: set proxy, default proxy = null, improte performation
  329. SetRequestProxy(httpWebRequest, config);
  330. //step4: https, default all true for "*.myqcloud.com"
  331. if (request.IsHttps)
  332. {
  333. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationCertificate);
  334. }
  335. //初始化长度
  336. httpWebRequest.ContentLength = 0L;
  337. }
  338. /// <summary>
  339. /// headle response headers
  340. /// </summary>
  341. /// <param name="response"></param>
  342. /// <param name="httpWebResponse"></param>
  343. private static void HandleHttpWebResponseHeaders(Response response, HttpWebResponse httpWebResponse)
  344. {
  345. response.Code = (int)httpWebResponse.StatusCode;
  346. response.Message = httpWebResponse.StatusDescription;
  347. WebHeaderCollection headers = httpWebResponse.Headers;
  348. // Transfer-Encoding: chunked
  349. bool isChunked = false;
  350. if (headers != null)
  351. {
  352. Dictionary<string, List<string>> result = new Dictionary<string, List<string>>(headers.Count);
  353. for (int i = 0; i < headers.Count; i++)
  354. {
  355. List<string> values = null;
  356. string key = headers.GetKey(i);
  357. if (headers.GetValues(i) != null)
  358. {
  359. values = new List<string>();
  360. foreach (string value in headers.GetValues(i))
  361. {
  362. values.Add(value);
  363. }
  364. }
  365. result.Add(key, values);
  366. if ("Transfer-Encoding".EndsWith(key, StringComparison.OrdinalIgnoreCase) && values.Contains("chunked"))
  367. {
  368. isChunked = true;
  369. }
  370. }
  371. response.Headers = result;
  372. }
  373. if (!isChunked)
  374. {
  375. response.ContentLength = httpWebResponse.ContentLength;
  376. }
  377. response.ContentType = httpWebResponse.ContentType;
  378. if (response.Body != null)
  379. {
  380. if (!isChunked)
  381. {
  382. response.Body.ContentLength = httpWebResponse.ContentLength;
  383. }
  384. response.Body.ContentType = httpWebResponse.ContentType;
  385. }
  386. //handle header
  387. response.HandleResponseHeader();
  388. }
  389. /// <summary>
  390. /// set proxy
  391. /// </summary>
  392. /// <param name="httpWebRequest"></param>
  393. /// <param name="config"></param>
  394. private static void SetRequestProxy(HttpWebRequest httpWebRequest, HttpClientConfig config)
  395. {
  396. httpWebRequest.Proxy = null;
  397. if (!String.IsNullOrEmpty(config.ProxyHost))
  398. {
  399. if (config.ProxyPort < 0)
  400. {
  401. httpWebRequest.Proxy = new WebProxy(config.ProxyHost);
  402. }
  403. else
  404. {
  405. httpWebRequest.Proxy = new WebProxy(config.ProxyHost, config.ProxyPort);
  406. }
  407. if (!String.IsNullOrEmpty(config.ProxyUserName))
  408. {
  409. httpWebRequest.Proxy.Credentials = String.IsNullOrEmpty(config.ProxyDomain) ?
  410. new NetworkCredential(config.ProxyUserName, config.ProxyUserPassword ?? String.Empty) :
  411. new NetworkCredential(config.ProxyUserName, config.ProxyUserPassword ?? String.Empty,
  412. config.ProxyDomain);
  413. }
  414. // 代理验证
  415. // 代理验证
  416. httpWebRequest.PreAuthenticate = true;
  417. }
  418. }
  419. /// <summary>
  420. /// check certificate
  421. /// </summary>
  422. /// <param name="sender"></param>
  423. /// <param name="certificate"></param>
  424. /// <param name="chain"></param>
  425. /// <param name="errors"></param>
  426. /// <returns></returns>
  427. private static bool CheckValidationCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
  428. {
  429. return true;
  430. }
  431. /// <summary>
  432. /// print request info
  433. /// </summary>
  434. /// <param name="httpWebRequest"></param>
  435. private static void PrintReqeustInfo(HttpWebRequest httpWebRequest)
  436. {
  437. StringBuilder requestLog = new StringBuilder("--->");
  438. requestLog.Append(httpWebRequest.Method).Append(' ').Append(httpWebRequest.Address.AbsoluteUri).Append('\n');
  439. int count = httpWebRequest.Headers.Count;
  440. for (int i = 0; i < count; i++)
  441. {
  442. requestLog.Append(httpWebRequest.Headers.GetKey(i)).Append(":").Append(httpWebRequest.Headers.GetValues(i)[0]).Append('\n');
  443. }
  444. requestLog.Append("allow auto redirect: " + httpWebRequest.AllowAutoRedirect).Append('\n');
  445. requestLog.Append("connect timeout: " + httpWebRequest.Timeout).Append('\n');
  446. requestLog.Append("read write timeout: " + httpWebRequest.ReadWriteTimeout).Append('\n');
  447. requestLog.Append("AllowWriteStreamBuffering: " + httpWebRequest.AllowWriteStreamBuffering).Append('\n');
  448. //requestLog.Append("proxy: " + (httpWebRequest.Proxy == null ? "null" : ((WebProxy)httpWebRequest.Proxy).Address.ToString()));
  449. requestLog.Append("<---");
  450. QLog.Debug(TAG, requestLog.ToString());
  451. }
  452. /// <summary>
  453. /// print response info
  454. /// </summary>
  455. /// <param name="httpWebResponse"></param>
  456. private static void PrintResponseInfo(HttpWebResponse httpWebResponse)
  457. {
  458. StringBuilder responseLog = new StringBuilder("<---");
  459. responseLog.Append(httpWebResponse.Method).Append(' ').Append(httpWebResponse.ResponseUri.AbsoluteUri).Append('\n');
  460. responseLog.Append((int)httpWebResponse.StatusCode).Append(' ').Append(httpWebResponse.StatusDescription).Append('\n');
  461. int count = httpWebResponse.Headers.Count;
  462. for (int i = 0; i < count; i++)
  463. {
  464. responseLog.Append(httpWebResponse.Headers.GetKey(i)).Append(":").Append(httpWebResponse.Headers.GetValues(i)[0]).Append('\n');
  465. }
  466. responseLog.Append("<---");
  467. QLog.Debug(TAG, responseLog.ToString());
  468. }
  469. internal static class HttpHeaderHandle
  470. {
  471. private static MethodInfo addHeaderMethod;
  472. private static readonly ICollection<PlatformID> monoPlatforms = new List<PlatformID>
  473. {
  474. PlatformID.MacOSX, PlatformID.Unix
  475. };
  476. private static bool? isMonoPlatform;
  477. internal static void AddHeader(WebHeaderCollection webHeaderCollection, string key, string value)
  478. {
  479. if (isMonoPlatform == null)
  480. {
  481. isMonoPlatform = monoPlatforms.Contains(Environment.OSVersion.Platform);
  482. }
  483. // HTTP headers should be encoded to iso-8859-1,
  484. // however it will be encoded automatically by HttpWebRequest in mono.
  485. if (false == isMonoPlatform)
  486. {
  487. // Encode headers for win platforms.
  488. }
  489. if (addHeaderMethod == null)
  490. {
  491. // Specify the internal method name for adding headers
  492. // mono: AddWithoutValidate
  493. // win: AddInternal
  494. //var internalMethodName = (isMonoPlatform == false) ? "AddWithoutValidate" : "AddInternal";
  495. var internalMethodName = "AddWithoutValidate";
  496. QLog.Debug(TAG, internalMethodName.ToString());
  497. var method = typeof(WebHeaderCollection).GetMethod(
  498. internalMethodName,
  499. BindingFlags.NonPublic | BindingFlags.Instance,
  500. null,
  501. new Type[] { typeof(string), typeof(string) },
  502. null);
  503. if (method == null)
  504. {
  505. internalMethodName = "AddInternal";
  506. QLog.Debug(TAG, internalMethodName.ToString());
  507. method = typeof(WebHeaderCollection).GetMethod(
  508. internalMethodName,
  509. BindingFlags.NonPublic | BindingFlags.Instance,
  510. null,
  511. new Type[] { typeof(string), typeof(string) },
  512. null);
  513. }
  514. addHeaderMethod = method;
  515. }
  516. addHeaderMethod.Invoke(webHeaderCollection, new Object[] { key, value });
  517. }
  518. }
  519. internal class RequestState
  520. {
  521. public HttpWebRequest httpWebRequest;
  522. public HttpWebResponse httpWebResponse;
  523. public Response response;
  524. public Request request;
  525. public int retryIndex;
  526. public RequestState()
  527. {
  528. httpWebRequest = null;
  529. httpWebResponse = null;
  530. response = null;
  531. request = null;
  532. }
  533. public void Clear()
  534. {
  535. if (httpWebRequest != null)
  536. {
  537. httpWebRequest.Abort();
  538. }
  539. if (httpWebResponse != null)
  540. {
  541. httpWebResponse.Close();
  542. }
  543. QLog.Debug(TAG, "Close");
  544. }
  545. }
  546. }
  547. }