Skip to content

Commit 637afcf

Browse files
committed
Merge branch 'tr/http-updates'
* tr/http-updates: Remove http.authAny Allow curl to rewind the RPC read buffer Add an option for using any HTTP authentication scheme, not only basic http: maintain curl sessions
2 parents 0b4ae29 + 525ecd2 commit 637afcf

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

Documentation/config.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,12 @@ http.maxRequests::
11361136
How many HTTP requests to launch in parallel. Can be overridden
11371137
by the 'GIT_HTTP_MAX_REQUESTS' environment variable. Default is 5.
11381138

1139+
http.minSessions::
1140+
The number of curl sessions (counted across slots) to be kept across
1141+
requests. They will not be ended with curl_easy_cleanup() until
1142+
http_cleanup() is invoked. If USE_CURL_MULTI is not defined, this
1143+
value will be capped at 1. Defaults to 1.
1144+
11391145
http.postBuffer::
11401146
Maximum size in bytes of the buffer used by smart HTTP
11411147
transports when POSTing data to the remote system.

http.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ int active_requests;
77
int http_is_verbose;
88
size_t http_post_buffer = 16 * LARGE_PACKET_MAX;
99

10+
#if LIBCURL_VERSION_NUM >= 0x070a06
11+
#define LIBCURL_CAN_HANDLE_AUTH_ANY
12+
#endif
13+
14+
static int min_curl_sessions = 1;
15+
static int curl_session_count;
1016
#ifdef USE_CURL_MULTI
1117
static int max_requests = -1;
1218
static CURLM *curlm;
@@ -152,6 +158,14 @@ static int http_options(const char *var, const char *value, void *cb)
152158
ssl_cert_password_required = 1;
153159
return 0;
154160
}
161+
if (!strcmp("http.minsessions", var)) {
162+
min_curl_sessions = git_config_int(var, value);
163+
#ifndef USE_CURL_MULTI
164+
if (min_curl_sessions > 1)
165+
min_curl_sessions = 1;
166+
#endif
167+
return 0;
168+
}
155169
#ifdef USE_CURL_MULTI
156170
if (!strcmp("http.maxrequests", var)) {
157171
max_requests = git_config_int(var, value);
@@ -230,6 +244,9 @@ static CURL *get_curl_handle(void)
230244
#if LIBCURL_VERSION_NUM >= 0x070907
231245
curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
232246
#endif
247+
#ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
248+
curl_easy_setopt(result, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
249+
#endif
233250

234251
init_curl_http_auth(result);
235252

@@ -372,6 +389,7 @@ void http_init(struct remote *remote)
372389
if (curl_ssl_verify == -1)
373390
curl_ssl_verify = 1;
374391

392+
curl_session_count = 0;
375393
#ifdef USE_CURL_MULTI
376394
if (max_requests < 1)
377395
max_requests = DEFAULT_MAX_REQUESTS;
@@ -480,6 +498,7 @@ struct active_request_slot *get_active_slot(void)
480498
#else
481499
slot->curl = curl_easy_duphandle(curl_default);
482500
#endif
501+
curl_session_count++;
483502
}
484503

485504
active_requests++;
@@ -558,9 +577,11 @@ void fill_active_slots(void)
558577
}
559578

560579
while (slot != NULL) {
561-
if (!slot->in_use && slot->curl != NULL) {
580+
if (!slot->in_use && slot->curl != NULL
581+
&& curl_session_count > min_curl_sessions) {
562582
curl_easy_cleanup(slot->curl);
563583
slot->curl = NULL;
584+
curl_session_count--;
564585
}
565586
slot = slot->next;
566587
}
@@ -633,12 +654,13 @@ static void closedown_active_slot(struct active_request_slot *slot)
633654
void release_active_slot(struct active_request_slot *slot)
634655
{
635656
closedown_active_slot(slot);
636-
if (slot->curl) {
657+
if (slot->curl && curl_session_count > min_curl_sessions) {
637658
#ifdef USE_CURL_MULTI
638659
curl_multi_remove_handle(curlm, slot->curl);
639660
#endif
640661
curl_easy_cleanup(slot->curl);
641662
slot->curl = NULL;
663+
curl_session_count--;
642664
}
643665
#ifdef USE_CURL_MULTI
644666
fill_active_slots();

remote-curl.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ struct rpc_state {
290290
int out;
291291
struct strbuf result;
292292
unsigned gzip_request : 1;
293+
unsigned initial_buffer : 1;
293294
};
294295

295296
static size_t rpc_out(void *ptr, size_t eltsize,
@@ -300,6 +301,7 @@ static size_t rpc_out(void *ptr, size_t eltsize,
300301
size_t avail = rpc->len - rpc->pos;
301302

302303
if (!avail) {
304+
rpc->initial_buffer = 0;
303305
avail = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
304306
if (!avail)
305307
return 0;
@@ -314,6 +316,29 @@ static size_t rpc_out(void *ptr, size_t eltsize,
314316
return avail;
315317
}
316318

319+
#ifndef NO_CURL_IOCTL
320+
curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
321+
{
322+
struct rpc_state *rpc = clientp;
323+
324+
switch (cmd) {
325+
case CURLIOCMD_NOP:
326+
return CURLIOE_OK;
327+
328+
case CURLIOCMD_RESTARTREAD:
329+
if (rpc->initial_buffer) {
330+
rpc->pos = 0;
331+
return CURLIOE_OK;
332+
}
333+
fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
334+
return CURLIOE_FAILRESTART;
335+
336+
default:
337+
return CURLIOE_UNKNOWNCMD;
338+
}
339+
}
340+
#endif
341+
317342
static size_t rpc_in(const void *ptr, size_t eltsize,
318343
size_t nmemb, void *buffer_)
319344
{
@@ -370,8 +395,13 @@ static int post_rpc(struct rpc_state *rpc)
370395
*/
371396
headers = curl_slist_append(headers, "Expect: 100-continue");
372397
headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
398+
rpc->initial_buffer = 1;
373399
curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
374400
curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
401+
#ifndef NO_CURL_IOCTL
402+
curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
403+
curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
404+
#endif
375405
if (options.verbosity > 1) {
376406
fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
377407
fflush(stderr);

0 commit comments

Comments
 (0)