Skip to content

Commit

Permalink
http: don't always add Git-Protocol header
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bmwill authored and gitster committed Mar 15, 2018
1 parent 8ff14ed commit 884e586
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
17 changes: 0 additions & 17 deletions http.c
Expand Up @@ -904,21 +904,6 @@ static void set_from_env(const char **var, const char *envname)
*var = val;
}

static void protocol_http_header(void)
{
if (get_protocol_version_config() > 0) {
struct strbuf protocol_header = STRBUF_INIT;

strbuf_addf(&protocol_header, GIT_PROTOCOL_HEADER ": version=%d",
get_protocol_version_config());


extra_http_headers = curl_slist_append(extra_http_headers,
protocol_header.buf);
strbuf_release(&protocol_header);
}
}

void http_init(struct remote *remote, const char *url, int proactive_auth)
{
char *low_speed_limit;
Expand Down Expand Up @@ -949,8 +934,6 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
if (remote)
var_override(&http_proxy_authmethod, remote->http_proxy_authmethod);

protocol_http_header();

pragma_header = curl_slist_append(http_copy_default_headers(),
"Pragma: no-cache");
no_pragma_header = curl_slist_append(http_copy_default_headers(),
Expand Down
33 changes: 33 additions & 0 deletions remote-curl.c
Expand Up @@ -291,6 +291,19 @@ static int show_http_message(struct strbuf *type, struct strbuf *charset,
return 0;
}

static int get_protocol_http_header(enum protocol_version version,
struct strbuf *header)
{
if (version > 0) {
strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
version);

return 1;
}

return 0;
}

static struct discovery *discover_refs(const char *service, int for_push)
{
struct strbuf exp = STRBUF_INIT;
Expand All @@ -299,6 +312,8 @@ static struct discovery *discover_refs(const char *service, int for_push)
struct strbuf buffer = STRBUF_INIT;
struct strbuf refs_url = STRBUF_INIT;
struct strbuf effective_url = STRBUF_INIT;
struct strbuf protocol_header = STRBUF_INIT;
struct string_list extra_headers = STRING_LIST_INIT_DUP;
struct discovery *last = last_discovery;
int http_ret, maybe_smart = 0;
struct http_get_options http_options;
Expand All @@ -318,11 +333,16 @@ static struct discovery *discover_refs(const char *service, int for_push)
strbuf_addf(&refs_url, "service=%s", service);
}

/* Add the extra Git-Protocol header */
if (get_protocol_http_header(get_protocol_version_config(), &protocol_header))
string_list_append(&extra_headers, protocol_header.buf);

memset(&http_options, 0, sizeof(http_options));
http_options.content_type = &type;
http_options.charset = &charset;
http_options.effective_url = &effective_url;
http_options.base_url = &url;
http_options.extra_headers = &extra_headers;
http_options.initial_request = 1;
http_options.no_cache = 1;
http_options.keep_error = 1;
Expand Down Expand Up @@ -389,6 +409,8 @@ static struct discovery *discover_refs(const char *service, int for_push)
strbuf_release(&charset);
strbuf_release(&effective_url);
strbuf_release(&buffer);
strbuf_release(&protocol_header);
string_list_clear(&extra_headers, 0);
last_discovery = last;
return last;
}
Expand Down Expand Up @@ -425,6 +447,7 @@ struct rpc_state {
char *service_url;
char *hdr_content_type;
char *hdr_accept;
char *protocol_header;
char *buf;
size_t alloc;
size_t len;
Expand Down Expand Up @@ -611,6 +634,10 @@ static int post_rpc(struct rpc_state *rpc)
headers = curl_slist_append(headers, needs_100_continue ?
"Expect: 100-continue" : "Expect:");

/* Add the extra Git-Protocol header */
if (rpc->protocol_header)
headers = curl_slist_append(headers, rpc->protocol_header);

retry:
slot = get_active_slot();

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

if (get_protocol_http_header(heads->version, &buf))
rpc->protocol_header = strbuf_detach(&buf, NULL);
else
rpc->protocol_header = NULL;

while (!err) {
int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
if (!n)
Expand Down Expand Up @@ -778,6 +810,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->protocol_header);
free(rpc->buf);
strbuf_release(&buf);
return err;
Expand Down

0 comments on commit 884e586

Please sign in to comment.