CyberChef/src/core/operations/DechunkHTTPResponse.mjs

63 lines
1.8 KiB
JavaScript
Raw Normal View History

/**
* @author sevzero [sevzero@protonmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
/**
* Dechunk HTTP response operation
*/
class DechunkHTTPResponse extends Operation {
/**
* DechunkHTTPResponse constructor
*/
constructor() {
super();
this.name = "Dechunk HTTP response";
this.module = "Default";
this.description = "Parses an HTTP response transferred using Transfer-Encoding: Chunked";
this.infoURL = "https://wikipedia.org/wiki/Chunked_transfer_encoding";
this.inputType = "string";
this.outputType = "string";
this.args = [];
2020-02-25 12:27:03 +01:00
this.checks = {
input: {
regex: [
{
match: "^\\s*[0-9A-F]+\r\n",
flags: "i",
args: []
}
]
}
};
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
2018-05-24 15:39:14 +02:00
const chunks = [];
2018-05-24 15:17:46 +02:00
let chunkSizeEnd = input.indexOf("\n") + 1;
2018-05-24 15:39:14 +02:00
const lineEndings = input.charAt(chunkSizeEnd - 2) === "\r" ? "\r\n" : "\n";
const lineEndingsLength = lineEndings.length;
2018-05-24 15:17:46 +02:00
let chunkSize = parseInt(input.slice(0, chunkSizeEnd), 16);
while (!isNaN(chunkSize)) {
2018-05-24 15:39:14 +02:00
chunks.push(input.slice(chunkSizeEnd, chunkSize + chunkSizeEnd));
input = input.slice(chunkSizeEnd + chunkSize + lineEndingsLength);
chunkSizeEnd = input.indexOf(lineEndings) + lineEndingsLength;
chunkSize = parseInt(input.slice(0, chunkSizeEnd), 16);
}
2018-05-24 15:17:46 +02:00
return chunks.join("") + input;
}
}
export default DechunkHTTPResponse;