index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. This file is part of web3.js.
  3. web3.js is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. web3.js is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with web3.js. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file httpprovider.js
  15. * @authors:
  16. * Marek Kotewicz <marek@parity.io>
  17. * Marian Oancea
  18. * Fabian Vogelsteller <fabian@ethereum.org>
  19. * @date 2015
  20. */
  21. var errors = require('web3-core-helpers').errors;
  22. var XHR2 = require('xhr2-cookies').XMLHttpRequest; // jshint ignore: line
  23. var http = require('http');
  24. var https = require('https');
  25. /**
  26. * HttpProvider should be used to send rpc calls over http
  27. */
  28. var HttpProvider = function HttpProvider(host, options) {
  29. options = options || {};
  30. this.withCredentials = options.withCredentials || false;
  31. this.timeout = options.timeout || 0;
  32. this.headers = options.headers;
  33. this.agent = options.agent;
  34. this.connected = false;
  35. // keepAlive is true unless explicitly set to false
  36. const keepAlive = options.keepAlive !== false;
  37. this.host = host || 'http://localhost:8545';
  38. if (!this.agent) {
  39. if (this.host.substring(0, 5) === "https") {
  40. this.httpsAgent = new https.Agent({ keepAlive });
  41. }
  42. else {
  43. this.httpAgent = new http.Agent({ keepAlive });
  44. }
  45. }
  46. };
  47. HttpProvider.prototype._prepareRequest = function () {
  48. var request;
  49. // the current runtime is a browser
  50. if (typeof XMLHttpRequest !== 'undefined') {
  51. request = new XMLHttpRequest();
  52. }
  53. else {
  54. request = new XHR2();
  55. var agents = { httpsAgent: this.httpsAgent, httpAgent: this.httpAgent, baseUrl: this.baseUrl };
  56. if (this.agent) {
  57. agents.httpsAgent = this.agent.https;
  58. agents.httpAgent = this.agent.http;
  59. agents.baseUrl = this.agent.baseUrl;
  60. }
  61. request.nodejsSet(agents);
  62. }
  63. request.open('POST', this.host, true);
  64. request.setRequestHeader('Content-Type', 'application/json');
  65. request.timeout = this.timeout;
  66. request.withCredentials = this.withCredentials;
  67. if (this.headers) {
  68. this.headers.forEach(function (header) {
  69. request.setRequestHeader(header.name, header.value);
  70. });
  71. }
  72. return request;
  73. };
  74. /**
  75. * Should be used to make async request
  76. *
  77. * @method send
  78. * @param {Object} payload
  79. * @param {Function} callback triggered on end with (err, result)
  80. */
  81. HttpProvider.prototype.send = function (payload, callback) {
  82. var _this = this;
  83. var request = this._prepareRequest();
  84. request.onreadystatechange = function () {
  85. if (request.readyState === 4 && request.timeout !== 1) {
  86. var result = request.responseText;
  87. var error = null;
  88. try {
  89. result = JSON.parse(result);
  90. }
  91. catch (e) {
  92. error = errors.InvalidResponse(request.responseText);
  93. }
  94. _this.connected = true;
  95. callback(error, result);
  96. }
  97. };
  98. request.ontimeout = function () {
  99. _this.connected = false;
  100. callback(errors.ConnectionTimeout(this.timeout));
  101. };
  102. try {
  103. request.send(JSON.stringify(payload));
  104. }
  105. catch (error) {
  106. this.connected = false;
  107. callback(errors.InvalidConnection(this.host));
  108. }
  109. };
  110. HttpProvider.prototype.disconnect = function () {
  111. //NO OP
  112. };
  113. /**
  114. * Returns the desired boolean.
  115. *
  116. * @method supportsSubscriptions
  117. * @returns {boolean}
  118. */
  119. HttpProvider.prototype.supportsSubscriptions = function () {
  120. return false;
  121. };
  122. module.exports = HttpProvider;