2018-05-24 15:09:04 +02:00
|
|
|
/**
|
|
|
|
* @author sevzero [sevzero@protonmail.com]
|
2018-08-19 18:36:40 +02:00
|
|
|
* @copyright Crown Copyright 2018
|
2018-05-24 15:09:04 +02:00
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2019-07-09 13:23:59 +02:00
|
|
|
import Operation from "../Operation.mjs";
|
2018-05-24 15:09:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Dechunk HTTP response operation
|
|
|
|
*/
|
2018-08-19 18:36:40 +02:00
|
|
|
class DechunkHTTPResponse extends Operation {
|
2018-05-24 15:09:04 +02:00
|
|
|
|
|
|
|
/**
|
2018-08-19 18:36:40 +02:00
|
|
|
* DechunkHTTPResponse constructor
|
2018-05-24 15:09:04 +02:00
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "Dechunk HTTP response";
|
|
|
|
this.module = "Default";
|
2018-08-19 18:36:40 +02:00
|
|
|
this.description = "Parses an HTTP response transferred using Transfer-Encoding: Chunked";
|
2018-08-21 20:07:13 +02:00
|
|
|
this.infoURL = "https://wikipedia.org/wiki/Chunked_transfer_encoding";
|
2018-05-24 15:09:04 +02:00
|
|
|
this.inputType = "string";
|
|
|
|
this.outputType = "string";
|
|
|
|
this.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);
|
2018-05-24 15:09:04 +02:00
|
|
|
while (!isNaN(chunkSize)) {
|
2018-05-24 15:39:14 +02:00
|
|
|
chunks.push(input.slice(chunkSizeEnd, chunkSize + chunkSizeEnd));
|
2018-05-24 15:09:04 +02:00
|
|
|
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;
|
2018-05-24 15:09:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-19 18:36:40 +02:00
|
|
|
export default DechunkHTTPResponse;
|