Commit 29762e4089d8619d281083f2fab1176b378668e1

Edward Thomson 2020-01-01T16:14:37

httpclient: use defines for status codes

diff --git a/src/transports/http.c b/src/transports/http.c
index e63827c..65435d1 100644
--- a/src/transports/http.c
+++ b/src/transports/http.c
@@ -243,22 +243,23 @@ static int handle_response(
 	/* If we're in the middle of challenge/response auth, continue. */
 	if (allow_replay && response->resend_credentials) {
 		return 0;
-	} else if (allow_replay && response->status == 401) {
+	} else if (allow_replay && response->status == GIT_HTTP_STATUS_UNAUTHORIZED) {
 		if ((error = handle_remote_auth(stream, response)) < 0)
 			return error;
 
 		return git_http_client_skip_body(transport->http_client);
-	} else if (allow_replay && response->status == 407) {
+	} else if (allow_replay && response->status == GIT_HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
 		if ((error = handle_proxy_auth(stream, response)) < 0)
 			return error;
 
 		return git_http_client_skip_body(transport->http_client);
-	} else if (response->status == 401 || response->status == 407) {
+	} else if (response->status == GIT_HTTP_STATUS_UNAUTHORIZED ||
+	           response->status == GIT_HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
 		git_error_set(GIT_ERROR_NET, "unexpected authentication failure");
 		return -1;
 	}
 
-	if (response->status != 200) {
+	if (response->status != GIT_HTTP_STATUS_OK) {
 		git_error_set(GIT_ERROR_NET, "unexpected http status code: %d", response->status);
 		return -1;
 	}
diff --git a/src/transports/httpclient.c b/src/transports/httpclient.c
index 585f04f..b565bf5 100644
--- a/src/transports/httpclient.c
+++ b/src/transports/httpclient.c
@@ -126,11 +126,11 @@ struct git_http_client {
 
 bool git_http_response_is_redirect(git_http_response *response)
 {
-	return (response->status == 301 ||
-	        response->status == 302 ||
-	        response->status == 303 ||
-		response->status == 307 ||
-		response->status == 308);
+	return (response->status == GIT_HTTP_MOVED_PERMANENTLY ||
+	        response->status == GIT_HTTP_FOUND ||
+	        response->status == GIT_HTTP_SEE_OTHER ||
+		response->status == GIT_HTTP_TEMPORARY_REDIRECT ||
+		response->status == GIT_HTTP_PERMANENT_REDIRECT);
 }
 
 void git_http_response_dispose(git_http_response *response)
@@ -316,13 +316,13 @@ static int resend_needed(git_http_client *client, git_http_response *response)
 {
 	git_http_auth_context *auth_context;
 
-	if (response->status == 401 &&
+	if (response->status == GIT_HTTP_STATUS_UNAUTHORIZED &&
 	    (auth_context = client->server.auth_context) &&
 	    auth_context->is_complete &&
 	    !auth_context->is_complete(auth_context))
 		return 1;
 
-	if (response->status == 407 &&
+	if (response->status == GIT_HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED &&
 	    (auth_context = client->proxy.auth_context) &&
 	    auth_context->is_complete &&
 	    !auth_context->is_complete(auth_context))
@@ -914,12 +914,12 @@ static int proxy_connect(
 
 	assert(client->state == DONE);
 
-	if (response.status == 407) {
+	if (response.status == GIT_HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
 		save_early_response(client, &response);
 
 		error = GIT_RETRY;
 		goto done;
-	} else if (response.status != 200) {
+	} else if (response.status != GIT_HTTP_STATUS_OK) {
 		git_error_set(GIT_ERROR_NET, "proxy returned unexpected status: %d", response.status);
 		error = -1;
 		goto done;
@@ -1236,7 +1236,7 @@ int git_http_client_send_request(
 
 		error = 0;
 
-		if (response.status != 100) {
+		if (response.status != GIT_HTTP_STATUS_CONTINUE) {
 			save_early_response(client, &response);
 			goto done;
 		}
diff --git a/src/transports/httpclient.h b/src/transports/httpclient.h
index cb21866..da764fd 100644
--- a/src/transports/httpclient.h
+++ b/src/transports/httpclient.h
@@ -11,6 +11,16 @@
 #include "common.h"
 #include "net.h"
 
+#define GIT_HTTP_STATUS_CONTINUE                      100
+#define GIT_HTTP_STATUS_OK                            200
+#define GIT_HTTP_MOVED_PERMANENTLY                    301
+#define GIT_HTTP_FOUND                                302
+#define GIT_HTTP_SEE_OTHER                            303
+#define GIT_HTTP_TEMPORARY_REDIRECT                   307
+#define GIT_HTTP_PERMANENT_REDIRECT                   308
+#define GIT_HTTP_STATUS_UNAUTHORIZED                  401
+#define GIT_HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED 407
+
 typedef struct git_http_client git_http_client;
 
 /** Method for the HTTP request */