http: don't always add Git-Protocol header

Instead of always sending the Git-Protocol header with the configured version with every http request, explicitly send it when discovering refs and then only send it on subsequent http requests if the server understood the version requested. Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Williams committed Mar 15, 2018 at 10:31 UTC 884e586f9ef46406263720523377298a90b41065
2 files changed +33 -17
http.c
-17
@@ -904,21 +904,6 @@ static void set_from_env(const char **var, const char *envname)
904 *var = val;
905 }
906
907 -static void protocol_http_header(void)
908 -{
909 - if (get_protocol_version_config() > 0) {
910 - struct strbuf protocol_header = STRBUF_INIT;
911 -
912 - strbuf_addf(&protocol_header, GIT_PROTOCOL_HEADER ": version=%d",
913 - get_protocol_version_config());
914 -
915 -
916 - extra_http_headers = curl_slist_append(extra_http_headers,
917 - protocol_header.buf);
918 - strbuf_release(&protocol_header);
919 - }
920 -}
921 -
907 void http_init(struct remote *remote, const char *url, int proactive_auth)
908 {
909 char *low_speed_limit;
@@ -949,8 +934,6 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
934 if (remote)
935 var_override(&http_proxy_authmethod, remote->http_proxy_authmethod);
936
952 - protocol_http_header();
953 -
937 pragma_header = curl_slist_append(http_copy_default_headers(),
938 "Pragma: no-cache");
939 no_pragma_header = curl_slist_append(http_copy_default_headers(),
remote-curl.c
+33
@@ -291,6 +291,19 @@ static int show_http_message(struct strbuf *type, struct strbuf *charset,
291 return 0;
292 }
293
294 +static int get_protocol_http_header(enum protocol_version version,
295 + struct strbuf *header)
296 +{
297 + if (version > 0) {
298 + strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
299 + version);
300 +
301 + return 1;
302 + }
303 +
304 + return 0;
305 +}
306 +
307 static struct discovery *discover_refs(const char *service, int for_push)
308 {
309 struct strbuf exp = STRBUF_INIT;
@@ -299,6 +312,8 @@ static struct discovery *discover_refs(const char *service, int for_push)
312 struct strbuf buffer = STRBUF_INIT;
313 struct strbuf refs_url = STRBUF_INIT;
314 struct strbuf effective_url = STRBUF_INIT;
315 + struct strbuf protocol_header = STRBUF_INIT;
316 + struct string_list extra_headers = STRING_LIST_INIT_DUP;
317 struct discovery *last = last_discovery;
318 int http_ret, maybe_smart = 0;
319 struct http_get_options http_options;
@@ -318,11 +333,16 @@ static struct discovery *discover_refs(const char *service, int for_push)
333 strbuf_addf(&refs_url, "service=%s", service);
334 }
335
336 + /* Add the extra Git-Protocol header */
337 + if (get_protocol_http_header(get_protocol_version_config(), &protocol_header))
338 + string_list_append(&extra_headers, protocol_header.buf);
339 +
340 memset(&http_options, 0, sizeof(http_options));
341 http_options.content_type = &type;
342 http_options.charset = &charset;
343 http_options.effective_url = &effective_url;
344 http_options.base_url = &url;
345 + http_options.extra_headers = &extra_headers;
346 http_options.initial_request = 1;
347 http_options.no_cache = 1;
348 http_options.keep_error = 1;
@@ -389,6 +409,8 @@ static struct discovery *discover_refs(const char *service, int for_push)
409 strbuf_release(&charset);
410 strbuf_release(&effective_url);
411 strbuf_release(&buffer);
412 + strbuf_release(&protocol_header);
413 + string_list_clear(&extra_headers, 0);
414 last_discovery = last;
415 return last;
416 }
@@ -425,6 +447,7 @@ struct rpc_state {
447 char *service_url;
448 char *hdr_content_type;
449 char *hdr_accept;
450 + char *protocol_header;
451 char *buf;
452 size_t alloc;
453 size_t len;
@@ -611,6 +634,10 @@ static int post_rpc(struct rpc_state *rpc)
634 headers = curl_slist_append(headers, needs_100_continue ?
635 "Expect: 100-continue" : "Expect:");
636
637 + /* Add the extra Git-Protocol header */
638 + if (rpc->protocol_header)
639 + headers = curl_slist_append(headers, rpc->protocol_header);
640 +
641 retry:
642 slot = get_active_slot();
643
@@ -751,6 +778,11 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
778 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
779 rpc->hdr_accept = strbuf_detach(&buf, NULL);
780
781 + if (get_protocol_http_header(heads->version, &buf))
782 + rpc->protocol_header = strbuf_detach(&buf, NULL);
783 + else
784 + rpc->protocol_header = NULL;
785 +
786 while (!err) {
787 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
788 if (!n)
@@ -778,6 +810,7 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
810 free(rpc->service_url);
811 free(rpc->hdr_content_type);
812 free(rpc->hdr_accept);
813 + free(rpc->protocol_header);
814 free(rpc->buf);
815 strbuf_release(&buf);
816 return err;