request_context.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /** **************************************************************************
  2. * request_context.c
  3. *
  4. * Copyright 2008 Bryan Ischo <[email protected]>
  5. *
  6. * This file is part of libs3.
  7. *
  8. * libs3 is free software: you can redistribute it and/or modify it under the
  9. * terms of the GNU Lesser General Public License as published by the Free
  10. * Software Foundation, version 3 or above of the License. You can also
  11. * redistribute and/or modify it under the terms of the GNU General Public
  12. * License, version 2 or above of the License.
  13. *
  14. * In addition, as a special exception, the copyright holders give
  15. * permission to link the code of this library and its programs with the
  16. * OpenSSL library, and distribute linked combinations including the two.
  17. *
  18. * libs3 is distributed in the hope that it will be useful, but WITHOUT ANY
  19. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  21. * details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public License
  24. * version 3 along with libs3, in a file named COPYING. If not, see
  25. * <http://www.gnu.org/licenses/>.
  26. *
  27. * You should also have received a copy of the GNU General Public License
  28. * version 2 along with libs3, in a file named COPYING-GPLv2. If not, see
  29. * <http://www.gnu.org/licenses/>.
  30. *
  31. ************************************************************************** **/
  32. #include <curl/curl.h>
  33. #include <stdlib.h>
  34. #include <sys/select.h>
  35. #include "request.h"
  36. #include "request_context.h"
  37. S3Status S3_create_request_context_ex(S3RequestContext **requestContextReturn,
  38. CURLM *curlm,
  39. S3SetupCurlCallback setupCurlCallback,
  40. void *setupCurlCallbackData)
  41. {
  42. *requestContextReturn =
  43. (S3RequestContext *) malloc(sizeof(S3RequestContext));
  44. if (!*requestContextReturn) {
  45. return S3StatusOutOfMemory;
  46. }
  47. if (curlm) {
  48. (*requestContextReturn)->curlm = curlm;
  49. (*requestContextReturn)->curl_mode = S3CurlModeMultiSocket;
  50. }
  51. else {
  52. if (!((*requestContextReturn)->curlm = curl_multi_init())) {
  53. free(*requestContextReturn);
  54. return S3StatusOutOfMemory;
  55. }
  56. (*requestContextReturn)->curl_mode = S3CurlModeMultiPerform;
  57. }
  58. (*requestContextReturn)->requests = 0;
  59. (*requestContextReturn)->verifyPeer = 0;
  60. (*requestContextReturn)->verifyPeerSet = 0;
  61. (*requestContextReturn)->setupCurlCallback = setupCurlCallback;
  62. (*requestContextReturn)->setupCurlCallbackData = setupCurlCallbackData;
  63. return S3StatusOK;
  64. }
  65. S3Status S3_create_request_context(S3RequestContext **requestContextReturn)
  66. {
  67. return S3_create_request_context_ex(requestContextReturn, NULL, NULL, NULL);
  68. }
  69. void S3_destroy_request_context(S3RequestContext *requestContext)
  70. {
  71. // For each request in the context, remove curl handle, call back its done
  72. // method with 'interrupted' status
  73. Request *r = requestContext->requests, *rFirst = r;
  74. if (r) do {
  75. r->status = S3StatusInterrupted;
  76. // remove easy handle from a multi session
  77. curl_multi_remove_handle(requestContext->curlm, r->curl);
  78. Request *rNext = r->next;
  79. request_finish(r);
  80. r = rNext;
  81. } while (r != rFirst);
  82. if (requestContext->curl_mode == S3CurlModeMultiPerform)
  83. curl_multi_cleanup(requestContext->curlm);
  84. free(requestContext);
  85. }
  86. S3Status S3_runall_request_context(S3RequestContext *requestContext)
  87. {
  88. int requestsRemaining;
  89. do {
  90. fd_set readfds, writefds, exceptfds;
  91. FD_ZERO(&readfds);
  92. FD_ZERO(&writefds);
  93. FD_ZERO(&exceptfds);
  94. int maxfd;
  95. S3Status status = S3_get_request_context_fdsets
  96. (requestContext, &readfds, &writefds, &exceptfds, &maxfd);
  97. if (status != S3StatusOK) {
  98. return status;
  99. }
  100. // curl will return -1 if it hasn't even created any fds yet because
  101. // none of the connections have started yet. In this case, don't
  102. // do the select at all, because it will wait forever; instead, just
  103. // skip it and go straight to running the underlying CURL handles
  104. if (maxfd != -1) {
  105. int64_t timeout = S3_get_request_context_timeout(requestContext);
  106. struct timeval tv = { timeout / 1000, (timeout % 1000) * 1000 };
  107. select(maxfd + 1, &readfds, &writefds, &exceptfds,
  108. (timeout == -1) ? 0 : &tv);
  109. }
  110. status = S3_runonce_request_context(requestContext,
  111. &requestsRemaining);
  112. if (status != S3StatusOK) {
  113. return status;
  114. }
  115. } while (requestsRemaining);
  116. return S3StatusOK;
  117. }
  118. static S3Status process_request_context(S3RequestContext *requestContext, int *retry)
  119. {
  120. CURLMsg *msg;
  121. int junk;
  122. *retry = 0;
  123. while ((msg = curl_multi_info_read(requestContext->curlm, &junk))) {
  124. if (msg->msg != CURLMSG_DONE) {
  125. return S3StatusInternalError;
  126. }
  127. Request *request;
  128. if (curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE,
  129. (char **) (char *) &request) != CURLE_OK) {
  130. return S3StatusInternalError;
  131. }
  132. // Remove the request from the list of requests
  133. if (request->prev == request->next) {
  134. // It was the only one on the list
  135. requestContext->requests = 0;
  136. }
  137. else {
  138. // It doesn't matter what the order of them are, so just in
  139. // case request was at the head of the list, put the one after
  140. // request to the head of the list
  141. requestContext->requests = request->next;
  142. request->prev->next = request->next;
  143. request->next->prev = request->prev;
  144. }
  145. if ((msg->data.result != CURLE_OK) &&
  146. (request->status == S3StatusOK)) {
  147. request->status = request_curl_code_to_status(
  148. msg->data.result);
  149. }
  150. if (curl_multi_remove_handle(requestContext->curlm,
  151. msg->easy_handle) != CURLM_OK) {
  152. return S3StatusInternalError;
  153. }
  154. // Finish the request, ensuring that all callbacks have been made,
  155. // and also releases the request
  156. request_finish(request);
  157. // Now, since a callback was made, there may be new requests
  158. // queued up to be performed immediately, so do so
  159. *retry = 1;
  160. }
  161. return S3StatusOK;
  162. }
  163. S3Status S3_runonce_request_context(S3RequestContext *requestContext,
  164. int *requestsRemainingReturn)
  165. {
  166. S3Status s3_status;
  167. CURLMcode status;
  168. int retry;
  169. do {
  170. status = curl_multi_perform(requestContext->curlm,
  171. requestsRemainingReturn);
  172. switch (status) {
  173. case CURLM_OK:
  174. case CURLM_CALL_MULTI_PERFORM:
  175. break;
  176. case CURLM_OUT_OF_MEMORY:
  177. return S3StatusOutOfMemory;
  178. default:
  179. return S3StatusInternalError;
  180. }
  181. s3_status = process_request_context(requestContext, &retry);
  182. } while (s3_status == S3StatusOK &&
  183. (status == CURLM_CALL_MULTI_PERFORM || retry));
  184. return s3_status;
  185. }
  186. S3Status S3_process_request_context(S3RequestContext *requestContext)
  187. {
  188. int retry;
  189. /* In curl_multi_socket_action mode any new requests created during
  190. the following call will have already started associated socket
  191. operations, so no need to retry here */
  192. return process_request_context(requestContext, &retry);
  193. }
  194. S3Status S3_get_request_context_fdsets(S3RequestContext *requestContext,
  195. fd_set *readFdSet, fd_set *writeFdSet,
  196. fd_set *exceptFdSet, int *maxFd)
  197. {
  198. return ((curl_multi_fdset(requestContext->curlm, readFdSet, writeFdSet,
  199. exceptFdSet, maxFd) == CURLM_OK) ?
  200. S3StatusOK : S3StatusInternalError);
  201. }
  202. int64_t S3_get_request_context_timeout(S3RequestContext *requestContext)
  203. {
  204. long timeout;
  205. if (curl_multi_timeout(requestContext->curlm, &timeout) != CURLM_OK) {
  206. timeout = 0;
  207. }
  208. return timeout;
  209. }
  210. void S3_set_request_context_verify_peer(S3RequestContext *requestContext,
  211. int verifyPeer)
  212. {
  213. requestContext->verifyPeerSet = 1;
  214. requestContext->verifyPeer = (verifyPeer != 0);
  215. }