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

Write Content-Length header if doForwardTrailers is not set #4259

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions runtime/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"net/http"
"net/textproto"
"strconv"
"strings"

"google.golang.org/genproto/googleapis/api/httpbody"
Expand Down Expand Up @@ -176,6 +177,10 @@ func ForwardResponseMessage(ctx context.Context, mux *ServeMux, marshaler Marsha
return
}

if !doForwardTrailers {
w.Header().Set("Content-Length", strconv.Itoa(len(buf)))
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @johanbrandhorst, I've pushed a change that updates the existing tests to check for the header


if _, err = w.Write(buf); err != nil {
grpclog.Infof("Failed to write response: %v", err)
}
Expand Down
23 changes: 16 additions & 7 deletions runtime/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ func TestOutgoingHeaderMatcher(t *testing.T) {
),
},
headers: http.Header{
"Content-Length": []string{"12"},
"Content-Type": []string{"application/json"},
"Grpc-Metadata-Foo": []string{"bar"},
"Grpc-Metadata-Baz": []string{"qux"},
Expand All @@ -353,8 +354,9 @@ func TestOutgoingHeaderMatcher(t *testing.T) {
),
},
headers: http.Header{
"Content-Type": []string{"application/json"},
"Custom-Foo": []string{"bar"},
"Content-Length": []string{"12"},
"Content-Type": []string{"application/json"},
"Custom-Foo": []string{"bar"},
},
matcher: func(key string) (string, bool) {
switch key {
Expand Down Expand Up @@ -412,8 +414,9 @@ func TestOutgoingTrailerMatcher(t *testing.T) {
"Te": []string{"trailers"},
},
headers: http.Header{
"Content-Type": []string{"application/json"},
"Trailer": []string{"Grpc-Trailer-Foo,Grpc-Trailer-Baz"},
"Transfer-Encoding": []string{"chunked"},
"Content-Type": []string{"application/json"},
"Trailer": []string{"Grpc-Trailer-Foo", "Grpc-Trailer-Baz"},
},
trailer: http.Header{
"Grpc-Trailer-Foo": []string{"bar"},
Expand All @@ -429,7 +432,8 @@ func TestOutgoingTrailerMatcher(t *testing.T) {
),
},
headers: http.Header{
"Content-Type": []string{"application/json"},
"Content-Length": []string{"12"},
"Content-Type": []string{"application/json"},
},
},
{
Expand All @@ -444,8 +448,9 @@ func TestOutgoingTrailerMatcher(t *testing.T) {
"Te": []string{"trailers"},
},
headers: http.Header{
"Content-Type": []string{"application/json"},
"Trailer": []string{"Custom-Trailer-Foo"},
"Transfer-Encoding": []string{"chunked"},
"Content-Type": []string{"application/json"},
"Trailer": []string{"Custom-Trailer-Foo"},
},
trailer: http.Header{
"Custom-Trailer-Foo": []string{"bar"},
Expand Down Expand Up @@ -478,6 +483,10 @@ func TestOutgoingTrailerMatcher(t *testing.T) {
t.Fatalf("StatusCode %d want %d", w.StatusCode, http.StatusOK)
}

if !reflect.DeepEqual(w.Header, tc.headers) {
t.Fatalf("Header %v want %v", w.Header, tc.headers)
}

if !reflect.DeepEqual(w.Trailer, tc.trailer) {
t.Fatalf("Trailer %v want %v", w.Trailer, tc.trailer)
}
Expand Down
Loading