コード例 #1
0
CURL* debug_curl_easy_duphandle(CURL* handle)
{
  CURL* ret;
  ret = curl_easy_duphandle(handle);
  Dout(dc::curl, "curl_easy_duphandle(" << (AICURL*)handle << ") = " << (AICURL*)ret);
  return ret;
}
コード例 #2
0
  CurlResource(CurlResource *src) : m_exception(NULL), m_phpException(false) {
    ASSERT(src && src != this);
    ASSERT(!src->m_exception);

    m_cp = curl_easy_duphandle(src->get());
    m_url = src->m_url;

    memset(m_error_str, 0, sizeof(m_error_str));
    m_error_no = CURLE_OK;

    m_write.method = src->m_write.method;
    m_write.type   = src->m_write.type;
    m_read.method  = src->m_read.method;
    m_write_header.method = src->m_write_header.method;

    m_write.fp        = src->m_write.fp;
    m_write_header.fp = src->m_write_header.fp;
    m_read.fp         = src->m_read.fp;

    m_write.callback = src->m_write.callback;
    m_read.callback = src->m_read.callback;
    m_write_header.callback = src->m_write_header.callback;

    curl_easy_setopt(m_cp, CURLOPT_ERRORBUFFER,       m_error_str);
    curl_easy_setopt(m_cp, CURLOPT_FILE,              (void*)this);
    curl_easy_setopt(m_cp, CURLOPT_INFILE,            (void*)this);
    curl_easy_setopt(m_cp, CURLOPT_WRITEHEADER,       (void*)this);

    m_to_free = src->m_to_free;
    m_emptyPost = src->m_emptyPost;
  }
コード例 #3
0
ファイル: CurlHandle.cpp プロジェクト: arangodb/fuerte
std::unique_ptr<CurlHandle> CurlHandle::clone() const {
  CURL* cHandle = curl_easy_duphandle(mCurl);
  runtimeAssert("Error when trying to curl_easy_duphandle() a handle",
                cHandle != NULL);
  unique_ptr<CurlHandle> newHandle(new CurlHandle(cHandle));

  return newHandle;
}
コード例 #4
0
static void *safe_curl_copy(void *p)
{
	CURL *ch;
	
	if ((ch = curl_easy_duphandle(p))) {
		init_curl_storage(ch);
		return ch;
	}
	return NULL;
}
コード例 #5
0
ファイル: Quvi.cpp プロジェクト: alexmarsev/quvif
	QuviSimpleStreamBackend(uint64_t length, CURL* curl, CURLSH* curlsh)
		: m_length(length)
		, m_curl(curl_easy_duphandle(curl))
	{
		assert(m_curl); // TODO: throw exception
		assert(curlsh); // TODO: throw exception
		curl_easy_setopt(m_curl, CURLOPT_SHARE, curlsh);

		size_t packets = (size_t)(m_length / CachePacketSize); // full
		if (m_length - packets * CachePacketSize) // eof stub
			packets++;
		m_cache.resize(packets);
		m_worker = std::thread(std::bind(&QuviSimpleStreamBackend::Loop, this));
	}
コード例 #6
0
ファイル: curl.c プロジェクト: arczi84/NetSurf-68k
/**
 * Find a CURL handle to use to dispatch a job
 */
