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

[PATCHv4 1/2] http.c: implement the GIT_TRACE_CURL environment variable

From
Elia Pinto <gitter.spiros@gmail.com>
Date
Apr 28, 2016, 11:57 UTC
Message-ID
<20160428115748.37177-2-gitter.spiros@gmail.com>
In-Reply-To
<20160428115748.37177-1-gitter.spiros@gmail.com>

Implement the GIT_TRACE_CURL environment variable to allow a greater degree of detail of GIT_CURL_VERBOSE, in particular the complete transport header and all the data payload exchanged. It might be useful if a particular situation could require a more thorough debugging analysis. Document the new GIT_TRACE_CURL environment variable.

Helped-by: Torsten Bögershausen <tboegi@web.de>
Helped-by: Ramsay Jones <ramsay@ramsayjones.plus.com>
Helped-by: Junio C Hamano <gitster@pobox.com>
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Elia Pinto <gitter.spiros@gmail.com>
---
 Documentation/git.txt |   8 ++++
 http.c                | 109 +++++++++++++++++++++++++++++++++++++++++++++++++-
 http.h                |   4 ++
 3 files changed, 120 insertions(+), 1 deletion(-)
diff --git a/Documentation/git.txt b/Documentation/git.txt
index 8afe349..958db0f 100644
--- a/Documentation/git.txt
+++ b/Documentation/git.txt
@@ -1075,6 +1075,14 @@ of clones and fetches.
 	cloning of shallow repositories.
 	See 'GIT_TRACE' for available trace output options.
 
+'GIT_TRACE_CURL'::
+	Enables a curl full trace dump of all incoming and outgoing data,
+	including descriptive information, of the git transport protocol.
+	This is similar to doing curl --trace-ascii on the command line.
+	This option overrides setting the GIT_CURL_VERBOSE environment
+	variable.
+	See 'GIT_TRACE' for available trace output options.
+
 'GIT_LITERAL_PATHSPECS'::
 	Setting this variable to `1` will cause Git to treat all
 	pathspecs literally, rather than as glob patterns. For example,
diff --git a/http.c b/http.c
index 985b995..5c2c729 100644
--- a/http.c
+++ b/http.c
@@ -11,6 +11,7 @@
 #include "gettext.h"
 #include "transport.h"
 
+static struct trace_key trace_curl = TRACE_KEY_INIT(CURL);
 #if LIBCURL_VERSION_NUM >= 0x070a08
 long int git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
 #else
@@ -478,6 +479,108 @@ static void set_curl_keepalive(CURL *c)
 }
 #endif
 
