Skip to content
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

templates: Access to proxy response (closes #4420) #4871

Closed
wants to merge 3 commits into from

Conversation

mholt
Copy link
Member

@mholt mholt commented Jul 7, 2022

This WIP branch allows accessing the reverse proxy response in templates:

Content-Type: {{.ProxyResponse.Header.Get "Content-Type"}}
Body: {{call .ProxyResponse.Body 1024}}

This, of course, reads and drains the response body. I'm not really sure what I'm doing here, so I'm deferring to @francislavoie for a review on this. Maybe there's a better way to handle this. The argument 1024 here is the maximum size to read (since the body has to be buffered).

Accessing the body is admittedly a little awkward currently, since you have to use call. I tried without call and it said Body has arguments but cannot be invoked as function, so 🤷‍♂️ I guess you can't invoke functions as functions when they're struct fields: https://go.dev/src/text/template/exec.go?s=5814:5878

@byoungdale could you please give this a try?

Also sorted the func map just for kicks.
@mholt mholt added feature ⚙️ New feature or request under review 🧐 Review is pending before merging labels Jul 7, 2022
@mholt mholt added this to the v2.6.0 milestone Jul 7, 2022
@mholt mholt requested a review from francislavoie July 7, 2022 19:20
@mholt
Copy link
Member Author

mholt commented Sep 13, 2022

@byoungdale Is there still interest in this? Would be a shame to have to close it if we can't confirm this meets the requirements of the original feature request.

@byoungdale
Copy link

I would still be interested, but unfortunately, I have no time at the moment to try it out.

@mholt mholt modified the milestones: v2.6.0, 2.x Sep 13, 2022
@mholt
Copy link
Member Author

mholt commented Sep 29, 2022

@byoungdale When will you try this?

@mholt
Copy link
Member Author

mholt commented Nov 29, 2022

Closing due to inactivity :(

We can merge this if someone who needs it tests it to confirm it works for them (rather than me just assuming it works for a real use case).

@mholt mholt closed this Nov 29, 2022
@frink
Copy link

frink commented Aug 16, 2024

I'm facing similar needs and can try. I know it's been awhile but If you want to reopen I'll test it.

@mholt mholt reopened this Aug 23, 2024
@mholt
Copy link
Member Author

mholt commented Aug 23, 2024

@frink Go for it 👍

@mholt
Copy link
Member Author

mholt commented Sep 26, 2024

Closing due to merge conflicts and inactivity. If anyone wants to take this up and fix the conflict, and test that it works for their use case, we can reopen.

@mholt mholt closed this Sep 26, 2024
@mholt mholt added help wanted 🆘 Extra attention is needed and removed under review 🧐 Review is pending before merging labels Sep 26, 2024
@MurrayGroves
Copy link

Would it be easy to modify this to allow passing the response body into another reverse_proxy? My usecase is to intercept all HTML responses from a service and add a script to their head section. From what I can tell the easiest way to do that would be to use some modified version of this feature to pass the response body to a service that returns a new HTML body with the script added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature ⚙️ New feature or request help wanted 🆘 Extra attention is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants