From 8dd2e88a923bd24775182f0a507c993e06a0aacb Mon Sep 17 00:00:00 2001 From: Masaya Suzuki Date: Thu, 10 Jan 2019 11:33:46 -0800 Subject: http: support file handles for HTTP_KEEP_ERROR HTTP_KEEP_ERROR makes it easy to debug HTTP transport errors. In order to make HTTP_KEEP_ERROR enabled for all requests, file handles need to be supported. Signed-off-by: Masaya Suzuki Signed-off-by: Junio C Hamano --- http.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'http.c') diff --git a/http.c b/http.c index eacc2a75ef..ab4279a1ca 100644 --- a/http.c +++ b/http.c @@ -1952,16 +1952,26 @@ static int http_request_reauth(const char *url, /* * 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. We only know how to do this for - * the strbuf case, but that is enough to satisfy current callers. + * 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("HTTP_KEEP_ERROR is only supported with strbufs"); + BUG("Unknown http_request target"); } } -- cgit v1.2.3 From e6cf87b12d3b85b31637c865bbfaed62c3e59e94 Mon Sep 17 00:00:00 2001 From: Masaya Suzuki Date: Thu, 10 Jan 2019 11:33:47 -0800 Subject: http: enable keep_error for HTTP requests 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 Signed-off-by: Junio C Hamano --- http.c | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) (limited to 'http.c') diff --git a/http.c b/http.c index ab4279a1ca..b39921636a 100644 --- a/http.c +++ b/http.c @@ -1837,8 +1837,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); @@ -1856,6 +1854,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); @@ -1950,29 +1949,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); -- cgit v1.2.3