static CURL *fetch_curl_get_handle(lwc_string *host)
{
	struct cache_handle *h;
	CURL *ret;
	RING_FINDBYLWCHOST(curl_handle_ring, h, host);
	if (h) {
		ret = h->handle;
		lwc_string_unref(h->host);
		RING_REMOVE(curl_handle_ring, h);
		free(h);
	} else {
		ret = curl_easy_duphandle(fetch_blank_curl);
	}
	return ret;
}
コード例 #7
0
ファイル: lib512.c プロジェクト: 0w/moai-dev
int test(char *URL)
{
  CURLcode code;
  CURL *curl;
  CURL *curl2;
  int rc = 99;

  code = curl_global_init(CURL_GLOBAL_ALL);
  if(code == CURLE_OK) {

    curl = curl_easy_init();
    if(curl) {

      curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
      curl_easy_setopt(curl, CURLOPT_HEADER, 1L);

      curl2 = curl_easy_duphandle(curl);
      if(curl2) {

        code = curl_easy_setopt(curl2, CURLOPT_URL, URL);
        if(code == CURLE_OK) {

          code = curl_easy_perform(curl2);
          if(code == CURLE_OK)
            rc = 0;
          else
            rc = 1;
        }
        else
          rc = 2;

        curl_easy_cleanup(curl2);
      }
      else
        rc = 3;

      curl_easy_cleanup(curl);
    }
    else
      rc = 4;

    curl_global_cleanup();
  }
  else
    rc = 5;

  return rc;
}
コード例 #8
0
ファイル: wget.c プロジェクト: Xirg/FlickrMS
int wget(const char *in, const char *out) {
    CURL *curl;
    CURLcode res;
    FILE *fp;

    if(!(curl = curl_easy_duphandle(curl_global)))
        return FAIL;

    if(!(fp = fopen(out, "wb")))    // Open in binary
        return FAIL;

    // Set the curl easy options
    curl_easy_setopt(curl, CURLOPT_URL, in);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);

    res = curl_easy_perform(curl);  // Perform the download and write

    curl_easy_cleanup(curl);
    fclose(fp);
    return res;
}
コード例 #9
0
ファイル: _httplibcurl.cpp プロジェクト: Belxjander/Kirito
CURL * HttpLibcurl::HandleCache::getHandle()
{
	CURL * ret(NULL);
	
	if (! mCache.empty())
	{
		// Fastest path to handle
		ret = mCache.back();
		mCache.pop_back();
	}
	else if (mHandleTemplate)
	{
		// Still fast path
		ret = curl_easy_duphandle(mHandleTemplate);
	}
	else
	{
		// When all else fails
		ret = curl_easy_init();
	}

	return ret;
}
コード例 #10
0
ファイル: http2.c プロジェクト: 601040605/WNetLicensor
static CURL *duphandle(struct SessionHandle *data)
{
  struct SessionHandle *second = curl_easy_duphandle(data);
  if(second) {
    /* setup the request struct */
    struct HTTP *http = calloc(1, sizeof(struct HTTP));
    if(!http) {
      (void)Curl_close(second);
      second = NULL;
    }
    else {
      second->req.protop = http;
      http->header_recvbuf = Curl_add_buffer_init();
      if(!http->header_recvbuf) {
        free(http);
        (void)Curl_close(second);
        second = NULL;
      }
      else
        Curl_http2_setup_req(second);
    }
  }
  return second;
}
コード例 #11
0
std::set<std::string> YoutubeCrawler::getVideoFile(std::map<std::string, std::string> &result){
	std::set<std::string> unFinishVids;
	if(result.size() == 0){
		return unFinishVids;
	}
	CURL *handle = curl_easy_init();
	if(handle == NULL){
		return unFinishVids;
	}
	CURLM *mhandle = curl_multi_init();
	if(mhandle == NULL){
		curl_easy_cleanup(handle);
		return unFinishVids;
	}
	CURLcode code;
	curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, writeFileCallback);
	curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1);
	curl_easy_setopt(handle, CURLOPT_MAXREDIRS, 10);
	curl_easy_setopt(handle, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
	curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, 10);
	curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1);
	curl_easy_setopt(handle, CURLOPT_LOW_SPEED_LIMIT, 1);
	curl_easy_setopt(handle, CURLOPT_LOW_SPEED_TIME, 60);
	std::map<std::string, CURL*> handleList;
	std::map<std::string, RecInfo> addrsList;
	BDB::AddrType tmpAddr;
	std::pair<std::map<std::string, RecInfo>::iterator, bool> addrIt;
	RecInfo recInfo;
	recInfo.ybdb = &ybdb;
	for(std::map<std::string, std::string>::iterator it = result.begin(); it != result.end(); it++){
		tmpAddr = ybdb.put("", 0);
		if(tmpAddr == -1){
			continue;
		}
		recInfo.addr = tmpAddr;
		recInfo.size = 0;
		addrIt = addrsList.insert(std::pair<std::string, RecInfo>(it->first, recInfo));
		curl_easy_setopt(handle, CURLOPT_WRITEDATA, &(addrIt.first->second));
		curl_easy_setopt(handle, CURLOPT_URL, it->second.c_str());
		handleList.insert(std::pair<std::string, CURL*>(it->first, handle));
		curl_multi_add_handle(mhandle, handle);
		handle = curl_easy_duphandle(handle);
	}
	curl_easy_cleanup(handle);
	if(handleList.size() == 0){
		curl_multi_cleanup(mhandle);
		return unFinishVids;
	}
	int runningHandles = 0;
	while(curl_multi_perform(mhandle, &runningHandles) == CURLM_CALL_MULTI_PERFORM){}
	struct timeval timeout;
	timeout.tv_sec = 0;
	timeout.tv_usec = 0;
	int max_fd = 0;
	fd_set read_fd_set;
	fd_set write_fd_set;
	fd_set exc_fd_set;
	FD_ZERO(&read_fd_set);
	FD_ZERO(&write_fd_set);
	FD_ZERO(&exc_fd_set);
	while(runningHandles > 0){
		curl_multi_fdset(mhandle, &read_fd_set, &write_fd_set, &exc_fd_set, &max_fd);
		curl_multi_timeout(mhandle, &(timeout.tv_usec));
		if(timeout.tv_usec > 0){
			select(max_fd + 1, &read_fd_set, &write_fd_set, &exc_fd_set, &timeout);
		}
		while(curl_multi_perform(mhandle, &runningHandles) == CURLM_CALL_MULTI_PERFORM){}
	}
	for(std::map<std::string, CURL*>::iterator it = handleList.begin(); it != handleList.end(); it++){
		curl_multi_remove_handle(mhandle, it->second);
		curl_easy_cleanup(it->second);
	}
	curl_multi_cleanup(mhandle);
	std::map<std::string, std::string>::iterator uIt;
	for(std::map<std::string, RecInfo>::iterator it = addrsList.begin(); it != addrsList.end(); it++){
		if(it->second.size == 0){
			unFinishVids.insert(it->first);
			ybdb.del(it->second.addr);
		}else{
			uIt = result.find(it->first);
			logRids << std::setw(11) << it->first 
				<< std::setfill('0') << std::setw(9) << std::hex << it->second.addr 
				<< std::setfill('0') << std::setw(9) << std::hex << it->second.size 
				<< getExtensionName(uIt->second) << std::endl;
			logRids.flush();
		}
	}
	return unFinishVids;
}
コード例 #12
0
ファイル: lib575.c プロジェクト: Andersbakken/curl
int test(char *URL)
{
  CURL *handle = NULL;
  CURL *duphandle = NULL;
  CURLM *mhandle = NULL;
  int res = 0;
  int still_running = 0;

  start_test_timing();

  global_init(CURL_GLOBAL_ALL);

  easy_init(handle);

  easy_setopt(handle, CURLOPT_URL, URL);
  easy_setopt(handle, CURLOPT_WILDCARDMATCH, 1L);
  easy_setopt(handle, CURLOPT_VERBOSE, 1L);

  res = curl_easy_perform(handle);
  if(res)
    goto test_cleanup;

  res = curl_easy_perform(handle);
  if(res)
    goto test_cleanup;

  duphandle = curl_easy_duphandle(handle);
  if(!duphandle)
    goto test_cleanup;
  curl_easy_cleanup(handle);
  handle = duphandle;

  multi_init(mhandle);

  multi_add_handle(mhandle, handle);

  multi_perform(mhandle, &still_running);

  abort_on_test_timeout();

  while(still_running) {
    struct timeval timeout;
    fd_set fdread;
    fd_set fdwrite;
    fd_set fdexcep;
    int maxfd = -99;

    timeout.tv_sec = 0;
    timeout.tv_usec = 100000L; /* 100 ms */

    FD_ZERO(&fdread);
    FD_ZERO(&fdwrite);
    FD_ZERO(&fdexcep);

    multi_fdset(mhandle, &fdread, &fdwrite, &fdexcep, &maxfd);

    /* At this point, maxfd is guaranteed to be greater or equal than -1. */

    select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);

    abort_on_test_timeout();

    multi_perform(mhandle, &still_running);

    abort_on_test_timeout();
  }

