HttpRequestCurlTask.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #include "HttpRequestCurlTask.h"
  2. #include "core/oxygine.h"
  3. #include "core/ThreadDispatcher.h"
  4. #include "SDL.h"
  5. #include "pthread.h"
  6. namespace oxygine
  7. {
  8. CURLM* multi_handle = 0;
  9. static pthread_t _thread;
  10. static ThreadDispatcher _messages;
  11. //ThreadMessages _main;
  12. static HttpRequestTask* createCurl()
  13. {
  14. return new HttpRequestTaskCURL;
  15. }
  16. const unsigned int ID_DONE = sysEventID('C', 'D', 'N');
  17. const unsigned int ID_PROGRESS = sysEventID('C', 'P', 'R');
  18. void mainThreadFunc(const ThreadDispatcher::message& msg)
  19. {
  20. switch (msg.msgid)
  21. {
  22. case ID_DONE:
  23. {
  24. CURL* easy = (CURL*)msg.arg1;
  25. HttpRequestTaskCURL* task = 0;
  26. curl_easy_getinfo(easy, CURLINFO_PRIVATE, &task);
  27. bool ok = (size_t)msg.arg2 == CURLE_OK;
  28. if (ok)
  29. {
  30. int response = 0;
  31. curl_easy_getinfo(easy, CURLINFO_RESPONSE_CODE, &response);
  32. task->_responseCode = response;
  33. //ok = response == 200;
  34. }
  35. #if 0
  36. const Uint8* data = SDL_GetKeyboardState(0);
  37. static bool fail = false;
  38. if (data[SDL_SCANCODE_N])
  39. fail = true;
  40. if (data[SDL_SCANCODE_M])
  41. fail = false;
  42. if (fail)
  43. ok = false;
  44. #endif
  45. if (ok)
  46. {
  47. task->onComplete();
  48. }
  49. else
  50. task->onError();
  51. task->releaseRef();
  52. } break;
  53. case ID_PROGRESS:
  54. {
  55. HttpRequestTaskCURL* task = (HttpRequestTaskCURL*)msg.cbData;
  56. //task->dispatchProgress((int)(size_t)msg.arg2, (int)(size_t)msg.arg1);
  57. } break;
  58. }
  59. }
  60. void* thread(void*)
  61. {
  62. while (true)
  63. {
  64. _messages.wait();
  65. int still_running = -1;
  66. while (still_running)
  67. {
  68. ThreadDispatcher::peekMessage tmsg;
  69. if (_messages.peek(tmsg, true))
  70. {
  71. if (tmsg.msgid == 1)
  72. return 0;
  73. curl_multi_add_handle(multi_handle, (CURL*)tmsg.arg1);
  74. }
  75. int prev = still_running;
  76. curl_multi_perform(multi_handle, &still_running);
  77. if (still_running)
  78. {
  79. struct timeval timeout;
  80. fd_set fdread;
  81. fd_set fdwrite;
  82. fd_set fdexcep;
  83. int maxfd;
  84. FD_ZERO(&fdread);
  85. FD_ZERO(&fdwrite);
  86. FD_ZERO(&fdexcep);
  87. /* set a suitable timeout to play around with */
  88. timeout.tv_sec = 1;
  89. timeout.tv_usec = 0;
  90. /* get file descriptors from the transfers */
  91. curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  92. if (maxfd == -1)
  93. {
  94. sleep(100);
  95. }
  96. else
  97. {
  98. int rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
  99. }
  100. }
  101. if (still_running != prev)
  102. {
  103. CURLMsg* msg = 0;
  104. int num;
  105. while ((msg = curl_multi_info_read(multi_handle, &num)))
  106. {
  107. if (msg->msg == CURLMSG_DONE)
  108. {
  109. #ifdef OX_HAS_CPP11 //msg broken in VS2010
  110. curl_multi_remove_handle(multi_handle, msg->easy_handle);
  111. core::getMainThreadDispatcher().postCallback(ID_DONE, msg->easy_handle, (void*)msg->data.result, mainThreadFunc, 0);
  112. #endif
  113. }
  114. }
  115. }
  116. }
  117. }
  118. return 0;
  119. }
  120. void HttpRequestTask::init()
  121. {
  122. if (multi_handle)
  123. return;
  124. setCustomRequests(createCurl);
  125. multi_handle = curl_multi_init();
  126. pthread_create(&_thread, 0, thread, 0);
  127. }
  128. void HttpRequestTask::release()
  129. {
  130. _messages.post(1, 0, 0);
  131. pthread_join(_thread, 0);
  132. if (multi_handle)
  133. curl_multi_cleanup(multi_handle);
  134. multi_handle = 0;
  135. }
  136. size_t HttpRequestTaskCURL::cbXRefInfoFunction(void* userData, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
  137. {
  138. return ((HttpRequestTaskCURL*)userData)->_cbXRefInfoFunction(dltotal, dlnow);
  139. }
  140. size_t HttpRequestTaskCURL::_cbXRefInfoFunction(curl_off_t dltotal, curl_off_t dlnow)
  141. {
  142. core::getMainThreadDispatcher().postCallback(ID_PROGRESS, (void*)dltotal, (void*)dlnow, mainThreadFunc, this);
  143. return 0;
  144. }
  145. int HttpRequestTaskCURL::cbProgressFunction(void* userData, double dltotal, double dlnow, double ultotal, double ulnow)
  146. {
  147. return ((HttpRequestTaskCURL*)userData)->_cbXRefInfoFunction((curl_off_t) dltotal, (curl_off_t)dlnow);
  148. }
  149. size_t HttpRequestTaskCURL::cbWriteFunction(char* d, size_t n, size_t l, void* userData)
  150. {
  151. return ((HttpRequestTaskCURL*)userData)->_cbWriteFunction(d, n, l);
  152. }
  153. size_t HttpRequestTaskCURL::_cbWriteFunction(char* d, size_t n, size_t l)
  154. {
  155. size_t size = n * l;
  156. if (!_continueDownload || _ok)
  157. write(d, size);
  158. return size;
  159. }
  160. size_t HttpRequestTaskCURL::cbHeaderFunction(char* d, size_t n, size_t l, void* userData)
  161. {
  162. return ((HttpRequestTaskCURL*)userData)->_cbHeaderFunction(d, n, l);
  163. }
  164. size_t HttpRequestTaskCURL::_cbHeaderFunction(char* d, size_t n, size_t l)
  165. {
  166. size_t s = n*l;
  167. if (_continueDownload && !_ok)
  168. {
  169. const char *GOOD1 = "HTTP/1.1 200 ";
  170. const char *GOOD2 = "HTTP/1.1 206 ";
  171. if (s >= sizeof(GOOD1) && (
  172. memcmp(d, GOOD1, sizeof(GOOD1)) == 0 ||
  173. memcmp(d, GOOD2, sizeof(GOOD2)) == 0))
  174. {
  175. _ok = true;
  176. }
  177. }
  178. return s;
  179. }
  180. HttpRequestTaskCURL::HttpRequestTaskCURL() : _easy(0), _httpHeaders(0), _ok(false)
  181. {
  182. _easy = curl_easy_init();
  183. }
  184. HttpRequestTaskCURL::~HttpRequestTaskCURL()
  185. {
  186. if (_easy)
  187. curl_easy_cleanup(_easy);
  188. _easy = 0;
  189. if (_httpHeaders)
  190. curl_slist_free_all(_httpHeaders);
  191. }
  192. void HttpRequestTaskCURL::_run()
  193. {
  194. curl_easy_setopt(_easy, CURLOPT_URL, _url.c_str());
  195. curl_easy_setopt(_easy, CURLOPT_PRIVATE, this);
  196. curl_easy_setopt(_easy, CURLOPT_WRITEFUNCTION, HttpRequestTaskCURL::cbWriteFunction);
  197. curl_easy_setopt(_easy, CURLOPT_WRITEDATA, this);
  198. curl_easy_setopt(_easy, CURLOPT_HEADERFUNCTION, HttpRequestTaskCURL::cbHeaderFunction);
  199. curl_easy_setopt(_easy, CURLOPT_HEADERDATA, this);
  200. curl_easy_setopt(_easy, CURLOPT_NOPROGRESS, 0);
  201. #ifdef CURLOPT_XFERINFOFUNCTION
  202. curl_easy_setopt(_easy, CURLOPT_XFERINFOFUNCTION, HttpRequestTaskCURL::cbXRefInfoFunction);
  203. curl_easy_setopt(_easy, CURLOPT_XFERINFODATA, this);
  204. #else
  205. curl_easy_setopt(_easy, CURLOPT_PROGRESSFUNCTION, HttpRequestTaskCURL::cbProgressFunction);
  206. curl_easy_setopt(_easy, CURLOPT_PROGRESSDATA, this);
  207. #endif
  208. curl_easy_setopt(_easy, CURLOPT_FOLLOWLOCATION, true);
  209. curl_easy_setopt(_easy, CURLOPT_SSL_VERIFYPEER, false);
  210. if (!_postData.empty())
  211. {
  212. curl_easy_setopt(_easy, CURLOPT_POSTFIELDS, &_postData.front());
  213. curl_easy_setopt(_easy, CURLOPT_POSTFIELDSIZE, _postData.size());
  214. }
  215. for (size_t i = 0; i < _headers.size(); ++i)
  216. _httpHeaders = curl_slist_append(_httpHeaders, (_headers[i].first + ": " + _headers[i].second).c_str());
  217. curl_easy_setopt(_easy, CURLOPT_HTTPHEADER, _httpHeaders);
  218. addRef();
  219. _messages.post(0, _easy, 0);
  220. }
  221. }