Skip to content

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

@Ymir1711

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;
}

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