Files
CyberChef/src/core/operations/DechunkHTTPResponse.mjs
T

59 lines
1.7 KiB
JavaScript
Raw Normal View History

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