Skip to content

Commit 1bbcc22

Browse files
peffjrn
authored andcommitted
http: refactor options to http_get_*
Over time, the http_get_strbuf function has grown several optional parameters. We now have a bitfield with multiple boolean options, as well as an optional strbuf for returning the content-type of the response. And a future patch in this series is going to add another strbuf option. Treating these as separate arguments has a few downsides: 1. Most call sites need to add extra NULLs and 0s for the options they aren't interested in. 2. The http_get_* functions are actually wrappers around 2 layers of low-level implementation functions. We have to pass these options through individually. 3. The http_get_strbuf wrapper learned these options, but nobody bothered to do so for http_get_file, even though it is backed by the same function that does understand the options. Let's consolidate the options into a single struct. For the common case of the default options, we'll allow callers to simply pass a NULL for the options struct. The resulting code is often a few lines longer, but it ends up being easier to read (and to change as we add new options, since we do not need to update each call site). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]>
1 parent 132b70a commit 1bbcc22

File tree

4 files changed

+44
-28
lines changed

4 files changed

+44
-28
lines changed

http-push.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,7 +1543,7 @@ static int remote_exists(const char *path)
15431543

15441544
sprintf(url, "%s%s", repo->url, path);
15451545

1546-
switch (http_get_strbuf(url, NULL, NULL, 0)) {
1546+
switch (http_get_strbuf(url, NULL, NULL)) {
15471547
case HTTP_OK:
15481548
ret = 1;
15491549
break;
@@ -1567,7 +1567,7 @@ static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
15671567
url = xmalloc(strlen(repo->url) + strlen(path) + 1);
15681568
sprintf(url, "%s%s", repo->url, path);
15691569

1570-
if (http_get_strbuf(url, NULL, &buffer, 0) != HTTP_OK)
1570+
if (http_get_strbuf(url, &buffer, NULL) != HTTP_OK)
15711571
die("Couldn't get %s for remote symref\n%s", url,
15721572
curl_errorstr);
15731573
free(url);

http.c

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -836,8 +836,9 @@ static CURLcode curlinfo_strbuf(CURL *curl, CURLINFO info, struct strbuf *buf)
836836
#define HTTP_REQUEST_STRBUF 0
837837
#define HTTP_REQUEST_FILE 1
838838

839-
static int http_request(const char *url, struct strbuf *type,
840-
void *result, int target, int options)
839+
static int http_request(const char *url,
840+
void *result, int target,
841+
const struct http_get_options *options)
841842
{
842843
struct active_request_slot *slot;
843844
struct slot_results results;
@@ -870,9 +871,9 @@ static int http_request(const char *url, struct strbuf *type,
870871
}
871872

872873
strbuf_addstr(&buf, "Pragma:");
873-
if (options & HTTP_NO_CACHE)
874+
if (options && options->no_cache)
874875
strbuf_addstr(&buf, " no-cache");
875-
if (options & HTTP_KEEP_ERROR)
876+
if (options && options->keep_error)
876877
curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
877878

878879
headers = curl_slist_append(headers, buf.buf);
@@ -890,8 +891,9 @@ static int http_request(const char *url, struct strbuf *type,
890891
ret = HTTP_START_FAILED;
891892
}
892893

893-
if (type)
894-
curlinfo_strbuf(slot->curl, CURLINFO_CONTENT_TYPE, type);
894+
if (options && options->content_type)
895+
curlinfo_strbuf(slot->curl, CURLINFO_CONTENT_TYPE,
896+
options->content_type);
895897

896898
curl_slist_free_all(headers);
897899
strbuf_release(&buf);
@@ -900,11 +902,10 @@ static int http_request(const char *url, struct strbuf *type,
900902
}
901903

902904
static int http_request_reauth(const char *url,
903-
struct strbuf *type,
904905
void *result, int target,
905-
int options)
906+
struct http_get_options *options)
906907
{
907-
int ret = http_request(url, type, result, target, options);
908+
int ret = http_request(url, result, target, options);
908909
if (ret != HTTP_REAUTH)
909910
return ret;
910911

@@ -914,7 +915,7 @@ static int http_request_reauth(const char *url,
914915
* making our next request. We only know how to do this for
915916
* the strbuf case, but that is enough to satisfy current callers.
916917
*/
917-
if (options & HTTP_KEEP_ERROR) {
918+
if (options && options->keep_error) {
918919
switch (target) {
919920
case HTTP_REQUEST_STRBUF:
920921
strbuf_reset(result);
@@ -923,15 +924,14 @@ static int http_request_reauth(const char *url,
923924
die("BUG: HTTP_KEEP_ERROR is only supported with strbufs");
924925
}
925926
}
926-
return http_request(url, type, result, target, options);
927+
return http_request(url, result, target, options);
927928
}
928929

929930
int http_get_strbuf(const char *url,
930-
struct strbuf *type,
931-
struct strbuf *result, int options)
931+
struct strbuf *result,
932+
struct http_get_options *options)
932933
{
933-
return http_request_reauth(url, type, result,
934-
HTTP_REQUEST_STRBUF, options);
934+
return http_request_reauth(url, result, HTTP_REQUEST_STRBUF, options);
935935
}
936936

937937
/*
@@ -940,7 +940,8 @@ int http_get_strbuf(const char *url,
940940
* If a previous interrupted download is detected (i.e. a previous temporary
941941
* file is still around) the download is resumed.
942942
*/
943-
static int http_get_file(const char *url, const char *filename, int options)
943+
static int http_get_file(const char *url, const char *filename,
944+
struct http_get_options *options)
944945
{
945946
int ret;
946947
struct strbuf tmpfile = STRBUF_INIT;
@@ -954,7 +955,7 @@ static int http_get_file(const char *url, const char *filename, int options)
954955
goto cleanup;
955956
}
956957

957-
ret = http_request_reauth(url, NULL, result, HTTP_REQUEST_FILE, options);
958+
ret = http_request_reauth(url, result, HTTP_REQUEST_FILE, options);
958959
fclose(result);
959960

960961
if (ret == HTTP_OK && move_temp_to_file(tmpfile.buf, filename))
@@ -966,12 +967,15 @@ static int http_get_file(const char *url, const char *filename, int options)
966967

967968
int http_fetch_ref(const char *base, struct ref *ref)
968969
{
970+
struct http_get_options options = {0};
969971
char *url;
970972
struct strbuf buffer = STRBUF_INIT;
971973
int ret = -1;
972974

975+
options.no_cache = 1;
976+
973977
url = quote_ref_url(base, ref->name);
974-
if (http_get_strbuf(url, NULL, &buffer, HTTP_NO_CACHE) == HTTP_OK) {
978+
if (http_get_strbuf(url, &buffer, &options) == HTTP_OK) {
975979
strbuf_rtrim(&buffer);
976980
if (buffer.len == 40)
977981
ret = get_sha1_hex(buffer.buf, ref->old_sha1);
@@ -1055,6 +1059,7 @@ static int fetch_and_setup_pack_index(struct packed_git **packs_head,
10551059

10561060
int http_get_info_packs(const char *base_url, struct packed_git **packs_head)
10571061
{
1062+
struct http_get_options options = {0};
10581063
int ret = 0, i = 0;
10591064
char *url, *data;
10601065
struct strbuf buf = STRBUF_INIT;
@@ -1064,7 +1069,8 @@ int http_get_info_packs(const char *base_url, struct packed_git **packs_head)
10641069
strbuf_addstr(&buf, "objects/info/packs");
10651070
url = strbuf_detach(&buf, NULL);
10661071

1067-
ret = http_get_strbuf(url, NULL, &buf, HTTP_NO_CACHE);
1072+
options.no_cache = 1;
1073+
ret = http_get_strbuf(url, &buf, &options);
10681074
if (ret != HTTP_OK)
10691075
goto cleanup;
10701076

http.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,16 @@ extern void append_remote_object_url(struct strbuf *buf, const char *url,
125125
extern char *get_remote_object_url(const char *url, const char *hex,
126126
int only_two_digit_prefix);
127127

128-
/* Options for http_request_*() */
129-
#define HTTP_NO_CACHE 1
130-
#define HTTP_KEEP_ERROR 2
128+
/* Options for http_get_*() */
129+
struct http_get_options {
130+
unsigned no_cache:1,
131+
keep_error:1;
131132

132-
/* Return values for http_request_*() */
133+
/* If non-NULL, returns the content-type of the response. */
134+
struct strbuf *content_type;
135+
};
136+
137+
/* Return values for http_get_*() */
133138
#define HTTP_OK 0
134139
#define HTTP_MISSING_TARGET 1
135140
#define HTTP_ERROR 2
@@ -142,7 +147,7 @@ extern char *get_remote_object_url(const char *url, const char *hex,
142147
*
143148
* If the result pointer is NULL, a HTTP HEAD request is made instead of GET.
144149
*/
145-
int http_get_strbuf(const char *url, struct strbuf *content_type, struct strbuf *result, int options);
150+
int http_get_strbuf(const char *url, struct strbuf *result, struct http_get_options *options);
146151

147152
extern int http_fetch_ref(const char *base, struct ref *ref);
148153

remote-curl.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ static struct discovery* discover_refs(const char *service, int for_push)
187187
struct discovery *last = last_discovery;
188188
char *refs_url;
189189
int http_ret, maybe_smart = 0;
190+
struct http_get_options options;
190191

191192
if (last && !strcmp(service, last->service))
192193
return last;
@@ -204,8 +205,12 @@ static struct discovery* discover_refs(const char *service, int for_push)
204205
}
205206
refs_url = strbuf_detach(&buffer, NULL);
206207

207-
http_ret = http_get_strbuf(refs_url, &type, &buffer,
208-
HTTP_NO_CACHE | HTTP_KEEP_ERROR);
208+
memset(&options, 0, sizeof(options));
209+
options.content_type = &type;
210+
options.no_cache = 1;
211+
options.keep_error = 1;
212+
213+
http_ret = http_get_strbuf(refs_url, &buffer, &options);
209214
switch (http_ret) {
210215
case HTTP_OK:
211216
break;

0 commit comments

Comments
 (0)