Skip to content

Commit

Permalink
HttpProvider: CORS issue with Firefox and Safari (web3#3112)
Browse files Browse the repository at this point in the history
* CORS error fixed
  • Loading branch information
nivida authored and nachomazzara committed Jun 4, 2020
1 parent d156289 commit 45707eb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ Released with 1.0.0-beta.37 code base.
- Fix incorrectly populating chainId param with `net_version` when signing txs (#2378)
- regeneratorRuntime error fixed (#3058)
- Fix accessing event.name where event is undefined (#3014)
- HttpProvider: CORS issue with Firefox and Safari (#2978)
1 change: 0 additions & 1 deletion dist/web3.min.js

This file was deleted.

32 changes: 19 additions & 13 deletions packages/web3-providers-http/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/

var errors = require('web3-core-helpers').errors;
var XHR2 = require('xhr2-cookies').XMLHttpRequest // jshint ignore: line
var XHR2 = require('xhr2-cookies').XMLHttpRequest; // jshint ignore: line
var http = require('http');
var https = require('https');

Expand All @@ -34,32 +34,38 @@ var https = require('https');
var HttpProvider = function HttpProvider(host, options) {
options = options || {};

var keepAlive =
(options.keepAlive === true || options.keepAlive !== false) ?
true :
false;
var keepAlive = (options.keepAlive === true || options.keepAlive !== false) ? true : false;
this.host = host || 'http://localhost:8545';
if (this.host.substring(0,5) === "https") {
this.httpsAgent = new https.Agent({ keepAlive: keepAlive });
}else{
} else {
this.httpAgent = new http.Agent({ keepAlive: keepAlive });
}

this.withCredentials = options.withCredentials || false;
this.timeout = options.timeout || 0;
this.headers = options.headers;
this.connected = false;
};

HttpProvider.prototype._prepareRequest = function(){
var request = new XHR2();
request.nodejsSet({
httpsAgent:this.httpsAgent,
httpAgent:this.httpAgent
});
var request;

// the current runtime is a browser
if (typeof XMLHttpRequest !== 'undefined') {
request = new XMLHttpRequest();
} else {
request = new XHR2();
request.nodejsSet({
httpsAgent:this.httpsAgent,
httpAgent:this.httpAgent
});
}

request.open('POST', this.host, true);
request.setRequestHeader('Content-Type','application/json');
request.timeout = this.timeout && this.timeout !== 1 ? this.timeout : 0;
request.withCredentials = true;
request.timeout = this.timeout;
request.withCredentials = this.withCredentials;

if(this.headers) {
this.headers.forEach(function(header) {
Expand Down

0 comments on commit 45707eb

Please sign in to comment.