Added decoder for chunked HTTP encoding

This decoder will join up a HTTP response sent using chunked transfer encoding, raised in issue #168.

This is useful when attempting to extract files or gzipped responses sent using chunked transfer encoding, particularly when combined with the gunzip operation.
This commit is contained in:
sevzero 2018-05-24 13:09:04 +00:00
parent c29ea53405
commit 53bf52c989
4 changed files with 79 additions and 0 deletions

View file

@ -0,0 +1,50 @@
/**
* @author sevzero [sevzero@protonmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import Operation from "../Operation";
/**
* Dechunk HTTP response operation
*/
class DechunkHTTP extends Operation {
/**
* DechunkHTTP constructor
*/
constructor() {
super();
this.name = "Dechunk HTTP response";
this.module = "Default";
this.description = "Parses a HTTP response transferred using transfer-encoding:chunked";
this.inputType = "string";
this.outputType = "string";
this.args = [];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
var chunks = [];
var chunkSizeEnd = input.indexOf("\n") + 1;
var lineEndings = input.charAt(chunkSizeEnd - 2) == "\r" ? "\r\n" : "\n";
var lineEndingsLength = lineEndings.length;
var chunkSize = parseInt(input.slice(0, chunkSizeEnd), 16);
while (!isNaN(chunkSize)) {
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);
}
return chunks.join('') + input;
}
}
export default DechunkHTTP;