@matheusbsbs / matheus-bagliano / commits / b1c7d4aafe
Fork 0

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 <rctay89@gmail.com> Acked-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ray Chuan committed Jan 24, 2009 at 10:00 UTC b1c7d4aafeda8e6b2bbeac29a3181cbe970b7874
1 file changed +39 -29
http-push.c
+39 -29
@@ -177,6 +177,37 @@ struct remote_ls_ctx
177 struct remote_ls_ctx *parent;
178 };
179
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 +
211 static void finish_request(struct transfer_request *request);
212 static void release_request(struct transfer_request *request);
213
@@ -588,18 +619,12 @@ static int refresh_lock(struct remote_lock *lock)
619 {
620 struct active_request_slot *slot;
621 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;
623 int rc = 0;
624
625 lock->refreshing = 1;
626
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);
628
629 slot = get_active_slot();
630 slot->results = &results;
@@ -622,7 +647,6 @@ static int refresh_lock(struct remote_lock *lock)
647
648 lock->refreshing = 0;
649 curl_slist_free_all(dav_headers);
625 - free(if_header);
650
651 return rc;
652 }
@@ -1303,14 +1327,10 @@ static int unlock_remote(struct remote_lock *lock)
1327 struct active_request_slot *slot;
1328 struct slot_results results;
1329 struct remote_lock *prev = remote->locks;
1306 - char *lock_token_header;
1307 - struct curl_slist *dav_headers = NULL;
1330 + struct curl_slist *dav_headers;
1331 int rc = 0;
1332
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);
1334
1335 slot = get_active_slot();
1336 slot->results = &results;
@@ -1331,7 +1351,6 @@ static int unlock_remote(struct remote_lock *lock)
1351 }
1352
1353 curl_slist_free_all(dav_headers);
1334 - free(lock_token_header);
1354
1355 if (remote->locks == lock) {
1356 remote->locks = lock->next;
@@ -1731,13 +1750,10 @@ static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1750 {
1751 struct active_request_slot *slot;
1752 struct slot_results results;
1734 - char *if_header;
1753 struct buffer out_buffer = { STRBUF_INIT, 0 };
1736 - struct curl_slist *dav_headers = NULL;
1754 + struct curl_slist *dav_headers;
1755
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);
1757
1758 strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
1759
@@ -1756,7 +1772,6 @@ static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1772 if (start_active_slot(slot)) {
1773 run_active_slot(slot);
1774 strbuf_release(&out_buffer.buf);
1759 - free(if_header);
1775 if (results.curl_result != CURLE_OK) {
1776 fprintf(stderr,
1777 "PUT error: curl result=%d, HTTP code=%ld\n",
@@ -1766,7 +1781,6 @@ static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1781 }
1782 } else {
1783 strbuf_release(&out_buffer.buf);
1769 - free(if_header);
1784 fprintf(stderr, "Unable to start PUT request\n");
1785 return 0;
1786 }
@@ -1948,15 +1962,12 @@ static void update_remote_info_refs(struct remote_lock *lock)
1962 struct buffer buffer = { STRBUF_INIT, 0 };
1963 struct active_request_slot *slot;
1964 struct slot_results results;
1951 - char *if_header;
1952 - struct curl_slist *dav_headers = NULL;
1965 + struct curl_slist *dav_headers;
1966
1967 remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
1968 add_remote_info_ref, &buffer.buf);
1969 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);
1971
1972 slot = get_active_slot();
1973 slot->results = &results;
@@ -1978,7 +1989,6 @@ static void update_remote_info_refs(struct remote_lock *lock)
1989 results.curl_result, results.http_code);
1990 }
1991 }
1981 - free(if_header);
1992 }
1993 strbuf_release(&buffer.buf);
1994 }