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

[PATCH v4 2/5] http: enable keep_error for HTTP requests

From
Masaya Suzuki <masayasuzuki@google.com>
Date
Jan 10, 2019, 19:33 UTC
Message-ID
<20190110193350.213327-3-masayasuzuki@google.com>
In-Reply-To
<20190110193350.213327-1-masayasuzuki@google.com>

curl stops parsing a response when it sees a bad HTTP status code and it has CURLOPT_FAILONERROR set. This prevents GIT_CURL_VERBOSE to show HTTP headers on error.

keep_error is an option to receive the HTTP response body for those error responses. By enabling this option, curl will process the HTTP response headers, and they're shown if GIT_CURL_VERBOSE is set.

Signed-off-by: Masaya Suzuki <masayasuzuki@google.com>
---
 http.c        | 42 +++++++++++++++++++-----------------------
 http.h        |  1 -
 remote-curl.c |  1 -
 3 files changed, 19 insertions(+), 25 deletions(-)
diff --git a/http.c b/http.c
index 4eccf4c5d8..954bebf684 100644
--- a/http.c
+++ b/http.c
@@ -1876,8 +1876,6 @@ static int http_request(const char *url,
 	strbuf_addstr(&buf, "Pragma:");
 	if (options && options->no_cache)
 		strbuf_addstr(&buf, " no-cache");
-	if (options && options->keep_error)
-		curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
 	if (options && options->initial_request &&
 	    http_follow_config == HTTP_FOLLOW_INITIAL)
 		curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 1);
@@ -1895,6 +1893,7 @@ static int http_request(const char *url,
 	curl_easy_setopt(slot->curl, CURLOPT_URL, url);
 	curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
 	curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
+	curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
 
 	ret = run_one_slot(slot, &results);
 
@@ -1989,29 +1988,26 @@ static int http_request_reauth(const char *url,
 		return ret;
 
 	/*
-	 * If we are using KEEP_ERROR, the previous request may have
-	 * put cruft into our output stream; we should clear it out before
-	 * making our next request.
+	 * The previous request may have put cruft into our output stream; we
+	 * should clear it out before making our next request.
 	 */
-	if (options && options->keep_error) {
-		switch (target) {
-		case HTTP_REQUEST_STRBUF:
-			strbuf_reset(result);
-			break;
-		case HTTP_REQUEST_FILE:
-			if (fflush(result)) {
-				error_errno("unable to flush a file");
-				return HTTP_START_FAILED;
-			}
-			rewind(result);
-			if (ftruncate(fileno(result), 0) < 0) {
-				error_errno("unable to truncate a file");
-				return HTTP_START_FAILED;
-			}
-			break;
-		default:
-			BUG("Unknown http_request target");
+	switch (target) {
+	case HTTP_REQUEST_STRBUF:
+		strbuf_reset(result);
+		break;
+	case HTTP_REQUEST_FILE:
+		if (fflush(result)) {
+			error_errno("unable to flush a file");
+			return HTTP_START_FAILED;
 		}
+		rewind(result);
+		if (ftruncate(fileno(result), 0) < 0) {
+			error_errno("unable to truncate a file");
+			return HTTP_START_FAILED;
+		}
+		break;
+	default:
+		BUG("Unknown http_request target");
 	}
 
 	credential_fill(&http_auth);
diff --git a/http.h b/http.h
index d305ca1dc7..eebf40688c 100644
--- a/http.h
+++ b/http.h
@@ -146,7 +146,6 @@ extern char *get_remote_object_url(const char *url, const char *hex,
 /* Options for http_get_*() */
 struct http_get_options {
 	unsigned no_cache:1,
-		 keep_error:1,
 		 initial_request:1;
 
 	/* If non-NULL, returns the content-type of the response. */
diff --git a/remote-curl.c b/remote-curl.c
index 1220dffcdc..d8eda2380a 100644
--- a/remote-curl.c
+++ b/remote-curl.c
@@ -380,7 +380,6 @@ static struct discovery *discover_refs(const char *service, int for_push)
 	http_options.extra_headers = &extra_headers;
 	http_options.initial_request = 1;
 	http_options.no_cache = 1;
-	http_options.keep_error = 1;
 
 	http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
 	switch (http_ret) {
-- 
2.20.1.97.g81188d93c3-goog
Previous: Masaya SuzukiNext: Masaya Suzuki
Message 25 of 29 in “Change how HTTP response body is returned”
  1. 1/2 Change how HTTP response body is returnedMasaya Suzuki, Dec 28, 2018
  2. 2/2 Unset CURLOPT_FAILONERRORMasaya Suzuki, Dec 28, 2018
  3. Eric SunshineDec 28, 2018
  4. Masaya SuzukiDec 28, 2018
  5. Eric SunshineDec 28, 2018
  6. Masaya SuzukiDec 28, 2018
  7. 0/2 Show HTTP headers of failed requests with GIT_CURL_VERBOSEMasaya Suzuki, Dec 29, 2018
  8. 1/2 Change how HTTP response body is returnedMasaya Suzuki, Dec 29, 2018
  9. Junio C HamanoJan 3, 2019
  10. Jeff KingJan 4, 2019
  11. Junio C HamanoJan 4, 2019
  12. Jeff KingJan 4, 2019
  13. 2/2 Unset CURLOPT_FAILONERRORMasaya Suzuki, Dec 29, 2018
  14. Jeff KingJan 4, 2019
  15. Masaya SuzukiJan 7, 2019
  16. 0/5 Show HTTP headers of failed requests with GIT_CURL_VERBOSEMasaya Suzuki, Jan 8, 2019
  17. 1/5 http: support file handles for HTTP_KEEP_ERRORMasaya Suzuki, Jan 8, 2019
  18. SZEDER GáborJan 9, 2019
  19. 2/5 http: enable keep_error for HTTP requestsMasaya Suzuki, Jan 8, 2019
  20. 3/5 remote-curl: define struct for CURLOPT_WRITEFUNCTIONMasaya Suzuki, Jan 8, 2019
  21. 4/5 remote-curl: unset CURLOPT_FAILONERRORMasaya Suzuki, Jan 8, 2019
  22. 5/5 test: test GIT_CURL_VERBOSE=1 shows an errorMasaya Suzuki, Jan 8, 2019
  23. 0/5 Show HTTP headers of failed requests with GIT_CURL_VERBOSEMasaya Suzuki, Jan 10, 2019
  24. 1/5 http: support file handles for HTTP_KEEP_ERRORMasaya Suzuki, Jan 10, 2019
  25. 2/5 http: enable keep_error for HTTP requestsMasaya Suzuki, Jan 10, 2019
  26. 3/5 remote-curl: define struct for CURLOPT_WRITEFUNCTIONMasaya Suzuki, Jan 10, 2019
  27. 4/5 remote-curl: unset CURLOPT_FAILONERRORMasaya Suzuki, Jan 10, 2019
  28. 5/5 test: test GIT_CURL_VERBOSE=1 shows an errorMasaya Suzuki, Jan 10, 2019
  29. Junio C HamanoJan 10, 2019

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.