2016-11-28 10:42:58 +00:00
|
|
|
/* globals unescape */
|
2017-03-23 17:52:20 +00:00
|
|
|
import Utils from "../Utils.js";
|
2017-03-06 12:45:51 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* URL operations.
|
|
|
|
* Namespace is appended with an underscore to prevent overwriting the global URL object.
|
|
|
|
*
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*
|
|
|
|
* @namespace
|
|
|
|
*/
|
2017-03-23 17:52:20 +00:00
|
|
|
const URL_ = {
|
2016-11-28 10:42:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
ENCODE_ALL: false,
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* URL Encode operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runTo: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
const encodeAll = args[0];
|
2017-01-31 18:24:56 +00:00
|
|
|
return encodeAll ? URL_._encodeAllChars(input) : encodeURI(input);
|
2016-11-28 10:42:58 +00:00
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* URL Decode operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runFrom: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
const data = input.replace(/\+/g, "%20");
|
2016-11-28 10:42:58 +00:00
|
|
|
try {
|
|
|
|
return decodeURIComponent(data);
|
2017-02-09 15:09:33 +00:00
|
|
|
} catch (err) {
|
2016-11-28 10:42:58 +00:00
|
|
|
return unescape(data);
|
|
|
|
}
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Parse URI operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runParse: function(input, args) {
|
2017-03-30 00:40:20 +01:00
|
|
|
if (!document) {
|
|
|
|
throw "This operation only works in a browser.";
|
|
|
|
}
|
2017-03-30 20:17:40 +01:00
|
|
|
|
2017-04-13 18:08:50 +01:00
|
|
|
const a = document.createElement("a");
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
// Overwrite base href which will be the current CyberChef URL to reduce confusion.
|
|
|
|
a.href = "http://example.com/";
|
|
|
|
a.href = input;
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
if (a.protocol) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let output = "";
|
2016-12-14 16:39:17 +00:00
|
|
|
if (a.hostname !== window.location.hostname) {
|
2016-11-28 10:42:58 +00:00
|
|
|
output = "Protocol:\t" + a.protocol + "\n";
|
|
|
|
if (a.hostname) output += "Hostname:\t" + a.hostname + "\n";
|
|
|
|
if (a.port) output += "Port:\t\t" + a.port + "\n";
|
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-12-31 17:12:39 +00:00
|
|
|
if (a.pathname && a.pathname !== window.location.pathname) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let pathname = a.pathname;
|
2016-11-28 10:42:58 +00:00
|
|
|
if (pathname.indexOf(window.location.pathname) === 0)
|
|
|
|
pathname = pathname.replace(window.location.pathname, "");
|
|
|
|
if (pathname)
|
|
|
|
output += "Path name:\t" + pathname + "\n";
|
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-12-31 17:12:39 +00:00
|
|
|
if (a.hash && a.hash !== window.location.hash) {
|
2016-11-28 10:42:58 +00:00
|
|
|
output += "Hash:\t\t" + a.hash + "\n";
|
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-12-31 17:12:39 +00:00
|
|
|
if (a.search && a.search !== window.location.search) {
|
2016-11-28 10:42:58 +00:00
|
|
|
output += "Arguments:\n";
|
2017-04-13 18:08:50 +01:00
|
|
|
const args_ = (a.search.slice(1, a.search.length)).split("&");
|
2017-04-13 18:31:26 +01:00
|
|
|
let splitArgs = [], padding = 0, i;
|
|
|
|
for (i = 0; i < args_.length; i++) {
|
2017-01-31 18:24:56 +00:00
|
|
|
splitArgs.push(args_[i].split("="));
|
|
|
|
padding = (splitArgs[i][0].length > padding) ? splitArgs[i][0].length : padding;
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
2017-01-31 18:24:56 +00:00
|
|
|
for (i = 0; i < splitArgs.length; i++) {
|
|
|
|
output += "\t" + Utils.padRight(splitArgs[i][0], padding);
|
|
|
|
if (splitArgs[i].length > 1 && splitArgs[i][1].length)
|
|
|
|
output += " = " + splitArgs[i][1] + "\n";
|
2016-11-28 10:42:58 +00:00
|
|
|
else output += "\n";
|
|
|
|
}
|
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
return output;
|
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
return "Invalid URI";
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* URL encodes additional special characters beyond the standard set.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {string} str
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
_encodeAllChars: function(str) {
|
2016-11-28 10:42:58 +00:00
|
|
|
//TODO Do this programatically
|
|
|
|
return encodeURIComponent(str)
|
|
|
|
.replace(/!/g, "%21")
|
|
|
|
.replace(/#/g, "%23")
|
|
|
|
.replace(/'/g, "%27")
|
|
|
|
.replace(/\(/g, "%28")
|
|
|
|
.replace(/\)/g, "%29")
|
|
|
|
.replace(/\*/g, "%2A")
|
2017-07-24 13:49:16 +00:00
|
|
|
.replace(/-/g, "%2D")
|
2016-11-28 10:42:58 +00:00
|
|
|
.replace(/\./g, "%2E")
|
|
|
|
.replace(/_/g, "%5F")
|
|
|
|
.replace(/~/g, "%7E");
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
2017-03-23 17:52:20 +00:00
|
|
|
|
|
|
|
export default URL_;
|