Skip to content

Commit b1c7d4a

Browse files
rctaygitster
authored andcommitted
http-push: refactor lock-related headers creation for curl requests
DAV-related headers (more specifically, headers related to the lock token, namely, If, Lock-Token, and Timeout) for curl requests are created and allocated individually, eg a "if_header" variable for the "If: " header, a "timeout_header" variable for the "Timeout: " header. This patch provides a new function ("get_dav_token_headers") that creates these header, saving methods from allocating memory, and from issuing a "curl_slist_append()" call. The temporary string storage given to curl_slist_append() is freed much earlier than the previous code with this patch, but this change is safe, because curl_slist_append() keeps a copy of the given string. In part, this patch also addresses the fact that commit 753bc91 (Remove the requirement opaquelocktoken uri scheme) did not update memory allocations for DAV-related headers. Signed-off-by: Tay Ray Chuan <[email protected]> Acked-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b80da42 commit b1c7d4a

File tree

1 file changed

+39
-29
lines changed

1 file changed

+39
-29
lines changed

http-push.c

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,37 @@ struct remote_ls_ctx
177177
struct remote_ls_ctx *parent;
178178
};
179179

180+
/* get_dav_token_headers options */
181+
enum dav_header_flag {
182+
DAV_HEADER_IF = (1u << 0),
183+
DAV_HEADER_LOCK = (1u << 1),
184+
DAV_HEADER_TIMEOUT = (1u << 2)
185+
};
186+
187+
static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options) {
188+
struct strbuf buf = STRBUF_INIT;
189+
struct curl_slist *dav_headers = NULL;
190+
191+
if(options & DAV_HEADER_IF) {
192+
strbuf_addf(&buf, "If: (<%s>)", lock->token);
193+
dav_headers = curl_slist_append(dav_headers, buf.buf);
194+
strbuf_reset(&buf);
195+
}
196+
if(options & DAV_HEADER_LOCK) {
197+
strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
198+
dav_headers = curl_slist_append(dav_headers, buf.buf);
199+
strbuf_reset(&buf);
200+
}
201+
if(options & DAV_HEADER_TIMEOUT) {
202+
strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
203+
dav_headers = curl_slist_append(dav_headers, buf.buf);
204+
strbuf_reset(&buf);
205+
}
206+
strbuf_release(&buf);
207+
208+
return dav_headers;
209+
}
210+
180211
static void finish_request(struct transfer_request *request);
181212
static void release_request(struct transfer_request *request);
182213

@@ -588,18 +619,12 @@ static int refresh_lock(struct remote_lock *lock)
588619
{
589620
struct active_request_slot *slot;
590621
struct slot_results results;
591-
char *if_header;
592-
char timeout_header[25];
593-
struct curl_slist *dav_headers = NULL;
622+
struct curl_slist *dav_headers;
594623
int rc = 0;
595624

596625
lock->refreshing = 1;
597626

598-
if_header = xmalloc(strlen(lock->token) + 25);
599-
sprintf(if_header, "If: (<%s>)", lock->token);
600-
sprintf(timeout_header, "Timeout: Second-%ld", lock->timeout);
601-
dav_headers = curl_slist_append(dav_headers, if_header);
602-
dav_headers = curl_slist_append(dav_headers, timeout_header);
627+
dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
603628

604629
slot = get_active_slot();
605630
slot->results = &results;
@@ -622,7 +647,6 @@ static int refresh_lock(struct remote_lock *lock)
622647

623648
lock->refreshing = 0;
624649
curl_slist_free_all(dav_headers);
625-
free(if_header);
626650

627651
return rc;
628652
}
@@ -1303,14 +1327,10 @@ static int unlock_remote(struct remote_lock *lock)
13031327
struct active_request_slot *slot;
13041328
struct slot_results results;
13051329
struct remote_lock *prev = remote->locks;
1306-
char *lock_token_header;
1307-
struct curl_slist *dav_headers = NULL;
1330+
struct curl_slist *dav_headers;
13081331
int rc = 0;
13091332

1310-
lock_token_header = xmalloc(strlen(lock->token) + 31);
1311-
sprintf(lock_token_header, "Lock-Token: <%s>",
1312-
lock->token);
1313-
dav_headers = curl_slist_append(dav_headers, lock_token_header);
1333+
dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
13141334

13151335
slot = get_active_slot();
13161336
slot->results = &results;
@@ -1331,7 +1351,6 @@ static int unlock_remote(struct remote_lock *lock)
13311351
}
13321352

13331353
curl_slist_free_all(dav_headers);
1334-
free(lock_token_header);
13351354

13361355
if (remote->locks == lock) {
13371356
remote->locks = lock->next;
@@ -1731,13 +1750,10 @@ static int update_remote(unsigned char *sha1, struct remote_lock *lock)
17311750
{
17321751
struct active_request_slot *slot;
17331752
struct slot_results results;
1734-
char *if_header;
17351753
struct buffer out_buffer = { STRBUF_INIT, 0 };
1736-
struct curl_slist *dav_headers = NULL;
1754+
struct curl_slist *dav_headers;
17371755

1738-
if_header = xmalloc(strlen(lock->token) + 25);
1739-
sprintf(if_header, "If: (<%s>)", lock->token);
1740-
dav_headers = curl_slist_append(dav_headers, if_header);
1756+
dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
17411757

17421758
strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
17431759

@@ -1756,7 +1772,6 @@ static int update_remote(unsigned char *sha1, struct remote_lock *lock)
17561772
if (start_active_slot(slot)) {
17571773
run_active_slot(slot);
17581774
strbuf_release(&out_buffer.buf);
1759-
free(if_header);
17601775
if (results.curl_result != CURLE_OK) {
17611776
fprintf(stderr,
17621777
"PUT error: curl result=%d, HTTP code=%ld\n",
@@ -1766,7 +1781,6 @@ static int update_remote(unsigned char *sha1, struct remote_lock *lock)
17661781
}
17671782
} else {
17681783
strbuf_release(&out_buffer.buf);
1769-
free(if_header);
17701784
fprintf(stderr, "Unable to start PUT request\n");
17711785
return 0;
17721786
}
@@ -1948,15 +1962,12 @@ static void update_remote_info_refs(struct remote_lock *lock)
19481962
struct buffer buffer = { STRBUF_INIT, 0 };
19491963
struct active_request_slot *slot;
19501964
struct slot_results results;
1951-
char *if_header;
1952-
struct curl_slist *dav_headers = NULL;
1965+
struct curl_slist *dav_headers;
19531966

19541967
remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
19551968
add_remote_info_ref, &buffer.buf);
19561969
if (!aborted) {
1957-
if_header = xmalloc(strlen(lock->token) + 25);
1958-
sprintf(if_header, "If: (<%s>)", lock->token);
1959-
dav_headers = curl_slist_append(dav_headers, if_header);
1970+
dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
19601971

19611972
slot = get_active_slot();
19621973
slot->results = &results;
@@ -1978,7 +1989,6 @@ static void update_remote_info_refs(struct remote_lock *lock)
19781989
results.curl_result, results.http_code);
19791990
}
19801991
}
1981-
free(if_header);
19821992
}
19831993
strbuf_release(&buffer.buf);
19841994
}

0 commit comments

Comments
 (0)