HttpRequestHeaders.jsm 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*******************************************************************************
  2. ηMatrix - a browser extension to black/white list requests.
  3. Copyright (C) 2014-2019 The uMatrix/uBlock Origin authors
  4. Copyright (C) 2019-2020-2021 Alessio Vanni
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see {http://www.gnu.org/licenses/}.
  15. Home: https://gitlab.com/vannilla/ematrix
  16. uMatrix Home: https://github.com/gorhill/uMatrix
  17. */
  18. 'use strict';
  19. var EXPORTED_SYMBOLS = ['HTTPRequestHeaders'];
  20. var junkyard = [];
  21. var HTTPRequestHeaders = function (channel) {
  22. this.init(channel);
  23. };
  24. HTTPRequestHeaders.factory = function (channel) {
  25. let entry = junkyard.pop();
  26. if (entry) {
  27. return entry.init(channel);
  28. }
  29. return new HTTPRequestHeaders(channel);
  30. }
  31. HTTPRequestHeaders.prototype.init = function (channel) {
  32. this.channel = channel;
  33. this.headers = new Array();
  34. this.originalHeaderNames = new Array();
  35. channel.visitRequestHeaders({
  36. visitHeader: function (name, value) {
  37. this.headers.push({name: name, value: value});
  38. this.originalHeaderNames.push(name);
  39. }.bind(this)
  40. });
  41. return this;
  42. };
  43. HTTPRequestHeaders.prototype.dispose = function () {
  44. this.channel = null;
  45. this.headers = null;
  46. this.originalHeaderNames = null;
  47. junkyard.push(this);
  48. };
  49. HTTPRequestHeaders.prototype.update = function () {
  50. let newHeaderNames = new Set();
  51. for (let header of this.headers) {
  52. this.setHeader(header.name, header.value, true);
  53. newHeaderNames.add(header.name);
  54. }
  55. //Clear any headers that were removed
  56. for (let name of this.originalHeaderNames) {
  57. if (!newHeaderNames.has(name)) {
  58. this.channel.setRequestHeader(name, '', false);
  59. }
  60. }
  61. };
  62. HTTPRequestHeaders.prototype.getHeader = function (name) {
  63. try {
  64. return this.channel.getRequestHeader(name);
  65. } catch (e) {
  66. // Ignore
  67. }
  68. return '';
  69. };
  70. HTTPRequestHeaders.prototype.setHeader = function (name, newValue, create) {
  71. let oldValue = this.getHeader(name);
  72. if (newValue === oldValue) {
  73. return false;
  74. }
  75. if (oldValue === '' && create !== true) {
  76. return false;
  77. }
  78. this.channel.setRequestHeader(name, newValue, false);
  79. return true;
  80. };