From c4520e1f3c7fea6d03f36967c4ee72a0df417f2f Mon Sep 17 00:00:00 2001 From: matt-mercer <449892+matt-mercer@users.noreply.github.com> Date: Wed, 19 Mar 2025 17:05:19 +0000 Subject: [PATCH] basic handling of gzip content-encoding on requests as per https://github.com/mendhak/docker-http-https-echo/issues/31 - new test added to tests.sh --- index.js | 10 ++++++++-- tests.sh | 12 ++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) 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."