Skip to content

Commit

Permalink
Merge branch 'sevzero-esm'
Browse files Browse the repository at this point in the history
  • Loading branch information
n1474335 committed Aug 19, 2018
2 parents b9703e2 + ad2424c commit 6be7ac8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
"ops": [
"HTTP request",
"Strip HTTP headers",
"Dechunk HTTP response",
"Parse User Agent",
"Parse IP range",
"Parse IPv6 address",
Expand Down
50 changes: 50 additions & 0 deletions src/core/operations/DechunkHTTPResponse.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @author sevzero [sevzero@protonmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/

import Operation from "../Operation";

/**
* 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.inputType = "string";
this.outputType = "string";
this.args = [];
}

/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const chunks = [];
let chunkSizeEnd = input.indexOf("\n") + 1;
const lineEndings = input.charAt(chunkSizeEnd - 2) === "\r" ? "\r\n" : "\n";
const lineEndingsLength = lineEndings.length;
let 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 DechunkHTTPResponse;

0 comments on commit 6be7ac8

Please sign in to comment.