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

[PATCH 2/2] http: use newer curl options for setting credentials

From
Jeff King <peff@peff.net>
Date
Apr 13, 2012, 06:19 UTC
Message-ID
<20120413061925.GB13690@sigill.intra.peff.net>
In-Reply-To
<20120413061622.GA27591@sigill.intra.peff.net>

We give the username and password to curl by sticking them in a buffer of the form "user:pass" and handing the result to CURLOPT_USERPWD. Since curl 7.19.1, there is a split mechanism, where you can specify each element individually.

This has the advantage that a username can contain a ":" character. It also is less code for us, since we can hand our strings over to curl directly. And since curl 7.17.0 and higher promise to copy the strings for us, we we don't even have to worry about memory ownership issues.

Unfortunately, we have to keep the ugly code for old curl around, but as it is now nicely #if'd out, we can easily get rid of it when we decide that 7.19.1 is "old enough".

Signed-off-by: Jeff King <peff@peff.net>
---
For reference, 7.19.1 is from late 2008.
 http.c |   13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
diff --git a/http.c b/http.c
index eaf7f40..2ec3789 100644
--- a/http.c
+++ b/http.c
@@ -210,14 +210,23 @@ static int http_options(const char *var, const char *value, void *cb)
 
 static void init_curl_http_auth(CURL *result)
 {
-	if (http_auth.username) {
+	if (!http_auth.username)
+		return;
+
+	credential_fill(&http_auth);
+
+#if LIBCURL_VERSION_NUM >= 0x071301
+	curl_easy_setopt(result, CURLOPT_USERNAME, http_auth.username);
+	curl_easy_setopt(result, CURLOPT_PASSWORD, http_auth.password);
+#else
+	{
 		static struct strbuf up = STRBUF_INIT;
-		credential_fill(&http_auth);
 		strbuf_reset(&up);
 		strbuf_addf(&up, "%s:%s",
 			    http_auth.username, http_auth.password);
 		curl_easy_setopt(result, CURLOPT_USERPWD, up.buf);
 	}
+#endif
 }
 
 static int has_cert_password(void)
-- 
1.7.9.6.6.g6b3b56
Previous: Jeff King
Message 12 of 12 in “Cannot clone the git repository shared over http with authorization.”
  1. Artur R. CzechowskiApr 1, 2012
  2. Clemens BuchacherApr 1, 2012
  3. Clemens BuchacherApr 1, 2012
  4. Jeff KingApr 2, 2012
  5. Clemens BuchacherApr 10, 2012
  6. 1/2 http auth fails with multiple curl handlesClemens Buchacher, Apr 10, 2012
  7. 2/2 fix http auth with multiple curl handlesClemens Buchacher, Apr 10, 2012
  8. Jeff KingApr 12, 2012
  9. Junio C HamanoApr 12, 2012
  10. Jeff KingApr 13, 2012
  11. 1/2 http: clean up leak in init_curl_http_authJeff King, Apr 13, 2012
  12. 2/2 http: use newer curl options for setting credentialsJeff King, Apr 13, 2012

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.