Skip to content

Commit 884e586

Browse files
bmwillgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8ff14ed commit 884e586

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

http.c

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -904,21 +904,6 @@ static void set_from_env(const char **var, const char *envname)
904904
*var = val;
905905
}
906906

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-
922907
void http_init(struct remote *remote, const char *url, int proactive_auth)
923908
{
924909
char *low_speed_limit;
@@ -949,8 +934,6 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
949934
if (remote)
950935
var_override(&http_proxy_authmethod, remote->http_proxy_authmethod);
951936

952-
protocol_http_header();
953-
954937
pragma_header = curl_slist_append(http_copy_default_headers(),
955938
"Pragma: no-cache");
956939
no_pragma_header = curl_slist_append(http_copy_default_headers(),

remote-curl.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,19 @@ static int show_http_message(struct strbuf *type, struct strbuf *charset,
291291
return 0;
292292
}
293293

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+
294307
static struct discovery *discover_refs(const char *service, int for_push)
295308
{
296309
struct strbuf exp = STRBUF_INIT;
@@ -299,6 +312,8 @@ static struct discovery *discover_refs(const char *service, int for_push)
299312
struct strbuf buffer = STRBUF_INIT;
300313
struct strbuf refs_url = STRBUF_INIT;
301314
struct strbuf effective_url = STRBUF_INIT;
315+
struct strbuf protocol_header = STRBUF_INIT;
316+
struct string_list extra_headers = STRING_LIST_INIT_DUP;
302317
struct discovery *last = last_discovery;
303318
int http_ret, maybe_smart = 0;
304319
struct http_get_options http_options;
@@ -318,11 +333,16 @@ static struct discovery *discover_refs(const char *service, int for_push)
318333
strbuf_addf(&refs_url, "service=%s", service);
319334
}
320335

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+
321340
memset(&http_options, 0, sizeof(http_options));
322341
http_options.content_type = &type;
323342
http_options.charset = &charset;
324343
http_options.effective_url = &effective_url;
325344
http_options.base_url = &url;
345+
http_options.extra_headers = &extra_headers;
326346
http_options.initial_request = 1;
327347
http_options.no_cache = 1;
328348
http_options.keep_error = 1;
@@ -389,6 +409,8 @@ static struct discovery *discover_refs(const char *service, int for_push)
389409
strbuf_release(&charset);
390410
strbuf_release(&effective_url);
391411
strbuf_release(&buffer);
412+
strbuf_release(&protocol_header);
413+
string_list_clear(&extra_headers, 0);
392414
last_discovery = last;
393415
return last;
394416
}
@@ -425,6 +447,7 @@ struct rpc_state {
425447
char *service_url;
426448
char *hdr_content_type;
427449
char *hdr_accept;
450+
char *protocol_header;
428451
char *buf;
429452
size_t alloc;
430453
size_t len;
@@ -611,6 +634,10 @@ static int post_rpc(struct rpc_state *rpc)
611634
headers = curl_slist_append(headers, needs_100_continue ?
612635
"Expect: 100-continue" : "Expect:");
613636

637+
/* Add the extra Git-Protocol header */
638+
if (rpc->protocol_header)
639+
headers = curl_slist_append(headers, rpc->protocol_header);
640+
614641
retry:
615642
slot = get_active_slot();
616643

@@ -751,6 +778,11 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
751778
strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
752779
rpc->hdr_accept = strbuf_detach(&buf, NULL);
753780

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+
754786
while (!err) {
755787
int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
756788
if (!n)
@@ -778,6 +810,7 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
778810
free(rpc->service_url);
779811
free(rpc->hdr_content_type);
780812
free(rpc->hdr_accept);
813+
free(rpc->protocol_header);
781814
free(rpc->buf);
782815
strbuf_release(&buf);
783816
return err;

0 commit comments

Comments
 (0)