+static void curl_dump(const char *text, unsigned char *ptr, size_t size, char nohex, char nopriv)
+{
+	size_t i;
+	struct strbuf out = STRBUF_INIT;
+	unsigned int width = 0x10;
+
+	/* without the hex output, we can fit more on screen */
+	if (nohex) width = 0x50;
+
+	strbuf_addf(&out, "%s, %10.10ld bytes (0x%8.8lx)\n",
+		text, (long)size, (long)size);
+	trace_strbuf(&trace_curl, &out);
+	strbuf_reset(&out);
+
+	for (i = 0; i < size; i += width) {
+		size_t w;
+		strbuf_addf(&out, "%s: ", text);
+		if (!nohex) {
+			for (w = 0; w < width; w++)
+				if (i + w < size)
+					strbuf_addf(&out, "%02x ", ptr[i + w]);
+				else
+					strbuf_addf(&out,"   ");
+		}
+		for (w = 0; (w < width) && (i + w < size); w++) {
+			if (nohex && (i + w + 1 < size) && ptr[i + w] == '\r'
+			    && ptr[i + w + 1] == '\n') {
+				i += (w + 2 - width);
+				break;
+			}
+			strbuf_addch(&out, (ptr[i + w] >= 0x20)
+				&& (ptr[i + w] < 0x80) ? ptr[i + w] : '.');
+			if (nohex && (i + w + 2 < size)
+			    && ptr[i + w + 1] == '\r'
+			    && ptr[i + w + 2] == '\n') {
+				i += (w + 3 - width);
+				break;
+			}
+		}
+		/* if we are called with nopriv we skip the Authorization field if present
+		 * and print a blank line
+		*/
+		if ( nopriv && strstr(out.buf, "Authorization:"))
+			strbuf_reset(&out);
+
+		strbuf_addch(&out, '\n');
+		trace_strbuf(&trace_curl, &out);
+		strbuf_release(&out);
+	}
+}
+
+int curl_trace_want(void)
+{
+	return trace_want(&trace_curl);
+}
+
+int curl_trace(CURL *handle, curl_infotype type, char *data, size_t size, void *userp)
+{
+	const char *text;
+	(void)handle;		/* prevent compiler unused parameter warning if checked */
+	(void)userp;		/* prevent compiler unused parameter warning if checked */
+	char nopriv = 0;	/* default: there are no sensitive data
+				 * in the trace to be skipped
+				*/
+
+	switch (type) {
+	case CURLINFO_TEXT:
+		trace_printf_key(&trace_curl, "== Info: %s", data);
+	default:		/* we ignore unknown types by default */
+		return 0;
+
+	case CURLINFO_HEADER_OUT:
+		text = "=> Send header";
+		nopriv = 1;
+		break;
+	case CURLINFO_DATA_OUT:
+		text = "=> Send data";
+		nopriv = 0;
+		break;
+	case CURLINFO_SSL_DATA_OUT:
+		text = "=> Send SSL data";
+		nopriv = 0;
+		break;
+	case CURLINFO_HEADER_IN:
+		text = "<= Recv header";
+		nopriv = 0;
+		break;
+	case CURLINFO_DATA_IN:
+		text = "<= Recv data";
+		nopriv = 0;
+		break;
+	case CURLINFO_SSL_DATA_IN:
+		text = "<= Recv SSL data";
+		nopriv = 0;
+		break;
+	}
+
+	curl_dump(text, (unsigned char *)data, size, 1, nopriv);
+	return 0;
+}
+
+
 static CURL *get_curl_handle(void)
 {
 	CURL *result = curl_easy_init();
@@ -577,7 +680,11 @@ static CURL *get_curl_handle(void)
 			"your curl version is too old (>= 7.19.4)");
 #endif
 
-	if (getenv("GIT_CURL_VERBOSE"))
+	if (curl_trace_want()) {
+		curl_easy_setopt(result, CURLOPT_VERBOSE, 1L);
+		curl_easy_setopt(result, CURLOPT_DEBUGFUNCTION, curl_trace);
+		curl_easy_setopt(result, CURLOPT_DEBUGDATA, NULL);
+	} else if (getenv("GIT_CURL_VERBOSE"))
 		curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
 
 	curl_easy_setopt(result, CURLOPT_USERAGENT,
diff --git a/http.h b/http.h
index 36f558b..1ee80e5 100644
--- a/http.h
+++ b/http.h
@@ -225,4 +225,8 @@ extern int finish_http_object_request(struct http_object_request *freq);
 extern void abort_http_object_request(struct http_object_request *freq);
 extern void release_http_object_request(struct http_object_request *freq);
 
+/* Debug callback and helper routine for curl_easy_setopt CURLOPT_DEBUGFUNCTION */
+int curl_trace_want(void);
+int curl_trace(CURL *handle, curl_infotype type, char *data, size_t size, void *userp);
+
 #endif /* HTTP_H */
-- 
2.8.1.487.gf8c3767.dirty
Previous: Elia PintoNext: Jeff King
Message 2 of 10 in “[PATCHv4 0/2] Implement the GIT_TRACE_CURL environment variable”
  1. Elia PintoApr 28, 2016
  2. 1/2 http.c: implement the GIT_TRACE_CURL environment variableElia Pinto, Apr 28, 2016
  3. Jeff KingApr 28, 2016
  4. Junio C HamanoApr 28, 2016
  5. Stefan BellerApr 28, 2016
  6. Jeff KingApr 28, 2016
  7. Stefan BellerApr 28, 2016
  8. Jeff KingApr 28, 2016
  9. 2/2 imap-send.c: introduce the GIT_TRACE_CURL enviroment variableElia Pinto, Apr 28, 2016
  10. Jeff KingApr 28, 2016

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.