Skip to content

Commit a3722bc

Browse files
peffgitster
authored andcommitted
http: factor out curl result code normalization
We make some requests with CURLOPT_FAILONERROR and some without, and then handle_curl_result() normalizes any failures to a uniform CURLcode. There are some other code paths in the dumb-http walker which don't use handle_curl_result(); let's pull the normalization into its own function so it can be reused. Arguably those code paths would benefit from the rest of handle_curl_result(), notably the auth handling. But retro-fitting it now would be a lot of work, and in practice it doesn't matter too much (whatever authentication we needed to make the initial contact with the server is generally sufficient for the rest of the dumb-http requests). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 041f5ea commit a3722bc

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

http.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,8 @@ char *get_remote_object_url(const char *url, const char *hex,
15441544
return strbuf_detach(&buf, NULL);
15451545
}
15461546

1547-
static int handle_curl_result(struct slot_results *results)
1547+
void normalize_curl_result(CURLcode *result, long http_code,
1548+
char *errorstr, size_t errorlen)
15481549
{
15491550
/*
15501551
* If we see a failing http code with CURLE_OK, we have turned off
@@ -1554,19 +1555,24 @@ static int handle_curl_result(struct slot_results *results)
15541555
* Likewise, if we see a redirect (30x code), that means we turned off
15551556
* redirect-following, and we should treat the result as an error.
15561557
*/
1557-
if (results->curl_result == CURLE_OK &&
1558-
results->http_code >= 300) {
1559-
results->curl_result = CURLE_HTTP_RETURNED_ERROR;
1558+
if (*result == CURLE_OK && http_code >= 300) {
1559+
*result = CURLE_HTTP_RETURNED_ERROR;
15601560
/*
15611561
* Normally curl will already have put the "reason phrase"
15621562
* from the server into curl_errorstr; unfortunately without
15631563
* FAILONERROR it is lost, so we can give only the numeric
15641564
* status code.
15651565
*/
1566-
xsnprintf(curl_errorstr, sizeof(curl_errorstr),
1566+
xsnprintf(errorstr, errorlen,
15671567
"The requested URL returned error: %ld",
1568-
results->http_code);
1568+
http_code);
15691569
}
1570+
}
1571+
1572+
static int handle_curl_result(struct slot_results *results)
1573+
{
1574+
normalize_curl_result(&results->curl_result, results->http_code,
1575+
curl_errorstr, sizeof(curl_errorstr));
15701576

15711577
if (results->curl_result == CURLE_OK) {
15721578
credential_approve(&http_auth);

http.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ static inline int missing__target(int code, int result)
136136

137137
#define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
138138

139+
/*
140+
* Normalize curl results to handle CURL_FAILONERROR (or lack thereof). Failing
141+
* http codes have their "result" converted to CURLE_HTTP_RETURNED_ERROR, and
142+
* an appropriate string placed in the errorstr buffer (pass curl_errorstr if
143+
* you don't have a custom buffer).
144+
*/
145+
void normalize_curl_result(CURLcode *result, long http_code, char *errorstr,
146+
size_t errorlen);
147+
139148
/* Helpers for modifying and creating URLs */
140149
extern void append_remote_object_url(struct strbuf *buf, const char *url,
141150
const char *hex,

0 commit comments

Comments
 (0)