Changes since version 1.44.1: * Add SHA to ChangeLog (Jameson Nash) * aix, ibmi: handle server hang when remote sends TCP RST (V-for-Vasili) * build: make CI a bit noisier (Jameson Nash) * process: reset the signal mask if the fork fails (Jameson Nash) * zos: implement cmpxchgi() using assembly (Shuowang (Wayne) Zhang) * build: AC_SUBST for AM_CFLAGS (Claes Nästén) * ibmi: Implement UDP disconnect (V-for-Vasili) * doc: update active maintainers list (Ben Noordhuis) * build: fix kFreeBSD build (James McCoy) * build: remove Windows 2016 workflows (Darshan Sen) * Revert "win,errors: remap ERROR_ACCESS_DENIED to UV_EACCES" (Darshan Sen) * unix: simplify getpwuid call (Jameson Nash) * build: filter CI by paths and branches (Jameson Nash) * build: add iOS to macos CI (Jameson Nash) * build: re-enable CI for windows changes (Jameson Nash) * process,iOS: fix build breakage in process.c (Denny C. Dai) * test: remove unused declarations in tcp_rst test (V-for-Vasili) * core: add thread-safe strtok implementation (Guilherme Íscaro) * win: fix incompatible-types warning (twosee) * test: fix flaky file watcher test (Ben Noordhuis) * build: fix AIX xlc autotools build (V-for-Vasili) * unix,win: fix UV_RUN_ONCE + uv_idle_stop loop hang (Ben Noordhuis) * win: fix unexpected ECONNRESET error on TCP socket (twosee) * doc: make sample cross-platform build (gengjiawen) * test: separate some static variables by test cases (Hannah Shi) * sunos: fs-event callback can be called after uv_close() (Andy Fiddaman) * uv: re-register interest in a file after change (Shuowang (Wayne) Zhang) * uv: register UV_RENAME event for _RFIM_UNLINK (Shuowang (Wayne) Zhang) * uv: register __rfim_event 156 as UV_RENAME (Shuowang (Wayne) Zhang) * doc: remove smartos from supported platforms (Ben Noordhuis) * macos: avoid posix_spawnp() cwd bug (Jameson Nash) * release: check versions of autogen scripts are newer (Jameson Nash) * test: rewrite embed test (Ben Noordhuis) * openbsd: use utimensat instead of lutimes (tuftedocelot) * doc: fix link to uvwget example main() function (blogdaren) * unix: use MSG_CMSG_CLOEXEC where supported (Ben Noordhuis) * test: remove disabled callback_order test (Ben Noordhuis) * win,pipe: fix bugs with pipe resource lifetime management (Jameson Nash) * loop: better align order-of-events behavior between platforms (Jameson Nash) * aix,test: uv_backend_fd is not supported by poll (V-for-Vasili) * kqueue: skip EVFILT_PROC when invalidating fds (chucksilvers) * darwin: fix atomic-ops.h ppc64 build (Sergey Fedorov) * zos: don't err when killing a zombie process (Shuowang (Wayne) Zhang) * zos: avoid fs event callbacks after uv_close() (Shuowang (Wayne) Zhang) * zos: correctly format interface addresses names (Shuowang (Wayne) Zhang) * zos: add uv_interface_addresses() netmask support (Shuowang (Wayne) Zhang) * zos: improve memory management of ip addresses (Shuowang (Wayne) Zhang) * tcp,pipe: fail `bind` or `listen` after `close` (theanarkh) * zos: implement uv_available_parallelism() (Shuowang (Wayne) Zhang) * udp,win: fix UDP compiler warning (Jameson Nash) * zos: fix early exit of epoll_wait() (Shuowang (Wayne) Zhang) * unix,tcp: fix errno handling in uv__tcp_bind() (Samuel Cabrero) * shutdown,unix: reduce code duplication (Jameson Nash) * unix: fix c99 comments (Ben Noordhuis) * unix: retry tcgetattr/tcsetattr() on EINTR (Ben Noordhuis) * docs: update introduction.rst (Ikko Ashimine) * unix,stream: optimize uv_shutdown() codepath (Jameson Nash) * zos: delay signal handling until after normal i/o (Shuowang (Wayne) Zhang) * stream: uv__drain() always needs to stop POLLOUT (Jameson Nash) * unix,tcp: allow EINVAL errno from setsockopt in uv_tcp_close_reset() (Stacey Marshall) * win,shutdown: improve how shutdown is dispatched (Jameson Nash) -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEz7ucqaW+r9cOKzxaeaZ8VaNnnIsFAmLNnoYACgkQeaZ8VaNn nIsPiRAApE7tNUEknH+7SJ//K910PA0GM8E9/tz8PFN5LDsVlIDa9p40IezhbjMu O3CbA9rPZfugohV8LJ+tDxAEqzPQmb4tLZ1jyzrTCm2RSS56MtJKhPXKKchITzb2 bAV4PNRsnzmZfMd/nJfVpTsBCf/rGOhJwFDChVVHkZgj+BibsYaipMUUwbm3XjCP fdzmhxh0EbCBsMWi8dxuV2R/FliaaH4PoZjvwJvk35mxN4H6ZhU/09Q9mvduljM8 fK3pgqGwge7D0dVoybeAkMDfhMpPkGBEhH6Zif8EkLLdUB1u4EO4lxlPjr4t6aL+ 2VoHqBAWcLk3MGFh9cIRtSWG41RnEtPFJPC5SXBzqMpF28bKGb3DYc9pxl+OkrHW 74UaDiXt446ccMlC3aF6kUbwNAHjYx6pthGPuKTxiPQPn38LE0KZbLH+G7dsRqQ/ DP3+s/UJR4D4IV8rrE3ys6UgTQrCU42gsA2psk6KLYGPaZw/boc7f+X8JOsP8W0g 2zwtmb27ustJQ/06MFhL2wC3J9x4cP9wdZyGSTwTbyXZ5pPQphrSinP/J8KFl5D+ gOEQjuGcyy1beLIsq5sHQz6HCkLGekMTEdz37PqU2+TMd/4VcBmzGvHx1ACPRZdF ljx3dbCE3w9lSbpOxM9DYONuRmfLSeroItLrhxEBL9xKbpKOZ2I= =C1zG -----END PGP SIGNATURE----- Merge tag 'v1.44.2' into merge_1.44.2
167 lines
4.5 KiB
C
167 lines
4.5 KiB
C
#include <assert.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <uv.h>
|
|
#include <curl/curl.h>
|
|
|
|
uv_loop_t *loop;
|
|
CURLM *curl_handle;
|
|
uv_timer_t timeout;
|
|
|
|
typedef struct curl_context_s {
|
|
uv_poll_t poll_handle;
|
|
curl_socket_t sockfd;
|
|
} curl_context_t;
|
|
|
|
curl_context_t *create_curl_context(curl_socket_t sockfd) {
|
|
curl_context_t *context;
|
|
|
|
context = (curl_context_t*) malloc(sizeof *context);
|
|
|
|
context->sockfd = sockfd;
|
|
|
|
int r = uv_poll_init(loop, &context->poll_handle, sockfd);
|
|
assert(r == 0);
|
|
context->poll_handle.data = context;
|
|
|
|
return context;
|
|
}
|
|
|
|
void curl_close_cb(uv_handle_t *handle) {
|
|
curl_context_t *context = (curl_context_t*) handle->data;
|
|
free(context);
|
|
}
|
|
|
|
void destroy_curl_context(curl_context_t *context) {
|
|
uv_close((uv_handle_t*) &context->poll_handle, curl_close_cb);
|
|
}
|
|
|
|
|
|
void add_download(const char *url, int num) {
|
|
char filename[50];
|
|
sprintf(filename, "%d.download", num);
|
|
FILE *file;
|
|
|
|
file = fopen(filename, "w");
|
|
if (file == NULL) {
|
|
fprintf(stderr, "Error opening %s\n", filename);
|
|
return;
|
|
}
|
|
|
|
CURL *handle = curl_easy_init();
|
|
curl_easy_setopt(handle, CURLOPT_WRITEDATA, file);
|
|
curl_easy_setopt(handle, CURLOPT_URL, url);
|
|
curl_multi_add_handle(curl_handle, handle);
|
|
fprintf(stderr, "Added download %s -> %s\n", url, filename);
|
|
}
|
|
|
|
void check_multi_info(void) {
|
|
char *done_url;
|
|
CURLMsg *message;
|
|
int pending;
|
|
|
|
while ((message = curl_multi_info_read(curl_handle, &pending))) {
|
|
switch (message->msg) {
|
|
case CURLMSG_DONE:
|
|
curl_easy_getinfo(message->easy_handle, CURLINFO_EFFECTIVE_URL,
|
|
&done_url);
|
|
printf("%s DONE\n", done_url);
|
|
|
|
curl_multi_remove_handle(curl_handle, message->easy_handle);
|
|
curl_easy_cleanup(message->easy_handle);
|
|
break;
|
|
|
|
default:
|
|
fprintf(stderr, "CURLMSG default\n");
|
|
abort();
|
|
}
|
|
}
|
|
}
|
|
|
|
void curl_perform(uv_poll_t *req, int status, int events) {
|
|
uv_timer_stop(&timeout);
|
|
int running_handles;
|
|
int flags = 0;
|
|
if (status < 0) flags = CURL_CSELECT_ERR;
|
|
if (!status && events & UV_READABLE) flags |= CURL_CSELECT_IN;
|
|
if (!status && events & UV_WRITABLE) flags |= CURL_CSELECT_OUT;
|
|
|
|
curl_context_t *context;
|
|
|
|
context = (curl_context_t*)req;
|
|
|
|
curl_multi_socket_action(curl_handle, context->sockfd, flags, &running_handles);
|
|
check_multi_info();
|
|
}
|
|
|
|
void on_timeout(uv_timer_t *req) {
|
|
int running_handles;
|
|
curl_multi_socket_action(curl_handle, CURL_SOCKET_TIMEOUT, 0, &running_handles);
|
|
check_multi_info();
|
|
}
|
|
|
|
void start_timeout(CURLM *multi, long timeout_ms, void *userp) {
|
|
if (timeout_ms <= 0)
|
|
timeout_ms = 1; /* 0 means directly call socket_action, but we'll do it in a bit */
|
|
uv_timer_start(&timeout, on_timeout, timeout_ms, 0);
|
|
}
|
|
|
|
int handle_socket(CURL *easy, curl_socket_t s, int action, void *userp, void *socketp) {
|
|
curl_context_t *curl_context;
|
|
if (action == CURL_POLL_IN || action == CURL_POLL_OUT) {
|
|
if (socketp) {
|
|
curl_context = (curl_context_t*) socketp;
|
|
}
|
|
else {
|
|
curl_context = create_curl_context(s);
|
|
curl_multi_assign(curl_handle, s, (void *) curl_context);
|
|
}
|
|
}
|
|
|
|
switch (action) {
|
|
case CURL_POLL_IN:
|
|
uv_poll_start(&curl_context->poll_handle, UV_READABLE, curl_perform);
|
|
break;
|
|
case CURL_POLL_OUT:
|
|
uv_poll_start(&curl_context->poll_handle, UV_WRITABLE, curl_perform);
|
|
break;
|
|
case CURL_POLL_REMOVE:
|
|
if (socketp) {
|
|
uv_poll_stop(&((curl_context_t*)socketp)->poll_handle);
|
|
destroy_curl_context((curl_context_t*) socketp);
|
|
curl_multi_assign(curl_handle, s, NULL);
|
|
}
|
|
break;
|
|
default:
|
|
abort();
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
loop = uv_default_loop();
|
|
|
|
if (argc <= 1)
|
|
return 0;
|
|
|
|
if (curl_global_init(CURL_GLOBAL_ALL)) {
|
|
fprintf(stderr, "Could not init cURL\n");
|
|
return 1;
|
|
}
|
|
|
|
uv_timer_init(loop, &timeout);
|
|
|
|
curl_handle = curl_multi_init();
|
|
curl_multi_setopt(curl_handle, CURLMOPT_SOCKETFUNCTION, handle_socket);
|
|
curl_multi_setopt(curl_handle, CURLMOPT_TIMERFUNCTION, start_timeout);
|
|
|
|
while (argc-- > 1) {
|
|
add_download(argv[argc], argc);
|
|
}
|
|
|
|
uv_run(loop, UV_RUN_DEFAULT);
|
|
curl_multi_cleanup(curl_handle);
|
|
return 0;
|
|
}
|