Skip to content

Commit

Permalink
Write Content-Length header if doForwardTrailers is not set (#4259)
Browse files Browse the repository at this point in the history
* Write Content-Length header if doForwardTrailers is not set

* TestOutgoingHeaderMatcher now checks that Content-Length is set and TestOutgoingTrailerMatcher now checks that the headers match
  • Loading branch information
joshgarnett authored Apr 26, 2024
1 parent 248032e commit 4afdabd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
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)))
}

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

0 comments on commit 4afdabd

Please sign in to comment.