Skip to content

Instantly share code, notes, and snippets.

@APTy
Last active April 6, 2017 16:29
Show Gist options
  • Save APTy/bc62904c1956837a01d49d11767f8102 to your computer and use it in GitHub Desktop.
Save APTy/bc62904c1956837a01d49d11767f8102 to your computer and use it in GitHub Desktop.
urldump

urldump

This is a hexdump-style stream processing tool to convert from readable, UTF8-encoded text into URL-encoded output.

Install

sudo npm -g install git+https://gist.github.com/bc62904c1956837a01d49d11767f8102.git

Usage

$ echo '<script>alert(1)</script>' | urldump
%3c%73%63%72%69%70%74%3e%61%6c%65%72%74%28%31%29%3c%2f%73%63%72%69%70%74%3e%a
#!/usr/bin/env node
const STDIN_ENCODING = "utf8";
// Represents input received from stdin
class Stdin {
constructor(encoding = STDIN_ENCODING) {
this.close = () => null;
process.stdin.setEncoding(encoding);
process.stdin.on("end", () => this.close());
}
// Registers a callback to be invoked when data is read
onRead(fn) {
process.stdin.on("readable", () => {
var chunk = process.stdin.read();
if (chunk !== null) {
fn(chunk);
}
});
}
// Registers a callback to be invoked when stdin closes
onClose(fn) {
this.close = fn.bind(this);
}
}
// Converts a string to its url-encoded version
function urlEncodeString(string) {
var urlEncodeChar = (chr) => "%" + chr.charCodeAt(0).toString(16);
return [].map.call(string, urlEncodeChar).join("");
}
(function main() {
var stdin = new Stdin();
stdin.onClose(() => {
process.stdout.write("\n");
process.exit.bind(0);
});
stdin.onRead((d) => process.stdout.write(urlEncodeString(d)));
})();
{
"name": "urldump",
"version": "0.1.0",
"description": "convert between URL encoding and UTF-8",
"main": "index.js",
"bin": "./index.js",
"author": "",
"license": "MIT"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment