123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/time.h>
- #include <unistd.h>
- #include <curl/curl.h>
- #ifndef CURLPIPE_MULTIPLEX
- #define CURLPIPE_MULTIPLEX 0
- #endif
- #define NUM_HANDLES 1000
- static void *curl_hnd[NUM_HANDLES];
- static int num_transfers;
- static int hnd2num(CURL *hnd)
- {
- int i;
- for(i = 0; i< num_transfers; i++) {
- if(curl_hnd[i] == hnd)
- return i;
- }
- return 0;
- }
- static
- void dump(const char *text, int num, unsigned char *ptr, size_t size,
- char nohex)
- {
- size_t i;
- size_t c;
- unsigned int width = 0x10;
- if(nohex)
-
- width = 0x40;
- fprintf(stderr, "%d %s, %lu bytes (0x%lx)\n",
- num, text, (unsigned long)size, (unsigned long)size);
- for(i = 0; i<size; i += width) {
- fprintf(stderr, "%4.4lx: ", (unsigned long)i);
- if(!nohex) {
-
- for(c = 0; c < width; c++)
- if(i + c < size)
- fprintf(stderr, "%02x ", ptr[i + c]);
- else
- fputs(" ", stderr);
- }
- for(c = 0; (c < width) && (i + c < size); c++) {
-
- if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
- ptr[i + c + 1] == 0x0A) {
- i += (c + 2 - width);
- break;
- }
- fprintf(stderr, "%c",
- (ptr[i + c] >= 0x20) && (ptr[i + c]<0x80)?ptr[i + c]:'.');
-
- if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
- ptr[i + c + 2] == 0x0A) {
- i += (c + 3 - width);
- break;
- }
- }
- fputc('\n', stderr);
- }
- }
- static
- int my_trace(CURL *handle, curl_infotype type,
- char *data, size_t size,
- void *userp)
- {
- char timebuf[60];
- const char *text;
- int num = hnd2num(handle);
- static time_t epoch_offset;
- static int known_offset;
- struct timeval tv;
- time_t secs;
- struct tm *now;
- (void)handle;
- (void)userp;
- gettimeofday(&tv, NULL);
- if(!known_offset) {
- epoch_offset = time(NULL) - tv.tv_sec;
- known_offset = 1;
- }
- secs = epoch_offset + tv.tv_sec;
- now = localtime(&secs);
- snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld",
- now->tm_hour, now->tm_min, now->tm_sec, (long)tv.tv_usec);
- switch(type) {
- case CURLINFO_TEXT:
- fprintf(stderr, "%s [%d] Info: %s", timebuf, num, data);
-
- default:
- return 0;
- case CURLINFO_HEADER_OUT:
- text = "=> Send header";
- break;
- case CURLINFO_DATA_OUT:
- text = "=> Send data";
- break;
- case CURLINFO_SSL_DATA_OUT:
- text = "=> Send SSL data";
- break;
- case CURLINFO_HEADER_IN:
- text = "<= Recv header";
- break;
- case CURLINFO_DATA_IN:
- text = "<= Recv data";
- break;
- case CURLINFO_SSL_DATA_IN:
- text = "<= Recv SSL data";
- break;
- }
- dump(text, num, (unsigned char *)data, size, 1);
- return 0;
- }
- struct input {
- FILE *in;
- size_t bytes_read;
- CURL *hnd;
- };
- static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
- {
- struct input *i = userp;
- size_t retcode = fread(ptr, size, nmemb, i->in);
- i->bytes_read += retcode;
- return retcode;
- }
- static struct input indata[NUM_HANDLES];
- static void setup(CURL *hnd, int num, const char *upload)
- {
- FILE *out;
- char url[256];
- char filename[128];
- struct stat file_info;
- curl_off_t uploadsize;
- snprintf(filename, 128, "dl-%d", num);
- out = fopen(filename, "wb");
- snprintf(url, 256, "https://localhost:8443/upload-%d", num);
-
- stat(upload, &file_info);
- uploadsize = file_info.st_size;
- indata[num].in = fopen(upload, "rb");
- indata[num].hnd = hnd;
-
- curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out);
-
- curl_easy_setopt(hnd, CURLOPT_READFUNCTION, read_callback);
-
- curl_easy_setopt(hnd, CURLOPT_READDATA, &indata[num]);
-
- curl_easy_setopt(hnd, CURLOPT_INFILESIZE_LARGE, uploadsize);
-
- curl_easy_setopt(hnd, CURLOPT_URL, url);
-
- curl_easy_setopt(hnd, CURLOPT_UPLOAD, 1L);
-
- curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
- curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
-
- curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
-
- curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
- curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
- #if (CURLPIPE_MULTIPLEX > 0)
-
- curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
- #endif
- curl_hnd[num] = hnd;
- }
- int main(int argc, char **argv)
- {
- CURL *easy[NUM_HANDLES];
- CURLM *multi_handle;
- int i;
- int still_running;
- const char *filename = "index.html";
- if(argc > 1)
-
- num_transfers = atoi(argv[1]);
- if(argc > 2)
-
- filename = argv[2];
- if(!num_transfers || (num_transfers > NUM_HANDLES))
- num_transfers = 3;
-
- multi_handle = curl_multi_init();
- for(i = 0; i<num_transfers; i++) {
- easy[i] = curl_easy_init();
-
- setup(easy[i], i, filename);
-
- curl_multi_add_handle(multi_handle, easy[i]);
- }
- curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
-
- curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, 1L);
-
- curl_multi_perform(multi_handle, &still_running);
- do {
- struct timeval timeout;
- int rc;
- CURLMcode mc;
- 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);
-
- 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;
- }
-
- 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;
- }
-
- if(maxfd == -1) {
- #ifdef _WIN32
- Sleep(100);
- rc = 0;
- #else
-
- struct timeval wait = { 0, 100 * 1000 };
- rc = select(0, NULL, NULL, NULL, &wait);
- #endif
- }
- else {
-
- rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
- }
- switch(rc) {
- case -1:
-
- break;
- case 0:
- default:
-
- 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;
- }
|