llcorehttplibcurl.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /**
  2. * @file llcorehttplibcurl.cpp
  3. * @brief Internal definitions of the Http libcurl thread
  4. *
  5. * $LicenseInfo:firstyear=2012&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2012-2013, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "llcorehttplibcurl.h"
  28. #include "llcorebufferarray.h"
  29. #include "llcorehttpheaders.h"
  30. #include "llcorehttpoprequest.h"
  31. #include "llcorehttppolicy.h"
  32. #include "llcoremutex.h"
  33. #include "llhttpconstants.h"
  34. namespace
  35. {
  36. // Error testing and reporting for libcurl status codes
  37. void check_curl_multi_code(CURLMcode code);
  38. void check_curl_multi_code(CURLMcode code, int curl_setopt_option);
  39. LLCoreInt::HttpMutex sStatMutex;
  40. }
  41. namespace LLCore
  42. {
  43. U64 HttpLibcurl::sDownloadedBytes = 0;
  44. U64 HttpLibcurl::sUploadedBytes = 0;
  45. HttpLibcurl::HttpLibcurl(HttpService* service)
  46. : mService(service),
  47. mPolicyCount(0),
  48. mMultiHandles(NULL),
  49. mActiveHandles(NULL),
  50. mDirtyPolicy(NULL)
  51. {
  52. }
  53. HttpLibcurl::~HttpLibcurl()
  54. {
  55. shutdown();
  56. mService = NULL;
  57. }
  58. void HttpLibcurl::shutdown()
  59. {
  60. while (!mActiveOps.empty())
  61. {
  62. HttpOpRequest::ptr_t op(*mActiveOps.begin());
  63. mActiveOps.hset_erase(mActiveOps.begin());
  64. cancelRequest(op);
  65. }
  66. if (mMultiHandles)
  67. {
  68. for (int policy_class = 0; policy_class < mPolicyCount; ++policy_class)
  69. {
  70. CURLM* multi_handle(mMultiHandles[policy_class]);
  71. if (multi_handle)
  72. {
  73. curl_multi_cleanup(multi_handle);
  74. mMultiHandles[policy_class] = NULL;
  75. }
  76. }
  77. delete[] mMultiHandles;
  78. mMultiHandles = NULL;
  79. delete[] mActiveHandles;
  80. mActiveHandles = NULL;
  81. delete[] mDirtyPolicy;
  82. mDirtyPolicy = NULL;
  83. }
  84. mPolicyCount = 0;
  85. }
  86. void HttpLibcurl::start(int policy_count)
  87. {
  88. llassert_always(policy_count <= HTTP_POLICY_CLASS_LIMIT);
  89. llassert_always(!mMultiHandles); // One-time call only
  90. mPolicyCount = policy_count;
  91. mMultiHandles = new CURLM*[mPolicyCount];
  92. mActiveHandles = new int[mPolicyCount];
  93. mDirtyPolicy = new bool[mPolicyCount];
  94. for (int policy_class = 0; policy_class < mPolicyCount; ++policy_class)
  95. {
  96. if ((mMultiHandles[policy_class] = curl_multi_init()) != NULL)
  97. {
  98. mActiveHandles[policy_class] = 0;
  99. mDirtyPolicy[policy_class] = false;
  100. policyUpdated(policy_class);
  101. }
  102. else
  103. {
  104. llwarns << "Failed to allocate multi handle in libcurl." << llendl;
  105. llassert(false);
  106. }
  107. }
  108. }
  109. HttpService::ELoopSpeed HttpLibcurl::processTransport()
  110. {
  111. HttpService::ELoopSpeed ret(HttpService::REQUEST_SLEEP);
  112. if (!mMultiHandles)
  113. {
  114. return ret;
  115. }
  116. // Give libcurl some cycles to do I/O & callbacks
  117. for (int policy_class = 0; policy_class < mPolicyCount; ++policy_class)
  118. {
  119. CURLM* multi_handle = mMultiHandles[policy_class];
  120. if (!multi_handle)
  121. {
  122. // *HACK: there used to be nothing but 'continue', here, but
  123. // Apple's clang wrongly optimized out (at either -O2 or -O3) the
  124. // NULL check, causing the resulting binary to crash !
  125. LL_DEBUGS("CoreHttp") << "NULL multi-handle found for policy class: "
  126. << policy_class << LL_ENDL;
  127. // No handle, nothing to do.
  128. continue;
  129. }
  130. if (!mActiveHandles[policy_class])
  131. {
  132. // If we have gone quiet and there is a dirty update, apply it,
  133. // otherwise we are done.
  134. if (mDirtyPolicy[policy_class])
  135. {
  136. policyUpdated(policy_class);
  137. }
  138. continue;
  139. }
  140. int running;
  141. CURLMcode status(CURLM_CALL_MULTI_PERFORM);
  142. do
  143. {
  144. running = 0;
  145. status = curl_multi_perform(multi_handle, &running);
  146. }
  147. while (running != 0 && CURLM_CALL_MULTI_PERFORM == status);
  148. // Run completion on anything done
  149. CURLMsg* msg = NULL;
  150. int msgs_in_queue = 0;
  151. while ((msg = curl_multi_info_read(multi_handle, &msgs_in_queue)))
  152. {
  153. if (msg->msg == CURLMSG_DONE)
  154. {
  155. CURL* handle = msg->easy_handle;
  156. CURLcode result = msg->data.result;
  157. completeRequest(multi_handle, handle, result);
  158. handle = NULL; // No longer valid on return
  159. // If anything completes, we may have a free slot.
  160. ret = HttpService::NORMAL;
  161. // Turning around quickly reduces connection gap by 7-10ms.
  162. }
  163. else if (msg->msg != CURLMSG_NONE)
  164. {
  165. llwarns_once << "Unexpected message from libcurl. Msg code: "
  166. << msg->msg << llendl;
  167. }
  168. msgs_in_queue = 0;
  169. }
  170. }
  171. if (!mActiveOps.empty())
  172. {
  173. ret = HttpService::NORMAL;
  174. }
  175. return ret;
  176. }
  177. // Caller has provided us with a ref count on op.
  178. void HttpLibcurl::addOp(const HttpOpRequest::ptr_t& op)
  179. {
  180. llassert_always((int)op->mReqPolicy < mPolicyCount &&
  181. mMultiHandles[op->mReqPolicy] != NULL);
  182. // Create standard handle
  183. if (!op->prepareRequest(mService))
  184. {
  185. // Could not issue request, fail with notification
  186. // *TODO: Need failure path
  187. return;
  188. }
  189. // Make the request live
  190. CURLMcode code = curl_multi_add_handle(mMultiHandles[op->mReqPolicy],
  191. op->mCurlHandle);
  192. if (code != CURLM_OK)
  193. {
  194. // *TODO: Better cleanup and recovery but not much we can do here.
  195. check_curl_multi_code(code);
  196. return;
  197. }
  198. op->mCurlActive = true;
  199. mActiveOps.emplace(op);
  200. ++mActiveHandles[op->mReqPolicy];
  201. if (op->mTracing > HTTP_TRACE_OFF)
  202. {
  203. HttpPolicy& policy(mService->getPolicy());
  204. llinfos << "TRACE, ToActiveQueue, Handle: " << op->getHandle()
  205. << ". Actives: " << mActiveOps.size() << ". Ready: "
  206. << policy.getReadyCount(op->mReqPolicy) << llendl;
  207. }
  208. }
  209. // Implements the transport part of any cancel operation. See if the handle is
  210. // an active operation and if so, use the more complicated transport-based
  211. // cancellation method to kill the request.
  212. bool HttpLibcurl::cancel(HttpHandle handle)
  213. {
  214. HttpOpRequest::ptr_t op = HttpOpRequest::fromHandle<HttpOpRequest>(handle);
  215. active_set_t::iterator it = mActiveOps.find(op);
  216. if (it == mActiveOps.end())
  217. {
  218. return false;
  219. }
  220. // Cancel request
  221. cancelRequest(op);
  222. // Drop references
  223. mActiveOps.hset_erase(it);
  224. --mActiveHandles[op->mReqPolicy];
  225. return true;
  226. }
  227. // *NOTE: cancelRequest logic parallels completeRequest logic. Keep them
  228. // synchronized as necessary. Caller is expected to remove the op from the
  229. // active list and release the op *after* calling this method. It must be
  230. // called first to deliver the op to the reply queue with refcount intact.
  231. void HttpLibcurl::cancelRequest(const HttpOpRequest::ptr_t& op)
  232. {
  233. // Deactivate request
  234. op->mCurlActive = false;
  235. // Detach from multi and recycle handle
  236. curl_multi_remove_handle(mMultiHandles[op->mReqPolicy], op->mCurlHandle);
  237. mHandleCache.freeHandle(op->mCurlHandle);
  238. op->mCurlHandle = NULL;
  239. // Tracing
  240. if (op->mTracing > HTTP_TRACE_OFF)
  241. {
  242. llinfos << "TRACE, request cancelled, Handle: " << op->getHandle()
  243. << ". Status: " << op->mStatus.toTerseString() << llendl;
  244. }
  245. // Cancel op and deliver for notification
  246. op->cancel();
  247. }
  248. // *NOTE: cancelRequest logic parallels completeRequest logic. Keep them
  249. // synchronized as necessary.
  250. bool HttpLibcurl::completeRequest(CURLM* multi_handle, CURL* handle,
  251. CURLcode status)
  252. {
  253. if (!handle)
  254. {
  255. llwarns << "Attempt to retrieve status from a NULL handle. Aborted."
  256. << llendl;
  257. llassert(false);
  258. return false;
  259. }
  260. double dbytes = 0;
  261. double ubytes = 0;
  262. CURLcode ccode = curl_easy_getinfo(handle, CURLINFO_SIZE_DOWNLOAD, &dbytes);
  263. CURLcode ccode2 = curl_easy_getinfo(handle, CURLINFO_SIZE_UPLOAD, &ubytes);
  264. if (ccode == CURLE_OK || ccode2 == CURLE_OK)
  265. {
  266. LLCoreInt::HttpScopedLock lock(sStatMutex);
  267. sDownloadedBytes += dbytes;
  268. sUploadedBytes += ubytes;
  269. }
  270. HttpHandle ophandle = NULL;
  271. ccode = curl_easy_getinfo(handle, CURLINFO_PRIVATE, &ophandle);
  272. if (ccode != CURLE_OK)
  273. {
  274. llwarns << "libcurl error: " << ccode
  275. << ". Unable to retrieve operation handle from CURL handle."
  276. << llendl;
  277. return false;
  278. }
  279. HttpOpRequest::ptr_t op(HttpOpRequest::fromHandle<HttpOpRequest>(ophandle));
  280. if (!op)
  281. {
  282. llwarns << "Unable to locate operation by handle. May have expired."
  283. << llendl;
  284. return false;
  285. }
  286. if (handle != op->mCurlHandle || !op->mCurlActive)
  287. {
  288. llwarns << "libcurl handle and HttpOpRequest handle in disagreement or inactive request. Handle: "
  289. << static_cast<HttpHandle>(handle) << llendl;
  290. return false;
  291. }
  292. active_set_t::iterator it = mActiveOps.find(op);
  293. if (it == mActiveOps.end())
  294. {
  295. llwarns << "libcurl completion for request not on active list. Continuing. Handle: "
  296. << static_cast<HttpHandle>(handle) << llendl;
  297. return false;
  298. }
  299. // Deactivate request
  300. mActiveOps.hset_erase(it);
  301. --mActiveHandles[op->mReqPolicy];
  302. op->mCurlActive = false;
  303. // Set final status of request if it has not failed by other mechanisms yet
  304. if (op->mStatus)
  305. {
  306. op->mStatus = HttpStatus(HttpStatus::EXT_CURL_EASY, status);
  307. }
  308. if (op->mStatus)
  309. {
  310. long http_status = HTTP_OK;
  311. ccode = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE,
  312. &http_status);
  313. if (ccode == CURLE_OK)
  314. {
  315. if (http_status >= 100 && http_status <= 999)
  316. {
  317. char* cont_type = NULL;
  318. ccode = curl_easy_getinfo(handle, CURLINFO_CONTENT_TYPE,
  319. &cont_type);
  320. if (ccode != CURLE_OK)
  321. {
  322. llwarns << "CURL error #" << ccode
  323. << " while attempting to get content type. Handle: "
  324. << static_cast<HttpHandle>(handle) << llendl;
  325. }
  326. else if (cont_type)
  327. {
  328. op->mReplyConType = cont_type;
  329. }
  330. op->mStatus = HttpStatus(http_status);
  331. }
  332. else
  333. {
  334. llwarns << "Invalid HTTP response code (" << http_status
  335. << ") received from server." << llendl;
  336. op->mStatus = HttpStatus(HttpStatus::LLCORE,
  337. HE_INVALID_HTTP_STATUS);
  338. }
  339. }
  340. else
  341. {
  342. llwarns << "CURL error #" << ccode
  343. << " while attempting to get response code. Handle: "
  344. << static_cast<HttpHandle>(handle) << llendl;
  345. op->mStatus = HttpStatus(HttpStatus::LLCORE,
  346. HE_INVALID_HTTP_STATUS);
  347. }
  348. }
  349. // Detach from multi and recycle handle.
  350. if (multi_handle)
  351. {
  352. curl_multi_remove_handle(multi_handle, handle);
  353. }
  354. else
  355. {
  356. llwarns << "Curl multi_handle is NULL on remove. Handle: "
  357. << static_cast<HttpHandle>(handle) << llendl;
  358. }
  359. mHandleCache.freeHandle(op->mCurlHandle);
  360. op->mCurlHandle = NULL;
  361. // Tracing
  362. if (op->mTracing > HTTP_TRACE_OFF)
  363. {
  364. llinfos << "TRACE, RequestComplete, Handle: " << op->getHandle()
  365. << ". Status: " << op->mStatus.toTerseString() << llendl;
  366. }
  367. // Dispatch to next stage
  368. HttpPolicy& policy(mService->getPolicy());
  369. return policy.stageAfterCompletion(op); // true if still active
  370. }
  371. int HttpLibcurl::getActiveCountInClass(int policy_class) const
  372. {
  373. llassert(policy_class >= 0 && policy_class < mPolicyCount);
  374. if (mActiveHandles && policy_class >= 0 && policy_class < mPolicyCount)
  375. {
  376. return mActiveHandles[policy_class];
  377. }
  378. else
  379. {
  380. return 0;
  381. }
  382. }
  383. void HttpLibcurl::policyUpdated(int policy_class)
  384. {
  385. if (policy_class < 0 || policy_class >= mPolicyCount || ! mMultiHandles)
  386. {
  387. return;
  388. }
  389. HttpPolicy& policy(mService->getPolicy());
  390. if (!mActiveHandles[policy_class])
  391. {
  392. // Clear to set options. As of libcurl 7.37.0, if a pipelining multi-
  393. // handle has active requests and you try to set the multi-handle to
  394. // non-pipelining, the library gets very angry and goes off the rails
  395. // corrupting memory. A clue that you are about to crash is that you'll
  396. // get a missing server response error (curl code 9). So, if options
  397. // are to be set, we let the multi handle run out of requests, then set
  398. // options, and re-enable request processing.
  399. //
  400. // All of this stall mechanism exists for this reason. If libcurl
  401. // becomes more resilient later, it should be possible to remove all of
  402. // this. The connection limit settings are fine, it's just that
  403. // pipelined-to-non-pipelined transition that is fatal at the moment.
  404. HttpPolicyClass& options(policy.getClassOptions(policy_class));
  405. CURLM* multi_handle(mMultiHandles[policy_class]);
  406. CURLMcode code;
  407. // Enable policy if stalled
  408. policy.stallPolicy(policy_class, false);
  409. mDirtyPolicy[policy_class] = false;
  410. if (options.mPipelining > 1)
  411. {
  412. // We will try to do pipelining on this multihandle
  413. code = curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, 1L);
  414. check_curl_multi_code(code, CURLMOPT_PIPELINING);
  415. code = curl_multi_setopt(multi_handle,
  416. CURLMOPT_MAX_PIPELINE_LENGTH,
  417. long(options.mPipelining));
  418. check_curl_multi_code(code, CURLMOPT_MAX_PIPELINE_LENGTH);
  419. code = curl_multi_setopt(multi_handle,
  420. CURLMOPT_MAX_HOST_CONNECTIONS,
  421. long(options.mPerHostConnectionLimit));
  422. check_curl_multi_code(code, CURLMOPT_MAX_HOST_CONNECTIONS);
  423. code = curl_multi_setopt(multi_handle,
  424. CURLMOPT_MAX_TOTAL_CONNECTIONS,
  425. long(options.mConnectionLimit));
  426. check_curl_multi_code(code, CURLMOPT_MAX_TOTAL_CONNECTIONS);
  427. }
  428. else
  429. {
  430. code = curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, 0L);
  431. check_curl_multi_code(code, CURLMOPT_PIPELINING);
  432. code = curl_multi_setopt(multi_handle,
  433. CURLMOPT_MAX_HOST_CONNECTIONS, 0L);
  434. check_curl_multi_code(code, CURLMOPT_MAX_HOST_CONNECTIONS);
  435. code = curl_multi_setopt(multi_handle,
  436. CURLMOPT_MAX_TOTAL_CONNECTIONS,
  437. long(options.mConnectionLimit));
  438. check_curl_multi_code(code, CURLMOPT_MAX_TOTAL_CONNECTIONS);
  439. }
  440. }
  441. else if (!mDirtyPolicy[policy_class])
  442. {
  443. // Mark policy dirty and request a stall in the policy. When policy
  444. // goes idle, we will re-invoke this method and perform the change.
  445. // Do not allow this thread to sleep while we are waiting for
  446. // quiescence, we will just stop processing.
  447. mDirtyPolicy[policy_class] = true;
  448. policy.stallPolicy(policy_class, true);
  449. }
  450. }
  451. // ---------------------------------------
  452. // HttpLibcurl::HandleCache
  453. // ---------------------------------------
  454. HttpLibcurl::HandleCache::HandleCache()
  455. : mHandleTemplate(NULL)
  456. {
  457. mCache.reserve(50);
  458. }
  459. HttpLibcurl::HandleCache::~HandleCache()
  460. {
  461. if (mHandleTemplate)
  462. {
  463. curl_easy_cleanup(mHandleTemplate);
  464. mHandleTemplate = NULL;
  465. }
  466. for (handle_cache_t::iterator it = mCache.begin(), end = mCache.end();
  467. it != end; ++it)
  468. {
  469. curl_easy_cleanup(*it);
  470. }
  471. mCache.clear();
  472. }
  473. CURL* HttpLibcurl::HandleCache::getHandle()
  474. {
  475. CURL* ret = NULL;
  476. if (!mCache.empty())
  477. {
  478. // Fastest path to handle
  479. ret = mCache.back();
  480. mCache.pop_back();
  481. }
  482. else if (mHandleTemplate)
  483. {
  484. // Still fast path
  485. ret = curl_easy_duphandle(mHandleTemplate);
  486. }
  487. else
  488. {
  489. // When all else fails
  490. ret = curl_easy_init();
  491. }
  492. return ret;
  493. }
  494. void HttpLibcurl::HandleCache::freeHandle(CURL* handle)
  495. {
  496. if (!handle)
  497. {
  498. return;
  499. }
  500. curl_easy_reset(handle);
  501. if (!mHandleTemplate)
  502. {
  503. // Save the first freed handle as a template.
  504. mHandleTemplate = handle;
  505. }
  506. else
  507. {
  508. // Otherwise add it to the cache
  509. if (mCache.size() >= mCache.capacity())
  510. {
  511. mCache.reserve(mCache.capacity() + 50);
  512. }
  513. mCache.push_back(handle);
  514. }
  515. }
  516. // ---------------------------------------
  517. // Free functions
  518. // ---------------------------------------
  519. struct curl_slist* append_headers_to_slist(const HttpHeaders::ptr_t& headers,
  520. struct curl_slist* slist)
  521. {
  522. static const char sep[] = ": ";
  523. std::string header;
  524. header.reserve(128); // Should be more than enough
  525. for (HttpHeaders::const_iterator it = headers->begin(),
  526. end = headers->end();
  527. it != end; ++it)
  528. {
  529. header = it->first + sep + it->second;
  530. slist = curl_slist_append(slist, header.c_str());
  531. }
  532. return slist;
  533. }
  534. } // End namespace LLCore
  535. namespace
  536. {
  537. void check_curl_multi_code(CURLMcode code, int curl_setopt_option)
  538. {
  539. if (code != CURLM_OK)
  540. {
  541. llwarns << "libcurl multi error detected: "
  542. << curl_multi_strerror(code)
  543. << " - curl_multi_setopt option: "
  544. << curl_setopt_option << llendl;
  545. }
  546. }
  547. void check_curl_multi_code(CURLMcode code)
  548. {
  549. if (code != CURLM_OK)
  550. {
  551. llwarns << "libcurl multi error detected: "
  552. << curl_multi_strerror(code) << llendl;
  553. }
  554. }
  555. } // end anonymous namespace