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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Aug 27, 2012 at 09:26 UTC
88097030725bf68d1801559cfb4785b93a50f5f8
2 files changed
+29
-23
http.c
+28
-23
@@ -744,6 +744,33 @@ char *get_remote_object_url(const char *url, const char *hex,
744
return strbuf_detach(&buf, NULL);
745
}
746
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
+
774
/* http_request() targets */
775
#define HTTP_REQUEST_STRBUF 0
776
#define HTTP_REQUEST_FILE 1
@@ -791,26 +818,7 @@ static int http_request(const char *url, void *result, int target, int options)
818
819
if (start_active_slot(slot)) {
820
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);
822
} else {
823
error("Unable to start HTTP request for %s", url);
824
ret = HTTP_START_FAILED;
@@ -819,9 +827,6 @@ static int http_request(const char *url, void *result, int target, int options)
827
curl_slist_free_all(headers);
828
strbuf_release(&buf);
829
822
- if (ret == HTTP_OK)
823
- credential_approve(&http_auth);
824
-
830
return ret;
831
}
832
http.h
+1
@@ -78,6 +78,7 @@ extern int start_active_slot(struct active_request_slot *slot);
78
extern void run_active_slot(struct active_request_slot *slot);
79
extern void finish_active_slot(struct active_request_slot *slot);
80
extern void finish_all_active_slots(void);
81
+extern int handle_curl_result(struct active_request_slot *slot);
82
83
#ifdef USE_CURL_MULTI
84
extern void fill_active_slots(void);