http: use new headers for each object request

Currently we create one set of headers for all object requests and reuse it. However, we'll need to adjust the headers for authentication purposes in the future, so let's create a new set for each request so that we can adjust them if the authentication changes. Note that the cost of allocation here is tiny compared to the fact that we're making a network call, not to mention probably a full TLS connection, so this shouldn't have a significant impact on performance. Moreover, nobody who cares about performance is using the dumb HTTP protocol anyway, since it often makes huge numbers of requests compared to the smart protocol. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Apr 17, 2024 at 00:02 UTC d01c76f1cfc96ba56f1c7c0e1b051d121ba6cc48
2 files changed +13 -9
http.c
+11 -9
@@ -128,7 +128,6 @@ static unsigned long empty_auth_useless =
128 | CURLAUTH_DIGEST;
129
130 static struct curl_slist *pragma_header;
131 -static struct curl_slist *no_pragma_header;
131 static struct string_list extra_http_headers = STRING_LIST_INIT_DUP;
132
133 static struct curl_slist *host_resolutions;
@@ -299,6 +298,11 @@ size_t fwrite_null(char *ptr UNUSED, size_t eltsize UNUSED, size_t nmemb,
298 return nmemb;
299 }
300
301 +static struct curl_slist *object_request_headers(void)
302 +{
303 + return curl_slist_append(http_copy_default_headers(), "Pragma:");
304 +}
305 +
306 static void closedown_active_slot(struct active_request_slot *slot)
307 {
308 active_requests--;
@@ -1275,8 +1279,6 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
1279
1280 pragma_header = curl_slist_append(http_copy_default_headers(),
1281 "Pragma: no-cache");
1278 - no_pragma_header = curl_slist_append(http_copy_default_headers(),
1279 - "Pragma:");
1282
1283 {
1284 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
@@ -1360,9 +1362,6 @@ void http_cleanup(void)
1362 curl_slist_free_all(pragma_header);
1363 pragma_header = NULL;
1364
1363 - curl_slist_free_all(no_pragma_header);
1364 - no_pragma_header = NULL;
1365 -
1365 curl_slist_free_all(host_resolutions);
1366 host_resolutions = NULL;
1367
@@ -2370,6 +2369,7 @@ void release_http_pack_request(struct http_pack_request *preq)
2369 }
2370 preq->slot = NULL;
2371 strbuf_release(&preq->tmpfile);
2372 + curl_slist_free_all(preq->headers);
2373 free(preq->url);
2374 free(preq);
2375 }
@@ -2454,11 +2454,11 @@ struct http_pack_request *new_direct_http_pack_request(
2454 }
2455
2456 preq->slot = get_active_slot();
2457 + preq->headers = object_request_headers();
2458 curl_easy_setopt(preq->slot->curl, CURLOPT_WRITEDATA, preq->packfile);
2459 curl_easy_setopt(preq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
2460 curl_easy_setopt(preq->slot->curl, CURLOPT_URL, preq->url);
2460 - curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER,
2461 - no_pragma_header);
2461 + curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER, preq->headers);
2462
2463 /*
2464 * If there is data present from a previous transfer attempt,
@@ -2624,13 +2624,14 @@ struct http_object_request *new_http_object_request(const char *base_url,
2624 }
2625
2626 freq->slot = get_active_slot();
2627 + freq->headers = object_request_headers();
2628
2629 curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEDATA, freq);
2630 curl_easy_setopt(freq->slot->curl, CURLOPT_FAILONERROR, 0);
2631 curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
2632 curl_easy_setopt(freq->slot->curl, CURLOPT_ERRORBUFFER, freq->errorstr);
2633 curl_easy_setopt(freq->slot->curl, CURLOPT_URL, freq->url);
2633 - curl_easy_setopt(freq->slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
2634 + curl_easy_setopt(freq->slot->curl, CURLOPT_HTTPHEADER, freq->headers);
2635
2636 /*
2637 * If we have successfully processed data from a previous fetch
@@ -2718,5 +2719,6 @@ void release_http_object_request(struct http_object_request *freq)
2719 release_active_slot(freq->slot);
2720 freq->slot = NULL;
2721 }
2722 + curl_slist_free_all(freq->headers);
2723 strbuf_release(&freq->tmpfile);
2724 }
http.h
+2
@@ -196,6 +196,7 @@ struct http_pack_request {
196 FILE *packfile;
197 struct strbuf tmpfile;
198 struct active_request_slot *slot;
199 + struct curl_slist *headers;
200 };
201
202 struct http_pack_request *new_http_pack_request(
@@ -229,6 +230,7 @@ struct http_object_request {
230 int zret;
231 int rename;
232 struct active_request_slot *slot;
233 + struct curl_slist *headers;
234 };
235
236 struct http_object_request *new_http_object_request(