From ff9c06c2b268eb44234112dde284f0db538162e2 Mon Sep 17 00:00:00 2001 From: Eugen Lechner Date: Mon, 11 Dec 2017 15:24:52 +0100 Subject: [PATCH 1/6] extend HttpProvider with custom http headers --- lib/web3/httpprovider.js | 146 ++++++++++++++++++++------------------- test/httpprovider.js | 10 +++ 2 files changed, 85 insertions(+), 71 deletions(-) diff --git a/lib/web3/httpprovider.js b/lib/web3/httpprovider.js index 539a5994710..80abf3eb9dc 100644 --- a/lib/web3/httpprovider.js +++ b/lib/web3/httpprovider.js @@ -28,10 +28,10 @@ var errors = require('./errors'); // 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 @@ -39,11 +39,12 @@ var XHR2 = require('xhr2'); // jshint ignore: line /** * HttpProvider should be used to send rpc calls over http */ -var HttpProvider = function (host, timeout, user, password) { - this.host = host || 'http://localhost:8545'; - this.timeout = timeout || 0; - this.user = user; - this.password = password; +var HttpProvider = function (host, timeout, user, password, headers) { + this.host = host || 'http://localhost:8545'; + this.timeout = timeout || 0; + this.user = user; + this.password = password; + this.header = headers; }; /** @@ -54,21 +55,24 @@ var HttpProvider = function (host, timeout, user, password) { * @return {XMLHttpRequest} object */ HttpProvider.prototype.prepareRequest = function (async) { - var request; - - if (async) { - request = new XHR2(); - request.timeout = this.timeout; - } else { - request = new XMLHttpRequest(); - } - - 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; + var request; + + if (async) { + request = new XHR2(); + request.timeout = this.timeout; + } else { + request = new XMLHttpRequest(); + } + + 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'); + headers.forEach(function(header) { + request.setRequestHeader(header.name, header.value); + }); + return request; }; /** @@ -79,23 +83,23 @@ HttpProvider.prototype.prepareRequest = function (async) { * @return {Object} result */ HttpProvider.prototype.send = function (payload) { - var request = this.prepareRequest(false); + var request = this.prepareRequest(false); - try { - request.send(JSON.stringify(payload)); - } catch (error) { - throw errors.InvalidConnection(this.host); - } + try { + request.send(JSON.stringify(payload)); + } catch (error) { + throw errors.InvalidConnection(this.host); + } - var result = request.responseText; + var result = request.responseText; - try { - result = JSON.parse(result); - } catch (e) { - throw errors.InvalidResponse(request.responseText); - } + try { + result = JSON.parse(result); + } catch (e) { + throw errors.InvalidResponse(request.responseText); + } - return result; + return result; }; /** @@ -106,32 +110,32 @@ HttpProvider.prototype.send = function (payload) { * @param {Function} callback triggered on end with (err, result) */ HttpProvider.prototype.sendAsync = function (payload, callback) { - var request = this.prepareRequest(true); - - request.onreadystatechange = function () { - if (request.readyState === 4 && request.timeout !== 1) { - var result = request.responseText; - var error = null; - - try { - result = JSON.parse(result); - } catch (e) { - error = errors.InvalidResponse(request.responseText); - } - - callback(error, result); + var request = this.prepareRequest(true); + + request.onreadystatechange = function () { + if (request.readyState === 4 && request.timeout !== 1) { + var result = request.responseText; + var error = null; + + try { + result = JSON.parse(result); + } catch (e) { + error = errors.InvalidResponse(request.responseText); + } + + callback(error, result); + } + }; + + request.ontimeout = function () { + callback(errors.ConnectionTimeout(this.timeout)); + }; + + try { + request.send(JSON.stringify(payload)); + } catch (error) { + callback(errors.InvalidConnection(this.host)); } - }; - - request.ontimeout = function () { - callback(errors.ConnectionTimeout(this.timeout)); - }; - - try { - request.send(JSON.stringify(payload)); - } catch (error) { - callback(errors.InvalidConnection(this.host)); - } }; /** @@ -141,17 +145,17 @@ HttpProvider.prototype.sendAsync = function (payload, callback) { * @return {Boolean} returns true if request haven't failed. Otherwise false */ HttpProvider.prototype.isConnected = function () { - try { - this.send({ - id: 9999999999, - jsonrpc: '2.0', - method: 'net_listening', - params: [] - }); - return true; - } catch (e) { - return false; - } + try { + this.send({ + id: 9999999999, + jsonrpc: '2.0', + method: 'net_listening', + params: [] + }); + return true; + } catch (e) { + return false; + } }; module.exports = HttpProvider; diff --git a/test/httpprovider.js b/test/httpprovider.js index 10714db19cd..bd769233e36 100644 --- a/test/httpprovider.js +++ b/test/httpprovider.js @@ -12,6 +12,16 @@ var HttpProvider = SandboxedModule.require('../lib/web3/httpprovider', { }); describe('lib/web3/httpprovider', function () { + describe('prepareRequest', function () { + it('should set request header', function () { + var provider = new HttpProvider('http://localhost:8545', 0 , null, null, [{name: 'Access-Control-Allow-Origin', value: '*'}]); + var result = provider.prepareRequest(true); + + assert.equal(typeof result, 'object'); + assert.equal(result.getRequestHeader('Access-Control-Allow-Origin'), '*'); + }); + }); + describe('send', function () { it('should send basic request', function () { var provider = new HttpProvider(); From c98f952b01a331a15370309b13802a465f379bc2 Mon Sep 17 00:00:00 2001 From: Eugen Lechner Date: Mon, 11 Dec 2017 15:32:05 +0100 Subject: [PATCH 2/6] reset formatting --- lib/web3/httpprovider.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/web3/httpprovider.js b/lib/web3/httpprovider.js index 80abf3eb9dc..f056e558441 100644 --- a/lib/web3/httpprovider.js +++ b/lib/web3/httpprovider.js @@ -44,7 +44,7 @@ var HttpProvider = function (host, timeout, user, password, headers) { this.timeout = timeout || 0; this.user = user; this.password = password; - this.header = headers; + this.headers = headers; }; /** From 8d84ac997e867f7e8d4da2a2c9b7185ae400825e Mon Sep 17 00:00:00 2001 From: Eugen Lechner Date: Mon, 11 Dec 2017 15:34:08 +0100 Subject: [PATCH 3/6] reset formatting --- lib/web3/httpprovider.js | 170 +++++++++++++++++++-------------------- 1 file changed, 85 insertions(+), 85 deletions(-) diff --git a/lib/web3/httpprovider.js b/lib/web3/httpprovider.js index f056e558441..2a8d2d95b75 100644 --- a/lib/web3/httpprovider.js +++ b/lib/web3/httpprovider.js @@ -1,18 +1,18 @@ /* - This file is part of web3.js. + This file is part of web3.js. - web3.js is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + web3.js is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - web3.js is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + web3.js is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with web3.js. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with web3.js. If not, see . */ /** @file httpprovider.js * @authors: @@ -28,10 +28,10 @@ var errors = require('./errors'); // 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 @@ -40,11 +40,11 @@ var XHR2 = require('xhr2'); // jshint ignore: line * HttpProvider should be used to send rpc calls over http */ var HttpProvider = function (host, timeout, user, password, headers) { - this.host = host || 'http://localhost:8545'; - this.timeout = timeout || 0; - this.user = user; - this.password = password; - this.headers = headers; + this.host = host || 'http://localhost:8545'; + this.timeout = timeout || 0; + this.user = user; + this.password = password; + this.headers = headers; }; /** @@ -55,24 +55,24 @@ var HttpProvider = function (host, timeout, user, password, headers) { * @return {XMLHttpRequest} object */ HttpProvider.prototype.prepareRequest = function (async) { - var request; - - if (async) { - request = new XHR2(); - request.timeout = this.timeout; - } else { - request = new XMLHttpRequest(); - } - - 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'); - headers.forEach(function(header) { - request.setRequestHeader(header.name, header.value); - }); - return request; + var request; + + if (async) { + request = new XHR2(); + request.timeout = this.timeout; + } else { + request = new XMLHttpRequest(); + } + + 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'); + headers.forEach(function(header) { + request.setRequestHeader(header.name, header.value); + }); + return request; }; /** @@ -83,23 +83,23 @@ HttpProvider.prototype.prepareRequest = function (async) { * @return {Object} result */ HttpProvider.prototype.send = function (payload) { - var request = this.prepareRequest(false); + var request = this.prepareRequest(false); - try { - request.send(JSON.stringify(payload)); - } catch (error) { - throw errors.InvalidConnection(this.host); - } + try { + request.send(JSON.stringify(payload)); + } catch (error) { + throw errors.InvalidConnection(this.host); + } - var result = request.responseText; + var result = request.responseText; - try { - result = JSON.parse(result); - } catch (e) { - throw errors.InvalidResponse(request.responseText); - } + try { + result = JSON.parse(result); + } catch (e) { + throw errors.InvalidResponse(request.responseText); + } - return result; + return result; }; /** @@ -110,32 +110,32 @@ HttpProvider.prototype.send = function (payload) { * @param {Function} callback triggered on end with (err, result) */ HttpProvider.prototype.sendAsync = function (payload, callback) { - var request = this.prepareRequest(true); - - request.onreadystatechange = function () { - if (request.readyState === 4 && request.timeout !== 1) { - var result = request.responseText; - var error = null; - - try { - result = JSON.parse(result); - } catch (e) { - error = errors.InvalidResponse(request.responseText); - } - - callback(error, result); - } - }; - - request.ontimeout = function () { - callback(errors.ConnectionTimeout(this.timeout)); - }; - - try { - request.send(JSON.stringify(payload)); - } catch (error) { - callback(errors.InvalidConnection(this.host)); + var request = this.prepareRequest(true); + + request.onreadystatechange = function () { + if (request.readyState === 4 && request.timeout !== 1) { + var result = request.responseText; + var error = null; + + try { + result = JSON.parse(result); + } catch (e) { + error = errors.InvalidResponse(request.responseText); + } + + callback(error, result); } + }; + + request.ontimeout = function () { + callback(errors.ConnectionTimeout(this.timeout)); + }; + + try { + request.send(JSON.stringify(payload)); + } catch (error) { + callback(errors.InvalidConnection(this.host)); + } }; /** @@ -145,17 +145,17 @@ HttpProvider.prototype.sendAsync = function (payload, callback) { * @return {Boolean} returns true if request haven't failed. Otherwise false */ HttpProvider.prototype.isConnected = function () { - try { - this.send({ - id: 9999999999, - jsonrpc: '2.0', - method: 'net_listening', - params: [] - }); - return true; - } catch (e) { - return false; - } + try { + this.send({ + id: 9999999999, + jsonrpc: '2.0', + method: 'net_listening', + params: [] + }); + return true; + } catch (e) { + return false; + } }; module.exports = HttpProvider; From bebb65816f1150ee2df73e747dbf933e604f1003 Mon Sep 17 00:00:00 2001 From: Eugen Lechner Date: Mon, 11 Dec 2017 15:34:59 +0100 Subject: [PATCH 4/6] reset formatting --- lib/web3/httpprovider.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/web3/httpprovider.js b/lib/web3/httpprovider.js index 2a8d2d95b75..b2d12ef1851 100644 --- a/lib/web3/httpprovider.js +++ b/lib/web3/httpprovider.js @@ -1,18 +1,18 @@ /* - This file is part of web3.js. + This file is part of web3.js. - web3.js is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + web3.js is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - web3.js is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. + web3.js is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public License - along with web3.js. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with web3.js. If not, see . */ /** @file httpprovider.js * @authors: From 588792f7c16119ef1365ea99bc316a0845c2a099 Mon Sep 17 00:00:00 2001 From: Eugen Lechner Date: Mon, 11 Dec 2017 15:44:39 +0100 Subject: [PATCH 5/6] fix null object --- lib/web3/httpprovider.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/web3/httpprovider.js b/lib/web3/httpprovider.js index b2d12ef1851..c12fdedeac8 100644 --- a/lib/web3/httpprovider.js +++ b/lib/web3/httpprovider.js @@ -69,9 +69,11 @@ HttpProvider.prototype.prepareRequest = function (async) { var auth = 'Basic ' + new Buffer(this.user + ':' + this.password).toString('base64'); request.setRequestHeader('Authorization', auth); } request.setRequestHeader('Content-Type', 'application/json'); - headers.forEach(function(header) { - request.setRequestHeader(header.name, header.value); - }); + if(this.headers) { + headers.forEach(function(header) { + request.setRequestHeader(header.name, header.value); + }); + } return request; }; From 92d79c6032cf046f39d21f502e6ffa32f7621b90 Mon Sep 17 00:00:00 2001 From: Eugen Lechner Date: Mon, 11 Dec 2017 15:45:24 +0100 Subject: [PATCH 6/6] fix null object --- lib/web3/httpprovider.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/web3/httpprovider.js b/lib/web3/httpprovider.js index c12fdedeac8..e12ebc0a819 100644 --- a/lib/web3/httpprovider.js +++ b/lib/web3/httpprovider.js @@ -70,7 +70,7 @@ HttpProvider.prototype.prepareRequest = function (async) { request.setRequestHeader('Authorization', auth); } request.setRequestHeader('Content-Type', 'application/json'); if(this.headers) { - headers.forEach(function(header) { + this.headers.forEach(function(header) { request.setRequestHeader(header.name, header.value); }); }