-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Content-Length not set in ForwardResponseMessage #4238
Comments
… which if set will write a Content-Length header before calling w.Write on non-streaming responses
Thanks for your issue! I'd like to ask some clarifying questions:
|
Hey @johanbrandhorst, On question 1:
https://github.com/golang/go/blob/960fa9bf66139e535d89934f56ae20a0e679e203/src/net/http/server.go#L1582 also has some additional comments. The library doesn't know if the calling code is going to call write once or multiple times. In this case, we know the Write method is only called once as long as doForwardTrailers isn't set. On question 2: I'm not sure of a way this can be done without writing the data to a buffer first, which adds additional allocations. This could be problematic for very large responses. |
Thanks for your responses. I wonder if we can't just enable this behavior by default since we know the size of the buffer we want to write. Worst case it's going to add a content-length header where we didn't before, but that seems like a better experience. What do you think? Feel free to update the PR and remove the option if you agree. |
I'm supportive of that, the only edge case I could think of is if a user already had some other middleware that was writing the header and maybe causing some trouble there. I'll update the PR now. |
I recreated the PR with a cleaner history |
Hi. This introduced a bug in my code after upgrading to the newer version. In my code base, I apply an on-the-fly gzip compression of the response on a higher level where the gzip writer is passed as the response writer. I was relying on the Go's automatically added With the following changes, grpc-gateway/runtime/handler.go Lines 193 to 195 in e99e853
May I know if we can plan anything to address that on the gateway level? Something like either enable or disable this new behavior using a feature flag? Although in my case, it happened for an emptypb response, it still may happen for any type of on-the-fly response modification. |
Hm, thanks for your report, that is unfortunate. As I see it though, you could still work around this in your code with a |
Yes. I can intercept this on my side and fix the issue for my case. |
Content-Length is not set before calling w.Write at https://github.com/grpc-ecosystem/grpc-gateway/blob/main/runtime/handler.go#L179. This ends up relying on the default Go behavior, which will only set Content-Length for small responses. When using CDNs like CloudFront they will not compress the origin responses unless Content-Length is set.
In theory the header could be added by a WithForwardResponseOption, the downside being that the message needs to be serialized twice once by the WithForwardResponseOption and once by the ForwardResponseMessage code. That said it could be incorrect if
if rb, ok := resp.(responseBody); ok {
a few lines up is true.Is there a better way to do this? Thanks in advance!
The text was updated successfully, but these errors were encountered: