git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] remote-curl: send Accept-Language header to server
@ 2022-06-08  8:53 Li Linchao via GitGitGadget
  2022-06-08 23:32 ` Junio C Hamano
                   ` (2 more replies)
  0 siblings, 3 replies; 20+ messages in thread
From: Li Linchao via GitGitGadget @ 2022-06-08  8:53 UTC (permalink / raw)
  To: git; +Cc: Jonathan Tan, Johannes Schindelin, Li Linchao, Cactusinhand

From: Cactusinhand <lilinchao@oschina.cn>

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.

Signed-off-by: Li Linchao <lilinchao@oschina.cn>
---
    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(-)

diff --git a/http.c b/http.c
index 11c6f69facd..0654e111d1d 100644
--- a/http.c
+++ b/http.c
@@ -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;
diff --git a/http.h b/http.h
index ba303cfb372..c5039a0208e 100644
--- a/http.h
+++ b/http.h
@@ -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;
 
diff --git a/remote-curl.c b/remote-curl.c
index 67f178b1120..8acf506705c 100644
--- a/remote-curl.c
+++ b/remote-curl.c
@@ -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);
diff --git a/t/t5541-http-push-smart.sh b/t/t5541-http-push-smart.sh
index 2f09ff4fac6..4288a279e9e 100755
--- a/t/t5541-http-push-smart.sh
+++ b/t/t5541-http-push-smart.sh
@@ -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
 '
diff --git a/t/t5550-http-fetch-dumb.sh b/t/t5550-http-fetch-dumb.sh
index f0d9cd584d3..bc308519af5 100755
--- a/t/t5550-http-fetch-dumb.sh
+++ b/t/t5550-http-fetch-dumb.sh
@@ -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
 '
diff --git a/t/t5551-http-fetch-smart.sh b/t/t5551-http-fetch-smart.sh
index b9351a732f6..6f65131a4e4 100755
--- a/t/t5551-http-fetch-smart.sh
+++ b/t/t5551-http-fetch-smart.sh
@@ -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
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH] remote-curl: send Accept-Language header to server
  2022-06-08  8:53 [PATCH] remote-curl: send Accept-Language header to server Li Linchao via GitGitGadget
@ 2022-06-08 23:32 ` Junio C Hamano
  2022-06-09  6:35 ` [PATCH v2] " Li Linchao via GitGitGadget
  2022-06-09  7:30 ` [PATCH] " Ævar Arnfjörð Bjarmason
  2 siblings, 0 replies; 20+ messages in thread
From: Junio C Hamano @ 2022-06-08 23:32 UTC (permalink / raw)
  To: Li Linchao via GitGitGadget
  Cc: git, Jonathan Tan, Johannes Schindelin, Li Linchao

"Li Linchao via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Cactusinhand <lilinchao@oschina.cn>

Huh? 

> 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".

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

"This patch teaches" -> "Teach"

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

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

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

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

", this" -> ". This"

> would be very helpful for many non-English speakers.
>
> Signed-off-by: Li Linchao <lilinchao@oschina.cn>

> diff --git a/http.c b/http.c
> index 11c6f69facd..0654e111d1d 100644
> --- a/http.c
> +++ b/http.c
> @@ -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"?

> diff --git a/remote-curl.c b/remote-curl.c
> index 67f178b1120..8acf506705c 100644
> --- a/remote-curl.c
> +++ b/remote-curl.c
> @@ -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:

>  	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 +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.

> +	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.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v2] remote-curl: send Accept-Language header to server
  2022-06-08  8:53 [PATCH] remote-curl: send Accept-Language header to server Li Linchao via GitGitGadget
  2022-06-08 23:32 ` Junio C Hamano
@ 2022-06-09  6:35 ` Li Linchao via GitGitGadget
  2022-06-09 23:55   ` Junio C Hamano
  2022-06-12 17:20   ` [PATCH v3] " Li Linchao via GitGitGadget
  2022-06-09  7:30 ` [PATCH] " Ævar Arnfjörð Bjarmason
  2 siblings, 2 replies; 20+ messages in thread
From: Li Linchao via GitGitGadget @ 2022-06-09  6:35 UTC (permalink / raw)
  To: git; +Cc: Jonathan Tan, Johannes Schindelin, Li Linchao, Li Linchao

From: Li Linchao <lilinchao@oschina.cn>

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.

Signed-off-by: Li Linchao <lilinchao@oschina.cn>
---
    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 <lilinchao@oschina.cn>
     +Author: Li Linchao <lilinchao@oschina.cn>
      
       ## 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 <lilinchao@oschina.cn>
      
     @@ 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(-)

diff --git a/http.c b/http.c
index 11c6f69facd..33301d8d5d5 100644
--- a/http.c
+++ b/http.c
@@ -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);
diff --git a/http.h b/http.h
index ba303cfb372..3c94c479100 100644
--- a/http.h
+++ b/http.h
@@ -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;
 
diff --git a/remote-curl.c b/remote-curl.c
index 67f178b1120..504bbdedbda 100644
--- a/remote-curl.c
+++ b/remote-curl.c
@@ -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);
diff --git a/t/t5541-http-push-smart.sh b/t/t5541-http-push-smart.sh
index 2f09ff4fac6..4288a279e9e 100755
--- a/t/t5541-http-push-smart.sh
+++ b/t/t5541-http-push-smart.sh
@@ -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
 '
diff --git a/t/t5550-http-fetch-dumb.sh b/t/t5550-http-fetch-dumb.sh
index f0d9cd584d3..42dd9fe2af7 100755
--- a/t/t5550-http-fetch-dumb.sh
+++ b/t/t5550-http-fetch-dumb.sh
@@ -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
 '
diff --git a/t/t5551-http-fetch-smart.sh b/t/t5551-http-fetch-smart.sh
index b9351a732f6..6f65131a4e4 100755
--- a/t/t5551-http-fetch-smart.sh
+++ b/t/t5551-http-fetch-smart.sh
@@ -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
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH] remote-curl: send Accept-Language header to server
  2022-06-08  8:53 [PATCH] remote-curl: send Accept-Language header to server Li Linchao via GitGitGadget
  2022-06-08 23:32 ` Junio C Hamano
  2022-06-09  6:35 ` [PATCH v2] " Li Linchao via GitGitGadget
@ 2022-06-09  7:30 ` Ævar Arnfjörð Bjarmason
  2022-06-09 17:34   ` Junio C Hamano
  2022-06-10  2:38   ` lilinchao
  2 siblings, 2 replies; 20+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-06-09  7:30 UTC (permalink / raw)
  To: Li Linchao via GitGitGadget
  Cc: git, Jonathan Tan, Johannes Schindelin, Cactusinhand


On Wed, Jun 08 2022, Li Linchao via GitGitGadget wrote:

> From: Cactusinhand <lilinchao@oschina.cn>
>
> 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.

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.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH] remote-curl: send Accept-Language header to server
  2022-06-09  7:30 ` [PATCH] " Ævar Arnfjörð Bjarmason
@ 2022-06-09 17:34   ` Junio C Hamano
  2022-06-10  2:38   ` lilinchao
  1 sibling, 0 replies; 20+ messages in thread
From: Junio C Hamano @ 2022-06-09 17:34 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: Li Linchao via GitGitGadget, git, Jonathan Tan,
	Johannes Schindelin, Cactusinhand

Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:

> 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.



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v2] remote-curl: send Accept-Language header to server
  2022-06-09  6:35 ` [PATCH v2] " Li Linchao via GitGitGadget
@ 2022-06-09 23:55   ` Junio C Hamano
  2022-06-10  3:49     ` lilinchao
  2022-06-12 17:20   ` [PATCH v3] " Li Linchao via GitGitGadget
  1 sibling, 1 reply; 20+ messages in thread
From: Junio C Hamano @ 2022-06-09 23:55 UTC (permalink / raw)
  To: Li Linchao via GitGitGadget
  Cc: git, Jonathan Tan, Johannes Schindelin, Li Linchao

"Li Linchao via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Li Linchao <lilinchao@oschina.cn>
>
> 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.

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

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

> 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" 

>  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.

> diff --git a/http.h b/http.h
> index ba303cfb372..3c94c479100 100644
> --- a/http.h
> +++ b/http.h
> @@ -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.

> @@ -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.

> +	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.

> +	}
> +

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.

>  	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);

And this is in line with that memory ownership model.

>  	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?

> diff --git a/t/t5541-http-push-smart.sh b/t/t5541-http-push-smart.sh
> index 2f09ff4fac6..4288a279e9e 100755
> --- a/t/t5541-http-push-smart.sh
> +++ b/t/t5541-http-push-smart.sh
> @@ -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.

> +	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.

> +	! 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.

> diff --git a/t/t5550-http-fetch-dumb.sh b/t/t5550-http-fetch-dumb.sh
> diff --git a/t/t5551-http-fetch-smart.sh b/t/t5551-http-fetch-smart.sh


Thanks.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Re: [PATCH] remote-curl: send Accept-Language header to server
  2022-06-09  7:30 ` [PATCH] " Ævar Arnfjörð Bjarmason
  2022-06-09 17:34   ` Junio C Hamano
@ 2022-06-10  2:38   ` lilinchao
  2022-07-03  0:57     ` Junio C Hamano
  1 sibling, 1 reply; 20+ messages in thread
From: lilinchao @ 2022-06-10  2:38 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason,
	Li Linchao via GitGitGadget; +Cc: git

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

>
>On Wed, Jun 08 2022, Li Linchao via GitGitGadget wrote:
>
>> From: Cactusinhand <lilinchao@oschina.cn>
>>
>> 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.
>
>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. 
>
>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?
>
>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.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Re: [PATCH v2] remote-curl: send Accept-Language header to server
  2022-06-09 23:55   ` Junio C Hamano
@ 2022-06-10  3:49     ` lilinchao
  2022-06-10  4:22       ` lilinchao
  0 siblings, 1 reply; 20+ messages in thread
From: lilinchao @ 2022-06-10  3:49 UTC (permalink / raw)
  To: Junio C Hamano, Li Linchao via GitGitGadget; +Cc: git, Jonathan Tan, dscho

>"Li Linchao via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
>> From: Li Linchao <lilinchao@oschina.cn>
>>
>> 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.
> 
OK, thanks for reminding.
>> only used by very early phase of the transfer, that's HTTP GET request to
>
>"that's" -> "which is", probably. 
OK.
>
>> 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"
>
>>  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. 
My bad, the updated test in t5550 can not test the updated code, but test 
the original code in f18604bb.
>
>> diff --git a/http.h b/http.h
>> index ba303cfb372..3c94c479100 100644
>> --- a/http.h
>> +++ b/http.h
>> @@ -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.
>
>> @@ -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.
>
>> +	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.
>
>> +	}
>> +
>
>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.
>
>>  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);
>
>And this is in line with that memory ownership model.
>
>>  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? 
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);

>
>> diff --git a/t/t5541-http-push-smart.sh b/t/t5541-http-push-smart.sh
>> index 2f09ff4fac6..4288a279e9e 100755
>> --- a/t/t5541-http-push-smart.sh
>> +++ b/t/t5541-http-push-smart.sh
>> @@ -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. 
OK. 
>
>> +	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. 
OK.
>
>> +	! 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.
>
>> diff --git a/t/t5550-http-fetch-dumb.sh b/t/t5550-http-fetch-dumb.sh
>> diff --git a/t/t5551-http-fetch-smart.sh b/t/t5551-http-fetch-smart.sh 
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.
>
>
>Thanks.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Re: [PATCH v2] remote-curl: send Accept-Language header to server
  2022-06-10  3:49     ` lilinchao
@ 2022-06-10  4:22       ` lilinchao
  0 siblings, 0 replies; 20+ messages in thread
From: lilinchao @ 2022-06-10  4:22 UTC (permalink / raw)
  To: Junio C Hamano, Li Linchao via GitGitGadget; +Cc: git


>>As I already said, I do not think changes to the following two tests
>>are warranted.
>>
>>> diff --git a/t/t5550-http-fetch-dumb.sh b/t/t5550-http-fetch-dumb.sh
>>> diff --git a/t/t5551-http-fetch-smart.sh b/t/t5551-http-fetch-smart.sh
>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'
>>
>>
>>Thanks.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v3] remote-curl: send Accept-Language header to server
  2022-06-09  6:35 ` [PATCH v2] " Li Linchao via GitGitGadget
  2022-06-09 23:55   ` Junio C Hamano
@ 2022-06-12 17:20   ` Li Linchao via GitGitGadget
  2022-06-13 18:15     ` Junio C Hamano
                       ` (2 more replies)
  1 sibling, 3 replies; 20+ messages in thread
From: Li Linchao via GitGitGadget @ 2022-06-12 17:20 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Johannes Schindelin,
	Ævar Arnfjörð Bjarmason, Li Linchao, Li Linchao

From: Li Linchao <lilinchao@oschina.cn>

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.

Signed-off-by: Li Linchao <lilinchao@oschina.cn>
---
    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(-)

diff --git a/http.c b/http.c
index 11c6f69facd..33301d8d5d5 100644
--- a/http.c
+++ b/http.c
@@ -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);
diff --git a/http.h b/http.h
index ba303cfb372..3c94c479100 100644
--- a/http.h
+++ b/http.h
@@ -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;
 
diff --git a/remote-curl.c b/remote-curl.c
index 67f178b1120..251d4ee64f6 100644
--- a/remote-curl.c
+++ b/remote-curl.c
@@ -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);
diff --git a/t/t5541-http-push-smart.sh b/t/t5541-http-push-smart.sh
index 2f09ff4fac6..fbad2d5ff5e 100755
--- a/t/t5541-http-push-smart.sh
+++ b/t/t5541-http-push-smart.sh
@@ -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
 '
diff --git a/t/t5550-http-fetch-dumb.sh b/t/t5550-http-fetch-dumb.sh
index f0d9cd584d3..bc308519af5 100755
--- a/t/t5550-http-fetch-dumb.sh
+++ b/t/t5550-http-fetch-dumb.sh
@@ -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
 '
diff --git a/t/t5551-http-fetch-smart.sh b/t/t5551-http-fetch-smart.sh
index b9351a732f6..245532df881 100755
--- a/t/t5551-http-fetch-smart.sh
+++ b/t/t5551-http-fetch-smart.sh
@@ -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
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH v3] remote-curl: send Accept-Language header to server
  2022-06-12 17:20   ` [PATCH v3] " Li Linchao via GitGitGadget
@ 2022-06-13 18:15     ` Junio C Hamano
  2022-06-13 21:32     ` Junio C Hamano
  2022-07-11  5:58     ` [PATCH v4] " Li Linchao via GitGitGadget
  2 siblings, 0 replies; 20+ messages in thread
From: Junio C Hamano @ 2022-06-13 18:15 UTC (permalink / raw)
  To: Li Linchao via GitGitGadget
  Cc: git, Jonathan Tan, Johannes Schindelin,
	Ævar Arnfjörð Bjarmason, Li Linchao

"Li Linchao via GitGitGadget" <gitgitgadget@gmail.com> writes:

> 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.


OK.

>      -+	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.

>      +@@ 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.

>      +@@ 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.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v3] remote-curl: send Accept-Language header to server
  2022-06-12 17:20   ` [PATCH v3] " Li Linchao via GitGitGadget
  2022-06-13 18:15     ` Junio C Hamano
@ 2022-06-13 21:32     ` Junio C Hamano
  2022-06-13 22:08       ` Junio C Hamano
  2022-07-11  5:58     ` [PATCH v4] " Li Linchao via GitGitGadget
  2 siblings, 1 reply; 20+ messages in thread
From: Junio C Hamano @ 2022-06-13 21:32 UTC (permalink / raw)
  To: Li Linchao via GitGitGadget
  Cc: git, Jonathan Tan, Johannes Schindelin,
	Ævar Arnfjörð Bjarmason, Li Linchao

"Li Linchao via GitGitGadget" <gitgitgadget@gmail.com> writes:

> @@ -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);

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'.


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v3] remote-curl: send Accept-Language header to server
  2022-06-13 21:32     ` Junio C Hamano
@ 2022-06-13 22:08       ` Junio C Hamano
  2022-06-13 22:15         ` Junio C Hamano
  0 siblings, 1 reply; 20+ messages in thread
From: Junio C Hamano @ 2022-06-13 22:08 UTC (permalink / raw)
  To: Li Linchao via GitGitGadget
  Cc: git, Jonathan Tan, Johannes Schindelin,
	Ævar Arnfjörð Bjarmason, Li Linchao

Junio C Hamano <gitster@pobox.com> writes:

>> +	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(-)

diff --git i/remote-curl.c w/remote-curl.c
index 251d4ee64f..ba6f76a0c1 100644
--- i/remote-curl.c
+++ w/remote-curl.c
@@ -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;
 

^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH v3] remote-curl: send Accept-Language header to server
  2022-06-13 22:08       ` Junio C Hamano
@ 2022-06-13 22:15         ` Junio C Hamano
  0 siblings, 0 replies; 20+ messages in thread
From: Junio C Hamano @ 2022-06-13 22:15 UTC (permalink / raw)
  To: Li Linchao via GitGitGadget
  Cc: git, Jonathan Tan, Johannes Schindelin,
	Ævar Arnfjörð Bjarmason, Li Linchao

Junio C Hamano <gitster@pobox.com> writes:

> +#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".

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH] remote-curl: send Accept-Language header to server
  2022-06-10  2:38   ` lilinchao
@ 2022-07-03  0:57     ` Junio C Hamano
  2022-07-05 10:06       ` lilinchao
  0 siblings, 1 reply; 20+ messages in thread
From: Junio C Hamano @ 2022-07-03  0:57 UTC (permalink / raw)
  To: lilinchao
  Cc: Ævar Arnfjörð Bjarmason,
	Li Linchao via GitGitGadget, git

"lilinchao@oschina.cn" <lilinchao@oschina.cn> writes:

>>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.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Re: [PATCH] remote-curl: send Accept-Language header to server
  2022-07-03  0:57     ` Junio C Hamano
@ 2022-07-05 10:06       ` lilinchao
  2022-07-05 10:15         ` Ævar Arnfjörð Bjarmason
  2022-07-05 17:53         ` Junio C Hamano
  0 siblings, 2 replies; 20+ messages in thread
From: lilinchao @ 2022-07-05 10:06 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Ævar Arnfjörð Bjarmason,
	Li Linchao via GitGitGadget, git

>"lilinchao@oschina.cn" <lilinchao@oschina.cn> writes:
>
>>>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.
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.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH] remote-curl: send Accept-Language header to server
  2022-07-05 10:06       ` lilinchao
@ 2022-07-05 10:15         ` Ævar Arnfjörð Bjarmason
  2022-07-05 18:06           ` Junio C Hamano
  2022-07-05 17:53         ` Junio C Hamano
  1 sibling, 1 reply; 20+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-07-05 10:15 UTC (permalink / raw)
  To: lilinchao@oschina.cn; +Cc: Junio C Hamano, Li Linchao via GitGitGadget, git


On Tue, Jul 05 2022, lilinchao@oschina.cn wrote:

>>"lilinchao@oschina.cn" <lilinchao@oschina.cn> writes:
>>
>>>>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.
> 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...).

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH] remote-curl: send Accept-Language header to server
  2022-07-05 10:06       ` lilinchao
  2022-07-05 10:15         ` Ævar Arnfjörð Bjarmason
@ 2022-07-05 17:53         ` Junio C Hamano
  1 sibling, 0 replies; 20+ messages in thread
From: Junio C Hamano @ 2022-07-05 17:53 UTC (permalink / raw)
  To: lilinchao
  Cc: Ævar Arnfjörð Bjarmason,
	Li Linchao via GitGitGadget, git

"lilinchao@oschina.cn" <lilinchao@oschina.cn> 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.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH] remote-curl: send Accept-Language header to server
  2022-07-05 10:15         ` Ævar Arnfjörð Bjarmason
@ 2022-07-05 18:06           ` Junio C Hamano
  0 siblings, 0 replies; 20+ messages in thread
From: Junio C Hamano @ 2022-07-05 18:06 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: lilinchao@oschina.cn, Li Linchao via GitGitGadget, git

Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:

> 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?

> 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.

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

But not this.

> 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".

^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v4] remote-curl: send Accept-Language header to server
  2022-06-12 17:20   ` [PATCH v3] " Li Linchao via GitGitGadget
  2022-06-13 18:15     ` Junio C Hamano
  2022-06-13 21:32     ` Junio C Hamano
@ 2022-07-11  5:58     ` Li Linchao via GitGitGadget
  2 siblings, 0 replies; 20+ messages in thread
From: Li Linchao via GitGitGadget @ 2022-07-11  5:58 UTC (permalink / raw)
  To: git
  Cc: Jonathan Tan, Johannes Schindelin,
	Ævar Arnfjörð Bjarmason, Li Linchao, Li Linchao

From: Li Linchao <lilinchao@oschina.cn>

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.

Helped-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Li Linchao <lilinchao@oschina.cn>
---
    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 <gitster@pobox.com>
          Signed-off-by: Li Linchao <lilinchao@oschina.cn>
      
       ## 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(-)

diff --git a/http.c b/http.c
index 168ca30c558..5d0502f51fd 100644
--- a/http.c
+++ b/http.c
@@ -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);
diff --git a/http.h b/http.h
index ba303cfb372..3c94c479100 100644
--- a/http.h
+++ b/http.h
@@ -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;
 
diff --git a/remote-curl.c b/remote-curl.c
index 67f178b1120..b8758757ece 100644
--- a/remote-curl.c
+++ b/remote-curl.c
@@ -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);
diff --git a/t/t5541-http-push-smart.sh b/t/t5541-http-push-smart.sh
index 2f09ff4fac6..fbad2d5ff5e 100755
--- a/t/t5541-http-push-smart.sh
+++ b/t/t5541-http-push-smart.sh
@@ -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
 '
diff --git a/t/t5550-http-fetch-dumb.sh b/t/t5550-http-fetch-dumb.sh
index f0d9cd584d3..bc308519af5 100755
--- a/t/t5550-http-fetch-dumb.sh
+++ b/t/t5550-http-fetch-dumb.sh
@@ -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
 '
diff --git a/t/t5551-http-fetch-smart.sh b/t/t5551-http-fetch-smart.sh
index b9351a732f6..245532df881 100755
--- a/t/t5551-http-fetch-smart.sh
+++ b/t/t5551-http-fetch-smart.sh
@@ -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
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2022-07-11  5:59 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08  8:53 [PATCH] remote-curl: send Accept-Language header to server Li Linchao via GitGitGadget
2022-06-08 23:32 ` Junio C Hamano
2022-06-09  6:35 ` [PATCH v2] " Li Linchao via GitGitGadget
2022-06-09 23:55   ` Junio C Hamano
2022-06-10  3:49     ` lilinchao
2022-06-10  4:22       ` lilinchao
2022-06-12 17:20   ` [PATCH v3] " Li Linchao via GitGitGadget
2022-06-13 18:15     ` Junio C Hamano
2022-06-13 21:32     ` Junio C Hamano
2022-06-13 22:08       ` Junio C Hamano
2022-06-13 22:15         ` Junio C Hamano
2022-07-11  5:58     ` [PATCH v4] " Li Linchao via GitGitGadget
2022-06-09  7:30 ` [PATCH] " Ævar Arnfjörð Bjarmason
2022-06-09 17:34   ` Junio C Hamano
2022-06-10  2:38   ` lilinchao
2022-07-03  0:57     ` Junio C Hamano
2022-07-05 10:06       ` lilinchao
2022-07-05 10:15         ` Ævar Arnfjörð Bjarmason
2022-07-05 18:06           ` Junio C Hamano
2022-07-05 17:53         ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).