From 0f53c43d7b0f3fbf6eba86bcd92373f3652537d1 Mon Sep 17 00:00:00 2001 From: Shayan Eskandari Date: Wed, 5 Jul 2017 08:55:06 -0400 Subject: [PATCH] fix broken basic auth for async calls (#917) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- lib/web3/httpprovider.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/web3/httpprovider.js b/lib/web3/httpprovider.js index 9676955..539a599 100644 --- a/lib/web3/httpprovider.js +++ b/lib/web3/httpprovider.js @@ -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; } };