Skip to content

Commit

Permalink
fix: content-length header issue #10
Browse files Browse the repository at this point in the history
  • Loading branch information
qianlang committed Sep 26, 2017
1 parent 2de1ec7 commit 2a7dee1
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var proxy = httpProxy.createProxyServer({

// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', function (proxyRes, req, res) {
modifyResponse(res, proxyRes.headers['content-encoding'], function (body) {
modifyResponse(res, proxyRes, function (body) {
if (body) {
// modify some information
body.age = 2;
Expand Down Expand Up @@ -88,7 +88,7 @@ var proxy = httpProxy.createProxyServer({

// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', function (proxyRes, req, res) {
modifyResponse(res, proxyRes.headers['content-encoding'], function (body) {
modifyResponse(res, proxyRes, function (body) {
if (body) {
// modify some information
body.age = 2;
Expand Down Expand Up @@ -145,7 +145,7 @@ var proxy = httpProxy.createProxyServer({

// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', function (proxyRes, req, res) {
modifyResponse(res, proxyRes.headers['content-encoding'], function (body) {
modifyResponse(res, proxyRes, function (body) {
if (body) {
// modify some information
body.age = 2;
Expand Down
16 changes: 13 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@ const BufferHelper = require('bufferhelper');
/**
* Modify the response of json
* @param res {Response} The http response
* @param contentEncoding {String} The http header content-encoding: gzip/deflate
* @param proxyRes {proxyRes|String} String: The http header content-encoding: gzip/deflate
* @param callback {Function} Custom modified logic
*/
module.exports = function modifyResponse(res, contentEncoding, callback) {
module.exports = function modifyResponse(res, proxyRes, callback) {
let contentEncoding = proxyRes;
if (proxyRes && proxyRes.headers) {
contentEncoding = proxyRes.headers['content-encoding'];
// Delete the content-length if it exists. Otherwise, an exception will occur
// @see: https://github.com/langjt/node-http-proxy-json/issues/10
if ('content-length' in proxyRes.headers) {
delete proxyRes.headers['content-length'];
}
}

let unzip, zip;
// Now only deal with the gzip/deflate/undefined content-encoding.
switch (contentEncoding) {
Expand All @@ -29,7 +39,7 @@ module.exports = function modifyResponse(res, contentEncoding, callback) {
let _end = res.end;

if (unzip) {
unzip.on('error', function(e) {
unzip.on('error', function (e) {
console.log('Unzip error: ', e);
_end.call(res);
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-http-proxy-json",
"version": "0.1.4",
"version": "0.1.5",
"description": "for node-http-proxy transform the response json from the proxied server.",
"main": "index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions test/deflate.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('modifyResponse--deflate', () => {
beforeEach(() => {
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', (proxyRes, req, res) => {
modifyResponse(res, proxyRes.headers['content-encoding'], body => {
modifyResponse(res, proxyRes, body => {
if (body) {
// modify some information
body.age = 2;
Expand Down Expand Up @@ -105,7 +105,7 @@ describe('modifyResponse--deflate', () => {
beforeEach(() => {
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', (proxyRes, req, res) => {
modifyResponse(res, proxyRes.headers['content-encoding'], body => {
modifyResponse(res, proxyRes, body => {
if (body) {
// modify some information
body.age = 2;
Expand Down
4 changes: 2 additions & 2 deletions test/gzip.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('modifyResponse--gzip', function() {
beforeEach(() => {
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', (proxyRes, req, res) => {
modifyResponse(res, proxyRes.headers['content-encoding'], body => {
modifyResponse(res, proxyRes, body => {
if (body) {
// modify some information
body.age = 2;
Expand Down Expand Up @@ -107,7 +107,7 @@ describe('modifyResponse--gzip', function() {
beforeEach(() => {
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', (proxyRes, req, res) => {
modifyResponse(res, proxyRes.headers['content-encoding'], body => {
modifyResponse(res, proxyRes, body => {
if (body) {
// modify some information
body.age = 2;
Expand Down
4 changes: 2 additions & 2 deletions test/uncompressed.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('modifyResponse--uncompressed', function() {
beforeEach(() => {
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', function(proxyRes, req, res) {
modifyResponse(res, proxyRes.headers['content-encoding'], body => {
modifyResponse(res, proxyRes, body => {
if (body) {
// modify some information
body.age = 2;
Expand Down Expand Up @@ -82,7 +82,7 @@ describe('modifyResponse--uncompressed', function() {
beforeEach(() => {
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', (proxyRes, req, res) => {
modifyResponse(res, proxyRes.headers['content-encoding'], body => {
modifyResponse(res, proxyRes, body => {
if (body) {
// modify some information
body.age = 2;
Expand Down

0 comments on commit 2a7dee1

Please sign in to comment.