Created
July 5, 2022 09:41
-
-
Save Sh4d1/53e4a00152890a565ca26096acced684 to your computer and use it in GitHub Desktop.
Curl retry with pipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <curl/curl.h> | |
size_t bytes_written = 0; | |
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) | |
{ | |
size_t written = fwrite(ptr, size, nmemb, (FILE *)stream); | |
bytes_written += written; | |
return written; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
CURL *curl_handle; | |
CURLcode return_code; | |
if(argc < 2) { | |
printf("Usage: %s <URL>\n", argv[0]); | |
return 1; | |
} | |
curl_global_init(CURL_GLOBAL_ALL); | |
curl_handle = curl_easy_init(); | |
curl_easy_setopt(curl_handle, CURLOPT_URL, argv[1]); | |
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L); | |
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 0L); | |
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data); | |
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, stdout); | |
return_code = curl_easy_perform(curl_handle); | |
while (return_code != CURLE_OK) | |
{ | |
size_t old = bytes_written; | |
curl_easy_setopt(curl_handle, CURLOPT_RESUME_FROM_LARGE, bytes_written); | |
if (old == bytes_written) | |
{ | |
curl_easy_cleanup(curl_handle); | |
curl_global_cleanup(); | |
printf("Received no bytes on retry, exiting."); | |
return 2; | |
} | |
return_code = curl_easy_perform(curl_handle); | |
} | |
curl_easy_cleanup(curl_handle); | |
curl_global_cleanup(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment