git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/4] http: add the ability to log progress

From
Toon Claes <toon@iotcl.com>
Date
May 8, 2024, 12:44 UTC
Message-ID
<20240508124453.600871-3-toon@iotcl.com>
In-Reply-To
<20240508124453.600871-1-toon@iotcl.com>

Add an option `progress` to `struct http_get_options` to allow the caller to enable download progress using the progress.c API.

Signed-off-by: Toon Claes <toon@iotcl.com>
---
 http.c | 32 ++++++++++++++++++++++++++++++++
 http.h |  5 +++++
 2 files changed, 37 insertions(+)
diff --git a/http.c b/http.c
index 3d80bd6116..15c5d53712 100644
--- a/http.c
+++ b/http.c
@@ -10,6 +10,7 @@
 #include "credential.h"
 #include "version.h"
 #include "pkt-line.h"
+#include "progress.h"
 #include "gettext.h"
 #include "trace.h"
 #include "transport.h"
@@ -1457,6 +1458,9 @@ struct active_request_slot *get_active_slot(void)
 	curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
 	curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 1);
 	curl_easy_setopt(slot->curl, CURLOPT_RANGE, NULL);
+	curl_easy_setopt(slot->curl, CURLOPT_NOPROGRESS, 1L);
+	curl_easy_setopt(slot->curl, CURLOPT_XFERINFODATA, NULL);
+	curl_easy_setopt(slot->curl, CURLOPT_XFERINFOFUNCTION, NULL);
 
 	/*
 	 * Default following to off unless "ALWAYS" is configured; this gives
@@ -2017,6 +2021,21 @@ static void http_opt_request_remainder(CURL *curl, off_t pos)
 #define HTTP_REQUEST_STRBUF	0
 #define HTTP_REQUEST_FILE	1
 
+static int http_progress_callback(void *clientp, curl_off_t dltotal,
+				  curl_off_t dlnow, curl_off_t ultotal,
+				  curl_off_t ulnow)
+{
+	struct progress *progress = clientp;
+
+	if (progress) {
+		progress_set_total(progress, dltotal);
+		display_progress(progress, dlnow);
+		display_throughput(progress, dlnow);
+	}
+
+	return 0;
+}
+
 static int http_request(const char *url,
 			void *result, int target,
 			const struct http_get_options *options)
@@ -2025,6 +2044,7 @@ static int http_request(const char *url,
 	struct slot_results results;
 	struct curl_slist *headers = http_copy_default_headers();
 	struct strbuf buf = STRBUF_INIT;
+	struct progress *progress = NULL;
 	const char *accept_language;
 	int ret;
 
@@ -2061,6 +2081,13 @@ static int http_request(const char *url,
 	if (options && options->initial_request &&
 	    http_follow_config == HTTP_FOLLOW_INITIAL)
 		curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 1);
+	if (options && options->progress) {
+		progress = start_progress(_("Downloading via HTTP"), 0);
+
+		curl_easy_setopt(slot->curl, CURLOPT_NOPROGRESS, 0L);
+		curl_easy_setopt(slot->curl, CURLOPT_XFERINFODATA, progress);
+		curl_easy_setopt(slot->curl, CURLOPT_XFERINFOFUNCTION, &http_progress_callback);
+	}
 
 	headers = curl_slist_append(headers, buf.buf);
 
@@ -2079,6 +2106,11 @@ static int http_request(const char *url,
 
 	ret = run_one_slot(slot, &results);
 
+	if (progress) {
+		curl_easy_setopt(slot->curl, CURLOPT_XFERINFODATA, NULL);
+		stop_progress(&progress);
+	}
+
 	if (options && options->content_type) {
 		struct strbuf raw = STRBUF_INIT;
 		curlinfo_strbuf(slot->curl, CURLINFO_CONTENT_TYPE, &raw);
diff --git a/http.h b/http.h
index 3af19a8bf5..37ecddec17 100644
--- a/http.h
+++ b/http.h
@@ -146,6 +146,11 @@ struct http_get_options {
 	 * request has completed.
 	 */
 	struct string_list *extra_headers;
+
+	/*
+	 * If not zero, display the progress.
+	 */
+	int progress;
 };
 
 /* Return values for http_get_*() */
-- 
2.44.0.651.g21306a098c
Previous: Toon ClaesNext: Eric Sunshine
Message 3 of 25 in “bundle-uri: show progress when downloading from bundle URIs”
  1. 0/4 bundle-uri: show progress when downloading from bundle URIsToon Claes, May 8, 2024
  2. 1/4 progress: add function to set totalToon Claes, May 8, 2024
  3. 2/4 http: add the ability to log progressToon Claes, May 8, 2024
  4. Eric SunshineMay 8, 2024
  5. Jeff KingMay 9, 2024
  6. Junio C HamanoMay 9, 2024
  7. Jeff KingMay 9, 2024
  8. Jeff KingMay 9, 2024
  9. 3/4 remote-curl: optionally show progress for HTTP getToon Claes, May 8, 2024
  10. Junio C HamanoMay 8, 2024
  11. 4/4 bundle-uri: enable git-remote-https progressToon Claes, May 8, 2024
  12. Jeff KingMay 9, 2024
  13. Toon ClaesFeb 14, 2025
  14. Jeff KingFeb 21, 2025
  15. Junio C HamanoMay 8, 2024
  16. 0/7 Show progress when downloading from bundle URIsToon Claes, Feb 19, 2025
  17. 1/7 progress: add function to set totalToon Claes, Feb 19, 2025
  18. Jeff KingFeb 21, 2025
  19. 2/7 progress: allow pure-throughput progress metersToon Claes, Feb 19, 2025
  20. 3/7 http: turn off curl signalsToon Claes, Feb 19, 2025
  21. 4/7 http: add the ability to log progressToon Claes, Feb 19, 2025
  22. 5/7 remote-curl: optionally show progress for HTTP getToon Claes, Feb 19, 2025
  23. 6/7 bundle-uri: enable git-remote-https progressToon Claes, Feb 19, 2025
  24. 7/7 http: silence stderr when progress is enabledToon Claes, Feb 19, 2025
  25. Jeff KingFeb 21, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.