Skip to content

Commit

Permalink
Added decoder for chunked HTTP encoding
Browse files Browse the repository at this point in the history
This decoder will join up a HTTP response sent using chunked transfer encoding, raised in issue gchq#168.

This is useful when attempting to extract files or gzipped responses sent using chunked transfer encoding, particularly when combined with the gunzip operation.
  • Loading branch information
sevzero committed May 23, 2018
1 parent dcc2843 commit 37a86b5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/core/config/Categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ const Categories = [
ops: [
"HTTP request",
"Strip HTTP headers",
"Dechunk HTTP response",
"Parse User Agent",
"Parse IP range",
"Parse IPv6 address",
Expand Down
7 changes: 7 additions & 0 deletions src/core/config/OperationConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -1846,6 +1846,13 @@ const OperationConfig = {
outputType: "string",
args: []
},
"Dechunk HTTP response": {
module: "HTTP",
description: "Parses a HTTP response transferred using transfer-encoding:chunked",
inputType: "string",
outputType: "string",
args: []
},
"Parse User Agent": {
module: "HTTP",
description: "Attempts to identify and categorise information contained in a user-agent string.",
Expand Down
1 change: 1 addition & 0 deletions src/core/config/modules/HTTP.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ let OpModules = typeof self === "undefined" ? {} : self.OpModules || {};
OpModules.HTTP = {
"HTTP request": HTTP.runHTTPRequest,
"Strip HTTP headers": HTTP.runStripHeaders,
"Dechunk HTTP response": HTTP.runDechunk,
"Parse User Agent": HTTP.runParseUserAgent,
};

Expand Down
22 changes: 22 additions & 0 deletions src/core/operations/HTTP.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ const HTTP = {
return (headerEnd < 2) ? input : input.slice(headerEnd, input.length);
},

/**
* Dechunk response operation
*
* @param {string} input
* @param {Object[]} args}
* @returns {string}
*/
runDechunk: function(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;
},


/**
* Parse User Agent operation.
Expand Down

0 comments on commit 37a86b5

Please sign in to comment.