fix broken basic auth for async calls (#917)

* HTTP Basic Auth, some cleanups

There’s no need for additional dependency `btoa` as `XHR2` already has
the support for Basic Auth.

* remove redundant code

* fix semicolons

* Rewrite Basic Auth to support both sync and async calls.

* semicolon consistency
This commit is contained in:
Shayan Eskandari 2017-07-05 08:55:06 -04:00 committed by Fabian Vogelsteller
parent e7641a7add
commit 0f53c43d7b
1 changed files with 11 additions and 8 deletions

View File

@ -22,19 +22,19 @@
* @date 2015
*/
var errors = require('./errors')
var errors = require('./errors');
// workaround to use httpprovider in different envs
// browser
if (typeof window !== 'undefined' && window.XMLHttpRequest) {
XMLHttpRequest = window.XMLHttpRequest // jshint ignore: line
XMLHttpRequest = window.XMLHttpRequest; // jshint ignore: line
// node
} else {
XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest // jshint ignore: line
XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // jshint ignore: line
}
var XHR2 = require('xhr2') // jshint ignore: line
var XHR2 = require('xhr2'); // jshint ignore: line
/**
* HttpProvider should be used to send rpc calls over http
@ -63,8 +63,11 @@ HttpProvider.prototype.prepareRequest = function (async) {
request = new XMLHttpRequest();
}
request.open('POST', this.host, async, this.user, this.password);
request.setRequestHeader('Content-Type', 'application/json');
request.open('POST', this.host, async);
if (this.user && this.password) {
var auth = 'Basic ' + new Buffer(this.user + ':' + this.password).toString('base64');
request.setRequestHeader('Authorization', auth);
} request.setRequestHeader('Content-Type', 'application/json');
return request;
};
@ -145,9 +148,9 @@ HttpProvider.prototype.isConnected = function () {
method: 'net_listening',
params: []
});
return true
return true;
} catch (e) {
return false
return false;
}
};