diff --git a/src/android/com/silkimen/cordovahttp/CordovaHttpBase.java b/src/android/com/silkimen/cordovahttp/CordovaHttpBase.java index b8f95aaa..6485a1b5 100644 --- a/src/android/com/silkimen/cordovahttp/CordovaHttpBase.java +++ b/src/android/com/silkimen/cordovahttp/CordovaHttpBase.java @@ -112,7 +112,10 @@ public void run() { } try { - if (response.hasFailed()) { + if (response.isRedirect()) { + this.url = response.getRedirectUrl(); + this.run(); + } else if (response.hasFailed()) { this.callbackContext.error(response.toJSON()); } else { this.callbackContext.success(response.toJSON()); @@ -205,6 +208,8 @@ protected void processResponse(HttpRequest request, CordovaHttpResponse response } else { response.setData(outputStream.toByteArray()); } + } else if (this.followRedirects && request.code() >= 300 && request.code() < 400) { + response.setRedirectUrl(request.header("Location")); } else { response.setErrorMessage(HttpBodyDecoder.decodeBody(outputStream.toByteArray(), request.charset())); } diff --git a/src/android/com/silkimen/cordovahttp/CordovaHttpResponse.java b/src/android/com/silkimen/cordovahttp/CordovaHttpResponse.java index e6051bf3..c4d3708f 100644 --- a/src/android/com/silkimen/cordovahttp/CordovaHttpResponse.java +++ b/src/android/com/silkimen/cordovahttp/CordovaHttpResponse.java @@ -20,9 +20,11 @@ class CordovaHttpResponse { private String body; private byte[] rawData; private JSONObject fileEntry; + private boolean isRedirect; private boolean hasFailed; private boolean isFileOperation; private boolean isRawResponse; + private String redirectUrl; private String error; public void setStatus(int status) { @@ -51,15 +53,28 @@ public void setFileEntry(JSONObject entry) { this.fileEntry = entry; } + public void setRedirectUrl(String redirectUrl) { + this.isRedirect = true; + this.redirectUrl = redirectUrl; + } + public void setErrorMessage(String message) { this.hasFailed = true; this.error = message; } + public boolean isRedirect() { + return this.isRedirect; + } + public boolean hasFailed() { return this.hasFailed; } + public String getRedirectUrl() { + return this.redirectUrl; + } + public JSONObject toJSON() throws JSONException { JSONObject json = new JSONObject(); @@ -70,7 +85,9 @@ public JSONObject toJSON() throws JSONException { json.put("headers", new JSONObject(getFilteredHeaders())); } - if (this.hasFailed) { + if (this.isRedirect) { + json.put("redirect", this.redirectUrl); + } else if (this.hasFailed) { json.put("error", this.error); } else if (this.isFileOperation) { json.put("file", this.fileEntry); diff --git a/test/e2e-specs.js b/test/e2e-specs.js index 7293abb6..6d2a1418 100644 --- a/test/e2e-specs.js +++ b/test/e2e-specs.js @@ -316,6 +316,15 @@ const tests = [ result.data.status.should.be.equal(302); } }, + { + description: 'should follow 302 redirect with protocol change #383', + expected: 'resolved: {"status": 200, url: "https://httpbin.org/anything", ...', + func: function (resolve, reject) { cordova.plugin.http.get('http://httpbingo.org/redirect-to?url=https://httpbin.org/anything', {}, {}, resolve, reject); }, + validationFunc: function (driver, result) { + result.type.should.be.equal('resolved'); + result.data.url.should.be.equal('https://httpbin.org/anything'); + } + }, { description: 'should download a file from given URL to given path in local filesystem', expected: 'resolved: {"content": "\\n\\n" ...',