Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Ymir1711 opened this issue May 11, 2021 · 2 comments

Comments

@Ymir1711
Copy link

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;
}
@bagder
Copy link
Member

bagder commented May 11, 2021

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

@Ymir1711
Copy link
Author

It's better for you to do it.

bagder added a commit that referenced this issue May 11, 2021
Otherwise the old value would linger from a previous use and would mess
up the network speed cap logic.

Reported-by: Ymir1711 on github

Fixes #7042
@bagder bagder closed this as completed in 1a20689 May 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants