-
-/*
- * Transfer()
- *
- * This function is what performs the actual transfer. It is capable of doing
- * both ways simultaneously. The transfer must already have been setup by a
- * call to Curl_setup_transfer().
- *
- * Note that headers are created in a preallocated buffer of a default size.
- * That buffer can be enlarged on demand, but it is never shrunken again.
- *
- */
-
-static CURLcode
-Transfer(struct connectdata *conn)
-{
- CURLcode result;
- struct SessionHandle *data = conn->data;
- struct SingleRequest *k = &data->req;
- bool done=FALSE;
- bool first=TRUE;
- int timeout_ms;
- int buffersize;
- int totmp;
-
- if((conn->sockfd == CURL_SOCKET_BAD) &&
- (conn->writesockfd == CURL_SOCKET_BAD))
- /* nothing to read, nothing to write, we're already OK! */
- return CURLE_OK;
-
- /* we want header and/or body, if neither then don't do this! */
- if(!k->getheader && data->set.opt_no_body)
- return CURLE_OK;
-
- while(!done) {
- curl_socket_t fd_read = conn->sockfd;
- curl_socket_t fd_write = conn->writesockfd;
- int keepon = k->keepon;
- timeout_ms = 1000;
-
- if(conn->waitfor) {
- /* if waitfor is set, get the RECV and SEND bits from that but keep the
- other bits */
- keepon &= ~ (KEEP_RECV|KEEP_SEND);
- keepon |= conn->waitfor & (KEEP_RECV|KEEP_SEND);
- }
-
- /* limit-rate logic: if speed exceeds threshold, then do not include fd in
- select set. The current speed is recalculated in each Curl_readwrite()
- call */
- if((keepon & KEEP_SEND) &&
- (!data->set.max_send_speed ||
- (data->progress.ulspeed < data->set.max_send_speed) )) {
- k->keepon &= ~KEEP_SEND_HOLD;
- }
- else {
- if (data->set.upload && data->set.max_send_speed &&
- (data->progress.ulspeed > data->set.max_send_speed) ) {
- /* calculate upload rate-limitation timeout. */
- buffersize = (int)(data->set.buffer_size ?
- data->set.buffer_size : BUFSIZE);
- totmp = (int)Curl_sleep_time(data->set.max_send_speed,
- data->progress.ulspeed, buffersize);
- if (totmp < timeout_ms)
- timeout_ms = totmp;
- }
- fd_write = CURL_SOCKET_BAD;
- if(keepon & KEEP_SEND)
- k->keepon |= KEEP_SEND_HOLD; /* hold it */
- }
-
- if((keepon & KEEP_RECV) &&
- (!data->set.max_recv_speed ||
- (data->progress.dlspeed < data->set.max_recv_speed)) ) {
- k->keepon &= ~KEEP_RECV_HOLD;
- }
- else {
- if ((!data->set.upload) && data->set.max_recv_speed &&
- (data->progress.dlspeed > data->set.max_recv_speed)) {
- /* Calculate download rate-limitation timeout. */
- buffersize = (int)(data->set.buffer_size ?
- data->set.buffer_size : BUFSIZE);
- totmp = (int)Curl_sleep_time(data->set.max_recv_speed,
- data->progress.dlspeed, buffersize);
- if (totmp < timeout_ms)
- timeout_ms = totmp;
- }
- fd_read = CURL_SOCKET_BAD;
- if(keepon & KEEP_RECV)
- k->keepon |= KEEP_RECV_HOLD; /* hold it */
- }
-
- /* pause logic. Don't check descriptors for paused connections */
- if(k->keepon & KEEP_RECV_PAUSE)
- fd_read = CURL_SOCKET_BAD;
- if(k->keepon & KEEP_SEND_PAUSE)
- fd_write = CURL_SOCKET_BAD;
-
- /* The *_HOLD and *_PAUSE logic is necessary since even though there might
- be no traffic during the select interval, we still call
- Curl_readwrite() for the timeout case and if we limit transfer speed we
- must make sure that this function doesn't transfer anything while in
- HOLD status.
-
- The no timeout for the first round is for the protocols for which data
- has already been slurped off the socket and thus waiting for action
- won't work since it'll wait even though there is already data present
- to work with. */
- if(first &&
- ((fd_read != CURL_SOCKET_BAD) || (fd_write != CURL_SOCKET_BAD)))
- /* if this is the first lap and one of the file descriptors is fine
- to work with, skip the timeout */
- timeout_ms = 0;
- else {
- totmp = Curl_timeleft(conn, &k->now, FALSE);
- if(totmp < 0)
- return CURLE_OPERATION_TIMEDOUT;
- else if(!totmp)
- totmp = 1000;
-
- if (totmp < timeout_ms)
- timeout_ms = totmp;
- }
-
- switch (Curl_socket_ready(fd_read, fd_write, timeout_ms)) {
- case -1: /* select() error, stop reading */
-#ifdef EINTR
- /* The EINTR is not serious, and it seems you might get this more
- often when using the lib in a multi-threaded environment! */
- if(SOCKERRNO == EINTR)
- continue;
-#endif
- return CURLE_RECV_ERROR; /* indicate a network problem */
- case 0: /* timeout */
- default: /* readable descriptors */
-
- result = Curl_readwrite(conn, &done);
- /* "done" signals to us if the transfer(s) are ready */
- break;
- }
- if(result)
- return result;
-
- first = FALSE; /* not the first lap anymore */
- }
-
- return CURLE_OK;
-}
-
-static void loadhostpairs(struct SessionHandle *data)
-{
- struct curl_slist *hostp;
- char hostname[256];
- char address[256];
- int port;
-
- for(hostp = data->change.resolve; hostp; hostp = hostp->next ) {
- if(!hostp->data)
- continue;
- if(hostp->data[0] == '-') {
- /* mark an entry for removal */
- }
- else if(3 == sscanf(hostp->data, "%255[^:]:%d:%255s", hostname, &port,
- address)) {
- struct Curl_dns_entry *dns;
- Curl_addrinfo *addr;
-
- addr = Curl_str2addr(address, port);
- if(!addr) {
- infof(data, "Resolve %s found illegal!\n", hostp->data);
- continue;
- }
- infof(data, "Added %s:%d:%s to DNS cache\n",
- hostname, port, address);
-
- if(data->share)
- Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
-
- /* put this host in the cache */
- dns = Curl_cache_addr(data, addr, hostname, port);
-
- if(data->share)
- Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
- }
- }
- data->change.resolve = NULL; /* dealt with now */
-}
-
-