Skip to content

basic handling of gzip content-encoding on requests as per https://gi… #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) => {

Expand Down
12 changes: 12 additions & 0 deletions tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
Loading