Skip to content

Commit

Permalink
- added more checks when processing HTTP response
Browse files Browse the repository at this point in the history
  • Loading branch information
Black-Thunder committed Jun 20, 2020
1 parent 13d406e commit 028135e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
31 changes: 29 additions & 2 deletions lib/melcloudDevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const request = require("request");
const commonDefines = require("./commonDefines");
const JSONHelper = require("./jsonHelper");
const HttpStatus = require("http-status-codes");

let gthat = null; // pointer to "this" from main.js/MelCloud instance

Expand Down Expand Up @@ -562,12 +563,25 @@ class MelcloudDevice {
"X-MitsContextKey": gthis.platform.contextKey
}
}, function handleDeviceInfoResponse(err, response) {
if (err || response.body.search("<!DOCTYPE html>") != -1) {
gthat.log.error("There was a problem getting info from: " + url);
if (err) {
gthat.log.error("There was a problem getting device data from: " + url);
gthat.log.error("Error: " + err);
gthis.airInfo = null;
}
else if(!response || response.body.search("<!DOCTYPE html>") != -1) {
gthat.log.error("There was a problem receiving the response from: " + url);
gthis.airInfo = null;
}
else {
const statusCode = response.statusCode;
gthat.log.debug("Received response from: " + url + " (status code: " + statusCode + " - " + HttpStatus.getStatusText(statusCode) + ")");

if(statusCode != HttpStatus.OK) {
gthis.airInfo = null;
gthat.log.error("Invalid HTTP status code (see line above). Getting device data failed!");
return;
}

const responseBoy = response.body;
gthis.airInfo = JSONHelper.JSONHelper.ParseCloudResponse(responseBoy, gthat);

Expand Down Expand Up @@ -668,7 +682,20 @@ class MelcloudDevice {
gthat.log.error("There was a problem setting info to: " + url);
gthat.log.error(err);
}
else if(!response) {
gthat.log.error("There was a problem receiving the response from: " + url);
gthis.airInfo = null;
}
else {
const statusCode = response.statusCode;
gthat.log.debug("Received response from: " + url + " (status code: " + statusCode + " - " + HttpStatus.getStatusText(statusCode) + ")");

if(statusCode != HttpStatus.OK) {
gthis.airInfo = null;
gthat.log.error("Invalid HTTP status code (see line above). Changing device option failed!");
return;
}

const responseBoy = response.body;
const jsonObject = JSONHelper.JSONHelper.ParseCloudResponse(responseBoy, gthat);

Expand Down
31 changes: 31 additions & 0 deletions lib/melcloudPlatform.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const request = require("request");
const MelCloudDevice = require("./melcloudDevice");
const JSONHelper = require("./jsonHelper");
const HttpStatus = require("http-status-codes");

let gthat = null; // pointer to "this" from main.js/MelCloud instance
let gthis = null; // pointer to "this" from MelcloudPlatform
Expand Down Expand Up @@ -51,7 +52,22 @@ class MelcloudPlatform {
gthis.isConnected = false;
gthat.setAdapterConnectionState(false);
}
else if(!response) {
gthat.log.error("There was a problem receiving the response from: " + loginUrl);
gthis.isConnected = false;
gthat.setAdapterConnectionState(false);
}
else {
const statusCode = response.statusCode;
gthat.log.debug("Received response from: " + loginUrl + " (status code: " + statusCode + " - " + HttpStatus.getStatusText(statusCode) + ")");

if(statusCode != HttpStatus.OK) {
gthis.isConnected = false;
gthat.setAdapterConnectionState(false);
gthat.log.error("Invalid HTTP status code (see line above). Login failed!");
return;
}

const r = JSONHelper.JSONHelper.ParseCloudResponse(response.body, gthat);

if (r.LoginData == null) {
Expand Down Expand Up @@ -101,7 +117,22 @@ class MelcloudPlatform {
gthis.isConnected = false;
gthat.setAdapterConnectionState(false);
}
else if(!response) {
gthat.log.error("There was a problem receiving the response from: " + url);
gthis.isConnected = false;
gthat.setAdapterConnectionState(false);
}
else {
const statusCode = response.statusCode;
gthat.log.debug("Received response from: " + url + " (status code: " + statusCode + " - " + HttpStatus.getStatusText(statusCode) + ")");

if(statusCode != HttpStatus.OK) {
gthis.isConnected = false;
gthat.setAdapterConnectionState(false);
gthat.log.error("Invalid HTTP status code (see line above). Getting devices failed!");
return;
}

gthis.isConnected = true;
gthat.setAdapterConnectionState(true);
const responseBody = response.body;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
},
"dependencies": {
"@iobroker/adapter-core": "^2.3.1",
"request": "^2.88.2"
"request": "^2.88.2",
"http-status-codes": "^1.4.0"
},
"devDependencies": {
"@iobroker/testing": "^2.2.0",
Expand Down

0 comments on commit 028135e

Please sign in to comment.