test_cleanup:

  /* undocumented cleanup sequence - type UA */

  curl_multi_cleanup(mhandle);
  curl_easy_cleanup(handle);
  curl_global_cleanup();

  return res;
}
コード例 #13
0
ファイル: http.c プロジェクト: DJHartley/git
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;
}
コード例 #14
0
void *htext_get_method(void *h)
{
  htext_handle  *handle = (htext_handle*)h;
  CURL          *curl;
  htext_partial *partial_array, head;
  size_t         unused, fsize, stream_size, last_size;
  char           err_buffer[CURL_ERROR_SIZE];
  void          *f;
  int            i, npartials;

  /* Create the file */
  f = GETIO(handle)->open(GETSTR(handle, HTEXTOP_DESTINATIONURL), "w",
                          GETPTR(handle, HTEXTOP_IO_HANDLER_DATA));
  if (!f) {
    handle->status = HTEXTS_FAILED;
    strerror_r(errno, err_buffer, sizeof(err_buffer));
    htext_error(handle, err_buffer);
    return NULL;
  }
  GETIO(handle)->close(f);

  /* Create and initialize CURL handle with common stuff */
  curl = curl_easy_init();

  curl_easy_setopt(curl, CURLOPT_ERRORBUFFER,      err_buffer);
  curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION,   htext_header_callback);
  curl_easy_setopt(curl, CURLOPT_NOPROGRESS,       1);
  curl_easy_setopt(curl, CURLOPT_URL,              GETSTR(handle, HTEXTOP_SOURCEURL));
  curl_easy_setopt(curl, CURLOPT_USERAGENT,        GETSTR(handle, HTEXTOP_CLIENTID));
  curl_easy_setopt(curl, CURLOPT_CAPATH,           GETSTR(handle, HTEXTOP_CAPATH));
  curl_easy_setopt(curl, CURLOPT_CAINFO,           GETSTR(handle, HTEXTOP_CAFILE));
  curl_easy_setopt(curl, CURLOPT_SSLCERT,          GETSTR(handle, HTEXTOP_USERCERTIFICATE));
  curl_easy_setopt(curl, CURLOPT_SSLKEY,           GETSTR(handle, HTEXTOP_USERPRIVKEY));
  curl_easy_setopt(curl, CURLOPT_SSLKEYPASSWD,     GETSTR(handle, HTEXTOP_USERPRIVKEYPASS));
  curl_easy_setopt(curl, CURLOPT_SSL_CIPHER_LIST,  htext_cipher_suite());
  curl_easy_setopt(curl, CURLOPT_NOSIGNAL,         1);

  if (GETINT(handle, HTEXTOP_VERBOSITY) > 1) {
    curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, htext_debug_callback);
    curl_easy_setopt(curl, CURLOPT_VERBOSE,       1);
  }

  if (GETINT(handle, HTEXTOP_BUFFERSIZE) > 0) {
    curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, GETINT(handle, HTEXTOP_BUFFERSIZE));
  }

  /* Shared */
  curl_easy_setopt(curl, CURLOPT_SHARE, handle->curl_share);

  /* Do a HEAD to get the size */
  htext_partial_init(&head);
  fsize              = 0;
  head.partial_total = &fsize;
  head.partial_done  = &unused;

  curl_easy_setopt(curl, CURLOPT_NOBODY,         1);
  curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  curl_easy_setopt(curl, CURLOPT_HEADERDATA,     &head);

  handle->status = HTEXTS_WAITING;
  htext_log(handle, "Asking for the size");
  if (curl_easy_perform(curl) != CURLE_OK) {
    htext_error(handle, err_buffer);
  }

  if (head.http_status >= 400 || handle->status == HTEXTS_FAILED) {
    handle->http_status = head.http_status;
    handle->status = HTEXTS_FAILED;
    htext_partial_clean(&head);
    curl_easy_cleanup(curl);
    return NULL;
  }

  /* Did we get the location, and is it cacheable? */
  if (head.location && head.redirect_is_cacheable) {
    curl_easy_setopt(curl, CURLOPT_URL, head.location);
  }

  /* Calculate stream size and final stream size */
  if (fsize) {
    npartials = GETINT(handle, HTEXTOP_NUMBEROFSTREAMS);
    if (fsize % npartials == 0) {
      stream_size = last_size = fsize / npartials;
    }
    else {
      stream_size = fsize / npartials;
      last_size = stream_size + (fsize % npartials);
    }
  }
  else {
    /* No size? Too bad, won't be able to stream */
    npartials = 1;
    stream_size = last_size = 0;
    htext_log(handle, "No Content-Length, so no multistream possible");
  }

  /* Set progress function */
  curl_easy_setopt(curl, CURLOPT_HTTPGET,          1);
  curl_easy_setopt(curl, CURLOPT_NOPROGRESS,       0);
  curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, htext_progress_callback);

  /* Set up partials */
  handle->partial_total = calloc(sizeof(size_t), npartials);
  handle->partial_done  = calloc(sizeof(size_t), npartials);
  handle->partials      = npartials;

  /* Spawn a thread per partial */
  htext_log(handle, "Downloading");
  handle->status = HTEXTS_RUNNING;
  partial_array  = calloc(sizeof(htext_partial), npartials);

  for (i = 0; i < npartials; ++i) {
    htext_partial_init(&(partial_array[i]));
    partial_array[i].index  = i;
    partial_array[i].handle = handle;
    partial_array[i].curl   = curl_easy_duphandle(curl);
    partial_array[i].partial_total = &(handle->partial_total[i]);
    partial_array[i].partial_done  = &(handle->partial_done[i]);

    /* duphandle doesn't duplicate this :( */
    curl_easy_setopt(partial_array[i].curl, CURLOPT_SHARE, handle->curl_share);

    /* Open */
    partial_array[i].fd = GETIO(handle)->open(GETSTR(handle, HTEXTOP_DESTINATIONURL),
                                              "w",
                                              GETPTR(handle, HTEXTOP_IO_HANDLER_DATA));
    if (!partial_array[i].fd) {
      handle->status = HTEXTS_FAILED;
      break;
    }

    /* Range and seek */
    partial_array[i].start = i * stream_size;
    if (i < handle->partials - 1)
      partial_array[i].end   = partial_array[i].start + stream_size - 1;
    else
      partial_array[i].end   = partial_array[i].start + last_size - 1;

    *(partial_array[i].partial_total) = partial_array[i].end - partial_array[i].start;

    if (GETIO(handle)->seek(partial_array[i].fd, partial_array[i].start, SEEK_SET) < 0) {
      handle->status = HTEXTS_FAILED;
      break;
    }

    /* Launch */
    pthread_create(&(partial_array[i].thread), NULL, htext_get_subthread, &(partial_array[i]));
  }

  /* Exited on error? */
  if (handle->status == HTEXTS_FAILED) {
    strerror_r(errno, err_buffer, sizeof(err_buffer));
    htext_error(handle, err_buffer);
  }

  /* Wait for all of them */
  for (i = 0; i < npartials; ++i) {
    /* Wait, or kill if failed */
    if (handle->status == HTEXTS_FAILED)
      pthread_cancel(partial_array[i].thread);

    pthread_join(partial_array[i].thread, NULL);

    if (handle->status != HTEXTS_FAILED)
      handle->http_status = partial_array[i].http_status;

    /* Clean partial */
    htext_partial_clean(&(partial_array[i]));

    /* Check code */
    if (handle->http_status < 200 || handle->http_status >= 300) {
      handle->status = HTEXTS_FAILED;
    }
  }

  /* Done */
  if (handle->status == HTEXTS_RUNNING) {
    handle->status      = HTEXTS_SUCCEEDED;
  }

  curl_easy_cleanup(curl);
  htext_partial_clean(&head);
  free(partial_array);
 
  return NULL;
}
コード例 #15
0
ファイル: lib575.c プロジェクト: dims/curl
int test(char *URL)
{
    CURLMcode m;
    CURL *handle = NULL, *duphandle;
    CURLM *mhandle = NULL;
    int res = 0;
    int still_running = 0;

    if(curl_global_init(CURL_GLOBAL_ALL)) {
        fprintf(stderr, "curl_global_init() failed\n");
        goto test_cleanup;
    }

    handle = curl_easy_init();
    if(!handle) {
        res = CURLE_OUT_OF_MEMORY;
        goto test_cleanup;
    }

    test_setopt(handle, CURLOPT_URL, URL);
    test_setopt(handle, CURLOPT_WILDCARDMATCH, 1L);
    test_setopt(handle, CURLOPT_VERBOSE, 1L);

    res = curl_easy_perform(handle);
    if(res)
        goto test_cleanup;

    res = curl_easy_perform(handle);
    if(res)
        goto test_cleanup;

    duphandle = curl_easy_duphandle(handle);
    if(!duphandle)
        goto test_cleanup;
    curl_easy_cleanup(handle);
    handle = duphandle;

    mhandle = curl_multi_init();
    if(!mhandle) {
        fprintf(stderr, "curl_multi_init() failed\n");
        goto test_cleanup;
    }

    curl_multi_add_handle(mhandle, handle);

    curl_multi_perform(mhandle, &still_running);

    while(still_running) {
        static struct timeval timeout = { /* 100 ms */
            0, 100000L
        };
        int rc;
        fd_set fdread;
        fd_set fdwrite;
        fd_set fdexcep;
        int max_fdset = -1;
        FD_ZERO(&fdread);
        FD_ZERO(&fdwrite);
        FD_ZERO(&fdexcep);

        m = curl_multi_fdset(mhandle, &fdread, &fdwrite, &fdexcep, &max_fdset);
        if(m != CURLM_OK) {
            fprintf(stderr, "curl_multi_fdset() error\n");
            goto test_cleanup;
        }
        /* We call select(max_fdset + 1, ...), specially in case of (maxfd == -1),
         * we call select(0, ...), which is basically equal to sleep. */
        rc = select(max_fdset + 1, &fdread, &fdwrite, &fdexcep, &timeout);
        if(rc == -1) {
            fprintf(stderr, "select() error\n");
            goto test_cleanup;
        }
        else {
            curl_multi_perform(mhandle, &still_running);
        }
    }

test_cleanup:
    if(mhandle)
        curl_multi_cleanup(mhandle);
    if(handle)
        curl_easy_cleanup(handle);
    curl_global_cleanup();
    return res;
}
コード例 #16
0
ファイル: http.c プロジェクト: Grdflo/git-core
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
		curl_session_count++;
	}

	active_requests++;
	slot->in_use = 1;
	slot->results = NULL;
	slot->finished = NULL;
	slot->callback_data = NULL;
	slot->callback_func = NULL;
	curl_easy_setopt(slot->curl, CURLOPT_COOKIEFILE, curl_cookie_file);
	if (curl_save_cookies)
		curl_easy_setopt(slot->curl, CURLOPT_COOKIEJAR, curl_cookie_file);
	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_POSTFIELDS, NULL);
	curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
	curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
	curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 1);
#ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
	curl_easy_setopt(slot->curl, CURLOPT_HTTPAUTH, http_auth_methods);
#endif
	if (http_auth.password)
		init_curl_http_auth(slot->curl);

	return slot;
}
コード例 #17
0
ファイル: fastestmirror.c プロジェクト: huttli/librepo
/** Create list of LrFastestMirror based on input list of URLs.
 */
static gboolean
lr_fastestmirror_prepare(LrHandle *handle,
                         GSList *in_list,
                         GSList **out_list,
                         LrFastestMirrorCache *cache,
                         GError **err)
{
    gboolean ret = TRUE;
    GSList *list = NULL;

    assert(!err || *err == NULL);

    if (!in_list) {
        *out_list = NULL;
        return TRUE;
    }

    gint64 maxage = LRO_FASTESTMIRRORMAXAGE_DEFAULT;
    gint64 current_time = g_get_real_time() / 1000000;

    if (handle)
        maxage = (gint64) handle->fastestmirrormaxage;

    for (GSList *elem = in_list; elem; elem = g_slist_next(elem)) {
        gchar *url = elem->data;
        CURLcode curlcode;
        CURL *curlh;

        // TODO: For prefixed by "file://" - set plain_connect_time to zero

        // Try to find item in the cache
        gint64 ts;
        double connecttime;
        if (lr_fastestmirrorcache_lookup(cache, url, &ts, &connecttime)) {
            if (ts >= (current_time - maxage)) {
                // Use cached entry
                g_debug("%s: Using cached connect time for: %s (%f)",
                        __func__, url, connecttime);
                LrFastestMirror *mirror = lr_lrfastestmirror_new();
                mirror->url = url;
                mirror->curl = NULL;
                mirror->plain_connect_time = connecttime;
                mirror->cached = TRUE;
                list = g_slist_append(list, mirror);
                continue;
            } else {
                g_debug("%s: Cached connect time too old: %s", __func__, url);
            }
        } else {
            g_debug("%s: Not found in cache: %s", __func__, url);
        }

        if (handle)
            curlh = curl_easy_duphandle(handle->curl_handle);
        else
            curlh = lr_get_curl_handle();

        if (!curlh) {
            g_set_error(err, LR_FASTESTMIRROR_ERROR, LRE_CURL,
                        "Cannot create curl handle");
            ret = FALSE;
            break;
        }

        LrFastestMirror *mirror = lr_lrfastestmirror_new();
        mirror->url = url;
        mirror->curl = curlh;

        curlcode = curl_easy_setopt(curlh, CURLOPT_URL, url);
        if (curlcode != CURLE_OK) {
            g_set_error(err, LR_FASTESTMIRROR_ERROR, LRE_CURL,
                        "curl_easy_setopt(_, CURLOPT_URL, %s) failed: %s",
                        url, curl_easy_strerror(curlcode));
            ret = FALSE;
            break;
        }

        curlcode = curl_easy_setopt(curlh, CURLOPT_CONNECT_ONLY, 1);
        if (curlcode != CURLE_OK) {
            g_set_error(err, LR_FASTESTMIRROR_ERROR, LRE_CURL,
                    "curl_easy_setopt(_, CURLOPT_CONNECT_ONLY, 1) failed: %s",
                    curl_easy_strerror(curlcode));
            ret = FALSE;
            break;
        }

        list = g_slist_append(list, mirror);
    }

    if (ret) {
        *out_list = list;
    } else {
        assert(!err || *err);
        g_slist_free_full(list, (GDestroyNotify)lr_lrfastestmirror_free);
        *out_list = NULL;
    }

    return ret;
}
コード例 #18
0
ファイル: lib1535.c プロジェクト: Andersbakken/curl
int test(char *URL)
{
  CURL *curl, *dupe = NULL;
  long protocol;
  int res = CURLE_OK;

  global_init(CURL_GLOBAL_ALL);

  easy_init(curl);

  /* Test that protocol is properly initialized on curl_easy_init.
  */

  res = curl_easy_getinfo(curl, CURLINFO_PROTOCOL, &protocol);
  if(res) {
    fprintf(stderr, "%s:%d curl_easy_getinfo() failed with code %d (%s)\n",
            __FILE__, __LINE__, res, curl_easy_strerror(res));
    goto test_cleanup;
  }
  if(protocol != 0) {
    fprintf(stderr, "%s:%d protocol init failed; expected 0 but is %ld\n",
            __FILE__, __LINE__, protocol);
    res = CURLE_FAILED_INIT;
    goto test_cleanup;
  }

  easy_setopt(curl, CURLOPT_URL, URL);

  res = curl_easy_perform(curl);
  if(res) {
    fprintf(stderr, "%s:%d curl_easy_perform() failed with code %d (%s)\n",
            __FILE__, __LINE__, res, curl_easy_strerror(res));
    goto test_cleanup;
  }

  /* Test that a protocol is properly set after receiving an HTTP resource.
  */

  res = curl_easy_getinfo(curl, CURLINFO_PROTOCOL, &protocol);
  if(res) {
    fprintf(stderr, "%s:%d curl_easy_getinfo() failed with code %d (%s)\n",
            __FILE__, __LINE__, res, curl_easy_strerror(res));
    goto test_cleanup;
  }
  if(protocol != CURLPROTO_HTTP) {
    fprintf(stderr, "%s:%d protocol of http resource is incorrect; "
            "expected %ld but is %ld\n",
            __FILE__, __LINE__, CURLPROTO_HTTP, protocol);
    res = CURLE_HTTP_RETURNED_ERROR;
    goto test_cleanup;
  }

  /* Test that a protocol is properly initialized on curl_easy_duphandle.
  */

  dupe = curl_easy_duphandle(curl);
  if(!dupe) {
    fprintf(stderr, "%s:%d curl_easy_duphandle() failed\n",
            __FILE__, __LINE__);
    res = CURLE_FAILED_INIT;
    goto test_cleanup;
  }

  res = curl_easy_getinfo(dupe, CURLINFO_PROTOCOL, &protocol);
  if(res) {
    fprintf(stderr, "%s:%d curl_easy_getinfo() failed with code %d (%s)\n",
            __FILE__, __LINE__, res, curl_easy_strerror(res));
    goto test_cleanup;
  }
  if(protocol != 0) {
    fprintf(stderr, "%s:%d protocol init failed; expected 0 but is %ld\n",
            __FILE__, __LINE__, protocol);
    res = CURLE_FAILED_INIT;
    goto test_cleanup;
  }


  /* Test that a protocol is properly initialized on curl_easy_reset.
  */

  curl_easy_reset(curl);

  res = curl_easy_getinfo(curl, CURLINFO_PROTOCOL, &protocol);
  if(res) {
    fprintf(stderr, "%s:%d curl_easy_getinfo() failed with code %d (%s)\n",
            __FILE__, __LINE__, res, curl_easy_strerror(res));
    goto test_cleanup;
  }
  if(protocol != 0) {
    fprintf(stderr, "%s:%d protocol init failed; expected 0 but is %ld\n",
            __FILE__, __LINE__, protocol);
    res = CURLE_FAILED_INIT;
    goto test_cleanup;
  }

test_cleanup:
  curl_easy_cleanup(curl);
  curl_easy_cleanup(dupe);
  curl_global_cleanup();
  return res;
}
コード例 #19
0
ファイル: DllLibCurl.cpp プロジェクト: intrcomp/xbmc
CURL_HANDLE* DllLibCurl::easy_duphandle(CURL_HANDLE* handle)
{
  return curl_easy_duphandle(handle);
}