Skip to content

CURLOPT_MAX_RECV_SPEED_LARGE/CURLOPT_MAX_SEND_SPEED_LARGE not work when reusing CURL easy_handle. #7042

Closed

Description

@Ymir1711

I did this

Download two part of file using the same CURL easy_hanle with maxspeed.

I expected the following

The speed limit is also effective for the second download.

curl/libcurl version

curl-7_76_1

Issue

data->progress.ul_limit_size / data->progress.dl_limit_size was not reset to zero at the beginning.

Diff

diff --git a/lib/progress.c b/lib/progress.c
index cc040a8..c9ff706 100644
--- a/lib/progress.c
+++ b/lib/progress.c
@@ -241,6 +241,8 @@ void Curl_pgrsStartNow(struct Curl_easy *data)
   data->progress.is_t_startransfer_set = false;
   data->progress.ul_limit_start = data->progress.start;
   data->progress.dl_limit_start = data->progress.start;
+  data->progress.ul_limit_size = 0;
+  data->progress.dl_limit_size = 0;
   data->progress.downloaded = 0;
   data->progress.uploaded = 0;
   /* clear all bits except HIDE and HEADERS_OUT */

Demo

#include <stdio.h>
#include <string.h>
#include <curl/curl.h>

static int progress_callback(void *clientp,   double dltotal,   double dlnow,   double ultotal,   double ulnow)
{
    static time_t time_last = 0;
    static double dlnow_last = 0;

    time_t now = time(NULL);
    if (time_last == 0) {
        time_last = now;
        dlnow_last = dlnow;
    }

    if (now > time_last + 1) {
        printf("Speed: %.2f KB/S, Progress: %.2f %%\n", (dlnow - dlnow_last) / (now - time_last) / 1024, dlnow / dltotal * 100);
        time_last = now;
        dlnow_last = dlnow;
    }

    return 0;
}

static size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
{
    return size * nmemb;
}

void perform(CURLM *multi_handle, CURL *easy_handle)
{
    CURLMcode mcode = curl_multi_add_handle(multi_handle, easy_handle);

    CURLcode result = CURLE_OK;
    while (!mcode) {
        int still_running = 0;
        int numfds = 0;

        mcode = curl_multi_perform(multi_handle, &still_running);

        if (!mcode && !still_running) {
            int rc;
            CURLMsg *msg = curl_multi_info_read(multi_handle, &rc);
            if (msg) {
                result = msg->data.result;
                printf("Curl result: %d\n", result);
                break;
            }
        }else {
            long timeout;
            curl_multi_timeout(multi_handle, &timeout);
            if (timeout < 0) {
                timeout = 100;
            }

            curl_multi_wait(multi_handle, NULL, 0, timeout, &numfds);
        }
    }

    curl_multi_remove_handle(multi_handle, easy_handle);
}

int main()
{
    curl_version_info_data *data = curl_version_info(CURLVERSION_NOW);
    printf("Curl version: %s\n", data->version);

    CURLM *multi_handle = curl_multi_init();
    if (!multi_handle) {
        return -1;
    }

    CURL *easy_handle = curl_easy_init();
    if (!easy_handle) {
        return -1;
    }

    const char *url = "https://xxx.com/file/bin/10MB.bin";
    curl_easy_setopt(easy_handle, CURLOPT_URL, url);
    curl_easy_setopt(easy_handle, CURLOPT_PROGRESSFUNCTION, progress_callback);
    curl_easy_setopt(easy_handle, CURLOPT_NOPROGRESS, 0L);
    curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_callback);
    curl_easy_setopt(easy_handle, CURLOPT_VERBOSE, 1L);
    curl_easy_setopt(easy_handle, CURLOPT_SSL_VERIFYPEER, 0L);
    curl_easy_setopt(easy_handle, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t)65535);

    char range[32] = {'\0'};
    strcpy(range, "0-1000000");
    curl_easy_setopt(easy_handle, CURLOPT_RANGE, range);

    perform(multi_handle, easy_handle);

    strcpy(range, "1000001-2000000");
    curl_easy_setopt(easy_handle, CURLOPT_RANGE, range);

    perform(multi_handle, easy_handle);

    curl_easy_cleanup(easy_handle);
    curl_multi_cleanup(multi_handle);

    return 0;
}

Activity

  1. bagder commented on May 11, 2021

    @bagder
    Member

    Seems accurate! Can you make a "real" pull request out of this change or would you like me to do it?

  2. Ymir1711 commented on May 11, 2021

    @Ymir1711
    Author

    It's better for you to do it.

  3. added a commit that references this issue on May 11, 2021
    7b1e85a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions