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

[PATCH v2 7/8] remote-curl: reencode http error messages

From
Jeff King <peff@peff.net>
Date
May 22, 2014, 09:30 UTC
Message-ID
<20140522093029.GG15032@sigill.intra.peff.net>
In-Reply-To
<20140522092824.GA14530@sigill.intra.peff.net>

We currently recognize an error message with a content-type "text/plain; charset=utf-16" as text, but we ignore the charset parameter entirely. Let's encode it to log_output_encoding, which is presumably something the user's terminal can handle.

Signed-off-by: Jeff King <peff@peff.net>
---
 remote-curl.c              | 17 ++++++++++-------
 t/lib-httpd/error.sh       |  4 ++++
 t/t5550-http-fetch-dumb.sh |  5 +++++
 3 files changed, 19 insertions(+), 7 deletions(-)
diff --git a/remote-curl.c b/remote-curl.c
index a5ab977..4493b38 100644
--- a/remote-curl.c
+++ b/remote-curl.c
@@ -194,19 +194,19 @@ static void free_discovery(struct discovery *d)
 	}
 }
 
-static int show_http_message(struct strbuf *type, struct strbuf *msg)
+static int show_http_message(struct strbuf *type, struct strbuf *charset,
+			     struct strbuf *msg)
 {
 	const char *p, *eol;
 
 	/*
 	 * We only show text/plain parts, as other types are likely
 	 * to be ugly to look at on the user's terminal.
-	 *
-	 * TODO should handle "; charset=XXX", and re-encode into
-	 * logoutputencoding
 	 */
 	if (strcmp(type->buf, "text/plain"))
 		return -1;
+	if (charset->len)
+		strbuf_reencode(msg, charset->buf, get_log_output_encoding());
 
 	strbuf_trim(msg);
 	if (!msg->len)
@@ -225,6 +225,7 @@ static struct discovery* discover_refs(const char *service, int for_push)
 {
 	struct strbuf exp = STRBUF_INIT;
 	struct strbuf type = STRBUF_INIT;
+	struct strbuf charset = STRBUF_INIT;
 	struct strbuf buffer = STRBUF_INIT;
 	struct strbuf refs_url = STRBUF_INIT;
 	struct strbuf effective_url = STRBUF_INIT;
@@ -249,6 +250,7 @@ static struct discovery* discover_refs(const char *service, int for_push)
 
 	memset(&options, 0, sizeof(options));
 	options.content_type = &type;
+	options.charset = &charset;
 	options.effective_url = &effective_url;
 	options.base_url = &url;
 	options.no_cache = 1;
@@ -259,13 +261,13 @@ static struct discovery* discover_refs(const char *service, int for_push)
 	case HTTP_OK:
 		break;
 	case HTTP_MISSING_TARGET:
-		show_http_message(&type, &buffer);
+		show_http_message(&type, &charset, &buffer);
 		die("repository '%s' not found", url.buf);
 	case HTTP_NOAUTH:
-		show_http_message(&type, &buffer);
+		show_http_message(&type, &charset, &buffer);
 		die("Authentication failed for '%s'", url.buf);
 	default:
-		show_http_message(&type, &buffer);
+		show_http_message(&type, &charset, &buffer);
 		die("unable to access '%s': %s", url.buf, curl_errorstr);
 	}
 
@@ -310,6 +312,7 @@ static struct discovery* discover_refs(const char *service, int for_push)
 	strbuf_release(&refs_url);
 	strbuf_release(&exp);
 	strbuf_release(&type);
+	strbuf_release(&charset);
 	strbuf_release(&effective_url);
 	strbuf_release(&buffer);
 	last_discovery = last;
diff --git a/t/lib-httpd/error.sh b/t/lib-httpd/error.sh
index 23cec97..eafc9d2 100755
--- a/t/lib-httpd/error.sh
+++ b/t/lib-httpd/error.sh
@@ -15,6 +15,10 @@ case "$PATH_INFO" in
 	printf "text/plain; charset=utf-8"
 	charset=utf-8
 	;;
+*utf16*)
+	printf "text/plain; charset=utf-16"
+	charset=utf-16
+	;;
 esac
 printf "\n"
 
diff --git a/t/t5550-http-fetch-dumb.sh b/t/t5550-http-fetch-dumb.sh
index b35b261..01b8aae 100755
--- a/t/t5550-http-fetch-dumb.sh
+++ b/t/t5550-http-fetch-dumb.sh
@@ -186,5 +186,10 @@ test_expect_success 'git client shows text/plain with a charset' '
 	grep "this is the error message" stderr
 '
 
+test_expect_success 'http error messages are reencoded' '
+	test_must_fail git clone "$HTTPD_URL/error/utf16" 2>stderr &&
+	grep "this is the error message" stderr
+'
+
 stop_httpd
 test_done
-- 
2.0.0.rc1.436.g03cb729
Previous: Jeff KingNext: Jeff King
Message 40 of 42 in “handle alternate charsets for remote http errors”
  1. 0/9 handle alternate charsets for remote http errorsJeff King, May 21, 2014
  2. 1/9 test-lib: preserve GIT_CURL_VERBOSE from the environmentJeff King, May 21, 2014
  3. 2/9 strbuf: add strbuf_tolower functionJeff King, May 21, 2014
  4. Kyle J. McKayMay 22, 2014
  5. Jeff KingMay 22, 2014
  6. Junio C HamanoMay 22, 2014
  7. Jeff KingMay 22, 2014
  8. Junio C HamanoMay 22, 2014
  9. Jeff KingMay 23, 2014
  10. Kyle J. McKayMay 22, 2014
  11. Jeff KingMay 23, 2014
  12. Kyle J. McKayMay 23, 2014
  13. 3/9 daemon/config: factor out duplicate xstrdup_tolowerJeff King, May 21, 2014
  14. 4/9 http: normalize case of returned content-typeJeff King, May 21, 2014
  15. 5/9 t/lib-httpd: use write_script to copy CGI scriptsJeff King, May 21, 2014
  16. 6/9 t5550: test display of remote http error messagesJeff King, May 21, 2014
  17. 7/9 remote-curl: recognize text/plain with a charset parameterJeff King, May 21, 2014
  18. Kyle J. McKayMay 22, 2014
  19. Jeff KingMay 22, 2014
  20. Kyle J. McKayMay 22, 2014
  21. Jeff KingMay 22, 2014
  22. Peter KreftingMay 22, 2014
  23. Jeff KingMay 22, 2014
  24. Peter KreftingMay 22, 2014
  25. 8/9 strbuf: add strbuf_reencode helperJeff King, May 21, 2014
  26. 9/9 remote-curl: reencode http error messagesJeff King, May 21, 2014
  27. Kyle J. McKayMay 22, 2014
  28. Jeff KingMay 22, 2014
  29. Peter KreftingMay 22, 2014
  30. 0/9 handle alternate charsets for remote http errorsJeff King, May 22, 2014
  31. 1/8 test-lib: preserve GIT_CURL_VERBOSE from the environmentJeff King, May 22, 2014
  32. 2/8 t/lib-httpd: use write_script to copy CGI scriptsJeff King, May 22, 2014
  33. 3/8 t5550: test display of remote http error messagesJeff King, May 22, 2014
  34. 4/8 http: extract type/subtype portion of content-typeJeff King, May 22, 2014
  35. Kyle J. McKayMay 22, 2014
  36. Jeff KingMay 23, 2014
  37. Kyle J. McKayMay 23, 2014
  38. 5/8 http: optionally extract charset parameter from content-typeJeff King, May 22, 2014
  39. 6/8 strbuf: add strbuf_reencode helperJeff King, May 22, 2014
  40. 7/8 remote-curl: reencode http error messagesJeff King, May 22, 2014
  41. 8/8 http: default text charset to iso-8859-1Jeff King, May 22, 2014
  42. brian m. carlsonMay 23, 2014

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.