chimurai / http-proxy-middleware

:zap: The one-liner node.js http-proxy middleware for connect, express, next.js and more
MIT License
10.77k stars 851 forks source link

Multipart upload reset connection #265

Open GiuliaBusnelli opened 6 years ago

GiuliaBusnelli commented 6 years ago

I need to proxy a multipart upload request from a web application to an API server. The target server checks the file size and if it is bigger than expected return a 413 - Payload Too Large with a message.

If I send the request directly to the API server, I get the expected 413. But if I send the request to the proxy, the connection is reset.

Setup

Code

Here is my code

// Set request timeout
var setTimeout = function(req, res, next) {
  req.setTimeout(30*60*1000);
  next();
}

// Set up API proxy
var options = {
  logLevel: process.env.LOG_LEVEL || 'info',
  target: process.env.PROXY_TARGET,
  pathRewrite: {
    '^/webapp/api' : '/api'
  },
  onProxyReq: function(proxyReq, req, res) {
    // Add username header to request
    proxyReq.setHeader('X-DevID', req.session[ cas.session_name ]);
  }
}
var apiProxy = proxy(options)

app.use('/graph4u-webapp/api', setTimeout, apiProxy);
var server = app.listen(process.env.PORT)

If I add the following snippet of code as an option

onProxyRes: function(proxyRes, req, res) {
  proxyRes.on('data', function(data) {
    console.log(data.toString('utf-8'));
  });
}

this is what is printed:

{
  "httpStatusValue":413,
  "errorName":"PAYLOAD_TOO_LARGE",
  "message":"La dimensione del file caricato (14MB) supera quella massima consentita (10MB).",
  "url":"..."
}

So it seems to me that the proxy receives the expected response form the API server but then it does not forward it correctly.

Can someone please help me fix this issue? Let me know if I must add more information and feel free to ask questions.

chimurai commented 6 years ago

Not sure what's happening.

Providing some extra information on the target server and its configuration might help.

Even better would be to provide a minimal setup in which this issue can be reproduced.

qianlongdoit commented 5 years ago

I meet the same quetion too, when i use the proxy ,the chrome show net::ERR_CONNECTION_RESET, when without proxy, chrome shows 413 (Request Entity Too Large)

chimurai commented 5 years ago

Think this is an issue for the upstream library http-proxy.

Try creating a issue on their issue tracker: https://github.com/nodejitsu/node-http-proxy/issues

To help out investigation; Is it reproducible with just the http-proxy library? You can use a their example: https://github.com/nodejitsu/node-http-proxy/blob/master/examples/http/reverse-proxy.js

ConsidNdr commented 3 years ago

I looked at this issue node-http-proxy. Found it quite similar to this issue. Try this

onProxyReq: (proxyReq, req, res) => {
        const contentType = proxyReq.getHeader('Content-Type');
        let bodyData;

        if (contentType === 'application/json') {
            bodyData = JSON.stringify(req.body);
        }

        if (contentType === 'application/x-www-form-urlencoded') {
            bodyData = queryString.stringify(req.body);
        }

        if (bodyData) {
            proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
            proxyReq.write(bodyData);
        }
}
SivaKrishnaKola commented 3 years ago

@ConsidNdr Thank you soo much. It is working for me..