Skip to content

Commit fc1b774

Browse files
peffgitster
authored andcommitted
remote-curl: reencode http error messages
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d4241f5 commit fc1b774

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

remote-curl.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,19 +194,19 @@ static void free_discovery(struct discovery *d)
194194
}
195195
}
196196

197-
static int show_http_message(struct strbuf *type, struct strbuf *msg)
197+
static int show_http_message(struct strbuf *type, struct strbuf *charset,
198+
struct strbuf *msg)
198199
{
199200
const char *p, *eol;
200201

201202
/*
202203
* We only show text/plain parts, as other types are likely
203204
* to be ugly to look at on the user's terminal.
204-
*
205-
* TODO should handle "; charset=XXX", and re-encode into
206-
* logoutputencoding
207205
*/
208206
if (strcmp(type->buf, "text/plain"))
209207
return -1;
208+
if (charset->len)
209+
strbuf_reencode(msg, charset->buf, get_log_output_encoding());
210210

211211
strbuf_trim(msg);
212212
if (!msg->len)
@@ -225,6 +225,7 @@ static struct discovery* discover_refs(const char *service, int for_push)
225225
{
226226
struct strbuf exp = STRBUF_INIT;
227227
struct strbuf type = STRBUF_INIT;
228+
struct strbuf charset = STRBUF_INIT;
228229
struct strbuf buffer = STRBUF_INIT;
229230
struct strbuf refs_url = STRBUF_INIT;
230231
struct strbuf effective_url = STRBUF_INIT;
@@ -249,6 +250,7 @@ static struct discovery* discover_refs(const char *service, int for_push)
249250

250251
memset(&options, 0, sizeof(options));
251252
options.content_type = &type;
253+
options.charset = &charset;
252254
options.effective_url = &effective_url;
253255
options.base_url = &url;
254256
options.no_cache = 1;
@@ -259,13 +261,13 @@ static struct discovery* discover_refs(const char *service, int for_push)
259261
case HTTP_OK:
260262
break;
261263
case HTTP_MISSING_TARGET:
262-
show_http_message(&type, &buffer);
264+
show_http_message(&type, &charset, &buffer);
263265
die("repository '%s' not found", url.buf);
264266
case HTTP_NOAUTH:
265-
show_http_message(&type, &buffer);
267+
show_http_message(&type, &charset, &buffer);
266268
die("Authentication failed for '%s'", url.buf);
267269
default:
268-
show_http_message(&type, &buffer);
270+
show_http_message(&type, &charset, &buffer);
269271
die("unable to access '%s': %s", url.buf, curl_errorstr);
270272
}
271273

@@ -310,6 +312,7 @@ static struct discovery* discover_refs(const char *service, int for_push)
310312
strbuf_release(&refs_url);
311313
strbuf_release(&exp);
312314
strbuf_release(&type);
315+
strbuf_release(&charset);
313316
strbuf_release(&effective_url);
314317
strbuf_release(&buffer);
315318
last_discovery = last;

t/lib-httpd/error.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ case "$PATH_INFO" in
1515
printf "text/plain; charset=utf-8"
1616
charset=utf-8
1717
;;
18+
*utf16*)
19+
printf "text/plain; charset=utf-16"
20+
charset=utf-16
21+
;;
1822
esac
1923
printf "\n"
2024

t/t5550-http-fetch-dumb.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,10 @@ test_expect_success 'git client shows text/plain with a charset' '
186186
grep "this is the error message" stderr
187187
'
188188

189+
test_expect_success 'http error messages are reencoded' '
190+
test_must_fail git clone "$HTTPD_URL/error/utf16" 2>stderr &&
191+
grep "this is the error message" stderr
192+
'
193+
189194
stop_httpd
190195
test_done

0 commit comments

Comments
 (0)