multi.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. #ifndef CURLINC_MULTI_H
  2. #define CURLINC_MULTI_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at https://curl.haxx.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. ***************************************************************************/
  24. /*
  25. This is an "external" header file. Don't give away any internals here!
  26. GOALS
  27. o Enable a "pull" interface. The application that uses libcurl decides where
  28. and when to ask libcurl to get/send data.
  29. o Enable multiple simultaneous transfers in the same thread without making it
  30. complicated for the application.
  31. o Enable the application to select() on its own file descriptors and curl's
  32. file descriptors simultaneous easily.
  33. */
  34. /*
  35. * This header file should not really need to include "curl.h" since curl.h
  36. * itself includes this file and we expect user applications to do #include
  37. * <curl/curl.h> without the need for especially including multi.h.
  38. *
  39. * For some reason we added this include here at one point, and rather than to
  40. * break existing (wrongly written) libcurl applications, we leave it as-is
  41. * but with this warning attached.
  42. */
  43. #include "curl.h"
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. #if defined(BUILDING_LIBCURL) || defined(CURL_STRICTER)
  48. typedef struct Curl_multi CURLM;
  49. #else
  50. typedef void CURLM;
  51. #endif
  52. typedef enum {
  53. CURLM_CALL_MULTI_PERFORM = -1, /* please call curl_multi_perform() or
  54. curl_multi_socket*() soon */
  55. CURLM_OK,
  56. CURLM_BAD_HANDLE, /* the passed-in handle is not a valid CURLM handle */
  57. CURLM_BAD_EASY_HANDLE, /* an easy handle was not good/valid */
  58. CURLM_OUT_OF_MEMORY, /* if you ever get this, you're in deep sh*t */
  59. CURLM_INTERNAL_ERROR, /* this is a libcurl bug */
  60. CURLM_BAD_SOCKET, /* the passed in socket argument did not match */
  61. CURLM_UNKNOWN_OPTION, /* curl_multi_setopt() with unsupported option */
  62. CURLM_ADDED_ALREADY, /* an easy handle already added to a multi handle was
  63. attempted to get added - again */
  64. CURLM_RECURSIVE_API_CALL, /* an api function was called from inside a
  65. callback */
  66. CURLM_WAKEUP_FAILURE, /* wakeup is unavailable or failed */
  67. CURLM_BAD_FUNCTION_ARGUMENT, /* function called with a bad parameter */
  68. CURLM_LAST
  69. } CURLMcode;
  70. /* just to make code nicer when using curl_multi_socket() you can now check
  71. for CURLM_CALL_MULTI_SOCKET too in the same style it works for
  72. curl_multi_perform() and CURLM_CALL_MULTI_PERFORM */
  73. #define CURLM_CALL_MULTI_SOCKET CURLM_CALL_MULTI_PERFORM
  74. /* bitmask bits for CURLMOPT_PIPELINING */
  75. #define CURLPIPE_NOTHING 0L
  76. #define CURLPIPE_HTTP1 1L
  77. #define CURLPIPE_MULTIPLEX 2L
  78. typedef enum {
  79. CURLMSG_NONE, /* first, not used */
  80. CURLMSG_DONE, /* This easy handle has completed. 'result' contains
  81. the CURLcode of the transfer */
  82. CURLMSG_LAST /* last, not used */
  83. } CURLMSG;
  84. struct CURLMsg {
  85. CURLMSG msg; /* what this message means */
  86. CURL *easy_handle; /* the handle it concerns */
  87. union {
  88. void *whatever; /* message-specific data */
  89. CURLcode result; /* return code for transfer */
  90. } data;
  91. };
  92. typedef struct CURLMsg CURLMsg;
  93. /* Based on poll(2) structure and values.
  94. * We don't use pollfd and POLL* constants explicitly
  95. * to cover platforms without poll(). */
  96. #define CURL_WAIT_POLLIN 0x0001
  97. #define CURL_WAIT_POLLPRI 0x0002
  98. #define CURL_WAIT_POLLOUT 0x0004
  99. struct curl_waitfd {
  100. curl_socket_t fd;
  101. short events;
  102. short revents; /* not supported yet */
  103. };
  104. /*
  105. * Name: curl_multi_init()
  106. *
  107. * Desc: inititalize multi-style curl usage
  108. *
  109. * Returns: a new CURLM handle to use in all 'curl_multi' functions.
  110. */
  111. CURL_EXTERN CURLM *curl_multi_init(void);
  112. /*
  113. * Name: curl_multi_add_handle()
  114. *
  115. * Desc: add a standard curl handle to the multi stack
  116. *
  117. * Returns: CURLMcode type, general multi error code.
  118. */
  119. CURL_EXTERN CURLMcode curl_multi_add_handle(CURLM *multi_handle,
  120. CURL *curl_handle);
  121. /*
  122. * Name: curl_multi_remove_handle()
  123. *
  124. * Desc: removes a curl handle from the multi stack again
  125. *
  126. * Returns: CURLMcode type, general multi error code.
  127. */
  128. CURL_EXTERN CURLMcode curl_multi_remove_handle(CURLM *multi_handle,
  129. CURL *curl_handle);
  130. /*
  131. * Name: curl_multi_fdset()
  132. *
  133. * Desc: Ask curl for its fd_set sets. The app can use these to select() or
  134. * poll() on. We want curl_multi_perform() called as soon as one of
  135. * them are ready.
  136. *
  137. * Returns: CURLMcode type, general multi error code.
  138. */
  139. CURL_EXTERN CURLMcode curl_multi_fdset(CURLM *multi_handle,
  140. fd_set *read_fd_set,
  141. fd_set *write_fd_set,
  142. fd_set *exc_fd_set,
  143. int *max_fd);
  144. /*
  145. * Name: curl_multi_wait()
  146. *
  147. * Desc: Poll on all fds within a CURLM set as well as any
  148. * additional fds passed to the function.
  149. *
  150. * Returns: CURLMcode type, general multi error code.
  151. */
  152. CURL_EXTERN CURLMcode curl_multi_wait(CURLM *multi_handle,
  153. struct curl_waitfd extra_fds[],
  154. unsigned int extra_nfds,
  155. int timeout_ms,
  156. int *ret);
  157. /*
  158. * Name: curl_multi_poll()
  159. *
  160. * Desc: Poll on all fds within a CURLM set as well as any
  161. * additional fds passed to the function.
  162. *
  163. * Returns: CURLMcode type, general multi error code.
  164. */
  165. CURL_EXTERN CURLMcode curl_multi_poll(CURLM *multi_handle,
  166. struct curl_waitfd extra_fds[],
  167. unsigned int extra_nfds,
  168. int timeout_ms,
  169. int *ret);
  170. /*
  171. * Name: curl_multi_wakeup()
  172. *
  173. * Desc: wakes up a sleeping curl_multi_poll call.
  174. *
  175. * Returns: CURLMcode type, general multi error code.
  176. */
  177. CURL_EXTERN CURLMcode curl_multi_wakeup(CURLM *multi_handle);
  178. /*
  179. * Name: curl_multi_perform()
  180. *
  181. * Desc: When the app thinks there's data available for curl it calls this
  182. * function to read/write whatever there is right now. This returns
  183. * as soon as the reads and writes are done. This function does not
  184. * require that there actually is data available for reading or that
  185. * data can be written, it can be called just in case. It returns
  186. * the number of handles that still transfer data in the second
  187. * argument's integer-pointer.
  188. *
  189. * Returns: CURLMcode type, general multi error code. *NOTE* that this only
  190. * returns errors etc regarding the whole multi stack. There might
  191. * still have occurred problems on individual transfers even when
  192. * this returns OK.
  193. */
  194. CURL_EXTERN CURLMcode curl_multi_perform(CURLM *multi_handle,
  195. int *running_handles);
  196. /*
  197. * Name: curl_multi_cleanup()
  198. *
  199. * Desc: Cleans up and removes a whole multi stack. It does not free or
  200. * touch any individual easy handles in any way. We need to define
  201. * in what state those handles will be if this function is called
  202. * in the middle of a transfer.
  203. *
  204. * Returns: CURLMcode type, general multi error code.
  205. */
  206. CURL_EXTERN CURLMcode curl_multi_cleanup(CURLM *multi_handle);
  207. /*
  208. * Name: curl_multi_info_read()
  209. *
  210. * Desc: Ask the multi handle if there's any messages/informationals from
  211. * the individual transfers. Messages include informationals such as
  212. * error code from the transfer or just the fact that a transfer is
  213. * completed. More details on these should be written down as well.
  214. *
  215. * Repeated calls to this function will return a new struct each
  216. * time, until a special "end of msgs" struct is returned as a signal
  217. * that there is no more to get at this point.
  218. *
  219. * The data the returned pointer points to will not survive calling
  220. * curl_multi_cleanup().
  221. *
  222. * The 'CURLMsg' struct is meant to be very simple and only contain
  223. * very basic information. If more involved information is wanted,
  224. * we will provide the particular "transfer handle" in that struct
  225. * and that should/could/would be used in subsequent
  226. * curl_easy_getinfo() calls (or similar). The point being that we
  227. * must never expose complex structs to applications, as then we'll
  228. * undoubtably get backwards compatibility problems in the future.
  229. *
  230. * Returns: A pointer to a filled-in struct, or NULL if it failed or ran out
  231. * of structs. It also writes the number of messages left in the
  232. * queue (after this read) in the integer the second argument points
  233. * to.
  234. */
  235. CURL_EXTERN CURLMsg *curl_multi_info_read(CURLM *multi_handle,
  236. int *msgs_in_queue);
  237. /*
  238. * Name: curl_multi_strerror()
  239. *
  240. * Desc: The curl_multi_strerror function may be used to turn a CURLMcode
  241. * value into the equivalent human readable error string. This is
  242. * useful for printing meaningful error messages.
  243. *
  244. * Returns: A pointer to a null-terminated error message.
  245. */
  246. CURL_EXTERN const char *curl_multi_strerror(CURLMcode);
  247. /*
  248. * Name: curl_multi_socket() and
  249. * curl_multi_socket_all()
  250. *
  251. * Desc: An alternative version of curl_multi_perform() that allows the
  252. * application to pass in one of the file descriptors that have been
  253. * detected to have "action" on them and let libcurl perform.
  254. * See man page for details.
  255. */
  256. #define CURL_POLL_NONE 0
  257. #define CURL_POLL_IN 1
  258. #define CURL_POLL_OUT 2
  259. #define CURL_POLL_INOUT 3
  260. #define CURL_POLL_REMOVE 4
  261. #define CURL_SOCKET_TIMEOUT CURL_SOCKET_BAD
  262. #define CURL_CSELECT_IN 0x01
  263. #define CURL_CSELECT_OUT 0x02
  264. #define CURL_CSELECT_ERR 0x04
  265. typedef int (*curl_socket_callback)(CURL *easy, /* easy handle */
  266. curl_socket_t s, /* socket */
  267. int what, /* see above */
  268. void *userp, /* private callback
  269. pointer */
  270. void *socketp); /* private socket
  271. pointer */
  272. /*
  273. * Name: curl_multi_timer_callback
  274. *
  275. * Desc: Called by libcurl whenever the library detects a change in the
  276. * maximum number of milliseconds the app is allowed to wait before
  277. * curl_multi_socket() or curl_multi_perform() must be called
  278. * (to allow libcurl's timed events to take place).
  279. *
  280. * Returns: The callback should return zero.
  281. */
  282. typedef int (*curl_multi_timer_callback)(CURLM *multi, /* multi handle */
  283. long timeout_ms, /* see above */
  284. void *userp); /* private callback
  285. pointer */
  286. CURL_EXTERN CURLMcode curl_multi_socket(CURLM *multi_handle, curl_socket_t s,
  287. int *running_handles);
  288. CURL_EXTERN CURLMcode curl_multi_socket_action(CURLM *multi_handle,
  289. curl_socket_t s,
  290. int ev_bitmask,
  291. int *running_handles);
  292. CURL_EXTERN CURLMcode curl_multi_socket_all(CURLM *multi_handle,
  293. int *running_handles);
  294. #ifndef CURL_ALLOW_OLD_MULTI_SOCKET
  295. /* This macro below was added in 7.16.3 to push users who recompile to use
  296. the new curl_multi_socket_action() instead of the old curl_multi_socket()
  297. */
  298. #define curl_multi_socket(x,y,z) curl_multi_socket_action(x,y,0,z)
  299. #endif
  300. /*
  301. * Name: curl_multi_timeout()
  302. *
  303. * Desc: Returns the maximum number of milliseconds the app is allowed to
  304. * wait before curl_multi_socket() or curl_multi_perform() must be
  305. * called (to allow libcurl's timed events to take place).
  306. *
  307. * Returns: CURLM error code.
  308. */
  309. CURL_EXTERN CURLMcode curl_multi_timeout(CURLM *multi_handle,
  310. long *milliseconds);
  311. typedef enum {
  312. /* This is the socket callback function pointer */
  313. CURLOPT(CURLMOPT_SOCKETFUNCTION, CURLOPTTYPE_FUNCTIONPOINT, 1),
  314. /* This is the argument passed to the socket callback */
  315. CURLOPT(CURLMOPT_SOCKETDATA, CURLOPTTYPE_OBJECTPOINT, 2),
  316. /* set to 1 to enable pipelining for this multi handle */
  317. CURLOPT(CURLMOPT_PIPELINING, CURLOPTTYPE_LONG, 3),
  318. /* This is the timer callback function pointer */
  319. CURLOPT(CURLMOPT_TIMERFUNCTION, CURLOPTTYPE_FUNCTIONPOINT, 4),
  320. /* This is the argument passed to the timer callback */
  321. CURLOPT(CURLMOPT_TIMERDATA, CURLOPTTYPE_OBJECTPOINT, 5),
  322. /* maximum number of entries in the connection cache */
  323. CURLOPT(CURLMOPT_MAXCONNECTS, CURLOPTTYPE_LONG, 6),
  324. /* maximum number of (pipelining) connections to one host */
  325. CURLOPT(CURLMOPT_MAX_HOST_CONNECTIONS, CURLOPTTYPE_LONG, 7),
  326. /* maximum number of requests in a pipeline */
  327. CURLOPT(CURLMOPT_MAX_PIPELINE_LENGTH, CURLOPTTYPE_LONG, 8),
  328. /* a connection with a content-length longer than this
  329. will not be considered for pipelining */
  330. CURLOPT(CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE, CURLOPTTYPE_OFF_T, 9),
  331. /* a connection with a chunk length longer than this
  332. will not be considered for pipelining */
  333. CURLOPT(CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE, CURLOPTTYPE_OFF_T, 10),
  334. /* a list of site names(+port) that are blocked from pipelining */
  335. CURLOPT(CURLMOPT_PIPELINING_SITE_BL, CURLOPTTYPE_OBJECTPOINT, 11),
  336. /* a list of server types that are blocked from pipelining */
  337. CURLOPT(CURLMOPT_PIPELINING_SERVER_BL, CURLOPTTYPE_OBJECTPOINT, 12),
  338. /* maximum number of open connections in total */
  339. CURLOPT(CURLMOPT_MAX_TOTAL_CONNECTIONS, CURLOPTTYPE_LONG, 13),
  340. /* This is the server push callback function pointer */
  341. CURLOPT(CURLMOPT_PUSHFUNCTION, CURLOPTTYPE_FUNCTIONPOINT, 14),
  342. /* This is the argument passed to the server push callback */
  343. CURLOPT(CURLMOPT_PUSHDATA, CURLOPTTYPE_OBJECTPOINT, 15),
  344. /* maximum number of concurrent streams to support on a connection */
  345. CURLOPT(CURLMOPT_MAX_CONCURRENT_STREAMS, CURLOPTTYPE_LONG, 16),
  346. CURLMOPT_LASTENTRY /* the last unused */
  347. } CURLMoption;
  348. /*
  349. * Name: curl_multi_setopt()
  350. *
  351. * Desc: Sets options for the multi handle.
  352. *
  353. * Returns: CURLM error code.
  354. */
  355. CURL_EXTERN CURLMcode curl_multi_setopt(CURLM *multi_handle,
  356. CURLMoption option, ...);
  357. /*
  358. * Name: curl_multi_assign()
  359. *
  360. * Desc: This function sets an association in the multi handle between the
  361. * given socket and a private pointer of the application. This is
  362. * (only) useful for curl_multi_socket uses.
  363. *
  364. * Returns: CURLM error code.
  365. */
  366. CURL_EXTERN CURLMcode curl_multi_assign(CURLM *multi_handle,
  367. curl_socket_t sockfd, void *sockp);
  368. /*
  369. * Name: curl_push_callback
  370. *
  371. * Desc: This callback gets called when a new stream is being pushed by the
  372. * server. It approves or denies the new stream.
  373. *
  374. * Returns: CURL_PUSH_OK or CURL_PUSH_DENY.
  375. */
  376. #define CURL_PUSH_OK 0
  377. #define CURL_PUSH_DENY 1
  378. struct curl_pushheaders; /* forward declaration only */
  379. CURL_EXTERN char *curl_pushheader_bynum(struct curl_pushheaders *h,
  380. size_t num);
  381. CURL_EXTERN char *curl_pushheader_byname(struct curl_pushheaders *h,
  382. const char *name);
  383. typedef int (*curl_push_callback)(CURL *parent,
  384. CURL *easy,
  385. size_t num_headers,
  386. struct curl_pushheaders *headers,
  387. void *userp);
  388. #ifdef __cplusplus
  389. } /* end of extern "C" */
  390. #endif
  391. #endif