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 <peff@peff.net>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
This commit is contained in:
Jeff King
2013-09-28 04:31:23 -04:00
committed by Jonathan Nieder
parent 132b70a2ed
commit 1bbcc224cc
4 changed files with 44 additions and 28 deletions

44
http.c
View File

@ -836,8 +836,9 @@ static CURLcode curlinfo_strbuf(CURL *curl, CURLINFO info, struct strbuf *buf)
#define HTTP_REQUEST_STRBUF 0
#define HTTP_REQUEST_FILE 1
static int http_request(const char *url, struct strbuf *type,
void *result, int target, int options)
static int http_request(const char *url,
void *result, int target,
const struct http_get_options *options)
{
struct active_request_slot *slot;
struct slot_results results;
@ -870,9 +871,9 @@ static int http_request(const char *url, struct strbuf *type,
}
strbuf_addstr(&buf, "Pragma:");
if (options & HTTP_NO_CACHE)
if (options && options->no_cache)
strbuf_addstr(&buf, " no-cache");
if (options & HTTP_KEEP_ERROR)
if (options && options->keep_error)
curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
headers = curl_slist_append(headers, buf.buf);
@ -890,8 +891,9 @@ static int http_request(const char *url, struct strbuf *type,
ret = HTTP_START_FAILED;
}
if (type)
curlinfo_strbuf(slot->curl, CURLINFO_CONTENT_TYPE, type);
if (options && options->content_type)
curlinfo_strbuf(slot->curl, CURLINFO_CONTENT_TYPE,
options->content_type);
curl_slist_free_all(headers);
strbuf_release(&buf);
@ -900,11 +902,10 @@ static int http_request(const char *url, struct strbuf *type,
}
static int http_request_reauth(const char *url,
struct strbuf *type,
void *result, int target,
int options)
struct http_get_options *options)
{
int ret = http_request(url, type, result, target, options);
int ret = http_request(url, result, target, options);
if (ret != HTTP_REAUTH)
return ret;
@ -914,7 +915,7 @@ static int http_request_reauth(const char *url,
* making our next request. We only know how to do this for
* the strbuf case, but that is enough to satisfy current callers.
*/
if (options & HTTP_KEEP_ERROR) {
if (options && options->keep_error) {
switch (target) {
case HTTP_REQUEST_STRBUF:
strbuf_reset(result);
@ -923,15 +924,14 @@ static int http_request_reauth(const char *url,
die("BUG: HTTP_KEEP_ERROR is only supported with strbufs");
}
}
return http_request(url, type, result, target, options);
return http_request(url, result, target, options);
}
int http_get_strbuf(const char *url,
struct strbuf *type,
struct strbuf *result, int options)
struct strbuf *result,
struct http_get_options *options)
{
return http_request_reauth(url, type, result,
HTTP_REQUEST_STRBUF, options);
return http_request_reauth(url, result, HTTP_REQUEST_STRBUF, options);
}
/*
@ -940,7 +940,8 @@ int http_get_strbuf(const char *url,
* If a previous interrupted download is detected (i.e. a previous temporary
* file is still around) the download is resumed.
*/
static int http_get_file(const char *url, const char *filename, int options)
static int http_get_file(const char *url, const char *filename,
struct http_get_options *options)
{
int ret;
struct strbuf tmpfile = STRBUF_INIT;
@ -954,7 +955,7 @@ static int http_get_file(const char *url, const char *filename, int options)
goto cleanup;
}
ret = http_request_reauth(url, NULL, result, HTTP_REQUEST_FILE, options);
ret = http_request_reauth(url, result, HTTP_REQUEST_FILE, options);
fclose(result);
if (ret == HTTP_OK && move_temp_to_file(tmpfile.buf, filename))
@ -966,12 +967,15 @@ cleanup:
int http_fetch_ref(const char *base, struct ref *ref)
{
struct http_get_options options = {0};
char *url;
struct strbuf buffer = STRBUF_INIT;
int ret = -1;
options.no_cache = 1;
url = quote_ref_url(base, ref->name);
if (http_get_strbuf(url, NULL, &buffer, HTTP_NO_CACHE) == HTTP_OK) {
if (http_get_strbuf(url, &buffer, &options) == HTTP_OK) {
strbuf_rtrim(&buffer);
if (buffer.len == 40)
ret = get_sha1_hex(buffer.buf, ref->old_sha1);
@ -1055,6 +1059,7 @@ add_pack:
int http_get_info_packs(const char *base_url, struct packed_git **packs_head)
{
struct http_get_options options = {0};
int ret = 0, i = 0;
char *url, *data;
struct strbuf buf = STRBUF_INIT;
@ -1064,7 +1069,8 @@ int http_get_info_packs(const char *base_url, struct packed_git **packs_head)
strbuf_addstr(&buf, "objects/info/packs");
url = strbuf_detach(&buf, NULL);
ret = http_get_strbuf(url, NULL, &buf, HTTP_NO_CACHE);
options.no_cache = 1;
ret = http_get_strbuf(url, &buf, &options);
if (ret != HTTP_OK)
goto cleanup;