bool CMultiHttp::Perform() { struct timeval timeout; int still_running=0; int rc=0; /* select() return code */ CURLMcode mc; /* curl_multi_fdset() return code */ fd_set fdread; fd_set fdwrite; fd_set fdexcep; int maxfd = -1; long curl_timeo = -1; FD_ZERO(&fdread); FD_ZERO(&fdwrite); FD_ZERO(&fdexcep); /* set a suitable timeout to play around with */ timeout.tv_sec = 5; timeout.tv_usec = 0; curl_multi_timeout(m_murl, &curl_timeo); if( curl_timeo==0 ) curl_timeo=50; if(curl_timeo >= 0) { timeout.tv_sec = curl_timeo/1000; if(timeout.tv_sec > 1) timeout.tv_sec = 1; else timeout.tv_usec = (curl_timeo % 1000) * 1000; } /* get file descriptors from the transfers */ mc = curl_multi_fdset(m_murl, &fdread, &fdwrite, &fdexcep, &maxfd); if(mc != CURLM_OK) { LOG_FUNC_P1(_T("curl_multi_fdset() failed, code %d"),mc); return false; } /* On success the value of maxfd is guaranteed to be >= -1. We call select(maxfd + 1, ...); specially in case of (maxfd == -1) there are no fds ready yet so we call select(0, ...) --or Sleep() on Windows-- to sleep 100ms, which is the minimum suggested value in the curl_multi_fdset() doc. */ if(maxfd == -1) { Sleep(100); rc = 0; } else { FD_SET(m_fds[1],&fdread); FD_SET(m_fds[1],&fdwrite); FD_SET(m_fds[1],&fdexcep); rc = ::select(maxfd+2, &fdread,&fdwrite, &fdexcep, &timeout); if(FD_ISSET(m_fds[1],&fdread)) { std::string cmd; if( !recvcmd(m_fds[1],cmd) ) { m_state = CMultiHttp::Terminate; return false; } if( cmd==CMD_CANCEL ) { LOG_FUNC_P0(_T("url 后台线程 收到退出指令")); m_state = CMultiHttp::Terminate; return false; } } } switch(rc) { case -1: /* select error */ break; case 0: default: while( CURLM_CALL_MULTI_PERFORM==curl_multi_perform(m_murl,&still_running) ) ; { struct CURLMsg *m; int msgQ=0; m = curl_multi_info_read(m_murl,&msgQ); if(m && (m->msg == CURLMSG_DONE)) { CURL *e = m->easy_handle; if( !OneComplete(e,m->data.result) ) still_running=0; } } break; } if( still_running ) return true; m_state = CMultiHttp::Terminate; return false; }
URL_FILE * CoolUrlFopen(const char *url,const char *operation) { /* this code could check for URLs or types in the 'url' and basicly use the real fopen() for standard files */ URL_FILE *file; int ret; (void)operation; char head_buffer[BUFFER_SIZE]; file = (URL_FILE *)malloc(sizeof(URL_FILE)); if(!file) return NULL; memset(file, 0, sizeof(URL_FILE)); if(url) file->url=strdup(url); else file->url=NULL; curl_global_init(CURL_GLOBAL_ALL); //get http head file->curl = curl_easy_init(); curl_easy_setopt(file->curl, CURLOPT_URL, file->url); curl_easy_setopt(file->curl, CURLOPT_WRITEDATA, head_buffer); curl_easy_setopt(file->curl, CURLOPT_NOBODY, 1); curl_easy_setopt(file->curl, CURLOPT_HEADER, 1); curl_easy_setopt(file->curl, CURLOPT_VERBOSE, 0); curl_easy_setopt(file->curl, CURLOPT_WRITEFUNCTION, write_head_callback); ret = curl_easy_perform(file->curl); curl_easy_cleanup(file->curl); if(ret != CURLE_OK) { printf("get file size error url=%s",file->url); return NULL; } file->size = get_file_size_from_head(head_buffer); if(!file->size) return NULL; file->curl = curl_easy_init(); curl_easy_setopt(file->curl, CURLOPT_URL, file->url); curl_easy_setopt(file->curl, CURLOPT_WRITEDATA, file); curl_easy_setopt(file->curl, CURLOPT_VERBOSE, 0); curl_easy_setopt(file->curl, CURLOPT_WRITEFUNCTION, write_callback); multi_handle = curl_multi_init(); curl_multi_add_handle(multi_handle, file->curl); // lets start the fetch while(curl_multi_perform(multi_handle, &file->still_running) == CURLM_CALL_MULTI_PERFORM ); if((file->buffer_pos == 0) && (!file->still_running)) { // if still_running is 0 now, we should return NULL // make sure the easy handle is not in the multi handle anymore curl_multi_remove_handle(multi_handle, file->curl); // cleanup curl_easy_cleanup(file->curl); free(file); file = NULL; } return file; }
static void curl_main () { fd_set rs; fd_set ws; fd_set es; int max; struct GNUNET_NETWORK_FDSet nrs; struct GNUNET_NETWORK_FDSet nws; struct GNUNET_TIME_Relative delay; long timeout; int running; struct CURLMsg *msg; max = 0; FD_ZERO (&rs); FD_ZERO (&ws); FD_ZERO (&es); curl_multi_perform (multi, &running); if (running == 0) { GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running))); if (msg->msg == CURLMSG_DONE) { if (msg->data.result != CURLE_OK) { fprintf (stderr, "%s failed at %s:%d: `%s'\n", "curl_multi_perform", __FILE__, __LINE__, curl_easy_strerror (msg->data.result)); global_ret = 1; } } curl_multi_remove_handle (multi, curl); curl_multi_cleanup (multi); curl_easy_cleanup (curl); curl = NULL; multi = NULL; if (cbc.pos != strlen ("/hello_world")) { GNUNET_break (0); global_ret = 2; } if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) { GNUNET_break (0); global_ret = 3; } GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n"); do_shutdown (); return; } GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max)); if ( (CURLM_OK != curl_multi_timeout (multi, &timeout)) || (-1 == timeout) ) delay = GNUNET_TIME_UNIT_SECONDS; else delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, (unsigned int) timeout); GNUNET_NETWORK_fdset_copy_native (&nrs, &rs, max + 1); GNUNET_NETWORK_fdset_copy_native (&nws, &ws, max + 1); curl_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, delay, &nrs, &nws, &curl_task, NULL); }
void rest_multi_perform(MultiRestState *state) { int still_running; curl_multi_perform(state->multi_handle, &still_running); }
CURLcode CURLNetRequest::CURLHolder::perform() { activate(); int const CHUNK = 1; long noactivity = 0; CURLcode result; for(;;) { int running; CURLMcode err = curl_multi_perform(m_curlm, &running); if (err == CURLM_CALL_MULTI_PERFORM) continue; if (err != CURLM_OK) { RAWLOG_ERROR1("curl_multi_perform error: %d", (int)err); } else { if (running > 0 && noactivity < timeout) { RAWTRACE("we still have active transfers but no data ready at this moment; waiting..."); fd_set rfd, wfd, efd; int n = 0; FD_ZERO(&rfd); FD_ZERO(&wfd); FD_ZERO(&efd); err = curl_multi_fdset(m_curlm, &rfd, &wfd, &efd, &n); if (err == CURLM_OK) { if (n > 0) { timeval tv; tv.tv_sec = CHUNK; tv.tv_usec = 0; int e = select(n + 1, &rfd, &wfd, &efd, &tv); if (e < 0) { RAWLOG_ERROR1("select (on curl handles) error: %d", errno); } else { if (e == 0) { RAWTRACE("No activity on sockets, check them again"); noactivity += CHUNK; } else noactivity = 0; continue; } } } else { RAWLOG_ERROR1("curl_multi_fdset error: %d", (int)err); } } } int nmsgs; CURLMsg *msg = curl_multi_info_read(m_curlm, &nmsgs); result = CURLE_OK; if (msg && msg->msg == CURLMSG_DONE) result = msg->data.result; if (result == CURLE_OK && noactivity >= timeout) result = CURLE_OPERATION_TIMEDOUT; if (result == CURLE_OK || result == CURLE_PARTIAL_FILE) RAWTRACE("Operation completed successfully"); else RAWLOG_ERROR2("Operation finished with error %d: %s", (int)result, curl_easy_strerror(result)); break; } deactivate(); return result; }
static int testExternalGet () { struct MHD_Daemon *d; CURL *c; char buf[2048]; struct CBC cbc; CURLM *multi; CURLMcode mret; fd_set rs; fd_set ws; fd_set es; int max; int running; struct CURLMsg *msg; time_t start; struct timeval tv; multi = NULL; cbc.buf = buf; cbc.size = 2048; cbc.pos = 0; d = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL, 1082, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem, MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem, MHD_OPTION_END); if (d == NULL) return 256; char *aes256_sha = "AES256-SHA"; if (curl_uses_nss_ssl() == 0) { aes256_sha = "rsa_aes_256_sha"; } c = curl_easy_init (); curl_easy_setopt (c, CURLOPT_URL, "https://localhost:1082/hello_world"); curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); /* TLS options */ curl_easy_setopt (c, CURLOPT_SSLVERSION, CURL_SSLVERSION_SSLv3); curl_easy_setopt (c, CURLOPT_SSL_CIPHER_LIST, aes256_sha); curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0); curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 0); curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); if (oneone) curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); else curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L); /* NOTE: use of CONNECTTIMEOUT without also setting NOSIGNAL results in really weird crashes on my system! */ curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); multi = curl_multi_init (); if (multi == NULL) { curl_easy_cleanup (c); MHD_stop_daemon (d); return 512; } mret = curl_multi_add_handle (multi, c); if (mret != CURLM_OK) { curl_multi_cleanup (multi); curl_easy_cleanup (c); MHD_stop_daemon (d); return 1024; } start = time (NULL); while ((time (NULL) - start < 5) && (multi != NULL)) { max = 0; FD_ZERO (&rs); FD_ZERO (&ws); FD_ZERO (&es); curl_multi_perform (multi, &running); mret = curl_multi_fdset (multi, &rs, &ws, &es, &max); if (mret != CURLM_OK) { curl_multi_remove_handle (multi, c); curl_multi_cleanup (multi); curl_easy_cleanup (c); MHD_stop_daemon (d); return 2048; } if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max)) { curl_multi_remove_handle (multi, c); curl_multi_cleanup (multi); curl_easy_cleanup (c); MHD_stop_daemon (d); return 4096; } tv.tv_sec = 0; tv.tv_usec = 1000; select (max + 1, &rs, &ws, &es, &tv); curl_multi_perform (multi, &running); if (running == 0) { msg = curl_multi_info_read (multi, &running); if (msg == NULL) break; if (msg->msg == CURLMSG_DONE) { if (msg->data.result != CURLE_OK) printf ("%s failed at %s:%d: `%s'\n", "curl_multi_perform", __FILE__, __LINE__, curl_easy_strerror (msg->data.result)); curl_multi_remove_handle (multi, c); curl_multi_cleanup (multi); curl_easy_cleanup (c); c = NULL; multi = NULL; } } MHD_run (d); } if (multi != NULL) { curl_multi_remove_handle (multi, c); curl_easy_cleanup (c); curl_multi_cleanup (multi); } MHD_stop_daemon (d); if (cbc.pos != strlen ("/hello_world")) return 8192; if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) return 16384; return 0; }
int test(char *URL) { CURL *easy; CURLM *multi_handle; int still_running; /* keep number of running handles */ CURLMsg *msg; /* for picking up messages with the transfer status */ int msgs_left; /* how many messages are left */ /* Allocate one CURL handle per transfer */ easy = curl_easy_init(); /* init a multi stack */ multi_handle = curl_multi_init(); /* add the individual transfer */ curl_multi_add_handle(multi_handle, easy); /* set the options (I left out a few, you'll get the point anyway) */ curl_easy_setopt(easy, CURLOPT_URL, URL); curl_easy_setopt(easy, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t)testDataSize); curl_easy_setopt(easy, CURLOPT_POSTFIELDS, testData); /* we start some action by calling perform right away */ curl_multi_perform(multi_handle, &still_running); do { struct timeval timeout; int rc; /* select() return code */ CURLMcode mc; /* curl_multi_fdset() return code */ fd_set fdread; fd_set fdwrite; fd_set fdexcep; int maxfd = -1; long curl_timeo = -1; FD_ZERO(&fdread); FD_ZERO(&fdwrite); FD_ZERO(&fdexcep); /* set a suitable timeout to play around with */ timeout.tv_sec = 1; timeout.tv_usec = 0; curl_multi_timeout(multi_handle, &curl_timeo); if(curl_timeo >= 0) { timeout.tv_sec = curl_timeo / 1000; if(timeout.tv_sec > 1) timeout.tv_sec = 1; else timeout.tv_usec = (curl_timeo % 1000) * 1000; } /* get file descriptors from the transfers */ mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); if(mc != CURLM_OK) { fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc); break; } /* On success the value of maxfd is guaranteed to be >= -1. We call select(maxfd + 1, ...); specially in case of (maxfd == -1) there are no fds ready yet so we call select(0, ...) --or Sleep() on Windows-- to sleep 100ms, which is the minimum suggested value in the curl_multi_fdset() doc. */ if(maxfd == -1) { #ifdef _WIN32 Sleep(100); rc = 0; #else /* Portable sleep for platforms other than Windows. */ struct timeval wait = { 0, 100 * 1000 }; /* 100ms */ rc = select(0, NULL, NULL, NULL, &wait); #endif } else { /* Note that on some platforms 'timeout' may be modified by select(). If you need access to the original value save a copy beforehand. */ rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); } switch(rc) { case -1: /* select error */ break; case 0: /* timeout */ default: /* action */ curl_multi_perform(multi_handle, &still_running); break; } } while(still_running); /* See how the transfers went */ while((msg = curl_multi_info_read(multi_handle, &msgs_left))) { if(msg->msg == CURLMSG_DONE) { printf("HTTP transfer completed with status %d\n", msg->data.result); break; } } curl_multi_cleanup(multi_handle); /* Free the CURL handles */ curl_easy_cleanup(easy); return 0; }
void CurlDownloadManager::downloadThread(void* data) { CurlDownloadManager* downloadManager = reinterpret_cast<CurlDownloadManager*>(data); while (downloadManager->runThread()) { downloadManager->updateHandleList(); // Retry 'select' if it was interrupted by a process signal. int rc = 0; do { fd_set fdread; fd_set fdwrite; fd_set fdexcep; int maxfd = 0; const int selectTimeoutMS = 5; struct timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = selectTimeoutMS * 1000; // select waits microseconds FD_ZERO(&fdread); FD_ZERO(&fdwrite); FD_ZERO(&fdexcep); curl_multi_fdset(downloadManager->getMultiHandle(), &fdread, &fdwrite, &fdexcep, &maxfd); // When the 3 file descriptors are empty, winsock will return -1 // and bail out, stopping the file download. So make sure we // have valid file descriptors before calling select. if (maxfd >= 0) rc = ::select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout); } while (rc == -1 && errno == EINTR); int activeDownloadCount = 0; while (curl_multi_perform(downloadManager->getMultiHandle(), &activeDownloadCount) == CURLM_CALL_MULTI_PERFORM) { } int messagesInQueue = 0; CURLMsg* msg = curl_multi_info_read(downloadManager->getMultiHandle(), &messagesInQueue); if (!msg) continue; CurlDownload* download = 0; CURLcode err = curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &download); if (msg->msg == CURLMSG_DONE) { if (msg->data.result == CURLE_OK) callOnMainThread([download] { if (download) download->didFinish(); }); else callOnMainThread([download] { if (download) download->didFail(); }); downloadManager->removeFromCurl(msg->easy_handle); } downloadManager->stopThreadIfIdle(); } }
int main(void) { signal(SIGINT, sighandler); LIBXML_TEST_VERSION; curl_global_init(CURL_GLOBAL_DEFAULT); CURLM *multi_handle = curl_multi_init(); curl_multi_setopt(multi_handle, CURLMOPT_MAX_TOTAL_CONNECTIONS, max_con); curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, 6L); /* enables http/2 if available */ #ifdef CURLPIPE_MULTIPLEX curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX); #endif /* sets html start page */ curl_multi_add_handle(multi_handle, make_handle(start_page)); int msgs_left; int pending = 0; int complete = 0; int still_running = 1; while(still_running && !pending_interrupt) { int numfds; curl_multi_wait(multi_handle, NULL, 0, 1000, &numfds); curl_multi_perform(multi_handle, &still_running); /* See how the transfers went */ CURLMsg *m = NULL; while((m = curl_multi_info_read(multi_handle, &msgs_left))) { if(m->msg == CURLMSG_DONE) { CURL *handle = m->easy_handle; char *url; memory *mem; curl_easy_getinfo(handle, CURLINFO_PRIVATE, &mem); curl_easy_getinfo(handle, CURLINFO_EFFECTIVE_URL, &url); if(m->data.result == CURLE_OK) { long res_status; curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &res_status); if(res_status == 200) { char *ctype; curl_easy_getinfo(handle, CURLINFO_CONTENT_TYPE, &ctype); printf("[%d] HTTP 200 (%s): %s\n", complete, ctype, url); if(is_html(ctype) && mem->size > 100) { if(pending < max_requests && (complete + pending) < max_total) { pending += follow_links(multi_handle, mem, url); still_running = 1; } } } else { printf("[%d] HTTP %d: %s\n", complete, (int) res_status, url); } } else { printf("[%d] Connection failure: %s\n", complete, url); } curl_multi_remove_handle(multi_handle, handle); curl_easy_cleanup(handle); free(mem->buf); free(mem); complete++; pending--; } } } curl_multi_cleanup(multi_handle); curl_global_cleanup(); return 0; }
/* * Source code in here hugely as reported in bug report 651464 by * Christopher R. Palmer. * * Use multi interface to get document over proxy with bad port number. * This caused the interface to "hang" in libcurl 7.10.2. */ int test(char *URL) { CURL *c; int ret=0; CURLM *m; fd_set rd, wr, exc; CURLMcode res; int running; int max_fd; int rc; curl_global_init(CURL_GLOBAL_ALL); c = curl_easy_init(); /* the point here being that there must not run anything on the given proxy port */ curl_easy_setopt(c, CURLOPT_PROXY, arg2); curl_easy_setopt(c, CURLOPT_URL, URL); curl_easy_setopt(c, CURLOPT_VERBOSE, 1); m = curl_multi_init(); res = curl_multi_add_handle(m, c); if(res && (res != CURLM_CALL_MULTI_PERFORM)) return 1; /* major failure */ do { struct timeval interval; interval.tv_sec = 1; interval.tv_usec = 0; fprintf(stderr, "curl_multi_perform()\n"); do { res = curl_multi_perform(m, &running); } while (res == CURLM_CALL_MULTI_PERFORM); if(!running) { /* This is where this code is expected to reach */ int numleft; CURLMsg *msg = curl_multi_info_read(m, &numleft); fprintf(stderr, "Expected: not running\n"); if(msg && !numleft) ret = 100; /* this is where we should be */ else ret = 99; /* not correct */ break; } fprintf(stderr, "running == %d, res == %d\n", running, res); if (res != CURLM_OK) { ret = 2; break; } FD_ZERO(&rd); FD_ZERO(&wr); FD_ZERO(&exc); max_fd = 0; fprintf(stderr, "curl_multi_fdset()\n"); if (curl_multi_fdset(m, &rd, &wr, &exc, &max_fd) != CURLM_OK) { fprintf(stderr, "unexpected failured of fdset.\n"); ret = 3; break; } rc = select(max_fd+1, &rd, &wr, &exc, &interval); fprintf(stderr, "select returned %d\n", rc); } while(rc); curl_multi_remove_handle(m, c); curl_easy_cleanup(c); curl_multi_cleanup(m); return ret; }
/* * Upload all files over HTTP/2, using the same physical connection! */ int main(int argc, char **argv) { CURL *easy[NUM_HANDLES]; CURLM *multi_handle; int i; int still_running; /* keep number of running handles */ const char *filename = "index.html"; if(argc > 1) /* if given a number, do that many transfers */ num_transfers = atoi(argv[1]); if(argc > 2) /* if given a file name, upload this! */ filename = argv[2]; if(!num_transfers || (num_transfers > NUM_HANDLES)) num_transfers = 3; /* a suitable low default */ /* init a multi stack */ multi_handle = curl_multi_init(); for(i=0; i<num_transfers; i++) { easy[i] = curl_easy_init(); /* set options */ setup(easy[i], i, filename); /* add the individual transfer */ curl_multi_add_handle(multi_handle, easy[i]); } curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX); /* We do HTTP/2 so let's stick to one connection per host */ curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, 1L); /* we start some action by calling perform right away */ curl_multi_perform(multi_handle, &still_running); do { struct timeval timeout; int rc; /* select() return code */ CURLMcode mc; /* curl_multi_fdset() return code */ fd_set fdread; fd_set fdwrite; fd_set fdexcep; int maxfd = -1; long curl_timeo = -1; FD_ZERO(&fdread); FD_ZERO(&fdwrite); FD_ZERO(&fdexcep); /* set a suitable timeout to play around with */ timeout.tv_sec = 1; timeout.tv_usec = 0; curl_multi_timeout(multi_handle, &curl_timeo); if(curl_timeo >= 0) { timeout.tv_sec = curl_timeo / 1000; if(timeout.tv_sec > 1) timeout.tv_sec = 1; else timeout.tv_usec = (curl_timeo % 1000) * 1000; } /* get file descriptors from the transfers */ mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); if(mc != CURLM_OK) { fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc); break; } /* On success the value of maxfd is guaranteed to be >= -1. We call select(maxfd + 1, ...); specially in case of (maxfd == -1) there are no fds ready yet so we call select(0, ...) --or Sleep() on Windows-- to sleep 100ms, which is the minimum suggested value in the curl_multi_fdset() doc. */ if(maxfd == -1) { #ifdef _WIN32 Sleep(100); rc = 0; #else /* Portable sleep for platforms other than Windows. */ struct timeval wait = { 0, 100 * 1000 }; /* 100ms */ rc = select(0, NULL, NULL, NULL, &wait); #endif } else { /* Note that on some platforms 'timeout' may be modified by select(). If you need access to the original value save a copy beforehand. */ rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); } switch(rc) { case -1: /* select error */ break; case 0: default: /* timeout or readable/writable sockets */ curl_multi_perform(multi_handle, &still_running); break; } } while(still_running); curl_multi_cleanup(multi_handle); for(i=0; i<num_transfers; i++) curl_easy_cleanup(easy[i]); return 0; }
void IStreamUrlImplCurl::fillBuffer( int wantBytes ) const { // first make sure we've started reading, and do so if not if( ! mStartedRead ) { while( curl_multi_perform( mMulti, &still_running ) == CURLM_CALL_MULTI_PERFORM ); if( ( bufferRemaining() == 0 ) && ( ! still_running ) ) { throw StreamExc(); } mStartedRead = true; } // only attempt to fill buffer if transactions still running and buffer // doesnt exceed required size already if( ( ! still_running ) || ( bufferRemaining() >= wantBytes ) ) return; // if we want more bytes than will fit in the rest of the buffer, let's make some room if( mBufferSize - mBufferedBytes < wantBytes ) { int bytesCulled = mBufferOffset; memmove( mBuffer, &mBuffer[mBufferOffset], mBufferedBytes - bytesCulled ); mBufferedBytes -= bytesCulled; mBufferOffset = 0; mBufferFileOffset += bytesCulled; } // attempt to fill buffer do { fd_set fdread; fd_set fdwrite; fd_set fdexcep; int maxfd; struct timeval timeout; FD_ZERO( &fdread ); FD_ZERO( &fdwrite ); FD_ZERO( &fdexcep ); // set a suitable timeout to fail on timeout.tv_sec = 60; /* 1 minute */ timeout.tv_usec = 0; // get file descriptors from the transfers curl_multi_fdset( mMulti, &fdread, &fdwrite, &fdexcep, &maxfd ); int rc = select( maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout ); switch( rc ) { case -1: throw StreamExc(); break; case 0: break; default: // timeout or readable/writable sockets // note we *could* be more efficient and not wait for // CURLM_CALL_MULTI_PERFORM to clear here and check it on re-entry // but that gets messy while( curl_multi_perform( mMulti, &still_running ) == CURLM_CALL_MULTI_PERFORM ); break; } } while( still_running && ( bufferRemaining() < wantBytes ) ); }
int main(void) { CURL *curl; CURLM *mcurl; int still_running = 1; struct timeval mp_start; char mp_timedout = 0; struct WriteThis pooh; struct curl_slist* rcpt_list = NULL; pooh.counter = 0; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if(!curl) return 1; mcurl = curl_multi_init(); if(!mcurl) return 2; rcpt_list = curl_slist_append(rcpt_list, RECEPIENT); /* more addresses can be added here rcpt_list = curl_slist_append(rcpt_list, "*****@*****.**"); */ curl_easy_setopt(curl, CURLOPT_URL, "smtp://" SMTPSERVER SMTPPORT); curl_easy_setopt(curl, CURLOPT_USERNAME, USERNAME); curl_easy_setopt(curl, CURLOPT_PASSWORD, PASSWORD); curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback); curl_easy_setopt(curl, CURLOPT_MAIL_FROM, MAILFROM); curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, rcpt_list); curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER,0); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); curl_easy_setopt(curl, CURLOPT_READDATA, &pooh); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); curl_easy_setopt(curl, CURLOPT_SSLVERSION, 0); curl_easy_setopt(curl, CURLOPT_SSL_SESSIONID_CACHE, 0); curl_multi_add_handle(mcurl, curl); mp_timedout = 0; mp_start = tvnow(); /* we start some action by calling perform right away */ curl_multi_perform(mcurl, &still_running); while(still_running) { struct timeval timeout; int rc; /* select() return code */ fd_set fdread; fd_set fdwrite; fd_set fdexcep; int maxfd = -1; long curl_timeo = -1; FD_ZERO(&fdread); FD_ZERO(&fdwrite); FD_ZERO(&fdexcep); /* set a suitable timeout to play around with */ timeout.tv_sec = 1; timeout.tv_usec = 0; curl_multi_timeout(mcurl, &curl_timeo); if(curl_timeo >= 0) { timeout.tv_sec = curl_timeo / 1000; if(timeout.tv_sec > 1) timeout.tv_sec = 1; else timeout.tv_usec = (curl_timeo % 1000) * 1000; } /* get file descriptors from the transfers */ curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd); /* In a real-world program you OF COURSE check the return code of the function calls. On success, the value of maxfd is guaranteed to be greater or equal than -1. We call select(maxfd + 1, ...), specially in case of (maxfd == -1), we call select(0, ...), which is basically equal to sleep. */ rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); if (tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) { fprintf(stderr, "ABORTING TEST, since it seems " "that it would have run forever.\n"); break; } switch(rc) { case -1: /* select error */ break; case 0: /* timeout */ default: /* action */ curl_multi_perform(mcurl, &still_running); break; } } curl_slist_free_all(rcpt_list); curl_multi_remove_handle(mcurl, curl); curl_multi_cleanup(mcurl); curl_easy_cleanup(curl); curl_global_cleanup(); return 0; }
/* * Simply download two HTTP files! */ int main(int argc, char **argv) { CURL *http_handle; CURL *http_handle2; CURLM *multi_handle; int still_running; /* keep number of running handles */ http_handle = curl_easy_init(); http_handle2 = curl_easy_init(); /* set options */ curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.haxx.se/"); /* set options */ curl_easy_setopt(http_handle2, CURLOPT_URL, "http://localhost/"); /* init a multi stack */ multi_handle = curl_multi_init(); /* add the individual transfers */ curl_multi_add_handle(multi_handle, http_handle); curl_multi_add_handle(multi_handle, http_handle2); /* we start some action by calling perform right away */ while(CURLM_CALL_MULTI_PERFORM == curl_multi_perform(multi_handle, &still_running)); while(still_running) { struct timeval timeout; int rc; /* select() return code */ fd_set fdread; fd_set fdwrite; fd_set fdexcep; int maxfd = -1; long curl_timeo = -1; FD_ZERO(&fdread); FD_ZERO(&fdwrite); FD_ZERO(&fdexcep); /* set a suitable timeout to play around with */ timeout.tv_sec = 1; timeout.tv_usec = 0; curl_multi_timeout(multi_handle, &curl_timeo); if(curl_timeo >= 0) { timeout.tv_sec = curl_timeo / 1000; if(timeout.tv_sec > 1) timeout.tv_sec = 1; else timeout.tv_usec = (curl_timeo % 1000) * 1000; } /* get file descriptors from the transfers */ curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); /* In a real-world program you OF COURSE check the return code of the function calls. On success, the value of maxfd is guaranteed to be greater or equal than -1. We call select(maxfd + 1, ...), specially in case of (maxfd == -1), we call select(0, ...), which is basically equal to sleep. */ rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); switch(rc) { case -1: /* select error */ break; case 0: default: /* timeout or readable/writable sockets */ while(CURLM_CALL_MULTI_PERFORM == curl_multi_perform(multi_handle, &still_running)); break; } } curl_multi_cleanup(multi_handle); curl_easy_cleanup(http_handle); curl_easy_cleanup(http_handle2); return 0; }
static int testExternalPut () { struct MHD_Daemon *d; CURL *c; char buf[2048]; struct CBC cbc; CURLM *multi; CURLMcode mret; fd_set rs; fd_set ws; fd_set es; MHD_socket max; int running; struct CURLMsg *msg; time_t start; struct timeval tv; unsigned int pos = 0; int done_flag = 0; multi = NULL; cbc.buf = buf; cbc.size = 2048; cbc.pos = 0; d = MHD_start_daemon (MHD_USE_DEBUG, 1082, NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END); if (d == NULL) return 256; c = curl_easy_init (); curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1:1082/hello_world"); curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer); curl_easy_setopt (c, CURLOPT_READDATA, &pos); curl_easy_setopt (c, CURLOPT_UPLOAD, 1L); curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L); curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); if (oneone) curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); else curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L); // NOTE: use of CONNECTTIMEOUT without also // setting NOSIGNAL results in really weird // crashes on my system! curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); multi = curl_multi_init (); if (multi == NULL) { curl_easy_cleanup (c); MHD_stop_daemon (d); return 512; } mret = curl_multi_add_handle (multi, c); if (mret != CURLM_OK) { curl_multi_cleanup (multi); curl_easy_cleanup (c); MHD_stop_daemon (d); return 1024; } start = time (NULL); while ((time (NULL) - start < 5) && (multi != NULL)) { max = 0; FD_ZERO (&rs); FD_ZERO (&ws); FD_ZERO (&es); curl_multi_perform (multi, &running); mret = curl_multi_fdset (multi, &rs, &ws, &es, &max); if (mret != CURLM_OK) { curl_multi_remove_handle (multi, c); curl_multi_cleanup (multi); curl_easy_cleanup (c); MHD_stop_daemon (d); return 2048; } if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max)) { curl_multi_remove_handle (multi, c); curl_multi_cleanup (multi); curl_easy_cleanup (c); MHD_stop_daemon (d); return 4096; } tv.tv_sec = 0; tv.tv_usec = 1000; select (max + 1, &rs, &ws, &es, &tv); curl_multi_perform (multi, &running); if (running == 0) { msg = curl_multi_info_read (multi, &running); if (msg == NULL) break; if (msg->msg == CURLMSG_DONE) { if (msg->data.result != CURLE_OK) printf ("%s failed at %s:%d: `%s'\n", "curl_multi_perform", __FILE__, __LINE__, curl_easy_strerror (msg->data.result)); curl_multi_remove_handle (multi, c); curl_multi_cleanup (multi); curl_easy_cleanup (c); c = NULL; multi = NULL; } } MHD_run (d); } if (multi != NULL) { curl_multi_remove_handle (multi, c); curl_easy_cleanup (c); curl_multi_cleanup (multi); } MHD_stop_daemon (d); if (cbc.pos != strlen ("/hello_world")) return 8192; if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) return 16384; return 0; }
void HttpMgr::run() { if (m_httpCmdList.empty()) { return; } int runningHandles = 0; CURLMcode code = curl_multi_perform( m_currentCurl, &runningHandles); if( CURLM_OK != code ) { LOG_ERROR << "curl_multi_perform failed!"; return; } // 用于缓存当前已执行完成的http请求 HttpCmdList doneHttpCmds; int MsgNumLeft = 0; CURLMsg* pMsg = NULL; // 获取已返回结果的http请求 while((pMsg = curl_multi_info_read( m_currentCurl, &MsgNumLeft)) != NULL) { if( pMsg->msg != CURLMSG_DONE ) { continue; } HttpCmdList::iterator itr = m_httpCmdList.begin(); HttpCmdList::iterator end = m_httpCmdList.end(); for(; itr != end; ++itr) { if( (*itr)->m_curl != pMsg->easy_handle) { continue; } break; } if( itr == m_httpCmdList.end() ) { LOG_ERROR << "find easy handle failed!"; break; } HttpCmd *cmd = *itr; cmd->m_ok = (CURLE_OK == pMsg->data.result) ? true : false; doneHttpCmds.push_back(cmd); m_httpCmdList.erase(itr); //释放 curl_multi_remove_handle(m_currentCurl, cmd->m_curl); } // 检查超时 HttpCmdList::iterator itr = m_httpCmdList.begin(); HttpCmdList::iterator end = m_httpCmdList.end(); Timestamp now = timetool::getTimeOfDay(); for(; itr != end;) { HttpCmd *cmd = *itr; if(now - cmd->m_lastAddTime > 30 * 1000) { cmd->m_ok = false; doneHttpCmds.push_back(cmd); m_httpCmdList.erase(itr++); //释放 curl_multi_remove_handle(m_currentCurl, cmd->m_curl); } else { itr++; } } // 处理已返回结果的http请求 for(HttpCmdList::iterator itr = doneHttpCmds.begin(); itr != doneHttpCmds.end(); ++itr) { HttpCmd *httpcmd = *itr; httpcmd->onExecuted(); httpcmd->release(); } }
static int testExternalPost () { struct MHD_Daemon *d; CURL *c; char buf[2048]; struct CBC cbc; CURLM *multi; CURLMcode mret; fd_set rs; fd_set ws; fd_set es; int max; int running; time_t start; struct timeval tv; struct curl_httppost *pd; int i; multi = NULL; cbc.buf = buf; cbc.size = 2048; cbc.pos = 0; d = MHD_start_daemon (MHD_NO_FLAG /* | MHD_USE_ERROR_LOG */ , 1082, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_NOTIFY_COMPLETED, &completed_cb, NULL, MHD_OPTION_END); if (d == NULL) return 256; multi = curl_multi_init (); if (multi == NULL) { MHD_stop_daemon (d); return 512; } zzuf_socat_start (); for (i = 0; i < LOOP_COUNT; i++) { fprintf (stderr, "."); c = curl_easy_init (); curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1082/hello_world"); curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); pd = make_form (); curl_easy_setopt (c, CURLOPT_HTTPPOST, pd); curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); if (oneone) curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); else curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L); // NOTE: use of CONNECTTIMEOUT without also // setting NOSIGNAL results in really weird // crashes on my system! curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); mret = curl_multi_add_handle (multi, c); if (mret != CURLM_OK) { curl_multi_cleanup (multi); curl_formfree (pd); curl_easy_cleanup (c); zzuf_socat_stop (); MHD_stop_daemon (d); return 1024; } start = time (NULL); while ((time (NULL) - start < 5) && (c != NULL)) { max = 0; FD_ZERO (&rs); FD_ZERO (&ws); FD_ZERO (&es); curl_multi_perform (multi, &running); mret = curl_multi_fdset (multi, &rs, &ws, &es, &max); if (mret != CURLM_OK) { curl_multi_remove_handle (multi, c); curl_multi_cleanup (multi); curl_easy_cleanup (c); zzuf_socat_stop (); MHD_stop_daemon (d); curl_formfree (pd); return 2048; } if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max)) { curl_multi_remove_handle (multi, c); curl_multi_cleanup (multi); curl_easy_cleanup (c); curl_formfree (pd); zzuf_socat_stop (); MHD_stop_daemon (d); return 4096; } tv.tv_sec = 0; tv.tv_usec = 1000; select (max + 1, &rs, &ws, &es, &tv); curl_multi_perform (multi, &running); if (running == 0) { curl_multi_info_read (multi, &running); curl_multi_remove_handle (multi, c); curl_easy_cleanup (c); c = NULL; } MHD_run (d); } if (c != NULL) { curl_multi_remove_handle (multi, c); curl_easy_cleanup (c); } curl_formfree (pd); } fprintf (stderr, "\n"); zzuf_socat_stop (); MHD_stop_daemon (d); return 0; }
int run() { if (NULL == m_handler) { LOGE("m_handler is null."); return -1; } int msgs_in_queue = -1; int running = -1; int max_fds = -1; fd_set read_fd; fd_set write_fd; fd_set exec_fd; CURLMsg *msg; if (add_multiple_handlers() != 0) return -2; while (running) { curl_multi_perform(m_handler, &running); if (running) { FD_ZERO(&read_fd); FD_ZERO(&write_fd); FD_ZERO(&exec_fd); if (curl_multi_fdset(m_handler, &read_fd, &write_fd, &exec_fd, &max_fds)) { LOGE("curl_multi_fdset"); return -3; } if (set_timeout()) return -4; if (max_fds == -1) { sleep(MULTI_SELECT_TIMEOUT / 1000); } else { #if 0 if (0 > select(max_fds + 1, &read_fd, &write_fd, &exec_fd, &m_timeout)) { LOGE("select(%i,,,,%d)", max_fds + 1, (int)m_timeout.tv_sec); return -5; } #endif if (0 > select(max_fds + 1, &read_fd, &write_fd, &exec_fd, NULL)) { LOGE("select(%i)", max_fds + 1); return -5; } } } while ((msg = curl_multi_info_read(m_handler, &msgs_in_queue))) { if (CURLMSG_DONE == msg->msg) { Conn* conn; CURL* eh = msg->easy_handle; curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &conn); fclose(conn->output); conn->output = NULL; m_done_callback(conn); LOGI("%d - %s", msg->data.result, curl_easy_strerror(msg->data.result)); remove_done_url(conn->url); free(conn); curl_multi_remove_handle(m_handler, eh); curl_easy_cleanup(eh); m_eh_array.erase(eh); m_crawling_urls.erase(conn->url); } else { LOGE("exec_fd: CURLMsg (%d)", msg->msg); } if (add_only_one_handler() == 0) { running++; break; } } } return 0; }
static int testExternalGet () { struct MHD_Daemon *d; CURL *c; char buf[2048]; struct CBC cbc; CURLM *multi; CURLMcode mret; fd_set rs; fd_set ws; fd_set es; MHD_socket max; int running; struct CURLMsg *msg; time_t start; struct timeval tv; multi = NULL; cbc.buf = buf; cbc.size = 2048; cbc.pos = 0; d = MHD_start_daemon (MHD_USE_DEBUG, 21080, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END); if (d == NULL) return 256; c = curl_easy_init (); curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1:21080/hello_world"); curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); /* note that the string below intentionally uses the various ways cookies can be specified to exercise the parser! Do not change! */ curl_easy_setopt (c, CURLOPT_COOKIE, "name1=var1; name2=var2,name3 ;name4=\"var4 with spaces\";"); if (oneone) curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); else curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L); /* NOTE: use of CONNECTTIMEOUT without also setting NOSIGNAL results in really weird crashes on my system! */ curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); multi = curl_multi_init (); if (multi == NULL) { curl_easy_cleanup (c); MHD_stop_daemon (d); return 512; } mret = curl_multi_add_handle (multi, c); if (mret != CURLM_OK) { curl_multi_cleanup (multi); curl_easy_cleanup (c); MHD_stop_daemon (d); return 1024; } start = time (NULL); while ((time (NULL) - start < 5) && (multi != NULL)) { max = 0; FD_ZERO (&rs); FD_ZERO (&ws); FD_ZERO (&es); curl_multi_perform (multi, &running); mret = curl_multi_fdset (multi, &rs, &ws, &es, &max); if (mret != CURLM_OK) { curl_multi_remove_handle (multi, c); curl_multi_cleanup (multi); curl_easy_cleanup (c); MHD_stop_daemon (d); return 2048; } if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max)) { curl_multi_remove_handle (multi, c); curl_multi_cleanup (multi); curl_easy_cleanup (c); MHD_stop_daemon (d); return 4096; } tv.tv_sec = 0; tv.tv_usec = 1000; select (max + 1, &rs, &ws, &es, &tv); curl_multi_perform (multi, &running); if (running == 0) { msg = curl_multi_info_read (multi, &running); if (msg == NULL) break; if (msg->msg == CURLMSG_DONE) { if (msg->data.result != CURLE_OK) printf ("%s failed at %s:%d: `%s'\n", "curl_multi_perform", __FILE__, __LINE__, curl_easy_strerror (msg->data.result)); curl_multi_remove_handle (multi, c); curl_multi_cleanup (multi); curl_easy_cleanup (c); c = NULL; multi = NULL; } } MHD_run (d); } if (multi != NULL) { curl_multi_remove_handle (multi, c); curl_easy_cleanup (c); curl_multi_cleanup (multi); } MHD_stop_daemon (d); if (cbc.pos != strlen ("/hello_world")) return 8192; if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) return 16384; return 0; }
static void tr_webThreadFunc( void * vsession ) { CURLM * multi; struct tr_web * web; int taskCount = 0; struct tr_web_task * task; tr_session * session = vsession; /* try to enable ssl for https support; but if that fails, * try a plain vanilla init */ if( curl_global_init( CURL_GLOBAL_SSL ) ) curl_global_init( 0 ); web = tr_new0( struct tr_web, 1 ); web->close_mode = ~0; web->taskLock = tr_lockNew( ); web->tasks = NULL; web->curl_verbose = getenv( "TR_CURL_VERBOSE" ) != NULL; web->cookie_filename = tr_buildPath( session->configDir, "cookies.txt", NULL ); multi = curl_multi_init( ); session->web = web; for( ;; ) { long msec; int unused; CURLMsg * msg; CURLMcode mcode; if( web->close_mode == TR_WEB_CLOSE_NOW ) break; if( ( web->close_mode == TR_WEB_CLOSE_WHEN_IDLE ) && ( web->tasks == NULL ) ) break; /* add tasks from the queue */ tr_lockLock( web->taskLock ); while( web->tasks != NULL ) { /* pop the task */ task = web->tasks; web->tasks = task->next; task->next = NULL; dbgmsg( "adding task to curl: [%s]", task->url ); curl_multi_add_handle( multi, createEasy( session, web, task )); /*fprintf( stderr, "adding a task.. taskCount is now %d\n", taskCount );*/ ++taskCount; } tr_lockUnlock( web->taskLock ); /* maybe wait a little while before calling curl_multi_perform() */ msec = 0; curl_multi_timeout( multi, &msec ); if( msec < 0 ) msec = THREADFUNC_MAX_SLEEP_MSEC; if( session->isClosed ) msec = 100; /* on shutdown, call perform() more frequently */ if( msec > 0 ) { int usec; int max_fd; struct timeval t; fd_set r_fd_set, w_fd_set, c_fd_set; max_fd = 0; FD_ZERO( &r_fd_set ); FD_ZERO( &w_fd_set ); FD_ZERO( &c_fd_set ); curl_multi_fdset( multi, &r_fd_set, &w_fd_set, &c_fd_set, &max_fd ); if( msec > THREADFUNC_MAX_SLEEP_MSEC ) msec = THREADFUNC_MAX_SLEEP_MSEC; usec = msec * 1000; t.tv_sec = usec / 1000000; t.tv_usec = usec % 1000000; tr_select( max_fd+1, &r_fd_set, &w_fd_set, &c_fd_set, &t ); } /* call curl_multi_perform() */ do { mcode = curl_multi_perform( multi, &unused ); } while( mcode == CURLM_CALL_MULTI_PERFORM ); /* pump completed tasks from the multi */ while(( msg = curl_multi_info_read( multi, &unused ))) { if(( msg->msg == CURLMSG_DONE ) && ( msg->easy_handle != NULL )) { double total_time; struct tr_web_task * task; long req_bytes_sent; CURL * e = msg->easy_handle; curl_easy_getinfo( e, CURLINFO_PRIVATE, (void*)&task ); curl_easy_getinfo( e, CURLINFO_RESPONSE_CODE, &task->code ); curl_easy_getinfo( e, CURLINFO_REQUEST_SIZE, &req_bytes_sent ); curl_easy_getinfo( e, CURLINFO_TOTAL_TIME, &total_time ); task->did_connect = task->code>0 || req_bytes_sent>0; task->did_timeout = !task->code && ( total_time >= task->timeout_secs ); curl_multi_remove_handle( multi, e ); curl_easy_cleanup( e ); /*fprintf( stderr, "removing a completed task.. taskCount is now %d (response code: %d, response len: %d)\n", taskCount, (int)task->code, (int)evbuffer_get_length(task->response) );*/ tr_runInEventThread( task->session, task_finish_func, task ); --taskCount; } } } /* Discard any remaining tasks. * This is rare, but can happen on shutdown with unresponsive trackers. */ while( web->tasks != NULL ) { task = web->tasks; web->tasks = task->next; dbgmsg( "Discarding task \"%s\"", task->url ); task_free( task ); } /* cleanup */ curl_multi_cleanup( multi ); tr_lockFree( web->taskLock ); tr_free( web->cookie_filename ); tr_free( web ); session->web = NULL; }
bool rest_multi_all_done(MultiRestState *state) { int still_running; curl_multi_perform(state->multi_handle, &still_running); return still_running == 0; }
std::string ZLCurlNetworkManager::perform(const ZLExecutionData::Vector &dataList) const { const ZLResource &errorResource = ZLResource::resource("dialog")["networkError"]; if (dataList.empty()) { return errorResource["emptyLibrariesList"].value(); } std::set<std::string> errors; const std::string proxy = proxyHost() + ':' + proxyPort(); CURLM *handle = curl_multi_init(); std::map<CURL*,shared_ptr<ZLExecutionData> > handleToRequest; for (ZLExecutionData::Vector::const_iterator it = dataList.begin(); it != dataList.end(); ++it) { if (it->isNull() || !(*it)->isInstanceOf(ZLNetworkRequest::TYPE_ID)) { continue; } ZLNetworkRequest &request = (ZLNetworkRequest&)**it; const std::string err = doBeforeRequest(request); if (!err.empty()) { errors.insert(err); continue; } CURL *easyHandle = curl_easy_init(); if (easyHandle != 0) { handleToRequest[easyHandle] = *it; setStandardOptions(easyHandle, proxy); setRequestOptions(easyHandle, request); curl_multi_add_handle(handle, easyHandle); } } int counter; CURLMcode res; do { res = curl_multi_perform(handle, &counter); } while ((res == CURLM_CALL_MULTI_PERFORM) || (counter > 0)); CURLMsg *message; do { int queueSize; message = curl_multi_info_read(handle, &queueSize); if ((message != 0) && (message->msg == CURLMSG_DONE)) { ZLNetworkRequest &request = (ZLNetworkRequest&)*handleToRequest[message->easy_handle]; const std::string &url = request.url(); CURLcode result = message->data.result; bool doAfterResult = request.doAfter(result == CURLE_OK); if (result == CURLE_OK && !doAfterResult) { result = CURLE_WRITE_ERROR; } switch (result) { case CURLE_OK: break; case CURLE_WRITE_ERROR: if (!request.errorMessage().empty()) { errors.insert(request.errorMessage()); } else { errors.insert(ZLStringUtil::printf(errorResource["somethingWrongMessage"].value(), ZLNetworkUtil::hostFromUrl(url))); } break; default: errors.insert(ZLStringUtil::printf(errorResource["somethingWrongMessage"].value(), ZLNetworkUtil::hostFromUrl(url))); break; case CURLE_COULDNT_RESOLVE_PROXY: errors.insert(ZLStringUtil::printf(errorResource["couldntResolveProxyMessage"].value(), proxyHost())); break; case CURLE_COULDNT_RESOLVE_HOST: errors.insert(ZLStringUtil::printf(errorResource["couldntResolveHostMessage"].value(), ZLNetworkUtil::hostFromUrl(url))); break; case CURLE_COULDNT_CONNECT: errors.insert(ZLStringUtil::printf(errorResource["couldntConnectMessage"].value(), ZLNetworkUtil::hostFromUrl(url))); break; case CURLE_OPERATION_TIMEDOUT: errors.insert(errorResource["operationTimedOutMessage"].value()); break; case CURLE_SSL_CONNECT_ERROR: errors.insert(ZLStringUtil::printf(errorResource["sslConnectErrorMessage"].value(), curl_easy_strerror(CURLE_SSL_CONNECT_ERROR))); break; #if LIBCURL_VERSION_NUM > 0x071100 case CURLE_PEER_FAILED_VERIFICATION: #else case CURLE_SSL_PEER_CERTIFICATE: #endif errors.insert(ZLStringUtil::printf(errorResource["peerFailedVerificationMessage"].value(), ZLNetworkUtil::hostFromUrl(url))); break; case CURLE_SSL_CACERT: errors.insert(ZLStringUtil::printf(errorResource["sslCertificateAuthorityMessage"].value(), ZLNetworkUtil::hostFromUrl(url))); break; case CURLE_SSL_CACERT_BADFILE: errors.insert(ZLStringUtil::printf(errorResource["sslBadCertificateFileMessage"].value(), request.sslCertificate().Path)); break; case CURLE_SSL_SHUTDOWN_FAILED: errors.insert(ZLStringUtil::printf(errorResource["sslShutdownFailedMessage"].value(), ZLNetworkUtil::hostFromUrl(url))); break; } } } while ((message != 0) && (errors.size() < 3)); for (std::map<CURL*,shared_ptr<ZLExecutionData> >::const_iterator jt = handleToRequest.begin(); jt != handleToRequest.end(); ++jt) { CURL *easyHandle = jt->first; curl_multi_remove_handle(handle, easyHandle); curl_easy_cleanup(easyHandle); ZLNetworkRequest &request = (ZLNetworkRequest&)*jt->second; clearRequestOptions(request); } handleToRequest.clear(); curl_multi_cleanup(handle); std::string result; for (std::set<std::string>::const_iterator et = errors.begin(); et != errors.end(); ++et) { if (!result.empty()) { result += '\n'; } result += *et; } return result; }
static void tr_webThreadFunc( void * vsession ) { int unused; CURLM * multi; struct tr_web * web; int taskCount = 0; tr_session * session = vsession; /* try to enable ssl for https support; but if that fails, * try a plain vanilla init */ if( curl_global_init( CURL_GLOBAL_SSL ) ) curl_global_init( 0 ); web = tr_new0( struct tr_web, 1 ); web->close_mode = ~0; web->taskLock = tr_lockNew( ); web->tasks = NULL; multi = curl_multi_init( ); session->web = web; for( ;; ) { long msec; CURLMsg * msg; CURLMcode mcode; struct tr_web_task * task; if( web->close_mode == TR_WEB_CLOSE_NOW ) break; if( ( web->close_mode == TR_WEB_CLOSE_WHEN_IDLE ) && !taskCount ) break; /* add tasks from the queue */ tr_lockLock( web->taskLock ); while(( task = tr_list_pop_front( &web->tasks ))) { dbgmsg( "adding task to curl: [%s]", task->url ); curl_multi_add_handle( multi, createEasy( session, task )); /*fprintf( stderr, "adding a task.. taskCount is now %d\n", taskCount );*/ ++taskCount; } tr_lockUnlock( web->taskLock ); /* maybe wait a little while before calling curl_multi_perform() */ msec = 0; curl_multi_timeout( multi, &msec ); if( msec < 0 ) msec = THREADFUNC_MAX_SLEEP_MSEC; if( msec > 0 ) { int usec; int max_fd; struct timeval t; fd_set r_fd_set, w_fd_set, c_fd_set; max_fd = 0; FD_ZERO( &r_fd_set ); FD_ZERO( &w_fd_set ); FD_ZERO( &c_fd_set ); curl_multi_fdset( multi, &r_fd_set, &w_fd_set, &c_fd_set, &max_fd ); if( msec > THREADFUNC_MAX_SLEEP_MSEC ) msec = THREADFUNC_MAX_SLEEP_MSEC; usec = msec * 1000; t.tv_sec = usec / 1000000; t.tv_usec = usec % 1000000; tr_select( max_fd+1, &r_fd_set, &w_fd_set, &c_fd_set, &t ); } /* call curl_multi_perform() */ do { mcode = curl_multi_perform( multi, &unused ); } while( mcode == CURLM_CALL_MULTI_PERFORM ); /* pump completed tasks from the multi */ while(( msg = curl_multi_info_read( multi, &unused ))) { if(( msg->msg == CURLMSG_DONE ) && ( msg->easy_handle != NULL )) { struct tr_web_task * task; CURL * e = msg->easy_handle; curl_easy_getinfo( e, CURLINFO_PRIVATE, (void*)&task ); curl_easy_getinfo( e, CURLINFO_RESPONSE_CODE, &task->code ); curl_multi_remove_handle( multi, e ); curl_easy_cleanup( e ); /*fprintf( stderr, "removing a completed task.. taskCount is now %d (response code: %d, response len: %d)\n", taskCount, (int)task->code, (int)evbuffer_get_length(task->response) );*/ tr_runInEventThread( task->session, task_finish_func, task ); --taskCount; } } } /* cleanup */ curl_multi_cleanup( multi ); tr_lockFree( web->taskLock ); tr_free( web ); session->web = NULL; }
struct active_request_slot *get_active_slot(void) { struct active_request_slot *slot = active_queue_head; struct active_request_slot *newslot; #ifdef USE_CURL_MULTI int num_transfers; /* Wait for a slot to open up if the queue is full */ while (active_requests >= max_requests) { curl_multi_perform(curlm, &num_transfers); if (num_transfers < active_requests) process_curl_messages(); } #endif while (slot != NULL && slot->in_use) slot = slot->next; if (slot == NULL) { newslot = xmalloc(sizeof(*newslot)); newslot->curl = NULL; newslot->in_use = 0; newslot->next = NULL; slot = active_queue_head; if (slot == NULL) { active_queue_head = newslot; } else { while (slot->next != NULL) slot = slot->next; slot->next = newslot; } slot = newslot; } if (slot->curl == NULL) { #ifdef NO_CURL_EASY_DUPHANDLE slot->curl = get_curl_handle(); #else slot->curl = curl_easy_duphandle(curl_default); #endif } active_requests++; slot->in_use = 1; slot->local = NULL; slot->results = NULL; slot->finished = NULL; slot->callback_data = NULL; slot->callback_func = NULL; curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header); curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr); curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL); curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL); curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL); curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0); curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); return slot; }
/*************************************************************************** * This function is still only for testing purposes. It makes a great way * to run the full test suite on the multi interface instead of the easy one. *************************************************************************** * * The *new* curl_easy_perform() is the external interface that performs a * transfer previously setup. * * Wrapper-function that: creates a multi handle, adds the easy handle to it, * runs curl_multi_perform() until the transfer is done, then detaches the * easy handle, destroys the multi handle and returns the easy handle's return * code. This will make everything internally use and assume multi interface. */ CURLcode curl_easy_perform(CURL *easy) { CURLM *multi; CURLMcode mcode; CURLcode code = CURLE_OK; int still_running; struct timeval timeout; int rc; CURLMsg *msg; fd_set fdread; fd_set fdwrite; fd_set fdexcep; int maxfd; if(!easy) return CURLE_BAD_FUNCTION_ARGUMENT; multi = curl_multi_init(); if(!multi) return CURLE_OUT_OF_MEMORY; mcode = curl_multi_add_handle(multi, easy); if(mcode) { curl_multi_cleanup(multi); if(mcode == CURLM_OUT_OF_MEMORY) return CURLE_OUT_OF_MEMORY; else return CURLE_FAILED_INIT; } /* we start some action by calling perform right away */ do { while(CURLM_CALL_MULTI_PERFORM == curl_multi_perform(multi, &still_running)); if(!still_running) break; FD_ZERO(&fdread); FD_ZERO(&fdwrite); FD_ZERO(&fdexcep); /* timeout once per second */ timeout.tv_sec = 1; timeout.tv_usec = 0; /* Old deprecated style: get file descriptors from the transfers */ curl_multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd); rc = Curl_select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); /* The way is to extract the sockets and wait for them without using select. This whole alternative version should probably rather use the curl_multi_socket() approach. */ if(rc == -1) /* select error */ break; /* timeout or data to send/receive => loop! */ } while(still_running); msg = curl_multi_info_read(multi, &rc); if(msg) code = msg->data.result; mcode = curl_multi_remove_handle(multi, easy); /* what to do if it fails? */ mcode = curl_multi_cleanup(multi); /* what to do if it fails? */ return code; }
int main(int argc, char *argv[]) { CURL *curl; CURLcode res; CURLM *multi_handle; int still_running; struct HttpPost *formpost=NULL; struct HttpPost *lastptr=NULL; struct curl_slist *headerlist=NULL; char buf[] = "Expect:"; /* Fill in the file upload field */ curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "sendfile", CURLFORM_FILE, "postit2.c", CURLFORM_END); /* Fill in the filename field */ curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "filename", CURLFORM_COPYCONTENTS, "postit2.c", CURLFORM_END); /* Fill in the submit field too, even if this is rarely needed */ curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "send", CURLFORM_END); curl = curl_easy_init(); multi_handle = curl_multi_init(); /* initalize custom header list (stating that Expect: 100-continue is not wanted */ headerlist = curl_slist_append(headerlist, buf); if(curl && multi_handle) { int perform=0; /* what URL that receives this POST */ curl_easy_setopt(curl, CURLOPT_URL, "http://www.fillinyoururl.com/upload.cgi"); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); curl_multi_add_handle(multi_handle, curl); while(CURLM_CALL_MULTI_PERFORM == curl_multi_perform(multi_handle, &still_running)); while(still_running) { struct timeval timeout; int rc; /* select() return code */ fd_set fdread; fd_set fdwrite; fd_set fdexcep; int maxfd; FD_ZERO(&fdread); FD_ZERO(&fdwrite); FD_ZERO(&fdexcep); /* set a suitable timeout to play around with */ timeout.tv_sec = 1; timeout.tv_usec = 0; /* get file descriptors from the transfers */ curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); switch(rc) { case -1: /* select error */ break; case 0: printf("timeout!\n"); default: /* timeout or readable/writable sockets */ printf("perform!\n"); while(CURLM_CALL_MULTI_PERFORM == curl_multi_perform(multi_handle, &still_running)); printf("running: %d!\n", still_running); break; } } curl_multi_cleanup(multi_handle); /* always cleanup */ curl_easy_cleanup(curl); /* then cleanup the formpost chain */ curl_formfree(formpost); /* free slist */ curl_slist_free_all (headerlist); } return 0; }
static int perform_curl_io_and_complete(int *left) { CURLMsg *msg; long ret; CURLMcode curlm_ret; CURLcode curl_ret; curlm_ret = curl_multi_perform(mcurl, left); if (curlm_ret != CURLM_OK) { return -1; } while (true) { CURL *handle; struct file *file; msg = curl_multi_info_read(mcurl, left); if (!msg) { break; } if (msg->msg != CURLMSG_DONE) { continue; } handle = msg->easy_handle; ret = 404; curl_ret = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &ret); if (curl_ret != CURLE_OK) { continue; } curl_ret = curl_easy_getinfo(handle, CURLINFO_PRIVATE, (char **)&file); if (curl_ret != CURLE_OK) { curl_easy_cleanup(handle); continue; } /* The easy handle may have an error set, even if the server returns * HTTP 200, so retry the download for this case. */ if (ret == 200 && msg->data.result != CURLE_OK) { printf("Error for %s download: %s\n", file->hash, curl_easy_strerror(msg->data.result)); failed = list_prepend_data(failed, file); } else if (ret == 200) { /* When both web server and CURL report success, only then * proceed to uncompress. */ if (untar_full_download(file)) { printf("Error for %s tarfile extraction, (check free space for %s?)\n", file->hash, state_dir); failed = list_prepend_data(failed, file); } } else if (ret == 0) { /* When using the FILE:// protocol, 0 indicates success. * Otherwise, it means the web server hasn't responded yet. */ if (local_download) { if (untar_full_download(file)) { printf("Error for %s tarfile extraction, (check free space for %s?)\n", file->hash, state_dir); failed = list_prepend_data(failed, file); } } else { printf("Error for %s download: No response received\n", file->hash); failed = list_prepend_data(failed, file); } } else { printf("Error for %s download: Received %ld response\n", file->hash, ret); failed = list_prepend_data(failed, file); unlink_all_staged_content(file); } if (file->staging) { free(file->staging); file->staging = NULL; } /* NOTE: Intentionally no removal of file from hashmap. All * needed files need determined and queued in one complete * preparation phase. Once all needed files are all present, * they can be staged. Otherwise a complex datastructure and * retries are needed to insure only one download of a file * happens fully to success AND a HASH.tar is uncompressed to * and HASH and staged to the _multiple_ filenames with that * hash. */ curl_multi_remove_handle(mcurl, handle); curl_easy_cleanup(handle); file->curl = NULL; } curlm_ret = curl_multi_perform(mcurl, left); if (curlm_ret != CURLM_OK) { return -1; } return 0; }
static CURLcode easy_transfer(struct Curl_multi *multi) { bool done = FALSE; CURLMcode mcode = CURLM_OK; CURLcode result = CURLE_OK; struct timeval before; int without_fds = 0; /* count number of consecutive returns from curl_multi_wait() without any filedescriptors */ while(!done && !mcode) { int still_running = 0; int rc; before = curlx_tvnow(); mcode = curl_multi_wait(multi, NULL, 0, 1000, &rc); if(!mcode) { if(!rc) { struct timeval after = curlx_tvnow(); /* If it returns without any filedescriptor instantly, we need to avoid busy-looping during periods where it has nothing particular to wait for */ if(curlx_tvdiff(after, before) <= 10) { without_fds++; if(without_fds > 2) { int sleep_ms = without_fds < 10 ? (1 << (without_fds - 1)) : 1000; Curl_wait_ms(sleep_ms); } } else /* it wasn't "instant", restart counter */ without_fds = 0; } else /* got file descriptor, restart counter */ without_fds = 0; mcode = curl_multi_perform(multi, &still_running); } /* only read 'still_running' if curl_multi_perform() return OK */ if(!mcode && !still_running) { CURLMsg *msg = curl_multi_info_read(multi, &rc); if(msg) { result = msg->data.result; done = TRUE; } } } /* Make sure to return some kind of error if there was a multi problem */ if(mcode) { result = (mcode == CURLM_OUT_OF_MEMORY) ? CURLE_OUT_OF_MEMORY : /* The other multi errors should never happen, so return something suitably generic */ CURLE_BAD_FUNCTION_ARGUMENT; } return result; }
/* * Simply download a HTTP file. */ int main(int argc, char **argv) { CURL *http_handle; CURLM *multi_handle; int still_running; /* keep number of running handles */ http_handle = curl_easy_init(); /* set the options (I left out a few, you'll get the point anyway) */ curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.haxx.se/"); curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace); curl_easy_setopt(http_handle, CURLOPT_VERBOSE, TRUE); /* init a multi stack */ multi_handle = curl_multi_init(); /* add the individual transfers */ curl_multi_add_handle(multi_handle, http_handle); /* we start some action by calling perform right away */ while(CURLM_CALL_MULTI_PERFORM == curl_multi_perform(multi_handle, &still_running)); while(still_running) { struct timeval timeout; int rc; /* select() return code */ fd_set fdread; fd_set fdwrite; fd_set fdexcep; int maxfd; FD_ZERO(&fdread); FD_ZERO(&fdwrite); FD_ZERO(&fdexcep); /* set a suitable timeout to play around with */ timeout.tv_sec = 1; timeout.tv_usec = 0; /* get file descriptors from the transfers */ curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); /* In a real-world program you OF COURSE check the return code of the function calls, *and* you make sure that maxfd is bigger than -1 so that the call to select() below makes sense! */ rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); switch(rc) { case -1: /* select error */ still_running = 0; printf("select() returns error, this is badness\n"); break; case 0: default: /* timeout or readable/writable sockets */ while(CURLM_CALL_MULTI_PERFORM == curl_multi_perform(multi_handle, &still_running)); break; } } curl_multi_cleanup(multi_handle); curl_easy_cleanup(http_handle); return 0; }
/* * Simply download a HTTP file. */ int main(void) { CURL *http_handle; CURLM *multi_handle; int still_running; /* keep number of running handles */ curl_global_init(CURL_GLOBAL_DEFAULT); http_handle = curl_easy_init(); /* set the options (I left out a few, you'll get the point anyway) */ curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/"); /* init a multi stack */ multi_handle = curl_multi_init(); /* add the individual transfers */ curl_multi_add_handle(multi_handle, http_handle); /* we start some action by calling perform right away */ curl_multi_perform(multi_handle, &still_running); do { struct timeval timeout; int rc; /* select() return code */ fd_set fdread; fd_set fdwrite; fd_set fdexcep; int maxfd = -1; long curl_timeo = -1; FD_ZERO(&fdread); FD_ZERO(&fdwrite); FD_ZERO(&fdexcep); /* set a suitable timeout to play around with */ timeout.tv_sec = 1; timeout.tv_usec = 0; curl_multi_timeout(multi_handle, &curl_timeo); if(curl_timeo >= 0) { timeout.tv_sec = curl_timeo / 1000; if(timeout.tv_sec > 1) timeout.tv_sec = 1; else timeout.tv_usec = (curl_timeo % 1000) * 1000; } /* get file descriptors from the transfers */ curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); /* In a real-world program you OF COURSE check the return code of the function calls. On success, the value of maxfd is guaranteed to be greater or equal than -1. We call select(maxfd + 1, ...), specially in case of (maxfd == -1), we call select(0, ...), which is basically equal to sleep. */ rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); switch(rc) { case -1: /* select error */ still_running = 0; printf("select() returns error, this is badness\n"); break; case 0: default: /* timeout or readable/writable sockets */ curl_multi_perform(multi_handle, &still_running); break; } } while(still_running); curl_multi_remove_handle(multi_handle, http_handle); curl_easy_cleanup(http_handle); curl_multi_cleanup(multi_handle); curl_global_cleanup(); return 0; }