Skip to content

add beforeProxyRequest callback config option #1091

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

Closed
wants to merge 7 commits into from

Conversation

rick-kilgore
Copy link

@rick-kilgore rick-kilgore commented Nov 14, 2016

Fix for #867, and possibly these others: #908, #930, #889, #680.

This solution was suggested by @giuliopaci in #867. The problem with the header re-writing example in the README is that when you do a POST, the headers are sent before the 'proxyReq' event is fired. So you really have to add the headers before calling request(). This patch allows the user to register a synchronous callback that gets called in setupOutgoing() in which the caller can setup headers, or alter the outgoing request() options in whatever way desired.

Rick Kilgore added 7 commits November 4, 2016 16:37
provide beforeProxyRequest hook for manipulating outgoing request options (e.g., adding headers)
add description of beforeProxyRequest option
update header re-writing example
test beforeProxyRequest
fix header re-writing example in README
@Jeremy-iCars
Copy link

👍

@jcrugzz
Copy link
Contributor

jcrugzz commented Dec 2, 2016

@Jeremy-iCars @rick-kilgore in this case why not just pass the headers in? I guess we could be more defensive in this case and not fire the event on post requests if it can have this type of unexpected consequences. I just think adding more options isnt the right solution.

@rick-kilgore
Copy link
Author

@jcrugzz Pass the headers in where? In my case, I need to dynamically choose what to send in the way of headers based on the request. Specifically, I'm supplying a default header value only if the original request does not contain the header.

@jcrugzz
Copy link
Contributor

jcrugzz commented Dec 3, 2016

@rick-kilgore before you call proxy.web with the req and res you can optionally pass in headers as options. You can see it in the docs here and in the code here.

@giuliopaci
Copy link

My current understanding, is that the "proxyReq" callback is called too late for the purpouse of changing headers. What about moving the event emission of "proxyReq" at the end of setupOngoing, without adding a new event?

As far as I understand from node-https-proxy, the only purpouse for this callback is to change headers, and this do not require a socket.

In my case I want to remove headers and, sometimes, adding new headers on the basis of the reveived ones, so the flexibility provided by proxyReq callback seems to be required.

@rick-kilgore
Copy link
Author

rick-kilgore commented Dec 8, 2016

@jcrugzz - now I see what you mean. I somehow got the mistaken impression that the function I pass into http.createServer() was being called only once at startup, instead of once for every request.

I think my confusion was partly reinforced by the example for setting headers, though. You might want to change the example in the README to do it the way you're suggesting, since it won't work for POST requests the way it is.

@aleclerc
Copy link

What if the header to be set is conditional?

For instance, I have a post request that should be "If this header foo is set on the incoming request, then also add header bar

@jcrugzz Is this currently supported?

@lrettig
Copy link

lrettig commented May 12, 2017

+1, how do I do conditional header changes? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants