diff --git a/index.js b/index.js index efe71a6..a380bea 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ const express = require('express') const concat = require('concat-stream'); const { promisify } = require('util'); const promBundle = require("express-prom-bundle"); +const zlib = require("zlib"); const { PROMETHEUS_ENABLED = false, @@ -40,11 +41,16 @@ if(process.env.DISABLE_REQUEST_LOGS !== 'true'){ app.use(function(req, res, next){ req.pipe(concat(function(data){ - req.body = data.toString('utf8'); + + if (req.get("Content-Encoding") === "gzip") { + req.body = zlib.gunzipSync(data).toString('utf8'); + } + else { + req.body = data.toString('utf8'); + } next(); })); }); - //Handle all paths app.all('*', (req, res) => { diff --git a/tests.sh b/tests.sh index ecb8785..d56f4e5 100755 --- a/tests.sh +++ b/tests.sh @@ -155,6 +155,18 @@ else exit 1 fi + +message " Make JSON request with gzip Content-Encoding, and test that json is in the output. " +REQUEST=$(echo -n '{"a":"b"}' | gzip | curl -s -X POST -H "Content-Encoding: gzip" -H "Content-Type: application/json" --data-binary @- http://localhost:8080/) +if [ $(echo $REQUEST | jq -r '.json.a') == 'b' ] +then + passed "JSON test passed." +else + failed "JSON test failed." + echo $REQUEST | jq + exit 1 +fi + REQUEST=$(curl -s -X POST -H "Content-Type: application/json" -d 'not-json' http://localhost:8080) if [ $(echo $REQUEST | jq -r '.json') == 'null' ]; then passed "JSON with Invalid Body test passed."