Skip to content

Commit

Permalink
Merge pull request #460 from F43nd1r/responsecode
Browse files Browse the repository at this point in the history
Discard request in case of 401 or 405
  • Loading branch information
william-ferguson-au committed May 10, 2016
2 parents ff0180c + 04de4fa commit 7d85726
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/main/java/org/acra/util/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public final class HttpRequest {
private static final int HTTP_SUCCESS = 200;
private static final int HTTP_REDIRECT = 300;
private static final int HTTP_CLIENT_ERROR = 400;
private static final int HTTP_UNAUTHORIZED = 401;
private static final int HTTP_FORBIDDEN = 403;
private static final int HTTP_METHOD_NOT_ALLOWED = 405;
private static final int HTTP_CONFLICT = 409;
private static final int MAX_HTTP_CODE = 600;

Expand Down Expand Up @@ -160,17 +162,25 @@ public void send(@NonNull Context context, @NonNull URL url, @NonNull Method met
if ((responseCode >= HTTP_SUCCESS) && (responseCode < HTTP_REDIRECT)) {
// All is good
ACRA.log.i(LOG_TAG, "Request received by server");
} else if (responseCode == HTTP_UNAUTHORIZED) {
//401 means the server rejected the authentication. The request must not be repeated. Discard it.
//This probably means that nothing can be sent with this configuration, maybe ACRA should disable itself after it?
ACRA.log.w(LOG_TAG, "401: Login validation error on server - request will be discarded");
} else if (responseCode == HTTP_FORBIDDEN) {
// 403 is an explicit data validation refusal from the server. The request must not be repeated. Discard it.
ACRA.log.w(LOG_TAG, "Data validation error on server - request will be discarded");
ACRA.log.w(LOG_TAG, "403: Data validation error on server - request will be discarded");
} else if (responseCode == HTTP_METHOD_NOT_ALLOWED) {
//405 means the server doesn't allow this request method. The request must not be repeated. Discard it.
//This probably means that nothing can be sent with this configuration, maybe ACRA should disable itself after it?
ACRA.log.w(LOG_TAG, "405: Server rejected Http " + method + " - request will be discarded");
} else if (responseCode == HTTP_CONFLICT) {
// 409 means that the report has been received already. So we can discard it.
ACRA.log.w(LOG_TAG, "Server has already received this post - request will be discarded");
ACRA.log.w(LOG_TAG, "409: Server has already received this post - request will be discarded");
} else if ((responseCode >= HTTP_CLIENT_ERROR) && (responseCode < MAX_HTTP_CODE)) {
ACRA.log.w(LOG_TAG, "Could not send ACRA Post responseCode=" + responseCode + " message=" + urlConnection.getResponseMessage());
throw new IOException("Host returned error code " + responseCode);
} else {
ACRA.log.w(LOG_TAG, "Could not send ACRA Post - request will be discarded responseCode=" + responseCode + " message=" + urlConnection.getResponseMessage());
ACRA.log.w(LOG_TAG, "Could not send ACRA Post - request will be discarded. responseCode=" + responseCode + " message=" + urlConnection.getResponseMessage());
}

urlConnection.disconnect();
Expand Down

0 comments on commit 7d85726

Please sign in to comment.