Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/android/com/silkimen/cordovahttp/CordovaHttpBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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()));
}
Expand Down
19 changes: 18 additions & 1 deletion src/android/com/silkimen/cordovahttp/CordovaHttpResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();

Expand All @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions test/e2e-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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": "<?xml version=\'1.0\' encoding=\'us-ascii\'?>\\n\\n<!-- A SAMPLE set of slides -->" ...',
Expand Down