Skip to contentSkip to conversation

remote-curl: send Accept-Language header to server

PATCH Superseded Li Linchao via GitGitGadget 4 participants 20 messages Jun 8, 2022 – Jul 11, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 remote-curl: send Accept-Language header to server

From: Cactusinhand <[email protected]>

Git server end's ability to accept Accept-Language header was introduced in f18604bbf2(http: add Accept-Language header if possible), but this is only used by very early phase of the transfer, that's HTTP GET request to discover references. For other phases, like POST request in the smart HTTP the server side don't know what language client speak.

This patch teaches git client to learn end-user's preferred language and throw accept-language header to server side. Once server get this header it have ability to talk to end-user with language they understand, this would be very helpful for many non-English speakers.

---
remote-curl: send Accept-Language header to server

Teach git client to learn end-user's preferred language and throw accept-language header to server side. As git is developmented and maintained by English, many developer may ignore Non-English speaker experience. This patch give git server the ability to speak to client end with their preferred language, which can be very helpfuly and friendly to understand the exact meaning of some prompt messages sent by git.

TODO: For SSH tranport, give it an environment variable to understand locale language.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1251%2FCactusinhand%2Fllc%2Fsend-Accept-Language-header-to-HTTP-server-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1251/Cactusinhand/llc/send-Accept-Language-header-to-HTTP-server-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1251

 http.c                      |  2 +-
 http.h                      |  3 +++
 remote-curl.c               | 18 +++++++++++++++++-
 t/t5541-http-push-smart.sh  | 19 +++++++++++++++++++
 t/t5550-http-fetch-dumb.sh  |  2 +-
 t/t5551-http-fetch-smart.sh | 10 ++++++++--
 6 files changed, 49 insertions(+), 5 deletions(-)
http.c +1 -1
index 11c6f69facd..0654e111d1d 100644
@@ -1775,7 +1775,7 @@ static void write_accept_language(struct strbuf *buf)
  *   LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1"
  *   LANGUAGE= LANG=C -> ""
  */
-static const char *get_accept_language(void)
+const char *get_accept_language(void)
 {
 	if (!cached_accept_language) {
 		struct strbuf buf = STRBUF_INIT;
http.h +3 -0
index ba303cfb372..c5039a0208e 100644
@@ -178,6 +178,9 @@ int http_fetch_ref(const char *base, struct ref *ref);
 int http_get_info_packs(const char *base_url,
 			struct packed_git **packs_head);
 
+/* Helper for getting Accept-Language header */
+const char *get_accept_language(void);
+
 struct http_pack_request {
 	char *url;
 
remote-curl.c +17 -1
index 67f178b1120..8acf506705c 100644
@@ -580,6 +580,7 @@ struct rpc_state {
 	char *service_url;
 	char *hdr_content_type;
 	char *hdr_accept;
+	char *hdr_accept_language;
 	char *protocol_header;
 	char *buf;
 	size_t alloc;
@@ -932,6 +933,10 @@ static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_rece
 	headers = curl_slist_append(headers, needs_100_continue ?
 		"Expect: 100-continue" : "Expect:");
 
+	/* Add Accept-Language header */
+	if (rpc->hdr_accept_language)
+		headers = curl_slist_append(headers, rpc->hdr_accept_language);
+
 	/* Add the extra Git-Protocol header */
 	if (rpc->protocol_header)
 		headers = curl_slist_append(headers, rpc->protocol_header);
@@ -1058,6 +1063,7 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
 		       struct strbuf *rpc_result)
 {
 	const char *svc = rpc->service_name;
+	const char *accept_language;
 	struct strbuf buf = STRBUF_INIT;
 	struct child_process client = CHILD_PROCESS_INIT;
 	int err = 0;
@@ -1080,6 +1086,12 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
 	strbuf_addf(&buf, "%s%s", url.buf, svc);
 	rpc->service_url = strbuf_detach(&buf, NULL);
 
+	accept_language = get_accept_language();
+	if (accept_language) {
+		strbuf_addstr(&buf, accept_language);
+		rpc->hdr_accept_language = strbuf_detach(&buf, NULL);
+	}
+
 	strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
 	rpc->hdr_content_type = strbuf_detach(&buf, NULL);
 
@@ -1400,7 +1412,7 @@ static int stateless_connect(const char *service_name)
 	struct discovery *discover;
 	struct rpc_state rpc;
 	struct strbuf buf = STRBUF_INIT;
-
+	const char *accept_language;
 	/*
 	 * Run the info/refs request and see if the server supports protocol
 	 * v2.  If and only if the server supports v2 can we successfully
@@ -1418,6 +1430,10 @@ static int stateless_connect(const char *service_name)
 		printf("\n");
 		fflush(stdout);
 	}
+	accept_language = get_accept_language();
+	if (accept_language) {
+		rpc.hdr_accept_language = xstrfmt("%s", accept_language);
+	}
 
 	rpc.service_name = service_name;
 	rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
t/t5541-http-push-smart.sh +19 -0
index 2f09ff4fac6..4288a279e9e 100755
@@ -80,6 +80,25 @@ test_expect_success 'push to remote repository (standard)' '
 	 test $HEAD = $(git rev-parse --verify HEAD))
 '
 
+test_expect_success 'push to remote repository (standard) with sending Accept-Language' '
+	cat >exp <<-\EOF &&
+	=> Send header: Accept-Language: zh-CN, en;q=0.9, *;q=0.8
+	=> Send header: Accept-Language: zh-CN, en;q=0.9, *;q=0.8
+	EOF
+
+	cd "$ROOT_PATH"/test_repo_clone &&
+	: >path_lang &&
+	git add path_lang &&
+	test_tick &&
+	git commit -m path_lang &&
+	HEAD=$(git rev-parse --verify HEAD) &&
+	GIT_TRACE_CURL=true LANGUAGE="zh_CN:en" git push -v -v 2>err &&
+	! grep "Expect: 100-continue" err &&
+
+	grep "=> Send header: Accept-Language:" err >err.language &&
+	test_cmp exp err.language
+'
+
 test_expect_success 'push already up-to-date' '
 	git push
 '
t/t5550-http-fetch-dumb.sh +1 -1
index f0d9cd584d3..bc308519af5 100755
@@ -369,7 +369,7 @@ ja;q=0.95, zh;q=0.94, sv;q=0.93, pt;q=0.92, nb;q=0.91, *;q=0.90" \
 		ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt:nb
 '
 
-test_expect_success 'git client does not send an empty Accept-Language' '
+test_expect_success 'git client send an empty Accept-Language' '
 	GIT_TRACE_CURL=true LANGUAGE= git ls-remote "$HTTPD_URL/dumb/repo.git" 2>stderr &&
 	! grep "^=> Send header: Accept-Language:" stderr
 '
t/t5551-http-fetch-smart.sh +8 -2
index b9351a732f6..6f65131a4e4 100755
@@ -31,6 +31,7 @@ test_expect_success 'clone http repository' '
 	> GET /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1
 	> Accept: */*
 	> Accept-Encoding: ENCODINGS
+	> Accept-Language: zh-CN, en;q=0.9, *;q=0.8
 	> Pragma: no-cache
 	< HTTP/1.1 200 OK
 	< Pragma: no-cache
@@ -40,13 +41,15 @@ test_expect_success 'clone http repository' '
 	> Accept-Encoding: ENCODINGS
 	> Content-Type: application/x-git-upload-pack-request
 	> Accept: application/x-git-upload-pack-result
+	> Accept-Language: zh-CN, en;q=0.9, *;q=0.8
 	> Content-Length: xxx
 	< HTTP/1.1 200 OK
 	< Pragma: no-cache
 	< Cache-Control: no-cache, max-age=0, must-revalidate
 	< Content-Type: application/x-git-upload-pack-result
 	EOF
-	GIT_TRACE_CURL=true GIT_TEST_PROTOCOL_VERSION=0 \
+
+	GIT_TRACE_CURL=true GIT_TEST_PROTOCOL_VERSION=0 LANGUAGE="zh_CN:en" \
 		git clone --quiet $HTTPD_URL/smart/repo.git clone 2>err &&
 	test_cmp file clone/file &&
 	tr '\''\015'\'' Q <err |
@@ -94,7 +97,10 @@ test_expect_success 'clone http repository' '
 		test_cmp exp actual.smudged &&
 
 		grep "Accept-Encoding:.*gzip" actual >actual.gzip &&
-		test_line_count = 2 actual.gzip
+		test_line_count = 2 actual.gzip &&
+
+		grep "Accept-Language: zh-CN, en" actual >actual.language &&
+		test_line_count = 2 actual.language
 	fi
 '
 

base-commit: ab336e8f1c8009c8b1aab8deb592148e69217085

Signature · 1 line
gitgitgadget
#1 lore raw
Quoted 1 line · Li Linchao via GitGitGadget

From: Cactusinhand <[email protected]>

Huh?

Quoted 5 lines

Git server end's ability to accept Accept-Language header was introduced in f18604bbf2(http: add Accept-Language header if possible), but this is only used by very early phase of the transfer, that's HTTP GET request to discover references. For other phases, like POST request in the smart HTTP the server side don't know what language client speak.

"client speak" -> "the client speaks".

Quoted 1 line

This patch teaches git client to learn end-user's preferred language and

"This patch teaches" -> "Teach"

Quoted 1 line

throw accept-language header to server side. Once server get this header

"server side" -> "the server side".
"server get" -> "the server gets"
"header" -> "header,"

Quoted 1 line

it have ability to talk to end-user with language they understand, this

"it have ability" -> "it has the ability"

", this" -> ". This"

Quoted 9 lines

would be very helpful for many non-English speakers.

http.c +0 -0
index 11c6f69facd..0654e111d1d 100644

http.c static void write_accept_language(struct strbuf *buf) · line 1778

Quoted 5 lines
http.c +1 -1
@@ -1775,7 +1775,7 @@ static void write_accept_language(struct strbuf *buf)
  *   LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1"
  *   LANGUAGE= LANG=C -> ""
  */
-static const char *get_accept_language(void)
+const char *get_accept_language(void)

It was an understandable name for a file-scope static function, but is this name suitable to be a global without making it more narrow and specific to "HTTP" and "Header"?

remote-curl.c static int stateless_connect(const char *service_name) · line 1415

Quoted 10 lines
remote-curl.c +1 -1
index 67f178b1120..8acf506705c 100644
@@ -1400,7 +1412,7 @@ static int stateless_connect(const char *service_name)
 	struct discovery *discover;
 	struct rpc_state rpc;
 	struct strbuf buf = STRBUF_INIT;
-
+	const char *accept_language;

Do not lose the blank line after the block with variable declarations. IOW, the patch around this line should read like this instead:

Quoted 10 lines
remote-curl.c +1 -0
 	struct discovery *discover;
 	struct rpc_state rpc;
 	struct strbuf buf = STRBUF_INIT;
+	const char *accept_language;
 
 	/*
 	 * Run the info/refs request and see if the server supports protocol

remote-curl.c static int stateless_connect(const char *service_name) · line 1436

Quoted 7 lines
remote-curl.c +4 -0
@@ -1418,6 +1430,10 @@ static int stateless_connect(const char *service_name)
 		printf("\n");
 		fflush(stdout);
 	}
+	accept_language = get_accept_language();
+	if (accept_language) {
+		rpc.hdr_accept_language = xstrfmt("%s", accept_language);
+	}

Drop {} around a single-statement block.

remote-curl.c static int stateless_connect(const char *service_name)

Quoted 12 lines
remote-curl.c +12 -0
+	cat >exp <<-\EOF &&
+	=> Send header: Accept-Language: zh-CN, en;q=0.9, *;q=0.8
+	=> Send header: Accept-Language: zh-CN, en;q=0.9, *;q=0.8
+	EOF
+
+	cd "$ROOT_PATH"/test_repo_clone &&
+	: >path_lang &&
+	git add path_lang &&
+	test_tick &&
+	git commit -m path_lang &&
+	HEAD=$(git rev-parse --verify HEAD) &&
+	GIT_TRACE_CURL=true LANGUAGE="zh_CN:en" git push -v -v 2>err &&

A few comments.

* In all gettext/locale tests we seem to set both LANGUAGE and LC_ALL environment variables. Shouldn't we do the same for consistency?

* In existing tests, we seem to use ko_KR, en_US, ja_JP, and random assortment of languages (e.g. t5550). Can we safely add any new languages to the mix without any downside to the tester? We should reuse what we already use, especially if this ends up forcing users and testers to install yet another "language pack" for zh_CN.

Thanks.

#2 lore raw

PATCH v2 1/1 remote-curl: send Accept-Language header to server

In reply to Li Linchao via GitGitGadget

From: Li Linchao <[email protected]>

Git server end's ability to accept Accept-Language header was introduced in f18604bbf2(http: add Accept-Language header if possible), but this is only used by very early phase of the transfer, that's HTTP GET request to discover references. For other phases, like POST request in the smart HTTP the server side don't know what language the client speaks.

Teach git client to learn end-user's preferred language and throw accept-language header to the server side. Once the server gets this header, it has the ability to talk to end-user with language they understand. This would be very helpful for many non-English speakers.

---
remote-curl: send Accept-Language header to server

Changes since v1:

* change get_accept_language() to http_get_accept_language_header()
* reuse test case in t5550
* reword commit message

TODO: For SSH tranport, give it an environment variable to understand locale language.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1251%2FCactusinhand%2Fllc%2Fsend-Accept-Language-header-to-HTTP-server-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1251/Cactusinhand/llc/send-Accept-Language-header-to-HTTP-server-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/1251

Range-diff vs v1:

1: b09f10b6c06 ! 1: a2dd9d4070e remote-curl: send Accept-Language header to server

     @@
       ## Metadata ##
     -Author: Cactusinhand <[email protected]>
     +Author: Li Linchao <[email protected]>
      
       ## Commit message ##
          remote-curl: send Accept-Language header to server
     @@ Commit message
          in f18604bbf2(http: add Accept-Language header if possible), but this is
          only used by very early phase of the transfer, that's HTTP GET request to
          discover references. For other phases, like POST request in the smart HTTP
     -    the server side don't know what language client speak.
     +    the server side don't know what language the client speaks.
      
     -    This patch teaches git client to learn end-user's preferred language and
     -    throw accept-language header to server side. Once server get this header
     -    it have ability to talk to end-user with language they understand, this
     -    would be very helpful for many non-English speakers.
     +    Teach git client to learn end-user's preferred language and throw
     +    accept-language header to the server side. Once the server gets this header,
     +    it has the ability to talk to end-user with language they understand.
     +    This would be very helpful for many non-English speakers.
      
          Signed-off-by: Li Linchao <[email protected]>
      
     @@ http.c: static void write_accept_language(struct strbuf *buf)
        *   LANGUAGE= LANG=C -> ""
        */
      -static const char *get_accept_language(void)
     -+const char *get_accept_language(void)
     ++const char *http_get_accept_language_header(void)
       {
       	if (!cached_accept_language) {
       		struct strbuf buf = STRBUF_INIT;
     +@@ http.c: static int http_request(const char *url,
     + 					 fwrite_buffer);
     + 	}
     + 
     +-	accept_language = get_accept_language();
     ++	accept_language = http_get_accept_language_header();
     + 
     + 	if (accept_language)
     + 		headers = curl_slist_append(headers, accept_language);
      
       ## http.h ##
      @@ http.h: int http_fetch_ref(const char *base, struct ref *ref);
     @@ http.h: int http_fetch_ref(const char *base, struct ref *ref);
       			struct packed_git **packs_head);
       
      +/* Helper for getting Accept-Language header */
     -+const char *get_accept_language(void);
     ++const char *http_get_accept_language_header(void);
      +
       struct http_pack_request {
       	char *url;
     @@ remote-curl.c: static int rpc_service(struct rpc_state *rpc, struct discovery *h
       	strbuf_addf(&buf, "%s%s", url.buf, svc);
       	rpc->service_url = strbuf_detach(&buf, NULL);
       
     -+	accept_language = get_accept_language();
     ++	accept_language = http_get_accept_language_header();
      +	if (accept_language) {
      +		strbuf_addstr(&buf, accept_language);
      +		rpc->hdr_accept_language = strbuf_detach(&buf, NULL);
     @@ remote-curl.c: static int stateless_connect(const char *service_name)
       	struct discovery *discover;
       	struct rpc_state rpc;
       	struct strbuf buf = STRBUF_INIT;
     --
      +	const char *accept_language;
     + 
       	/*
       	 * Run the info/refs request and see if the server supports protocol
     - 	 * v2.  If and only if the server supports v2 can we successfully
      @@ remote-curl.c: static int stateless_connect(const char *service_name)
       		printf("\n");
       		fflush(stdout);
       	}
     -+	accept_language = get_accept_language();
     -+	if (accept_language) {
     ++	accept_language = http_get_accept_language_header();
     ++	if (accept_language)
      +		rpc.hdr_accept_language = xstrfmt("%s", accept_language);
     -+	}
       
       	rpc.service_name = service_name;
       	rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
     @@ t/t5541-http-push-smart.sh: test_expect_success 'push to remote repository (stan
       '
      
       ## t/t5550-http-fetch-dumb.sh ##
     -@@ t/t5550-http-fetch-dumb.sh: ja;q=0.95, zh;q=0.94, sv;q=0.93, pt;q=0.92, nb;q=0.91, *;q=0.90" \
     +@@ t/t5550-http-fetch-dumb.sh: test_expect_success 'git client sends Accept-Language correctly with unordinary
     + 	check_language "ko-KR, en-US;q=0.9, *;q=0.8" "ko_KR::en_US" &&
     + 	check_language "ko-KR, *;q=0.9" ":::ko_KR" &&
     + 	check_language "ko-KR, en-US;q=0.9, *;q=0.8" "ko_KR!!:en_US" &&
     +-	check_language "ko-KR, ja-JP;q=0.9, *;q=0.8" "ko_KR en_US:ja_JP"'
     ++	check_language "ko-KR, ja-JP;q=0.9, zh-CN;q=0.8, *;q=0.7" "ko_KR en_US:ja_JP:zh_CN"'
     + 
     + test_expect_success 'git client sends Accept-Language with many preferred languages' '
     +-	check_language "ko-KR, en-US;q=0.9, fr-CA;q=0.8, de;q=0.7, sr;q=0.6, \
     +-ja;q=0.5, zh;q=0.4, sv;q=0.3, pt;q=0.2, *;q=0.1" \
     +-		ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt &&
     ++	check_language "ko-KR, en-US;q=0.99, fr-CA;q=0.98, de;q=0.97, sr;q=0.96, \
     ++ja;q=0.95, zh;q=0.94, sv;q=0.93, pt;q=0.92, zh-CN;q=0.91, *;q=0.90" \
     ++		ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt:zh_CN &&
     + 	check_language "ko-KR, en-US;q=0.99, fr-CA;q=0.98, de;q=0.97, sr;q=0.96, \
     + ja;q=0.95, zh;q=0.94, sv;q=0.93, pt;q=0.92, nb;q=0.91, *;q=0.90" \
       		ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt:nb
       '
 http.c                      |  4 ++--
 http.h                      |  3 +++
 remote-curl.c               | 16 ++++++++++++++++
 t/t5541-http-push-smart.sh  | 19 +++++++++++++++++++
 t/t5550-http-fetch-dumb.sh  | 10 +++++-----
 t/t5551-http-fetch-smart.sh | 10 ++++++++--
 6 files changed, 53 insertions(+), 9 deletions(-)
http.c +2 -2
index 11c6f69facd..33301d8d5d5 100644
@@ -1775,7 +1775,7 @@ static void write_accept_language(struct strbuf *buf)
  *   LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1"
  *   LANGUAGE= LANG=C -> ""
  */
-static const char *get_accept_language(void)
+const char *http_get_accept_language_header(void)
 {
 	if (!cached_accept_language) {
 		struct strbuf buf = STRBUF_INIT;
@@ -1829,7 +1829,7 @@ static int http_request(const char *url,
 					 fwrite_buffer);
 	}
 
-	accept_language = get_accept_language();
+	accept_language = http_get_accept_language_header();
 
 	if (accept_language)
 		headers = curl_slist_append(headers, accept_language);
http.h +3 -0
index ba303cfb372..3c94c479100 100644
@@ -178,6 +178,9 @@ int http_fetch_ref(const char *base, struct ref *ref);
 int http_get_info_packs(const char *base_url,
 			struct packed_git **packs_head);
 
+/* Helper for getting Accept-Language header */
+const char *http_get_accept_language_header(void);
+
 struct http_pack_request {
 	char *url;
 
remote-curl.c +16 -0
index 67f178b1120..504bbdedbda 100644
@@ -580,6 +580,7 @@ struct rpc_state {
 	char *service_url;
 	char *hdr_content_type;
 	char *hdr_accept;
+	char *hdr_accept_language;
 	char *protocol_header;
 	char *buf;
 	size_t alloc;
@@ -932,6 +933,10 @@ static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_rece
 	headers = curl_slist_append(headers, needs_100_continue ?
 		"Expect: 100-continue" : "Expect:");
 
+	/* Add Accept-Language header */
+	if (rpc->hdr_accept_language)
+		headers = curl_slist_append(headers, rpc->hdr_accept_language);
+
 	/* Add the extra Git-Protocol header */
 	if (rpc->protocol_header)
 		headers = curl_slist_append(headers, rpc->protocol_header);
@@ -1058,6 +1063,7 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
 		       struct strbuf *rpc_result)
 {
 	const char *svc = rpc->service_name;
+	const char *accept_language;
 	struct strbuf buf = STRBUF_INIT;
 	struct child_process client = CHILD_PROCESS_INIT;
 	int err = 0;
@@ -1080,6 +1086,12 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
 	strbuf_addf(&buf, "%s%s", url.buf, svc);
 	rpc->service_url = strbuf_detach(&buf, NULL);
 
+	accept_language = http_get_accept_language_header();
+	if (accept_language) {
+		strbuf_addstr(&buf, accept_language);
+		rpc->hdr_accept_language = strbuf_detach(&buf, NULL);
+	}
+
 	strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
 	rpc->hdr_content_type = strbuf_detach(&buf, NULL);
 
@@ -1400,6 +1412,7 @@ static int stateless_connect(const char *service_name)
 	struct discovery *discover;
 	struct rpc_state rpc;
 	struct strbuf buf = STRBUF_INIT;
+	const char *accept_language;
 
 	/*
 	 * Run the info/refs request and see if the server supports protocol
@@ -1418,6 +1431,9 @@ static int stateless_connect(const char *service_name)
 		printf("\n");
 		fflush(stdout);
 	}
+	accept_language = http_get_accept_language_header();
+	if (accept_language)
+		rpc.hdr_accept_language = xstrfmt("%s", accept_language);
 
 	rpc.service_name = service_name;
 	rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
t/t5541-http-push-smart.sh +19 -0
index 2f09ff4fac6..4288a279e9e 100755
@@ -80,6 +80,25 @@ test_expect_success 'push to remote repository (standard)' '
 	 test $HEAD = $(git rev-parse --verify HEAD))
 '
 
+test_expect_success 'push to remote repository (standard) with sending Accept-Language' '
+	cat >exp <<-\EOF &&
+	=> Send header: Accept-Language: zh-CN, en;q=0.9, *;q=0.8
+	=> Send header: Accept-Language: zh-CN, en;q=0.9, *;q=0.8
+	EOF
+
+	cd "$ROOT_PATH"/test_repo_clone &&
+	: >path_lang &&
+	git add path_lang &&
+	test_tick &&
+	git commit -m path_lang &&
+	HEAD=$(git rev-parse --verify HEAD) &&
+	GIT_TRACE_CURL=true LANGUAGE="zh_CN:en" git push -v -v 2>err &&
+	! grep "Expect: 100-continue" err &&
+
+	grep "=> Send header: Accept-Language:" err >err.language &&
+	test_cmp exp err.language
+'
+
 test_expect_success 'push already up-to-date' '
 	git push
 '
t/t5550-http-fetch-dumb.sh +5 -5
index f0d9cd584d3..42dd9fe2af7 100755
@@ -358,18 +358,18 @@ test_expect_success 'git client sends Accept-Language correctly with unordinary
 	check_language "ko-KR, en-US;q=0.9, *;q=0.8" "ko_KR::en_US" &&
 	check_language "ko-KR, *;q=0.9" ":::ko_KR" &&
 	check_language "ko-KR, en-US;q=0.9, *;q=0.8" "ko_KR!!:en_US" &&
-	check_language "ko-KR, ja-JP;q=0.9, *;q=0.8" "ko_KR en_US:ja_JP"'
+	check_language "ko-KR, ja-JP;q=0.9, zh-CN;q=0.8, *;q=0.7" "ko_KR en_US:ja_JP:zh_CN"'
 
 test_expect_success 'git client sends Accept-Language with many preferred languages' '
-	check_language "ko-KR, en-US;q=0.9, fr-CA;q=0.8, de;q=0.7, sr;q=0.6, \
-ja;q=0.5, zh;q=0.4, sv;q=0.3, pt;q=0.2, *;q=0.1" \
-		ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt &&
+	check_language "ko-KR, en-US;q=0.99, fr-CA;q=0.98, de;q=0.97, sr;q=0.96, \
+ja;q=0.95, zh;q=0.94, sv;q=0.93, pt;q=0.92, zh-CN;q=0.91, *;q=0.90" \
+		ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt:zh_CN &&
 	check_language "ko-KR, en-US;q=0.99, fr-CA;q=0.98, de;q=0.97, sr;q=0.96, \
 ja;q=0.95, zh;q=0.94, sv;q=0.93, pt;q=0.92, nb;q=0.91, *;q=0.90" \
 		ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt:nb
 '
 
-test_expect_success 'git client does not send an empty Accept-Language' '
+test_expect_success 'git client send an empty Accept-Language' '
 	GIT_TRACE_CURL=true LANGUAGE= git ls-remote "$HTTPD_URL/dumb/repo.git" 2>stderr &&
 	! grep "^=> Send header: Accept-Language:" stderr
 '
t/t5551-http-fetch-smart.sh +8 -2
index b9351a732f6..6f65131a4e4 100755
@@ -31,6 +31,7 @@ test_expect_success 'clone http repository' '
 	> GET /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1
 	> Accept: */*
 	> Accept-Encoding: ENCODINGS
+	> Accept-Language: zh-CN, en;q=0.9, *;q=0.8
 	> Pragma: no-cache
 	< HTTP/1.1 200 OK
 	< Pragma: no-cache
@@ -40,13 +41,15 @@ test_expect_success 'clone http repository' '
 	> Accept-Encoding: ENCODINGS
 	> Content-Type: application/x-git-upload-pack-request
 	> Accept: application/x-git-upload-pack-result
+	> Accept-Language: zh-CN, en;q=0.9, *;q=0.8
 	> Content-Length: xxx
 	< HTTP/1.1 200 OK
 	< Pragma: no-cache
 	< Cache-Control: no-cache, max-age=0, must-revalidate
 	< Content-Type: application/x-git-upload-pack-result
 	EOF
-	GIT_TRACE_CURL=true GIT_TEST_PROTOCOL_VERSION=0 \
+
+	GIT_TRACE_CURL=true GIT_TEST_PROTOCOL_VERSION=0 LANGUAGE="zh_CN:en" \
 		git clone --quiet $HTTPD_URL/smart/repo.git clone 2>err &&
 	test_cmp file clone/file &&
 	tr '\''\015'\'' Q <err |
@@ -94,7 +97,10 @@ test_expect_success 'clone http repository' '
 		test_cmp exp actual.smudged &&
 
 		grep "Accept-Encoding:.*gzip" actual >actual.gzip &&
-		test_line_count = 2 actual.gzip
+		test_line_count = 2 actual.gzip &&
+
+		grep "Accept-Language: zh-CN, en" actual >actual.language &&
+		test_line_count = 2 actual.language
 	fi
 '
 

base-commit: ab336e8f1c8009c8b1aab8deb592148e69217085

Signature · 1 line
gitgitgadget
#3 lore raw
Quoted 4 lines · Li Linchao via GitGitGadget

From: Li Linchao <[email protected]>

Git server end's ability to accept Accept-Language header was introduced in f18604bbf2(http: add Accept-Language header if possible), but this is

Pleaes refer to the commit like so:

    f18604bb (http: add Accept-Language header if possible, 2015-01-28)

(cf. Documentation/SubmittingPatches::commit-reference)

"git show -s --pretty=reference f18604bb" is one way to format a commit name in that format.

Quoted 1 line

only used by very early phase of the transfer, that's HTTP GET request to

"that's" -> "which is", probably.

Quoted 2 lines

discover references. For other phases, like POST request in the smart HTTP the server side don't know what language the client speaks.

"HTTP the server side don't" -> "HTTP, the server does not"

Quoted 7 lines
 http.c                      |  4 ++--
 http.h                      |  3 +++
 remote-curl.c               | 16 ++++++++++++++++
 t/t5541-http-push-smart.sh  | 19 +++++++++++++++++++
 t/t5550-http-fetch-dumb.sh  | 10 +++++-----
 t/t5551-http-fetch-smart.sh | 10 ++++++++--
 6 files changed, 53 insertions(+), 9 deletions(-)

What is curious is that without any of changes to the *.[ch] files, updated test 5550 and 5551 pass already.

In other words, these updated tests in 5550 and 5551 probably are not testing the behaviour the updated code intends to show. Of course, if we revert the code that taught the Accept-Language to the GET requests in f18604bb, these tests will fail. There is no reason to touch these two tests to "prove" that the code change in this patch does not break existing support, either.

http.h int http_fetch_ref(const char *base, struct ref *ref); · line 182

Quoted 10 lines
http.h +2 -0
index ba303cfb372..3c94c479100 100644
@@ -178,6 +178,9 @@ int http_fetch_ref(const char *base, struct ref *ref);
 int http_get_info_packs(const char *base_url,
 			struct packed_git **packs_head);
 
+/* Helper for getting Accept-Language header */
+const char *http_get_accept_language_header(void);

OK.

http.h static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_rece · line 938

Quoted 7 lines
http.h +3 -0
@@ -932,6 +933,10 @@ static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_rece
 	headers = curl_slist_append(headers, needs_100_continue ?
 		"Expect: 100-continue" : "Expect:");
 
+	/* Add Accept-Language header */
+	if (rpc->hdr_accept_language)
+		headers = curl_slist_append(headers, rpc->hdr_accept_language);

curl_slist_append() makes a copy of .hdr_accept_language, so rpc struct is still responsible to release the resource used for the member when it goes out of scope.

http.h static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_rece · line 942

Quoted 4 lines
http.h +4 -0
+	accept_language = http_get_accept_language_header();
+	if (accept_language) {
+		strbuf_addstr(&buf, accept_language);
+		rpc->hdr_accept_language = strbuf_detach(&buf, NULL);

That looks like a roundabout way to say xstrdup(). The whole thing can be done like so:

	rpc->hdr_accept_language = xstrdup_or_null(http_get_accept_language_header());

And by doing so we kill another bug. "struct rpc" is allocated on the stack without any initialization, so the new code leaves the hdr_accept_language member uninitialized. Rather, we want to explicitly set NULL to the member when the new header is not in use.

http.h static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_rece

Quoted 2 lines
http.h +2 -0
+	}
+

The memory ownership model for this new .hdr_accept_language member in the RPC struct seems to be that the struct owns the resource of the member.

Quoted 12 lines
http.h +1 -0
 	strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
 	rpc->hdr_content_type = strbuf_detach(&buf, NULL);
@@ -1400,6 +1412,7 @@ static int stateless_connect(const char *service_name)
 	struct discovery *discover;
 	struct rpc_state rpc;
 	struct strbuf buf = STRBUF_INIT;
+	const char *accept_language;
 
 	/*
 	 * Run the info/refs request and see if the server supports protocol

http.h static int stateless_connect(const char *service_name) · line 1436

Quoted 6 lines
http.h +3 -0
@@ -1418,6 +1431,9 @@ static int stateless_connect(const char *service_name)
 		printf("\n");
 		fflush(stdout);
 	}
+	accept_language = http_get_accept_language_header();
+	if (accept_language)
+		rpc.hdr_accept_language = xstrfmt("%s", accept_language);

And this is in line with that memory ownership model.

http.h static int stateless_connect(const char *service_name) · line 1438

Quoted 2 lines
http.h +0 -0
 	rpc.service_name = service_name;
 	rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);

I however do not see anybody that actually freeing when rpc is done.

Are we adding a new memory leak? Shouldn't we be releasing the resources held in rpc.hdr_accept_language when rpc goes out of scope?

t/t5541-http-push-smart.sh test_expect_success 'push to remote repository (standard)' ' · line 87

Quoted 13 lines
t/t5541-http-push-smart.sh +5 -0
index 2f09ff4fac6..4288a279e9e 100755
@@ -80,6 +80,25 @@ test_expect_success 'push to remote repository (standard)' '
 	 test $HEAD = $(git rev-parse --verify HEAD))
 '
 
+test_expect_success 'push to remote repository (standard) with sending Accept-Language' '
+	cat >exp <<-\EOF &&
+	=> Send header: Accept-Language: zh-CN, en;q=0.9, *;q=0.8
+	=> Send header: Accept-Language: zh-CN, en;q=0.9, *;q=0.8
+	EOF

As I already asked, do we need to use a language code that has never been used in our existing test to test this new codepath, or is it sufficient to reuse what we already know that will not cause problems in developers' testing environment, like those used in other existing tests, like ko_KR, en_US, etc. If the latter, I strongly do not want to see a new language added to the test. We are *not* in the business of testing the system locale support on the user's platform.

t/t5541-http-push-smart.sh test_expect_success 'push to remote repository (standard)' ' · line 95

Quoted 7 lines
t/t5541-http-push-smart.sh +7 -0
+	cd "$ROOT_PATH"/test_repo_clone &&
+	: >path_lang &&
+	git add path_lang &&
+	test_tick &&
+	git commit -m path_lang &&
+	HEAD=$(git rev-parse --verify HEAD) &&
+	GIT_TRACE_CURL=true LANGUAGE="zh_CN:en" git push -v -v 2>err &&

If this test, or existing tests in other scripts, do not actually require the LANGUAGE specified in the environment variable to be "installed" on the user's platform, then it might be an acceptable alternative to use a locale (like "tlh_AQ") that is implausible to exist on the user's system, but using what we already use in other tests would be the safest thing to do.

Use ko_KR.UTF8 (and nothing else) like 5550 does with its first use of check_language helper. Or using en_US is also fine, as that is also used over there.

t/t5541-http-push-smart.sh test_expect_success 'push to remote repository (standard)' ' · line 104

Quoted 9 lines
t/t5541-http-push-smart.sh +6 -0
+	! grep "Expect: 100-continue" err &&
+
+	grep "=> Send header: Accept-Language:" err >err.language &&
+	test_cmp exp err.language
+'
+
 test_expect_success 'push already up-to-date' '
 	git push
 '

As I already said, I do not think changes to the following two tests are warranted.

t/t5551-http-fetch-smart.sh

Quoted 2 lines
t/t5550-http-fetch-dumb.sh +0 -0
t/t5551-http-fetch-smart.sh +0 -0

Thanks.

#4 lore raw
Quoted 7 lines
Quoted 4 lines · Li Linchao via GitGitGadget

From: Li Linchao <[email protected]>

Git server end's ability to accept Accept-Language header was introduced in f18604bbf2(http: add Accept-Language header if possible), but this is

Quoted 9 lines

Pleaes refer to the commit like so:

    f18604bb (http: add Accept-Language header if possible, 2015-01-28)

(cf. Documentation/SubmittingPatches::commit-reference)

"git show -s --pretty=reference f18604bb" is one way to format a commit name in that format.

OK, thanks for reminding.

Quoted 3 lines
Quoted 1 line

only used by very early phase of the transfer, that's HTTP GET request to

"that's" -> "which is", probably.

OK.

Quoted 17 lines
Quoted 3 lines

discover references. For other phases, like POST request in the smart HTTP the server side don't know what language the client speaks.

"HTTP the server side don't" -> "HTTP, the server does not"

Quoted 7 lines

  http.c                      |  4 ++-- http.h                      |  3 +++ remote-curl.c               | 16 ++++++++++++++++ t/t5541-http-push-smart.sh  | 19 +++++++++++++++++++ t/t5550-http-fetch-dumb.sh  | 10 +++++----- t/t5551-http-fetch-smart.sh | 10 ++++++++--

  6 files changed, 53 insertions(+), 9 deletions(-)

What is curious is that without any of changes to the *.[ch] files, updated test 5550 and 5551 pass already.

Quoted 6 lines

In other words, these updated tests in 5550 and 5551 probably are not testing the behaviour the updated code intends to show.  Of course, if we revert the code that taught the Accept-Language to the GET requests in f18604bb, these tests will fail.  There is no reason to touch these two tests to "prove" that the code change in this patch does not break existing support, either.

My bad, the updated test in t5550 can not test the updated code, but test the original code in f18604bb.

Quoted 72 lines

http.h int http_fetch_ref(const char *base, struct ref *ref); · line 182

Quoted 11 lines
http.h +0 -0
index ba303cfb372..3c94c479100 100644
@@ -178,6 +178,9 @@ int http_fetch_ref(const char *base, struct ref *ref);
   int http_get_info_packs(const char *base_url,
   struct packed_git **packs_head);

http.h +2 -0
+/* Helper for getting Accept-Language header */
+const char *http_get_accept_language_header(void);

OK.

http.h

Quoted 7 lines
http.h +0 -0
@@ -932,6 +933,10 @@ static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_rece

  headers = curl_slist_append(headers, needs_100_continue ? "Expect: 100-continue" : "Expect:");

http.h +3 -0
+	/* Add Accept-Language header */
+	if (rpc->hdr_accept_language)
+	headers = curl_slist_append(headers, rpc->hdr_accept_language);

curl_slist_append() makes a copy of .hdr_accept_language, so rpc struct is still responsible to release the resource used for the member when it goes out of scope.

http.h

Quoted 4 lines
http.h +4 -0
+	accept_language = http_get_accept_language_header();
+	if (accept_language) {
+	strbuf_addstr(&buf, accept_language);
+	rpc->hdr_accept_language = strbuf_detach(&buf, NULL);

That looks like a roundabout way to say xstrdup().  The whole thing can be done like so:

	rpc->hdr_accept_language = xstrdup_or_null(http_get_accept_language_header());

And by doing so we kill another bug.  "struct rpc" is allocated on the stack without any initialization, so the new code leaves the hdr_accept_language member uninitialized.  Rather, we want to explicitly set NULL to the member when the new header is not in use.

http.h

Quoted 2 lines
http.h +2 -0
+	}
+

The memory ownership model for this new .hdr_accept_language member in the RPC struct seems to be that the struct owns the resource of the member.

Quoted 12 lines

  strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc); rpc->hdr_content_type = strbuf_detach(&buf, NULL);

http.h +1 -0
@@ -1400,6 +1412,7 @@ static int stateless_connect(const char *service_name)
   struct discovery *discover;
   struct rpc_state rpc;
   struct strbuf buf = STRBUF_INIT;
+	const char *accept_language;


  /*
  * Run the info/refs request and see if the server supports protocol

http.h static int stateless_connect(const char *service_name) · line 1436

Quoted 6 lines
http.h +3 -0
@@ -1418,6 +1431,9 @@ static int stateless_connect(const char *service_name)
   printf("\n");
   fflush(stdout);
   }
+	accept_language = http_get_accept_language_header();
+	if (accept_language)
+	rpc.hdr_accept_language = xstrfmt("%s", accept_language);

And this is in line with that memory ownership model.

http.h static int stateless_connect(const char *service_name) · line 1438

Quoted 2 lines
http.h +0 -0
   rpc.service_name = service_name;
   rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
Quoted 6 lines

I however do not see anybody that actually freeing when rpc is done.

Are we adding a new memory leak?  Shouldn't we be releasing the resources held in rpc.hdr_accept_language when rpc goes out of scope?

Right. we should free it in the end of the method, like so:
        free(rpc.service_url); free(rpc.hdr_content_type); free(rpc.hdr_accept);
+      free(rpc.hdr_accept_language); free(rpc.protocol_header);
        free(rpc.buf);
        strbuf_release(&buf);

Quoted 15 lines
Quoted 9 lines
t/t5541-http-push-smart.sh +0 -0
index 2f09ff4fac6..4288a279e9e 100755
@@ -80,6 +80,25 @@ test_expect_success 'push to remote repository (standard)' '
   test $HEAD = $(git rev-parse --verify HEAD))

  '

t/t5541-http-push-smart.sh test_expect_success 'push to remote repository (standard)' ' · line 87

Quoted 5 lines
t/t5541-http-push-smart.sh +5 -0
+test_expect_success 'push to remote repository (standard) with sending Accept-Language' '
+	cat >exp <<-\EOF &&
+	=> Send header: Accept-Language: zh-CN, en;q=0.9, *;q=0.8
+	=> Send header: Accept-Language: zh-CN, en;q=0.9, *;q=0.8
+	EOF
Quoted 8 lines

As I already asked, do we need to use a language code that has never been used in our existing test to test this new codepath, or is it sufficient to reuse what we already know that will not cause problems in developers' testing environment, like those used in other existing tests, like ko_KR, en_US, etc.  If the latter, I strongly do not want to see a new language added to the test.  We are *not* in the business of testing the system locale support on the user's platform.

OK.

Quoted 16 lines

t/t5541-http-push-smart.sh test_expect_success 'push to remote repository (standard)' ' · line 95

Quoted 8 lines
t/t5541-http-push-smart.sh +7 -0
+	cd "$ROOT_PATH"/test_repo_clone &&
+	: >path_lang &&
+	git add path_lang &&
+	test_tick &&
+	git commit -m path_lang &&
+	HEAD=$(git rev-parse --verify HEAD) &&
+	GIT_TRACE_CURL=true LANGUAGE="zh_CN:en" git push -v -v 2>err &&

If this test, or existing tests in other scripts, do not actually require the LANGUAGE specified in the environment variable to be "installed" on the user's platform, then it might be an acceptable alternative to use a locale (like "tlh_AQ") that is implausible to exist on the user's system, but using what we already use in other tests would be the safest thing to do.

Quoted 3 lines

Use ko_KR.UTF8 (and nothing else) like 5550 does with its first use of check_language helper.  Or using en_US is also fine, as that is also used over there.

OK.

Quoted 14 lines
Quoted 10 lines
t/t5541-http-push-smart.sh +6 -0
+	! grep "Expect: 100-continue" err &&
+
+	grep "=> Send header: Accept-Language:" err >err.language &&
+	test_cmp exp err.language
+'
+

  test_expect_success 'push already up-to-date' '
  git push
  '

As I already said, I do not think changes to the following two tests are warranted.

Quoted 2 lines

t/t5551-http-fetch-smart.sh

Quoted 2 lines
t/t5550-http-fetch-dumb.sh +0 -0
t/t5551-http-fetch-smart.sh +0 -0

Well, after I made some tests, the reason t5551 fail to test what we want is "if test "$GIT_TEST_PROTOCOL_VERSION" = 2" this statement block the real test.

Quoted 3 lines

Thanks.

#5 lore raw
Quoted 9 lines
Quoted 6 lines

As I already said, I do not think changes to the following two tests are warranted.

t/t5551-http-fetch-smart.sh

Quoted 2 lines
t/t5550-http-fetch-dumb.sh +0 -0
t/t5551-http-fetch-smart.sh +0 -0

Well, after I made some tests, the reason t5551 fail to test what we want is "if test "$GIT_TEST_PROTOCOL_VERSION" = 2" this statement block the real test.

Fix: '"$GIT_TEST_PROTOCOL_VERSION" = 0'

Quoted 3 lines
Quoted 3 lines

Thanks.

#6 lore raw

PATCH v3 1/1 remote-curl: send Accept-Language header to server

In reply to Li Linchao via GitGitGadget

From: Li Linchao <[email protected]>

Git server end's ability to accept Accept-Language header was introduced in f18604bbf2 (http: add Accept-Language header if possible, 2015-01-28), but this is only used by very early phase of the transfer, which is HTTP GET request to discover references. For other phases, like POST request in the smart HTTP, the server does not know what language the client speaks.

Teach git client to learn end-user's preferred language and throw accept-language header to the server side. Once the server gets this header, it has the ability to talk to end-user with language they understand. This would be very helpful for many non-English speakers.

---
remote-curl: send Accept-Language header to server

Change since v2:

* free rpc.hdr_accept_language to avoid memory leak
* fix test to reuse language to avoid install new language pack
* reword commit message

Changes since v1:

* change get_accept_language() to http_get_accept_language_header()
* reuse test case in t5550
* reword commit message

TODO: For SSH tranport, give it an environment variable to understand locale language.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1251%2FCactusinhand%2Fllc%2Fsend-Accept-Language-header-to-HTTP-server-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1251/Cactusinhand/llc/send-Accept-Language-header-to-HTTP-server-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/1251

Range-diff vs v2:

1: a2dd9d4070e ! 1: 99a4e23ceb1 remote-curl: send Accept-Language header to server

     @@ Commit message
          remote-curl: send Accept-Language header to server
      
          Git server end's ability to accept Accept-Language header was introduced
     -    in f18604bbf2(http: add Accept-Language header if possible), but this is
     -    only used by very early phase of the transfer, that's HTTP GET request to
     -    discover references. For other phases, like POST request in the smart HTTP
     -    the server side don't know what language the client speaks.
     +    in f18604bbf2 (http: add Accept-Language header if possible, 2015-01-28),
     +    but this is only used by very early phase of the transfer, which is HTTP
     +    GET request to discover references. For other phases, like POST request
     +    in the smart HTTP, the server does not know what language the client
     +    speaks.
      
          Teach git client to learn end-user's preferred language and throw
          accept-language header to the server side. Once the server gets this header,
     @@ remote-curl.c: static int post_rpc(struct rpc_state *rpc, int stateless_connect,
       	/* Add the extra Git-Protocol header */
       	if (rpc->protocol_header)
       		headers = curl_slist_append(headers, rpc->protocol_header);
     -@@ remote-curl.c: static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
     - 		       struct strbuf *rpc_result)
     - {
     - 	const char *svc = rpc->service_name;
     -+	const char *accept_language;
     - 	struct strbuf buf = STRBUF_INIT;
     - 	struct child_process client = CHILD_PROCESS_INIT;
     - 	int err = 0;
      @@ remote-curl.c: static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
       	strbuf_addf(&buf, "%s%s", url.buf, svc);
       	rpc->service_url = strbuf_detach(&buf, NULL);
       
     -+	accept_language = http_get_accept_language_header();
     -+	if (accept_language) {
     -+		strbuf_addstr(&buf, accept_language);
     -+		rpc->hdr_accept_language = strbuf_detach(&buf, NULL);
     -+	}
     ++	rpc->hdr_accept_language = xstrdup_or_null(http_get_accept_language_header());
      +
       	strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
       	rpc->hdr_content_type = strbuf_detach(&buf, NULL);
       
     +@@ remote-curl.c: static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
     + 	free(rpc->service_url);
     + 	free(rpc->hdr_content_type);
     + 	free(rpc->hdr_accept);
     ++	free(rpc->hdr_accept_language);
     + 	free(rpc->protocol_header);
     + 	free(rpc->buf);
     + 	strbuf_release(&buf);
      @@ remote-curl.c: static int stateless_connect(const char *service_name)
       	struct discovery *discover;
       	struct rpc_state rpc;
     @@ remote-curl.c: static int stateless_connect(const char *service_name)
       
       	rpc.service_name = service_name;
       	rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
     +@@ remote-curl.c: static int stateless_connect(const char *service_name)
     + 	free(rpc.service_url);
     + 	free(rpc.hdr_content_type);
     + 	free(rpc.hdr_accept);
     ++	free(rpc.hdr_accept_language);
     + 	free(rpc.protocol_header);
     + 	free(rpc.buf);
     + 	strbuf_release(&buf);
      
       ## t/t5541-http-push-smart.sh ##
      @@ t/t5541-http-push-smart.sh: test_expect_success 'push to remote repository (standard)' '
     @@ t/t5541-http-push-smart.sh: test_expect_success 'push to remote repository (stan
       
      +test_expect_success 'push to remote repository (standard) with sending Accept-Language' '
      +	cat >exp <<-\EOF &&
     -+	=> Send header: Accept-Language: zh-CN, en;q=0.9, *;q=0.8
     -+	=> Send header: Accept-Language: zh-CN, en;q=0.9, *;q=0.8
     ++	=> Send header: Accept-Language: ko-KR, *;q=0.9
     ++	=> Send header: Accept-Language: ko-KR, *;q=0.9
      +	EOF
      +
      +	cd "$ROOT_PATH"/test_repo_clone &&
     @@ t/t5541-http-push-smart.sh: test_expect_success 'push to remote repository (stan
      +	test_tick &&
      +	git commit -m path_lang &&
      +	HEAD=$(git rev-parse --verify HEAD) &&
     -+	GIT_TRACE_CURL=true LANGUAGE="zh_CN:en" git push -v -v 2>err &&
     ++	GIT_TRACE_CURL=true LANGUAGE="ko_KR.UTF-8" git push -v -v 2>err &&
      +	! grep "Expect: 100-continue" err &&
      +
      +	grep "=> Send header: Accept-Language:" err >err.language &&
     @@ t/t5541-http-push-smart.sh: test_expect_success 'push to remote repository (stan
       '
      
       ## t/t5550-http-fetch-dumb.sh ##
     -@@ t/t5550-http-fetch-dumb.sh: test_expect_success 'git client sends Accept-Language correctly with unordinary
     - 	check_language "ko-KR, en-US;q=0.9, *;q=0.8" "ko_KR::en_US" &&
     - 	check_language "ko-KR, *;q=0.9" ":::ko_KR" &&
     - 	check_language "ko-KR, en-US;q=0.9, *;q=0.8" "ko_KR!!:en_US" &&
     --	check_language "ko-KR, ja-JP;q=0.9, *;q=0.8" "ko_KR en_US:ja_JP"'
     -+	check_language "ko-KR, ja-JP;q=0.9, zh-CN;q=0.8, *;q=0.7" "ko_KR en_US:ja_JP:zh_CN"'
     - 
     - test_expect_success 'git client sends Accept-Language with many preferred languages' '
     --	check_language "ko-KR, en-US;q=0.9, fr-CA;q=0.8, de;q=0.7, sr;q=0.6, \
     --ja;q=0.5, zh;q=0.4, sv;q=0.3, pt;q=0.2, *;q=0.1" \
     --		ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt &&
     -+	check_language "ko-KR, en-US;q=0.99, fr-CA;q=0.98, de;q=0.97, sr;q=0.96, \
     -+ja;q=0.95, zh;q=0.94, sv;q=0.93, pt;q=0.92, zh-CN;q=0.91, *;q=0.90" \
     -+		ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt:zh_CN &&
     - 	check_language "ko-KR, en-US;q=0.99, fr-CA;q=0.98, de;q=0.97, sr;q=0.96, \
     - ja;q=0.95, zh;q=0.94, sv;q=0.93, pt;q=0.92, nb;q=0.91, *;q=0.90" \
     +@@ t/t5550-http-fetch-dumb.sh: ja;q=0.95, zh;q=0.94, sv;q=0.93, pt;q=0.92, nb;q=0.91, *;q=0.90" \
       		ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt:nb
       '
       
     @@ t/t5551-http-fetch-smart.sh: test_expect_success 'clone http repository' '
       	> GET /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1
       	> Accept: */*
       	> Accept-Encoding: ENCODINGS
     -+	> Accept-Language: zh-CN, en;q=0.9, *;q=0.8
     ++	> Accept-Language: ko-KR, *;q=0.9
       	> Pragma: no-cache
       	< HTTP/1.1 200 OK
       	< Pragma: no-cache
     @@ t/t5551-http-fetch-smart.sh: test_expect_success 'clone http repository' '
       	> Accept-Encoding: ENCODINGS
       	> Content-Type: application/x-git-upload-pack-request
       	> Accept: application/x-git-upload-pack-result
     -+	> Accept-Language: zh-CN, en;q=0.9, *;q=0.8
     ++	> Accept-Language: ko-KR, *;q=0.9
       	> Content-Length: xxx
       	< HTTP/1.1 200 OK
       	< Pragma: no-cache
     @@ t/t5551-http-fetch-smart.sh: test_expect_success 'clone http repository' '
       	EOF
      -	GIT_TRACE_CURL=true GIT_TEST_PROTOCOL_VERSION=0 \
      +
     -+	GIT_TRACE_CURL=true GIT_TEST_PROTOCOL_VERSION=0 LANGUAGE="zh_CN:en" \
     ++	GIT_TRACE_CURL=true GIT_TEST_PROTOCOL_VERSION=0 LANGUAGE="ko_KR.UTF-8" \
       		git clone --quiet $HTTPD_URL/smart/repo.git clone 2>err &&
       	test_cmp file clone/file &&
       	tr '\''\015'\'' Q <err |
     @@ t/t5551-http-fetch-smart.sh: test_expect_success 'clone http repository' '
      -		test_line_count = 2 actual.gzip
      +		test_line_count = 2 actual.gzip &&
      +
     -+		grep "Accept-Language: zh-CN, en" actual >actual.language &&
     ++		grep "Accept-Language: ko-KR, *" actual >actual.language &&
      +		test_line_count = 2 actual.language
       	fi
       '
 http.c                      |  4 ++--
 http.h                      |  3 +++
 remote-curl.c               | 13 +++++++++++++
 t/t5541-http-push-smart.sh  | 19 +++++++++++++++++++
 t/t5550-http-fetch-dumb.sh  |  2 +-
 t/t5551-http-fetch-smart.sh | 10 ++++++++--
 6 files changed, 46 insertions(+), 5 deletions(-)
http.c +2 -2
index 11c6f69facd..33301d8d5d5 100644
@@ -1775,7 +1775,7 @@ static void write_accept_language(struct strbuf *buf)
  *   LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1"
  *   LANGUAGE= LANG=C -> ""
  */
-static const char *get_accept_language(void)
+const char *http_get_accept_language_header(void)
 {
 	if (!cached_accept_language) {
 		struct strbuf buf = STRBUF_INIT;
@@ -1829,7 +1829,7 @@ static int http_request(const char *url,
 					 fwrite_buffer);
 	}
 
-	accept_language = get_accept_language();
+	accept_language = http_get_accept_language_header();
 
 	if (accept_language)
 		headers = curl_slist_append(headers, accept_language);
http.h +3 -0
index ba303cfb372..3c94c479100 100644
@@ -178,6 +178,9 @@ int http_fetch_ref(const char *base, struct ref *ref);
 int http_get_info_packs(const char *base_url,
 			struct packed_git **packs_head);
 
+/* Helper for getting Accept-Language header */
+const char *http_get_accept_language_header(void);
+
 struct http_pack_request {
 	char *url;
 
remote-curl.c +13 -0
index 67f178b1120..251d4ee64f6 100644
@@ -580,6 +580,7 @@ struct rpc_state {
 	char *service_url;
 	char *hdr_content_type;
 	char *hdr_accept;
+	char *hdr_accept_language;
 	char *protocol_header;
 	char *buf;
 	size_t alloc;
@@ -932,6 +933,10 @@ static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_rece
 	headers = curl_slist_append(headers, needs_100_continue ?
 		"Expect: 100-continue" : "Expect:");
 
+	/* Add Accept-Language header */
+	if (rpc->hdr_accept_language)
+		headers = curl_slist_append(headers, rpc->hdr_accept_language);
+
 	/* Add the extra Git-Protocol header */
 	if (rpc->protocol_header)
 		headers = curl_slist_append(headers, rpc->protocol_header);
@@ -1080,6 +1085,8 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
 	strbuf_addf(&buf, "%s%s", url.buf, svc);
 	rpc->service_url = strbuf_detach(&buf, NULL);
 
+	rpc->hdr_accept_language = xstrdup_or_null(http_get_accept_language_header());
+
 	strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
 	rpc->hdr_content_type = strbuf_detach(&buf, NULL);
 
@@ -1118,6 +1125,7 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
 	free(rpc->service_url);
 	free(rpc->hdr_content_type);
 	free(rpc->hdr_accept);
+	free(rpc->hdr_accept_language);
 	free(rpc->protocol_header);
 	free(rpc->buf);
 	strbuf_release(&buf);
@@ -1400,6 +1408,7 @@ static int stateless_connect(const char *service_name)
 	struct discovery *discover;
 	struct rpc_state rpc;
 	struct strbuf buf = STRBUF_INIT;
+	const char *accept_language;
 
 	/*
 	 * Run the info/refs request and see if the server supports protocol
@@ -1418,6 +1427,9 @@ static int stateless_connect(const char *service_name)
 		printf("\n");
 		fflush(stdout);
 	}
+	accept_language = http_get_accept_language_header();
+	if (accept_language)
+		rpc.hdr_accept_language = xstrfmt("%s", accept_language);
 
 	rpc.service_name = service_name;
 	rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
@@ -1467,6 +1479,7 @@ static int stateless_connect(const char *service_name)
 	free(rpc.service_url);
 	free(rpc.hdr_content_type);
 	free(rpc.hdr_accept);
+	free(rpc.hdr_accept_language);
 	free(rpc.protocol_header);
 	free(rpc.buf);
 	strbuf_release(&buf);
t/t5541-http-push-smart.sh +19 -0
index 2f09ff4fac6..fbad2d5ff5e 100755
@@ -80,6 +80,25 @@ test_expect_success 'push to remote repository (standard)' '
 	 test $HEAD = $(git rev-parse --verify HEAD))
 '
 
+test_expect_success 'push to remote repository (standard) with sending Accept-Language' '
+	cat >exp <<-\EOF &&
+	=> Send header: Accept-Language: ko-KR, *;q=0.9
+	=> Send header: Accept-Language: ko-KR, *;q=0.9
+	EOF
+
+	cd "$ROOT_PATH"/test_repo_clone &&
+	: >path_lang &&
+	git add path_lang &&
+	test_tick &&
+	git commit -m path_lang &&
+	HEAD=$(git rev-parse --verify HEAD) &&
+	GIT_TRACE_CURL=true LANGUAGE="ko_KR.UTF-8" git push -v -v 2>err &&
+	! grep "Expect: 100-continue" err &&
+
+	grep "=> Send header: Accept-Language:" err >err.language &&
+	test_cmp exp err.language
+'
+
 test_expect_success 'push already up-to-date' '
 	git push
 '
t/t5550-http-fetch-dumb.sh +1 -1
index f0d9cd584d3..bc308519af5 100755
@@ -369,7 +369,7 @@ ja;q=0.95, zh;q=0.94, sv;q=0.93, pt;q=0.92, nb;q=0.91, *;q=0.90" \
 		ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt:nb
 '
 
-test_expect_success 'git client does not send an empty Accept-Language' '
+test_expect_success 'git client send an empty Accept-Language' '
 	GIT_TRACE_CURL=true LANGUAGE= git ls-remote "$HTTPD_URL/dumb/repo.git" 2>stderr &&
 	! grep "^=> Send header: Accept-Language:" stderr
 '
t/t5551-http-fetch-smart.sh +8 -2
index b9351a732f6..245532df881 100755
@@ -31,6 +31,7 @@ test_expect_success 'clone http repository' '
 	> GET /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1
 	> Accept: */*
 	> Accept-Encoding: ENCODINGS
+	> Accept-Language: ko-KR, *;q=0.9
 	> Pragma: no-cache
 	< HTTP/1.1 200 OK
 	< Pragma: no-cache
@@ -40,13 +41,15 @@ test_expect_success 'clone http repository' '
 	> Accept-Encoding: ENCODINGS
 	> Content-Type: application/x-git-upload-pack-request
 	> Accept: application/x-git-upload-pack-result
+	> Accept-Language: ko-KR, *;q=0.9
 	> Content-Length: xxx
 	< HTTP/1.1 200 OK
 	< Pragma: no-cache
 	< Cache-Control: no-cache, max-age=0, must-revalidate
 	< Content-Type: application/x-git-upload-pack-result
 	EOF
-	GIT_TRACE_CURL=true GIT_TEST_PROTOCOL_VERSION=0 \
+
+	GIT_TRACE_CURL=true GIT_TEST_PROTOCOL_VERSION=0 LANGUAGE="ko_KR.UTF-8" \
 		git clone --quiet $HTTPD_URL/smart/repo.git clone 2>err &&
 	test_cmp file clone/file &&
 	tr '\''\015'\'' Q <err |
@@ -94,7 +97,10 @@ test_expect_success 'clone http repository' '
 		test_cmp exp actual.smudged &&
 
 		grep "Accept-Encoding:.*gzip" actual >actual.gzip &&
-		test_line_count = 2 actual.gzip
+		test_line_count = 2 actual.gzip &&
+
+		grep "Accept-Language: ko-KR, *" actual >actual.language &&
+		test_line_count = 2 actual.language
 	fi
 '
 

base-commit: ab336e8f1c8009c8b1aab8deb592148e69217085

Signature · 1 line
gitgitgadget
#7 lore raw
Quoted 6 lines · Li Linchao via GitGitGadget

Range-diff vs v2:

1: a2dd9d4070e ! 1: 99a4e23ceb1 remote-curl: send Accept-Language header to server
@@ Commit message
remote-curl: send Accept-Language header to server

Quoted 10 lines

Git server end's ability to accept Accept-Language header was introduced
- in f18604bbf2(http: add Accept-Language header if possible), but this is
- only used by very early phase of the transfer, that's HTTP GET request to
- discover references. For other phases, like POST request in the smart HTTP
- the server side don't know what language the client speaks.
+ in f18604bbf2 (http: add Accept-Language header if possible, 2015-01-28),
+ but this is only used by very early phase of the transfer, which is HTTP
+ GET request to discover references. For other phases, like POST request
+ in the smart HTTP, the server does not know what language the client
+ speaks.

OK.

Quoted 6 lines
     -+	accept_language = http_get_accept_language_header();
     -+	if (accept_language) {
     -+		strbuf_addstr(&buf, accept_language);
     -+		rpc->hdr_accept_language = strbuf_detach(&buf, NULL);
     -+	}
     ++	rpc->hdr_accept_language = xstrdup_or_null(http_get_accept_language_header());

Nice.

Quoted 8 lines

+@@ remote-curl.c: static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
+ free(rpc->service_url);
+ free(rpc->hdr_content_type);
+ free(rpc->hdr_accept);
++ free(rpc->hdr_accept_language);
+ free(rpc->protocol_header);
+ free(rpc->buf);
+ strbuf_release(&buf);

OK.

Quoted 8 lines

+@@ remote-curl.c: static int stateless_connect(const char *service_name)
+ free(rpc.service_url);
+ free(rpc.hdr_content_type);
+ free(rpc.hdr_accept);
++ free(rpc.hdr_accept_language);
+ free(rpc.protocol_header);
+ free(rpc.buf);
+ strbuf_release(&buf);

OK.

Thanks. Will queue.

#8 lore raw
In reply to Li Linchao via GitGitGadget
Quoted 37 lines · Li Linchao via GitGitGadget
remote-curl.c +8 -0
@@ -932,6 +933,10 @@ static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_rece
 	headers = curl_slist_append(headers, needs_100_continue ?
 		"Expect: 100-continue" : "Expect:");
 
+	/* Add Accept-Language header */
+	if (rpc->hdr_accept_language)
+		headers = curl_slist_append(headers, rpc->hdr_accept_language);
+
 	/* Add the extra Git-Protocol header */
 	if (rpc->protocol_header)
 		headers = curl_slist_append(headers, rpc->protocol_header);
@@ -1080,6 +1085,8 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
 	strbuf_addf(&buf, "%s%s", url.buf, svc);
 	rpc->service_url = strbuf_detach(&buf, NULL);
 
+	rpc->hdr_accept_language = xstrdup_or_null(http_get_accept_language_header());
+
 	strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
 	rpc->hdr_content_type = strbuf_detach(&buf, NULL);
 
@@ -1118,6 +1125,7 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
 	free(rpc->service_url);
 	free(rpc->hdr_content_type);
 	free(rpc->hdr_accept);
+	free(rpc->hdr_accept_language);
 	free(rpc->protocol_header);
 	free(rpc->buf);
 	strbuf_release(&buf);
@@ -1400,6 +1408,7 @@ static int stateless_connect(const char *service_name)
 	struct discovery *discover;
 	struct rpc_state rpc;
 	struct strbuf buf = STRBUF_INIT;
+	const char *accept_language;
 
 	/*
 	 * Run the info/refs request and see if the server supports protocol

remote-curl.c static int stateless_connect(const char *service_name) · line 1432

Quoted 6 lines
remote-curl.c +3 -0
@@ -1418,6 +1427,9 @@ static int stateless_connect(const char *service_name)
 		printf("\n");
 		fflush(stdout);
 	}
+	accept_language = http_get_accept_language_header();
+	if (accept_language)
+		rpc.hdr_accept_language = xstrfmt("%s", accept_language);

Isn't rpc.hdr_accept_language left uninitialized garbage if accept_language is NULL? It is the same bug I pointed out earlier, whose fix may have to be different.

Has this been tested? I got immediate segfault with this patch in 'seen'.

#9 lore raw
Quoted 10 lines · Junio C Hamano

Review

Quoted 3 lines
(quoted diff) +3 -0
+	accept_language = http_get_accept_language_header();
+	if (accept_language)
+		rpc.hdr_accept_language = xstrfmt("%s", accept_language);

Isn't rpc.hdr_accept_language left uninitialized garbage if accept_language is NULL? It is the same bug I pointed out earlier, whose fix may have to be different.

Has this been tested? I got immediate segfault with this patch in 'seen'.

Having said all that, I wonder if we want to use something like this to make it hard to use an uninitialized data.

The smart-http is quite outside of my area of expertise, and I do not know what Shawn was thinking when de1a2fdd (Smart push over
HTTP: client side, 2009-10-30) was written (it could be that filling all members explicitly was the more prevalent stype back then?). I'd appreciate input from folks who regularly deal with smart-http on the approach.

Thanks.

 remote-curl.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
remote-curl.c +5 -3
index 251d4ee64f..ba6f76a0c1 100644
@@ -608,6 +608,8 @@ struct rpc_state {
 	unsigned flush_read_but_not_sent : 1;
 };
 
+#define RPC_STATE_INIT { 0, }
+
 /*
  * Appends the result of reading from rpc->out to the string represented by
  * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
@@ -1161,7 +1163,7 @@ static int fetch_dumb(int nr_heads, struct ref **to_fetch)
 static int fetch_git(struct discovery *heads,
 	int nr_heads, struct ref **to_fetch)
 {
-	struct rpc_state rpc;
+	struct rpc_state rpc = RPC_STATE_INIT;
 	struct strbuf preamble = STRBUF_INIT;
 	int i, err;
 	struct strvec args = STRVEC_INIT;
@@ -1307,7 +1309,7 @@ static int push_dav(int nr_spec, const char **specs)
 
 static int push_git(struct discovery *heads, int nr_spec, const char **specs)
 {
-	struct rpc_state rpc;
+	struct rpc_state rpc = RPC_STATE_INIT;
 	int i, err;
 	struct strvec args;
 	struct string_list_item *cas_option;
@@ -1406,7 +1408,7 @@ static void parse_push(struct strbuf *buf)
 static int stateless_connect(const char *service_name)
 {
 	struct discovery *discover;
-	struct rpc_state rpc;
+	struct rpc_state rpc = RPC_STATE_INIT;
 	struct strbuf buf = STRBUF_INIT;
 	const char *accept_language;
 
#10 lore raw
Quoted 1 line · Junio C Hamano

+#define RPC_STATE_INIT { 0, }

Make it

	#define RPC_STATE_INIT { 0 }

Curiously, the former form with trailing comma makes sparse unhappy, while it seems that the latter is taken as a special form "idiom".

#11 lore raw

PATCH v4 1/1 remote-curl: send Accept-Language header to server

In reply to Li Linchao via GitGitGadget

From: Li Linchao <[email protected]>

Git server end's ability to accept Accept-Language header was introduced in f18604bbf2 (http: add Accept-Language header if possible, 2015-01-28), but this is only used by very early phase of the transfer, which is HTTP GET request to discover references. For other phases, like POST request in the smart HTTP, the server does not know what language the client speaks.

Teach git client to learn end-user's preferred language and throw accept-language header to the server side. Once the server gets this header, it has the ability to talk to end-user with language they understand. This would be very helpful for many non-English speakers.

---
remote-curl: send Accept-Language header to server

Changes sin v3:

* fix rpc_state initialization issue

Changes since v2:

* free rpc.hdr_accept_language to avoid memory leak
* fix test to reuse language to avoid install new language pack
* reword commit message

Changes since v1:

* change get_accept_language() to http_get_accept_language_header()
* reuse test case in t5550
* reword commit message

TODO: For SSH tranport, give it an environment variable to understand locale language.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1251%2FCactusinhand%2Fllc%2Fsend-Accept-Language-header-to-HTTP-server-v4
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1251/Cactusinhand/llc/send-Accept-Language-header-to-HTTP-server-v4
Pull-Request: https://github.com/gitgitgadget/git/pull/1251

Range-diff vs v3:

1: 99a4e23ceb1 ! 1: 3bec1e85e26 remote-curl: send Accept-Language header to server
@@ Commit message
it has the ability to talk to end-user with language they understand. This would be very helpful for many non-English speakers.

+ Helped-by: Junio C Hamano <[email protected]>

## http.c ##
@@ remote-curl.c: struct rpc_state {
char *protocol_header;
char *buf;
size_t alloc;
+@@ remote-curl.c: struct rpc_state {
+ unsigned flush_read_but_not_sent : 1;
+ };
+
++#define RPC_STATE_INIT { 0 }
++
+ /*
+ * Appends the result of reading from rpc->out to the string represented by
+ * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
@@ remote-curl.c: static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_rece
headers = curl_slist_append(headers, needs_100_continue ?
"Expect: 100-continue" : "Expect:");
@@ remote-curl.c: static int rpc_service(struct rpc_state *rpc, struct discovery *h
free(rpc->protocol_header);
free(rpc->buf);
strbuf_release(&buf);
-@@ remote-curl.c: static int stateless_connect(const char *service_name)
+@@ remote-curl.c: static int fetch_dumb(int nr_heads, struct ref **to_fetch)
+ static int fetch_git(struct discovery *heads,
+ int nr_heads, struct ref **to_fetch)
+ {
+- struct rpc_state rpc;
++ struct rpc_state rpc = RPC_STATE_INIT;
+ struct strbuf preamble = STRBUF_INIT;
+ int i, err;
+ struct strvec args = STRVEC_INIT;
+@@ remote-curl.c: static int push_dav(int nr_spec, const char **specs)
+
+ static int push_git(struct discovery *heads, int nr_spec, const char **specs)
+ {
+- struct rpc_state rpc;
++ struct rpc_state rpc = RPC_STATE_INIT;
+ int i, err;
+ struct strvec args;
+ struct string_list_item *cas_option;
+@@ remote-curl.c: free_specs:
+ static int stateless_connect(const char *service_name)
+ {
struct discovery *discover;
- struct rpc_state rpc;
+- struct rpc_state rpc;
++ struct rpc_state rpc = RPC_STATE_INIT;
struct strbuf buf = STRBUF_INIT;
+ const char *accept_language;

 http.c                      |  4 ++--
 http.h                      |  3 +++
 remote-curl.c               | 21 ++++++++++++++++++---
 t/t5541-http-push-smart.sh  | 19 +++++++++++++++++++
 t/t5550-http-fetch-dumb.sh  |  2 +-
 t/t5551-http-fetch-smart.sh | 10 ++++++++--
 6 files changed, 51 insertions(+), 8 deletions(-)
http.c +2 -2
index 168ca30c558..5d0502f51fd 100644
@@ -1775,7 +1775,7 @@ static void write_accept_language(struct strbuf *buf)
  *   LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1"
  *   LANGUAGE= LANG=C -> ""
  */
-static const char *get_accept_language(void)
+const char *http_get_accept_language_header(void)
 {
 	if (!cached_accept_language) {
 		struct strbuf buf = STRBUF_INIT;
@@ -1829,7 +1829,7 @@ static int http_request(const char *url,
 					 fwrite_buffer);
 	}
 
-	accept_language = get_accept_language();
+	accept_language = http_get_accept_language_header();
 
 	if (accept_language)
 		headers = curl_slist_append(headers, accept_language);
http.h +3 -0
index ba303cfb372..3c94c479100 100644
@@ -178,6 +178,9 @@ int http_fetch_ref(const char *base, struct ref *ref);
 int http_get_info_packs(const char *base_url,
 			struct packed_git **packs_head);
 
+/* Helper for getting Accept-Language header */
+const char *http_get_accept_language_header(void);
+
 struct http_pack_request {
 	char *url;
 
remote-curl.c +18 -3
index 67f178b1120..b8758757ece 100644
@@ -580,6 +580,7 @@ struct rpc_state {
 	char *service_url;
 	char *hdr_content_type;
 	char *hdr_accept;
+	char *hdr_accept_language;
 	char *protocol_header;
 	char *buf;
 	size_t alloc;
@@ -607,6 +608,8 @@ struct rpc_state {
 	unsigned flush_read_but_not_sent : 1;
 };
 
+#define RPC_STATE_INIT { 0 }
+
 /*
  * Appends the result of reading from rpc->out to the string represented by
  * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
@@ -932,6 +935,10 @@ static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_rece
 	headers = curl_slist_append(headers, needs_100_continue ?
 		"Expect: 100-continue" : "Expect:");
 
+	/* Add Accept-Language header */
+	if (rpc->hdr_accept_language)
+		headers = curl_slist_append(headers, rpc->hdr_accept_language);
+
 	/* Add the extra Git-Protocol header */
 	if (rpc->protocol_header)
 		headers = curl_slist_append(headers, rpc->protocol_header);
@@ -1080,6 +1087,8 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
 	strbuf_addf(&buf, "%s%s", url.buf, svc);
 	rpc->service_url = strbuf_detach(&buf, NULL);
 
+	rpc->hdr_accept_language = xstrdup_or_null(http_get_accept_language_header());
+
 	strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
 	rpc->hdr_content_type = strbuf_detach(&buf, NULL);
 
@@ -1118,6 +1127,7 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
 	free(rpc->service_url);
 	free(rpc->hdr_content_type);
 	free(rpc->hdr_accept);
+	free(rpc->hdr_accept_language);
 	free(rpc->protocol_header);
 	free(rpc->buf);
 	strbuf_release(&buf);
@@ -1153,7 +1163,7 @@ static int fetch_dumb(int nr_heads, struct ref **to_fetch)
 static int fetch_git(struct discovery *heads,
 	int nr_heads, struct ref **to_fetch)
 {
-	struct rpc_state rpc;
+	struct rpc_state rpc = RPC_STATE_INIT;
 	struct strbuf preamble = STRBUF_INIT;
 	int i, err;
 	struct strvec args = STRVEC_INIT;
@@ -1299,7 +1309,7 @@ static int push_dav(int nr_spec, const char **specs)
 
 static int push_git(struct discovery *heads, int nr_spec, const char **specs)
 {
-	struct rpc_state rpc;
+	struct rpc_state rpc = RPC_STATE_INIT;
 	int i, err;
 	struct strvec args;
 	struct string_list_item *cas_option;
@@ -1398,8 +1408,9 @@ free_specs:
 static int stateless_connect(const char *service_name)
 {
 	struct discovery *discover;
-	struct rpc_state rpc;
+	struct rpc_state rpc = RPC_STATE_INIT;
 	struct strbuf buf = STRBUF_INIT;
+	const char *accept_language;
 
 	/*
 	 * Run the info/refs request and see if the server supports protocol
@@ -1418,6 +1429,9 @@ static int stateless_connect(const char *service_name)
 		printf("\n");
 		fflush(stdout);
 	}
+	accept_language = http_get_accept_language_header();
+	if (accept_language)
+		rpc.hdr_accept_language = xstrfmt("%s", accept_language);
 
 	rpc.service_name = service_name;
 	rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
@@ -1467,6 +1481,7 @@ static int stateless_connect(const char *service_name)
 	free(rpc.service_url);
 	free(rpc.hdr_content_type);
 	free(rpc.hdr_accept);
+	free(rpc.hdr_accept_language);
 	free(rpc.protocol_header);
 	free(rpc.buf);
 	strbuf_release(&buf);
t/t5541-http-push-smart.sh +19 -0
index 2f09ff4fac6..fbad2d5ff5e 100755
@@ -80,6 +80,25 @@ test_expect_success 'push to remote repository (standard)' '
 	 test $HEAD = $(git rev-parse --verify HEAD))
 '
 
+test_expect_success 'push to remote repository (standard) with sending Accept-Language' '
+	cat >exp <<-\EOF &&
+	=> Send header: Accept-Language: ko-KR, *;q=0.9
+	=> Send header: Accept-Language: ko-KR, *;q=0.9
+	EOF
+
+	cd "$ROOT_PATH"/test_repo_clone &&
+	: >path_lang &&
+	git add path_lang &&
+	test_tick &&
+	git commit -m path_lang &&
+	HEAD=$(git rev-parse --verify HEAD) &&
+	GIT_TRACE_CURL=true LANGUAGE="ko_KR.UTF-8" git push -v -v 2>err &&
+	! grep "Expect: 100-continue" err &&
+
+	grep "=> Send header: Accept-Language:" err >err.language &&
+	test_cmp exp err.language
+'
+
 test_expect_success 'push already up-to-date' '
 	git push
 '
t/t5550-http-fetch-dumb.sh +1 -1
index f0d9cd584d3..bc308519af5 100755
@@ -369,7 +369,7 @@ ja;q=0.95, zh;q=0.94, sv;q=0.93, pt;q=0.92, nb;q=0.91, *;q=0.90" \
 		ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt:nb
 '
 
-test_expect_success 'git client does not send an empty Accept-Language' '
+test_expect_success 'git client send an empty Accept-Language' '
 	GIT_TRACE_CURL=true LANGUAGE= git ls-remote "$HTTPD_URL/dumb/repo.git" 2>stderr &&
 	! grep "^=> Send header: Accept-Language:" stderr
 '
t/t5551-http-fetch-smart.sh +8 -2
index b9351a732f6..245532df881 100755
@@ -31,6 +31,7 @@ test_expect_success 'clone http repository' '
 	> GET /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1
 	> Accept: */*
 	> Accept-Encoding: ENCODINGS
+	> Accept-Language: ko-KR, *;q=0.9
 	> Pragma: no-cache
 	< HTTP/1.1 200 OK
 	< Pragma: no-cache
@@ -40,13 +41,15 @@ test_expect_success 'clone http repository' '
 	> Accept-Encoding: ENCODINGS
 	> Content-Type: application/x-git-upload-pack-request
 	> Accept: application/x-git-upload-pack-result
+	> Accept-Language: ko-KR, *;q=0.9
 	> Content-Length: xxx
 	< HTTP/1.1 200 OK
 	< Pragma: no-cache
 	< Cache-Control: no-cache, max-age=0, must-revalidate
 	< Content-Type: application/x-git-upload-pack-result
 	EOF
-	GIT_TRACE_CURL=true GIT_TEST_PROTOCOL_VERSION=0 \
+
+	GIT_TRACE_CURL=true GIT_TEST_PROTOCOL_VERSION=0 LANGUAGE="ko_KR.UTF-8" \
 		git clone --quiet $HTTPD_URL/smart/repo.git clone 2>err &&
 	test_cmp file clone/file &&
 	tr '\''\015'\'' Q <err |
@@ -94,7 +97,10 @@ test_expect_success 'clone http repository' '
 		test_cmp exp actual.smudged &&
 
 		grep "Accept-Encoding:.*gzip" actual >actual.gzip &&
-		test_line_count = 2 actual.gzip
+		test_line_count = 2 actual.gzip &&
+
+		grep "Accept-Language: ko-KR, *" actual >actual.language &&
+		test_line_count = 2 actual.language
 	fi
 '
 

base-commit: 30cc8d0f147546d4dd77bf497f4dec51e7265bd8

Signature · 1 line
gitgitgadget
#12 lore raw
In reply to Li Linchao via GitGitGadget
Quoted 8 lines · Li Linchao via GitGitGadget

From: Cactusinhand <[email protected]>

Git server end's ability to accept Accept-Language header was introduced in f18604bbf2(http: add Accept-Language header if possible), but this is only used by very early phase of the transfer, that's HTTP GET request to discover references. For other phases, like POST request in the smart HTTP the server side don't know what language client speak.

Quoted 4 lines

This patch teaches git client to learn end-user's preferred language and throw accept-language header to server side. Once server get this header it have ability to talk to end-user with language they understand, this would be very helpful for many non-English speakers.

I may be missing something, but this is just the "Accept-Language" part of this change, i.e. there is no "round-tripping" here of actually doing the work on the server of doing setlocale(), no?

I think the end-goal of having the "remote: " messages translated, if possible, is very worthwhile, but I'd always imagined we'd do that with a protocol extension, because even if we do this with HTTP headers we won't get the same over ssh/git transports.

But then again we don't have protocol v2 push yet :(

So perfect certainly shouldn't be the enemy of the good here, I just wonder what the end-goal is and if there's a plan to get there.

#13 lore raw
Quoted 3 lines · Ævar Arnfjörð Bjarmason

I may be missing something, but this is just the "Accept-Language" part of this change, i.e. there is no "round-tripping" here of actually doing the work on the server of doing setlocale(), no?

I think the wish is that if Accept-Language is interpreted by the HTTP(s) server and it gets turned into LC_something=<locale> when the HTTP server spawns us, we'd know what language to issue our error messages.

#14 lore raw
In reply to Ævar Arnfjörð Bjarmason

Sorry, I mistakenly sent the reply email to you yesterday, I should click "Reply all" button :(

Quoted 11 lines · Li Linchao via GitGitGadget
Quoted 9 lines

From: Cactusinhand <[email protected]>

Git server end's ability to accept Accept-Language header was introduced in f18604bbf2(http: add Accept-Language header if possible), but this is only used by very early phase of the transfer, that's HTTP GET request to discover references. For other phases, like POST request in the smart HTTP the server side don't know what language client speak.

Quoted 8 lines
Quoted 4 lines

This patch teaches git client to learn end-user's preferred language and throw accept-language header to server side. Once server get this header it have ability to talk to end-user with language they understand, this would be very helpful for many non-English speakers.

I may be missing something, but this is just the "Accept-Language" part of this change, i.e. there is no "round-tripping" here of actually doing the work on the server of doing setlocale(), no?

Yes,  here Git just holds this header message, and the actual work depends on the git service providers, like Github, Gitlab, or Gitee.

Quoted 5 lines

I think the end-goal of having the "remote: " messages translated, if possible, is very worthwhile, but I'd always imagined we'd do that with a protocol extension, because even if we do this with HTTP headers we won't get the same over ssh/git transports.

As for ssh transport, can we use ssh environment to reach our goal?

Quoted 5 lines

But then again we don't have protocol v2 push yet :(

So perfect certainly shouldn't be the enemy of the good here, I just wonder what the end-goal is and if there's a plan to get there.

Thanks.

#15 lore raw
Quoted 6 lines · "[email protected]" <[email protected]> writes:
Quoted 4 lines

I think the end-goal of having the "remote: " messages translated, if possible, is very worthwhile, but I'd always imagined we'd do that with a protocol extension, because even if we do this with HTTP headers we won't get the same over ssh/git transports.

As for ssh transport, can we use ssh environment to reach our goal?

Not really. Before forcing us to invent completely separate mechanisms for different transports, it is a very good idea to consider if we can use a single mechanism that can apply to all transports. Adding something at the protocol level would be a step in the right direction.

#16 lore raw
Quoted 7 lines
Quoted 5 lines · "[email protected]" <[email protected]> writes:
Quoted 4 lines

I think the end-goal of having the "remote: " messages translated, if possible, is very worthwhile, but I'd always imagined we'd do that with a protocol extension, because even if we do this with HTTP headers we won't get the same over ssh/git transports.

Quoted 7 lines
Quoted 1 line

As for ssh transport, can we use ssh environment to reach our goal?

Not really.  Before forcing us to invent completely separate mechanisms for different transports, it is a very good idea to consider if we can use a single mechanism that can apply to all transports.  Adding something at the protocol level would be a step in the right direction.

I wonder if we can use a new protocol-capability like local-lang or something else, then Git client and server can tell each other's language ability in the negotiation stage.

#17 lore raw
Quoted 9 lines · On Tue, Jul 05 2022, [email protected] wrote:
Quoted 9 lines
Quoted 6 lines · "[email protected]" <[email protected]> writes:
Quoted 4 lines

I think the end-goal of having the "remote: " messages translated, if possible, is very worthwhile, but I'd always imagined we'd do that with a protocol extension, because even if we do this with HTTP headers we won't get the same over ssh/git transports.

As for ssh transport, can we use ssh environment to reach our goal?

Quoted 8 lines
Quoted 5 lines

Not really.  Before forcing us to invent completely separate mechanisms for different transports, it is a very good idea to consider if we can use a single mechanism that can apply to all transports.  Adding something at the protocol level would be a step in the right direction.

I wonder if we can use a new protocol-capability like local-lang or something else, then Git client and server can tell each other's language ability in the negotiation stage.

It would make sense to call this protocol verb "setenv", and just give it support for setting arbitrary remote environment, which we'd then have a whitelist configuration variable for, similar to how sshd(1) does it.

Or maybe we can just add this as a "capability", which seems like a more natural fit, we could even stick it into "agent" I guess...

Then it becomes a very thin layer for emulating what we already get with the ssh and http(s) transport(s).

Anyway, while it definitely would be an improvement to pass this along, a much better way to go IMO (but also harder) is to extend the protocol so that we don't a emita human language at all, but emit defined error states for our various known errors.

I have some WIP incomplete patches in that direction somewhere (that I haven't dug up now), and part of it is currently stalled on there being no "push" support in protocol v2.

But if we can get to that point it would make for much better UX, even if you get e.g. "git push" errors in your language now your editor/IDE is probably needing to spew terminal "git push" output at you as-is, it would make for better UX if it could just render it as it prefers to.

This would also mean you'd get translations even if the server doesn't have the needed *.po files.

It *isn't* a full replacement, e.g. if you have a custom hook having the locale is still useful, but for anything that's git native...

FWIW I think the WIP patches I'm referencing were only to upload-pack.c, i.e. to make various parts where it calls die() send over ERR packet(s) instead, and either pick that up magically on the client-side, or add a new ERR_CODE packet or whatever (I can't remember...).

#18 lore raw
Quoted 4 lines · Ævar Arnfjörð Bjarmason

It would make sense to call this protocol verb "setenv", and just give it support for setting arbitrary remote environment, which we'd then have a whitelist configuration variable for, similar to how sshd(1) does it.

Sounds like a security hole in the making, with dubious risk-benefit tradeoff. I cannot honestly answer favourably to this question if somebody asked me as the project read: Are you solving a real-world problem, or creating one?

Quoted 2 lines

Or maybe we can just add this as a "capability", which seems like a more natural fit,

I took your suggestion upthread to be hinting this.

Quoted 1 line

we could even stick it into "agent" I guess...

But not this.

Quoted 4 lines

Anyway, while it definitely would be an improvement to pass this along, a much better way to go IMO (but also harder) is to extend the protocol so that we don't a emita human language at all, but emit defined error states for our various known errors.

I would not go there.

The end that sends errors in status code may be running a newer version of the software and the particular status code it sent is so new that the receiving end does not know how to translate it into human language.

Doing the Accept-Language at the HTTP level, or its equivalent at the protocol-capability level, has the opposite problem that the remote end may not know the requested language at all, but at least the side that sends unlocalized messages is aware of it doing so.

Also the error message sideband carries the same messages that are meant to be read by humans in Git subcommands that are run by the protocol software as well as human users. We could introduce such a "error status code" language as an artificial locale, translate _("...") messages into such "status code language" on the end that sends errors, and then re-translate them into human language locale, but it is of a dubious value. Such an approach would not work well at the gettext layer anyway, as we need to deal with placeholders, so it would be a lot more involved than just "lets have catalog of printf formatting templates and translate them".

#19 lore raw
In reply to [email protected]
Quoted 3 lines · "[email protected]" <[email protected]> writes:

I wonder if we can use a new protocol-capability like local-lang or something else, then Git client and server can tell each other's language ability in the negotiation stage.

That is how I read Ævar's message you were responding to.

Having said that, a mechanism that applies only to HTTP transport was already there, and the patch in question recognises that the coverage of the mechanism is incomplete even within the HTTP codepath and attempts to make it more complete. Unless we are planning to deprecate the HTTP specific mechanism and replace it with protocol-capability to cover other protocols in the same way, I do not think it is a bad idea to have it.

Thanks.