Skip to content

Commit 8809703

Browse files
peffgitster
authored andcommitted
http: factor out http error code handling
Most of our http requests go through the http_request() interface, which does some nice post-processing on the results. In particular, it handles prompting for missing credentials as well as approving and rejecting valid or invalid credentials. Unfortunately, it only handles GET requests. Making it handle POSTs would be quite complex, so let's pull result handling code into its own function so that it can be reused from the POST code paths. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4c71009 commit 8809703

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

http.c

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,33 @@ char *get_remote_object_url(const char *url, const char *hex,
744744
return strbuf_detach(&buf, NULL);
745745
}
746746

747+
int handle_curl_result(struct active_request_slot *slot)
748+
{
749+
struct slot_results *results = slot->results;
750+
751+
if (results->curl_result == CURLE_OK) {
752+
credential_approve(&http_auth);
753+
return HTTP_OK;
754+
} else if (missing_target(results))
755+
return HTTP_MISSING_TARGET;
756+
else if (results->http_code == 401) {
757+
if (http_auth.username && http_auth.password) {
758+
credential_reject(&http_auth);
759+
return HTTP_NOAUTH;
760+
} else {
761+
credential_fill(&http_auth);
762+
init_curl_http_auth(slot->curl);
763+
return HTTP_REAUTH;
764+
}
765+
} else {
766+
if (!curl_errorstr[0])
767+
strlcpy(curl_errorstr,
768+
curl_easy_strerror(results->curl_result),
769+
sizeof(curl_errorstr));
770+
return HTTP_ERROR;
771+
}
772+
}
773+
747774
/* http_request() targets */
748775
#define HTTP_REQUEST_STRBUF 0
749776
#define HTTP_REQUEST_FILE 1
@@ -791,26 +818,7 @@ static int http_request(const char *url, void *result, int target, int options)
791818

792819
if (start_active_slot(slot)) {
793820
run_active_slot(slot);
794-
if (results.curl_result == CURLE_OK)
795-
ret = HTTP_OK;
796-
else if (missing_target(&results))
797-
ret = HTTP_MISSING_TARGET;
798-
else if (results.http_code == 401) {
799-
if (http_auth.username && http_auth.password) {
800-
credential_reject(&http_auth);
801-
ret = HTTP_NOAUTH;
802-
} else {
803-
credential_fill(&http_auth);
804-
init_curl_http_auth(slot->curl);
805-
ret = HTTP_REAUTH;
806-
}
807-
} else {
808-
if (!curl_errorstr[0])
809-
strlcpy(curl_errorstr,
810-
curl_easy_strerror(results.curl_result),
811-
sizeof(curl_errorstr));
812-
ret = HTTP_ERROR;
813-
}
821+
ret = handle_curl_result(slot);
814822
} else {
815823
error("Unable to start HTTP request for %s", url);
816824
ret = HTTP_START_FAILED;
@@ -819,9 +827,6 @@ static int http_request(const char *url, void *result, int target, int options)
819827
curl_slist_free_all(headers);
820828
strbuf_release(&buf);
821829

822-
if (ret == HTTP_OK)
823-
credential_approve(&http_auth);
824-
825830
return ret;
826831
}
827832

http.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ extern int start_active_slot(struct active_request_slot *slot);
7878
extern void run_active_slot(struct active_request_slot *slot);
7979
extern void finish_active_slot(struct active_request_slot *slot);
8080
extern void finish_all_active_slots(void);
81+
extern int handle_curl_result(struct active_request_slot *slot);
8182

8283
#ifdef USE_CURL_MULTI
8384
extern void fill_active_slots(void);

0 commit comments

Comments
 (0)