|
1 | 1 | /* globals unescape */ |
2 | 2 | import Utils from "../Utils.js"; |
| 3 | +import url from "url"; |
3 | 4 |
|
4 | 5 |
|
5 | 6 | /** |
@@ -58,56 +59,36 @@ const URL_ = { |
58 | 59 | * @returns {string} |
59 | 60 | */ |
60 | 61 | runParse: function(input, args) { |
61 | | - if (!document) { |
62 | | - throw "This operation only works in a browser."; |
63 | | - } |
64 | | - |
65 | | - const a = document.createElement("a"); |
66 | | - |
67 | | - // Overwrite base href which will be the current CyberChef URL to reduce confusion. |
68 | | - a.href = "http://example.com/"; |
69 | | - a.href = input; |
70 | | - |
71 | | - if (a.protocol) { |
72 | | - let output = ""; |
73 | | - if (a.hostname !== window.location.hostname) { |
74 | | - output = "Protocol:\t" + a.protocol + "\n"; |
75 | | - if (a.hostname) output += "Hostname:\t" + a.hostname + "\n"; |
76 | | - if (a.port) output += "Port:\t\t" + a.port + "\n"; |
77 | | - } |
78 | | - |
79 | | - if (a.pathname && a.pathname !== window.location.pathname) { |
80 | | - let pathname = a.pathname; |
81 | | - if (pathname.indexOf(window.location.pathname) === 0) |
82 | | - pathname = pathname.replace(window.location.pathname, ""); |
83 | | - if (pathname) |
84 | | - output += "Path name:\t" + pathname + "\n"; |
85 | | - } |
86 | | - |
87 | | - if (a.hash && a.hash !== window.location.hash) { |
88 | | - output += "Hash:\t\t" + a.hash + "\n"; |
89 | | - } |
90 | | - |
91 | | - if (a.search && a.search !== window.location.search) { |
92 | | - output += "Arguments:\n"; |
93 | | - const args_ = (a.search.slice(1, a.search.length)).split("&"); |
94 | | - let splitArgs = [], padding = 0, i; |
95 | | - for (i = 0; i < args_.length; i++) { |
96 | | - splitArgs.push(args_[i].split("=")); |
97 | | - padding = (splitArgs[i][0].length > padding) ? splitArgs[i][0].length : padding; |
98 | | - } |
99 | | - for (i = 0; i < splitArgs.length; i++) { |
100 | | - output += "\t" + Utils.padRight(splitArgs[i][0], padding); |
101 | | - if (splitArgs[i].length > 1 && splitArgs[i][1].length) |
102 | | - output += " = " + splitArgs[i][1] + "\n"; |
103 | | - else output += "\n"; |
| 62 | + const uri = url.parse(input, true); |
| 63 | + |
| 64 | + let output = ""; |
| 65 | + |
| 66 | + if (uri.protocol) output += "Protocol:\t" + uri.protocol + "\n"; |
| 67 | + if (uri.auth) output += "Auth:\t\t" + uri.auth + "\n"; |
| 68 | + if (uri.hostname) output += "Hostname:\t" + uri.hostname + "\n"; |
| 69 | + if (uri.port) output += "Port:\t\t" + uri.port + "\n"; |
| 70 | + if (uri.pathname) output += "Path name:\t" + uri.pathname + "\n"; |
| 71 | + if (uri.query) { |
| 72 | + let keys = Object.keys(uri.query), |
| 73 | + padding = 0; |
| 74 | + |
| 75 | + keys.forEach(k => { |
| 76 | + padding = (k.length > padding) ? k.length : padding; |
| 77 | + }); |
| 78 | + |
| 79 | + output += "Arguments:\n"; |
| 80 | + for (let key in uri.query) { |
| 81 | + output += "\t" + Utils.padRight(key, padding); |
| 82 | + if (uri.query[key].length) { |
| 83 | + output += " = " + uri.query[key] + "\n"; |
| 84 | + } else { |
| 85 | + output += "\n"; |
104 | 86 | } |
105 | 87 | } |
106 | | - |
107 | | - return output; |
108 | 88 | } |
| 89 | + if (uri.hash) output += "Hash:\t\t" + uri.hash + "\n"; |
109 | 90 |
|
110 | | - return "Invalid URI"; |
| 91 | + return output; |
111 | 92 | }, |
112 | 93 |
|
113 | 94 |
|
|
0 commit comments