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

View File

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