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

[bug] http.Request.Body is not readable in RegisterInterceptFunc or RegisterBeforeFunc #80

Open
groovili opened this issue May 1, 2020 · 4 comments
Labels

Comments

@groovili
Copy link
Contributor

groovili commented May 1, 2020

Describe the bug

In v2/server package newCodecRequest is called before interceptFunc and
beforeFunc and performs request.Body.Close(), in such way funcs which would be called later can't read or alter data in body.

Versions

Go version: go1.14.2 darwin/amd64
package version: 27d3316

Steps to Reproduce

Try to modify request.Body in functions registered with RegisterInterceptFunc or RegisterBeforeFunc.

Expected behavior

Documentation says that functions which added with RegisterInterceptFunc or RegisterBeforeFunc would be called before every request. I assume, that these methods are made to alter or access request content. Since RPC is a protocol which mostly uses request.Body to transfer data, I think it should be accessible and changeable in these functions.

I can make PR to change this behavior if these changes are ok. Also, it wouldn't be breaking changes.
Thanks for the package and looking forward to your feedback!

@groovili groovili added the bug label May 1, 2020
@elithrar
Copy link
Contributor

elithrar commented May 2, 2020 via email

@groovili
Copy link
Contributor Author

groovili commented May 3, 2020

Hi @elithrar! Please check the PR above.

@groovili
Copy link
Contributor Author

groovili commented Jul 5, 2020

@elithrar up

@groovili
Copy link
Contributor Author

up

AlexVulaj pushed a commit that referenced this issue Mar 6, 2024
…Func (#81)

Hi! 

This PR gives users the ability to access and change `request.Body` in
functions registered with `RegisterInterceptFunc` and
`RegisterBeforeFunc`. I've already described it in issue #80.
Also, I've added couple of simple tests to check ability to change
request data in these functions.

**Changes:**

1. In **rpc/v2** close `request.Body` after the execution of
`beforeFunc`'s and `interceptFunc`'s. Update codec request info after
calls to functions above.
2. Read request body bytes, decode it to codec format and provide
`bytes.Buffer` and `request.Body` for underlying functions in
**v2/json**, **v2/json2** and **v2/protorpc** codecs.

Of course, exists a better way to do that, but it will require changes
in the signature of `RegisterInterceptFunc` and `RegisterBeforeFunc` and
it would be breaking changes. If both of these methods will have an
original `*http.Request` as input parameter, they could be executed
before the creation of codec. In this case, users can access and alter
request data, and only after that it would be read by the codec and
marshaled to service request params. But since these changes are
breaking, it's not an option at the moment, probably it can fit the next
version or release.

Would be nice to know your opinion, thanks!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

2 participants