CurlManager.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/Core/CoreEvents.h>
  23. #include <Atomic/Core/Context.h>
  24. #include "CurlManager.h"
  25. #include <curl/curl.h>
  26. namespace ToolCore
  27. {
  28. CurlRequest::CurlRequest(Context* context, const String& url, const String& postData) :
  29. Object(context)
  30. {
  31. curl_ = curl_easy_init();
  32. // take care, curl doesn't make copies of all data
  33. url_ = url;
  34. postData_ = postData;
  35. #if !(defined WIN32 || defined APPLE)
  36. // This line will eventually go away with a CA bundle in place, or other TLS options.
  37. curl_easy_setopt(curl_, CURLOPT_SSL_VERIFYPEER, 0L);
  38. #endif
  39. curl_easy_setopt(curl_, CURLOPT_URL, url_.CString());
  40. curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, Writer);
  41. if (postData.Length())
  42. {
  43. curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, postData_.CString());
  44. }
  45. curl_easy_setopt(curl_, CURLOPT_WRITEDATA, (void *) this);
  46. }
  47. CurlRequest::~CurlRequest()
  48. {
  49. curl_easy_cleanup(curl_);
  50. }
  51. void CurlRequest::ThreadFunction()
  52. {
  53. CURLcode res;
  54. res = curl_easy_perform(curl_);
  55. if(res != CURLE_OK)
  56. {
  57. error_ = curl_easy_strerror(res);
  58. }
  59. shouldRun_ = false;
  60. }
  61. size_t CurlRequest::Writer(void *ptr, size_t size, size_t nmemb, void *crequest)
  62. {
  63. CurlRequest* request = (CurlRequest*) crequest;
  64. size_t realsize = size * nmemb;
  65. const char* text = (const char*) ptr;
  66. for (size_t i = 0; i < realsize; i++)
  67. {
  68. request->response_ += text[i];
  69. }
  70. return realsize;
  71. }
  72. CurlManager::CurlManager(Context* context) :
  73. Object(context)
  74. {
  75. curl_global_init(CURL_GLOBAL_DEFAULT);
  76. SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(CurlManager, HandleUpdate));
  77. }
  78. CurlManager::~CurlManager()
  79. {
  80. curl_global_cleanup();
  81. }
  82. SharedPtr<CurlRequest> CurlManager::MakeRequest(const String& url, const String& postData)
  83. {
  84. SharedPtr<CurlRequest> request(new CurlRequest(context_, url, postData));
  85. request->Run();
  86. requests_.Push(request);
  87. return request;
  88. }
  89. void CurlManager::HandleUpdate(StringHash eventType, VariantMap& eventData)
  90. {
  91. Vector<CurlRequest*> remove;
  92. for (unsigned i = 0; i < requests_.Size(); i++)
  93. {
  94. CurlRequest* request = requests_[i];
  95. if (!request->shouldRun_)
  96. {
  97. remove.Push(request);
  98. VariantMap eventData;
  99. eventData[CurlComplete::P_CURLREQUEST] = request;
  100. request->SendEvent(E_CURLCOMPLETE, eventData);
  101. }
  102. }
  103. for (unsigned i = 0; i < remove.Size(); i++)
  104. {
  105. requests_.Remove(SharedPtr<CurlRequest>(remove[i]));
  106. }
  107. }
  108. }