git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: Eric Wong <e@80x24.org>
Cc: Wolfgang Denk <wd@denx.de>,
	Heinrich Schuchardt <xypron.glpk@gmx.de>,
	git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>,
	Takahiro Akashi <takahiro.akashi@linaro.org>
Subject: [PATCH 1/3] http: factor out curl result code normalization
Date: Sun, 24 Mar 2019 08:08:38 -0400	[thread overview]
Message-ID: <20190324120838.GA312@sigill.intra.peff.net> (raw)
In-Reply-To: <20190324120757.GA18684@sigill.intra.peff.net>

We make some requests with CURLOPT_FAILONERROR and some without, and
then handle_curl_result() normalizes any failures to a uniform CURLcode.

There are some other code paths in the dumb-http walker which don't use
handle_curl_result(); let's pull the normalization into its own function
so it can be reused.

Arguably those code paths would benefit from the rest of
handle_curl_result(), notably the auth handling. But retro-fitting it
now would be a lot of work, and in practice it doesn't matter too much
(whatever authentication we needed to make the initial contact with the
server is generally sufficient for the rest of the dumb-http requests).

Signed-off-by: Jeff King <peff@peff.net>
---
 http.c | 18 ++++++++++++------
 http.h |  9 +++++++++
 2 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/http.c b/http.c
index a32ad36ddf..89fcd36a80 100644
--- a/http.c
+++ b/http.c
@@ -1544,7 +1544,8 @@ char *get_remote_object_url(const char *url, const char *hex,
 	return strbuf_detach(&buf, NULL);
 }
 
-static int handle_curl_result(struct slot_results *results)
+void normalize_curl_result(CURLcode *result, long http_code,
+			   char *errorstr, size_t errorlen)
 {
 	/*
 	 * If we see a failing http code with CURLE_OK, we have turned off
@@ -1554,19 +1555,24 @@ static int handle_curl_result(struct slot_results *results)
 	 * Likewise, if we see a redirect (30x code), that means we turned off
 	 * redirect-following, and we should treat the result as an error.
 	 */
-	if (results->curl_result == CURLE_OK &&
-	    results->http_code >= 300) {
-		results->curl_result = CURLE_HTTP_RETURNED_ERROR;
+	if (*result == CURLE_OK && http_code >= 300) {
+		*result = CURLE_HTTP_RETURNED_ERROR;
 		/*
 		 * Normally curl will already have put the "reason phrase"
 		 * from the server into curl_errorstr; unfortunately without
 		 * FAILONERROR it is lost, so we can give only the numeric
 		 * status code.
 		 */
-		xsnprintf(curl_errorstr, sizeof(curl_errorstr),
+		xsnprintf(errorstr, errorlen,
 			  "The requested URL returned error: %ld",
-			  results->http_code);
+			  http_code);
 	}
+}
+
+static int handle_curl_result(struct slot_results *results)
+{
+	normalize_curl_result(&results->curl_result, results->http_code,
+			      curl_errorstr, sizeof(curl_errorstr));
 
 	if (results->curl_result == CURLE_OK) {
 		credential_approve(&http_auth);
diff --git a/http.h b/http.h
index 4eb4e808e5..f0d271bb7b 100644
--- a/http.h
+++ b/http.h
@@ -136,6 +136,15 @@ static inline int missing__target(int code, int result)
 
 #define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
 
+/*
+ * Normalize curl results to handle CURL_FAILONERROR (or lack thereof). Failing
+ * http codes have their "result" converted to CURLE_HTTP_RETURNED_ERROR, and
+ * an appropriate string placed in the errorstr buffer (pass curl_errorstr if
+ * you don't have a custom buffer).
+ */
+void normalize_curl_result(CURLcode *result, long http_code, char *errorstr,
+			   size_t errorlen);
+
 /* Helpers for modifying and creating URLs */
 extern void append_remote_object_url(struct strbuf *buf, const char *url,
 				     const char *hex,
-- 
2.21.0.705.g64cb90f133


  reply	other threads:[~2019-03-24 12:08 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <7e4a2f1d-2b3a-eb83-d3f1-9ac63d68991b@gmx.de>
     [not found] ` <20190322005107.GL9937@linaro.org>
2019-03-22  6:02   ` [BUG] Cloning with git HEAD fails for some repositories Heinrich Schuchardt
2019-03-22  7:12     ` Jeff King
2019-03-22  8:21       ` Wolfgang Denk
2019-03-22  9:31         ` Jeff King
2019-03-22 16:50           ` Eric Wong
2019-03-22 17:42             ` Heinrich Schuchardt
2019-03-22 18:09               ` Eric Wong
2019-03-22 18:41                 ` Heinrich Schuchardt
2019-03-22 20:35                   ` Eric Wong
2019-03-23  8:53             ` Jeff King
2019-03-24 12:07               ` [PATCH 0/3] fix dumb-http fetch with alternates Jeff King
2019-03-24 12:08                 ` Jeff King [this message]
2019-03-24 12:09                 ` [PATCH 2/3] http: normalize curl results for dumb loose and alternates fetches Jeff King
2019-03-24 12:13                 ` [PATCH 3/3] http: use normalize_curl_result() instead of manual conversion Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190324120838.GA312@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=e@80x24.org \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=takahiro.akashi@linaro.org \
    --cc=wd@denx.de \
    --cc=xypron.glpk@gmx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).