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

[PATCH 3/2] remote-curl: add Transfer-Encoding header only for older curl

From
Jeff King <peff@peff.net>
Date
Apr 5, 2024, 20:04 UTC
Message-ID
<20240405200451.GA3700566@coredump.intra.peff.net>
In-Reply-To
<20240402200254.GA874754@coredump.intra.peff.net>
On Tue, Apr 02, 2024 at 04:02:54PM -0400, Jeff King wrote:
Show 6 quoted lines
> Ultimately the issue will be fixed by moving to a different version of
> libcurl, but here's an easy workaround in the meantime, with a small doc
> cleanup I found along the way.
> 
>   [1/2]: http: reset POSTFIELDSIZE when clearing curl handle
>   [2/2]: INSTALL: bump libcurl version to 7.21.3

Here's a final patch on top that gives us a workaround for the HTTP/2 half of this. It's a cleaned-up version of what I showed earlier.

-- >8 --
Subject: remote-curl: add Transfer-Encoding header only for older curl

As of curl 7.66.0, we don't need to manually specify a "chunked" Transfer-Encoding header. Instead, modern curl deduces the need for it in a POST that has a POSTFIELDSIZE of -1 and uses READFUNCTION rather than POSTFIELDS.

That version is recent enough that we can't just drop the header; we need to do so conditionally. Since it's only a single line, it seems like the simplest thing would just be to keep setting it unconditionally (after all, the #ifdefs are much longer than the actual code). But there's another wrinkle: HTTP/2.

Curl may choose to use HTTP/2 under the hood if the server supports it. And in that protocol, we do not use the chunked encoding for streaming at all. Most versions of curl handle this just fine by recognizing and removing the header. But there's a regression in curl 8.7.0 and 8.7.1 where it doesn't, and large requests over HTTP/2 are broken (which t5559 notices). That regression has since been fixed upstream, but not yet released.

Make the setting of this header conditional, which will let Git work even with those buggy curl versions. And as a bonus, it serves as a reminder that we can eventually clean up the code as we bump the supported curl versions.

Signed-off-by: Jeff King <peff@peff.net>
---
Manually tested against curl 8.7.0 (where skipping the header fixes
t5559), as well as 7.65.0 (which continues to pass t5551, but would fail
if we didn't set the header).
 git-curl-compat.h | 9 +++++++++
 remote-curl.c     | 3 +++
 2 files changed, 12 insertions(+)
diff --git a/git-curl-compat.h b/git-curl-compat.h
index fd96b3cdff..e1d0bdd273 100644
--- a/git-curl-compat.h
+++ b/git-curl-compat.h
@@ -126,6 +126,15 @@
 #define GIT_CURL_HAVE_CURLSSLSET_NO_BACKENDS
 #endif
 
+/**
+ * Versions before curl 7.66.0 (September 2019) required manually setting the
+ * transfer-encoding for a streaming POST; after that this is handled
+ * automatically.
+ */
+#if LIBCURL_VERSION_NUM < 0x074200
+#define GIT_CURL_NEED_TRANSFER_ENCODING_HEADER
+#endif
+
 /**
  * CURLOPT_PROTOCOLS_STR and CURLOPT_REDIR_PROTOCOLS_STR were added in 7.85.0,
  * released in August 2022.
diff --git a/remote-curl.c b/remote-curl.c
index 31b02b8840..0b6d7815fd 100644
--- a/remote-curl.c
+++ b/remote-curl.c
@@ -1,4 +1,5 @@
 #include "git-compat-util.h"
+#include "git-curl-compat.h"
 #include "config.h"
 #include "environment.h"
 #include "gettext.h"
@@ -955,7 +956,9 @@ static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_rece
 		/* The request body is large and the size cannot be predicted.
 		 * We must use chunked encoding to send it.
 		 */
+#ifdef GIT_CURL_NEED_TRANSFER_ENCODING_HEADER
 		headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
+#endif
 		rpc->initial_buffer = 1;
 		curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
 		curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
-- 
2.44.0.872.gbe303efee2
Previous: Jeff KingNext: Daniel Stenberg
Message 13 of 15 in “tests broken with curl-8.7.0”
  1. Jeff KingMar 30, 2024
  2. Daniel StenbergMar 30, 2024
  3. 0/2 git+curl 8.7.0 workaroundJeff King, Apr 2, 2024
  4. 1/2 http: reset POSTFIELDSIZE when clearing curl handleJeff King, Apr 2, 2024
  5. Junio C HamanoApr 2, 2024
  6. Jeff KingApr 3, 2024
  7. Patrick SteinhardtApr 3, 2024
  8. Patrick SteinhardtApr 3, 2024
  9. Jeff KingApr 3, 2024
  10. 2/2 INSTALL: bump libcurl version to 7.21.3Jeff King, Apr 2, 2024
  11. rsbecker@nexbridge.comApr 2, 2024
  12. Jeff KingApr 2, 2024
  13. 3/2 remote-curl: add Transfer-Encoding header only for older curlJeff King, Apr 5, 2024
  14. Daniel StenbergApr 5, 2024
  15. Junio C HamanoApr 5, 2024

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.