Skip to content

Commit

Permalink
fix: #438 requests not working correctly on browser platform
Browse files Browse the repository at this point in the history
  • Loading branch information
silkimen committed Oct 15, 2021
1 parent b20faa9 commit b3b9730
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

# 3.2.2

- Fixed #438: requests not working correctly on browser platform because request options are not processed correctly

## 3.2.1

- Fixed #425: plugin crashes on Android SDK levels < 24
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ This defaults to `urlencoded`. You can also override the default content type he

### setRequestTimeout
Set how long to wait for a request to respond, in seconds.
For Android, this will set both [connectTimeout](https://developer.android.com/reference/java/net/URLConnection#getConnectTimeout()) and [readTimeout](https://developer.android.com/reference/java/net/URLConnection#setReadTimeout(int))
For iOS, this will set [timeout interval](https://developer.apple.com/documentation/foundation/nsmutableurlrequest/1414063-timeoutinterval)
For Android, this will set both [connectTimeout](https://developer.android.com/reference/java/net/URLConnection#getConnectTimeout()) and [readTimeout](https://developer.android.com/reference/java/net/URLConnection#setReadTimeout(int)).
For iOS, this will set [timeout interval](https://developer.apple.com/documentation/foundation/nsmutableurlrequest/1414063-timeoutinterval).
For browser platform, this will set [timeout](https://developer.mozilla.org/fr/docs/Web/API/XMLHttpRequest/timeout).
```js
cordova.plugin.http.setRequestTimeout(5.0);
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-advanced-http",
"version": "3.2.1",
"version": "3.2.2",
"description": "Cordova / Phonegap plugin for communicating with HTTP servers using SSL pinning",
"scripts": {
"updatecert": "node ./scripts/update-e2e-server-cert.js && node ./scripts/update-e2e-client-cert.js",
Expand Down
27 changes: 17 additions & 10 deletions src/browser/cordova-http-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,27 @@ function setHeaders(xhr, headers) {
}

function sendRequest(method, withData, opts, success, failure) {
var data, serializer, headers, timeout, followRedirect, responseType, reqId;
var data, serializer, headers, readTimeout, followRedirect, responseType, reqId;
var url = opts[0];

if (withData) {
data = opts[1];
serializer = opts[2];
headers = opts[3];
timeout = opts[4];
followRedirect = opts[5];
responseType = opts[6];
reqId = opts[7];
// connect timeout not applied
// connectTimeout = opts[4];
readTimeout = opts[5];
followRedirect = opts[6];
responseType = opts[7];
reqId = opts[8];
} else {
headers = opts[1];
timeout = opts[2];
followRedirect = opts[3];
responseType = opts[4];
reqId = opts[5];
// connect timeout not applied
// connectTimeout = opts[2];
readTimeout = opts[3];
followRedirect = opts[4];
responseType = opts[5];
reqId = opts[6];
}

var onSuccess = injectRequestIdHandler(reqId, success);
Expand Down Expand Up @@ -229,7 +233,10 @@ function sendRequest(method, withData, opts, success, failure) {

// requesting text instead of JSON because it's parsed in the response handler
xhr.responseType = responseType === 'json' ? 'text' : responseType;
xhr.timeout = timeout * 1000;

// we can't set connect timeout and read timeout separately on browser platform
xhr.timeout = readTimeout * 1000;

setHeaders(xhr, headers);

xhr.onerror = function () {
Expand Down

0 comments on commit b3b9730

Please sign in to comment.