URL.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* globals unescape */
  2. /**
  3. * URL operations.
  4. * Namespace is appended with an underscore to prevent overwriting the global URL object.
  5. *
  6. * @author n1474335 [n1474335@gmail.com]
  7. * @copyright Crown Copyright 2016
  8. * @license Apache-2.0
  9. *
  10. * @namespace
  11. */
  12. var URL_ = {
  13. /**
  14. * @constant
  15. * @default
  16. */
  17. ENCODE_ALL: false,
  18. /**
  19. * URL Encode operation.
  20. *
  21. * @param {string} input
  22. * @param {Object[]} args
  23. * @returns {string}
  24. */
  25. runTo: function(input, args) {
  26. var encodeAll = args[0];
  27. return encodeAll ? URL_._encodeAllChars(input) : encodeURI(input);
  28. },
  29. /**
  30. * URL Decode operation.
  31. *
  32. * @param {string} input
  33. * @param {Object[]} args
  34. * @returns {string}
  35. */
  36. runFrom: function(input, args) {
  37. var data = input.replace(/\+/g, "%20");
  38. try {
  39. return decodeURIComponent(data);
  40. } catch (err) {
  41. return unescape(data);
  42. }
  43. },
  44. /**
  45. * Parse URI operation.
  46. *
  47. * @param {string} input
  48. * @param {Object[]} args
  49. * @returns {string}
  50. */
  51. runParse: function(input, args) {
  52. var a = document.createElement("a");
  53. // Overwrite base href which will be the current CyberChef URL to reduce confusion.
  54. a.href = "http://example.com/";
  55. a.href = input;
  56. if (a.protocol) {
  57. var output = "";
  58. if (a.hostname !== window.location.hostname) {
  59. output = "Protocol:\t" + a.protocol + "\n";
  60. if (a.hostname) output += "Hostname:\t" + a.hostname + "\n";
  61. if (a.port) output += "Port:\t\t" + a.port + "\n";
  62. }
  63. if (a.pathname && a.pathname !== window.location.pathname) {
  64. var pathname = a.pathname;
  65. if (pathname.indexOf(window.location.pathname) === 0)
  66. pathname = pathname.replace(window.location.pathname, "");
  67. if (pathname)
  68. output += "Path name:\t" + pathname + "\n";
  69. }
  70. if (a.hash && a.hash !== window.location.hash) {
  71. output += "Hash:\t\t" + a.hash + "\n";
  72. }
  73. if (a.search && a.search !== window.location.search) {
  74. output += "Arguments:\n";
  75. var args_ = (a.search.slice(1, a.search.length)).split("&");
  76. var splitArgs = [], padding = 0;
  77. for (var i = 0; i < args_.length; i++) {
  78. splitArgs.push(args_[i].split("="));
  79. padding = (splitArgs[i][0].length > padding) ? splitArgs[i][0].length : padding;
  80. }
  81. for (i = 0; i < splitArgs.length; i++) {
  82. output += "\t" + Utils.padRight(splitArgs[i][0], padding);
  83. if (splitArgs[i].length > 1 && splitArgs[i][1].length)
  84. output += " = " + splitArgs[i][1] + "\n";
  85. else output += "\n";
  86. }
  87. }
  88. return output;
  89. }
  90. return "Invalid URI";
  91. },
  92. /**
  93. * URL encodes additional special characters beyond the standard set.
  94. *
  95. * @private
  96. * @param {string} str
  97. * @returns {string}
  98. */
  99. _encodeAllChars: function(str) {
  100. //TODO Do this programatically
  101. return encodeURIComponent(str)
  102. .replace(/!/g, "%21")
  103. .replace(/#/g, "%23")
  104. .replace(/'/g, "%27")
  105. .replace(/\(/g, "%28")
  106. .replace(/\)/g, "%29")
  107. .replace(/\*/g, "%2A")
  108. .replace(/\-/g, "%2D")
  109. .replace(/\./g, "%2E")
  110. .replace(/_/g, "%5F")
  111. .replace(/~/g, "%7E");
  112. },
  113. };