-
Notifications
You must be signed in to change notification settings - Fork 22
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
"Can't remove headers after they are sent" error when using interceptor with proxy #32
Comments
This is what the log looks like when using Tamper:
|
Hey @David-Hari, the way interceptor works is by overriding res.write and res.end, and it's a Node.js thing (perhaps more general to say HTTP 1.x thing?) that it's not possible to write headers after the first chunk of body is sent, in this particular case, the logs point out that something is writing to the socket, and then there is a header invalid change, which is a node error in this case. Some ideas to try out:
|
The only bit of my code that is intentionally modifying the headers, is the line in proxyRequest which sets 'accept-encoding'. But that's just changing the property directly, and that error is only triggered when the private _headers property is set in OutgoingMessage. Again, the tamper library works perfectly in this case (and indeed I am using it for the moment, although it seems to currently be unmaintained), so it must be something wrong with express-interceptor. |
I can see that tamper is overwriting "writeHead", whereas interceptor does not appear to. I think that may have something to do with the problem. |
@David-Hari Your theory sounds plausible. The http-proxy module is indeed writing headers directly. https://github.com/nodejitsu/node-http-proxy/blob/master/lib/http-proxy/passes/web-outgoing.js#L99 I have a test case locally that reproduces the error, and I will push a pull request as soon as I have had some time to look into it. |
Maybe i'm missing something but the problem as i've perceived is not in interceptor according to the logs presented. Headers setting doesn't cause I vote this a no-fix, both because it can create complexities for the future, and there are ways as I've suggested above to make it work properly within existing mechanisms. Altho this lib exist in the hacky as can be space it seems that |
One doesn't rule out the other. We can proxy the |
I haven't had the time to review either, crazy last weeks. I think I can taking a look on next week. Thanks @flockonus and @gustavnikolaj for the quick response 💯. |
I have some partial work on my checkout at work. I'll try to see what I can come up with, hopefully early next week. |
Perhaps there is a way I can get the proxy to not send the header at that point. It's being done on response of the proxy request (https://github.com/nodejitsu/node-http-proxy/blob/master/lib/http-proxy/passes/web-incoming.js#L149). If not, @gustavnikolaj's idea of disabling streaming when {{writeHead}} has been called sounds good. |
Unfortunately, streaming is not yet a thing. We buffer up every thing until I read some of my comments this morning and they seemed non-sensical until Everything is buffered, and the Content-Length is set in the end-event The call to writeHead also flushes headers, which is why we get the error When we introduce streaming we would have to change this last-minute header If we are buffering and we see a call to writeHead, we should remember the Hope this makes more sense. And I hope that my memory of the code is good Den fredag den 4. september 2015 skrev David notifications@github.com:
|
Yes, that's what the tamper library appears to be doing: https://github.com/fgnass/tamper/blob/master/lib/tamper.js#L63 |
We were not setting the content-length header but removing it. But the result was the same. Please take a look at my pr above :-) |
A new version was released @David-Hari. If there is a problem with it let us know, and in case you're using streaming check #15 or https://github.com/axiomzen/express-interceptor#similar-to. |
Hi,
I have a mock server (for development purposes) that serves client-side files and proxies API requests to a real server. I am using node-http-proxy for the proxy, and have it set up as a middleware.
Now I need to intercept the data coming back from the server and mangle it a bit before sending it back to the browser.
I have set up express-interceptor to run before the proxy, intercepting all requests of content type 'application/json'.
When I make a request to the API I get a "Can't remove headers after they are sent" error. Below is the debug log:
I should point out that the tamper library does work when used this way, so I know it is possible to do.
The text was updated successfully, but these errors were encountered: