node-http-proxy icon indicating copy to clipboard operation
node-http-proxy copied to clipboard

selfHandleResponse: true => response broken

Open HugoMuller opened this issue 7 years ago • 8 comments
trafficstars

I'm trying to manually handle the response with { selfHandleResponse: true }, but, as the doc says:

none of the webOutgoing passes are called

And that prevents me from correctly sending response.

This is the code causing the problem:

// passes/web-incoming.js
if(!res.headersSent && !options.selfHandleResponse) {
  for(var i=0; i < web_o.length; i++) {
    if(web_o[i](req, res, proxyRes, options)) { break; }
  }
}

Doing this, fixes it:

if(!res.headersSent) {
  for(var i=0; i < web_o.length; i++) {
    if(web_o[i](req, res, proxyRes, options)) { break; }
  }
}

I need to call the webOutgoing passes and do some extra stuff. But I can't. Maybe you can expose passes/web-outgoing, or add a flag like callWebOutgoing to let users call them if they want to.

HugoMuller avatar May 17 '18 16:05 HugoMuller

Hi, I am having the same issue. I am trying to forward a response after I have edited its body but the headers are empty.

Have you found another solution maybe?

@jcrugzz an idea maybe?

Thank you!

ctessier avatar Jun 05 '18 14:06 ctessier

Hi,

I found an ugly hack actually. I copy/paste a piece of node-http-proxy's internal code, and I give credit...

const web_o = Object.values(require('http-proxy/lib/http-proxy/passes/web-outgoing'));

// ...
  .on('proxyRes', (proxyRes, req, res) => {
    // code taken from here: https://github.com/nodejitsu/node-http-proxy/blob/42e8e1e099c086d818d8f62c8f15ec5a8f1a6624/lib/http-proxy/passes/web-incoming.js#L174
    for(var i=0; i < web_o.length; i++) {
      if(web_o[i](req, res, proxyRes, options)) { break; }
    }
  });

HugoMuller avatar Jun 05 '18 15:06 HugoMuller

Thank you @HugoMuller, gonna try that out!

ctessier avatar Jun 06 '18 05:06 ctessier

The issue is not fixed yet. I want to log the response in some scenarios. But it is always empty.

amitguptagwl avatar Jan 03 '19 08:01 amitguptagwl

@HugoMuller solution worked for me! But it would be nice if we didn't have to use that kind of hack... How would you feel about exposing some API that would allow us to utilize the web outgoing passes without the hack? If you're down with it, I can do some work and submit a PR

chiedo avatar Feb 03 '19 18:02 chiedo

@HugoMuller well, where is the options param defined?

Pines-Cheng avatar Apr 08 '21 08:04 Pines-Cheng

This issue is still not fixed. Is this repository still maintained?

cuixiping avatar Jul 14 '23 09:07 cuixiping

This issue is still not fixed. Is this repository still maintained?

I suppose not, as the last commit in the master branch is nearly 4 years old now.

PrimalZed avatar Apr 06 '24 00:04 PrimalZed