@@ -177,6 +177,37 @@ struct remote_ls_ctx
177
177
struct remote_ls_ctx * parent ;
178
178
};
179
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
+
180
211
static void finish_request (struct transfer_request * request );
181
212
static void release_request (struct transfer_request * request );
182
213
@@ -588,18 +619,12 @@ static int refresh_lock(struct remote_lock *lock)
588
619
{
589
620
struct active_request_slot * slot ;
590
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 ;
594
623
int rc = 0 ;
595
624
596
625
lock -> refreshing = 1 ;
597
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 );
603
628
604
629
slot = get_active_slot ();
605
630
slot -> results = & results ;
@@ -622,7 +647,6 @@ static int refresh_lock(struct remote_lock *lock)
622
647
623
648
lock -> refreshing = 0 ;
624
649
curl_slist_free_all (dav_headers );
625
- free (if_header );
626
650
627
651
return rc ;
628
652
}
@@ -1303,14 +1327,10 @@ static int unlock_remote(struct remote_lock *lock)
1303
1327
struct active_request_slot * slot ;
1304
1328
struct slot_results results ;
1305
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 ;
1308
1331
int rc = 0 ;
1309
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 );
1314
1334
1315
1335
slot = get_active_slot ();
1316
1336
slot -> results = & results ;
@@ -1331,7 +1351,6 @@ static int unlock_remote(struct remote_lock *lock)
1331
1351
}
1332
1352
1333
1353
curl_slist_free_all (dav_headers );
1334
- free (lock_token_header );
1335
1354
1336
1355
if (remote -> locks == lock ) {
1337
1356
remote -> locks = lock -> next ;
@@ -1731,13 +1750,10 @@ static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1731
1750
{
1732
1751
struct active_request_slot * slot ;
1733
1752
struct slot_results results ;
1734
- char * if_header ;
1735
1753
struct buffer out_buffer = { STRBUF_INIT , 0 };
1736
- struct curl_slist * dav_headers = NULL ;
1754
+ struct curl_slist * dav_headers ;
1737
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 );
1741
1757
1742
1758
strbuf_addf (& out_buffer .buf , "%s\n" , sha1_to_hex (sha1 ));
1743
1759
@@ -1756,7 +1772,6 @@ static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1756
1772
if (start_active_slot (slot )) {
1757
1773
run_active_slot (slot );
1758
1774
strbuf_release (& out_buffer .buf );
1759
- free (if_header );
1760
1775
if (results .curl_result != CURLE_OK ) {
1761
1776
fprintf (stderr ,
1762
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)
1766
1781
}
1767
1782
} else {
1768
1783
strbuf_release (& out_buffer .buf );
1769
- free (if_header );
1770
1784
fprintf (stderr , "Unable to start PUT request\n" );
1771
1785
return 0 ;
1772
1786
}
@@ -1948,15 +1962,12 @@ static void update_remote_info_refs(struct remote_lock *lock)
1948
1962
struct buffer buffer = { STRBUF_INIT , 0 };
1949
1963
struct active_request_slot * slot ;
1950
1964
struct slot_results results ;
1951
- char * if_header ;
1952
- struct curl_slist * dav_headers = NULL ;
1965
+ struct curl_slist * dav_headers ;
1953
1966
1954
1967
remote_ls ("refs/" , (PROCESS_FILES | RECURSIVE ),
1955
1968
add_remote_info_ref , & buffer .buf );
1956
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 );
1960
1971
1961
1972
slot = get_active_slot ();
1962
1973
slot -> results = & results ;
@@ -1978,7 +1989,6 @@ static void update_remote_info_refs(struct remote_lock *lock)
1978
1989
results .curl_result , results .http_code );
1979
1990
}
1980
1991
}
1981
- free (if_header );
1982
1992
}
1983
1993
strbuf_release (& buffer .buf );
1984
1994
}
0 commit comments