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

Added support for writing Etag headers and handling If-None-Match request headers #4262

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions runtime/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package runtime

import (
"context"
"crypto/md5"
"encoding/hex"
"errors"
"io"
"net/http"
Expand Down Expand Up @@ -181,8 +183,25 @@ func ForwardResponseMessage(ctx context.Context, mux *ServeMux, marshaler Marsha
w.Header().Set("Content-Length", strconv.Itoa(len(buf)))
}

if _, err = w.Write(buf); err != nil {
grpclog.Infof("Failed to write response: %v", err)
// Generate an Etag for any GET requests with messages larger than 100 bytes.
// Writing the Etag in the response takes 39 bytes, so it's not worth doing for smaller messages.
etag := ""
if len(buf) > 100 && req.Method == http.MethodGet {
h := md5.New()
h.Write(buf)
etag = hex.EncodeToString(h.Sum(nil))
w.Header().Set("Etag", "\""+etag+"\"")
}

// Check if the client has provided an Etag and if it matches the generated Etag.
// If it does, send a 304 Not Modified response.
ifNoneMatch := req.Header.Get("If-None-Match")
if ifNoneMatch != "" && ifNoneMatch == etag {
w.WriteHeader(http.StatusNotModified)
} else {
if _, err = w.Write(buf); err != nil {
grpclog.Infof("Failed to write response: %v", err)
}
}

if doForwardTrailers {
Expand Down
210 changes: 205 additions & 5 deletions runtime/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/http/httptest"
"reflect"
"sort"
"testing"

"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
Expand Down Expand Up @@ -321,16 +322,99 @@ func TestForwardResponseMessage(t *testing.T) {
}
}

func TestOutgoingEtagIfNoneMatch(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
msg *pb.SimpleMessage
requestHeaders http.Header
method string
name string
headers http.Header
expectedStatus int
}{
{
msg: &pb.SimpleMessage{Id: "foo"},
method: http.MethodGet,
name: "small message",
headers: http.Header{
"Content-Length": []string{"12"},
"Content-Type": []string{"application/json"},
},
expectedStatus: http.StatusOK,
},
{
msg: &pb.SimpleMessage{Id: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam rhoncus magna ante, sed malesuada nibh vehicula in nec."},
method: http.MethodGet,
name: "large message",
headers: http.Header{
"Content-Length": []string{"129"},
"Content-Type": []string{"application/json"},
"Etag": []string{"\"41bf5d28a47f59b2a649e44f2607b0ea\""},
},
expectedStatus: http.StatusOK,
},
{
msg: &pb.SimpleMessage{Id: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam rhoncus magna ante, sed malesuada nibh vehicula in nec."},
method: http.MethodGet,
requestHeaders: http.Header{
"If-None-Match": []string{"41bf5d28a47f59b2a649e44f2607b0ea"},
},
name: "large message with If-None-Match header",
headers: http.Header{
"Content-Length": []string{"129"},
"Content-Type": []string{"application/json"},
"Etag": []string{"\"41bf5d28a47f59b2a649e44f2607b0ea\""},
},
expectedStatus: http.StatusNotModified,
},
{
msg: &pb.SimpleMessage{Id: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam rhoncus magna ante, sed malesuada nibh vehicula in nec."},
method: http.MethodPost,
requestHeaders: http.Header{
"If-None-Match": []string{"41bf5d28a47f59b2a649e44f2607b0ea"},
},
name: "large message with If-None-Match header",
headers: http.Header{
"Content-Length": []string{"129"},
"Content-Type": []string{"application/json"},
},
expectedStatus: http.StatusOK,
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

req := httptest.NewRequest(tc.method, "http://example.com/foo", nil)
req.Header = tc.requestHeaders
resp := httptest.NewRecorder()

runtime.ForwardResponseMessage(context.Background(), runtime.NewServeMux(), &runtime.JSONPb{}, resp, req, tc.msg)

w := resp.Result()
defer w.Body.Close()
if w.StatusCode != tc.expectedStatus {
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)
}
})
}
}

func TestOutgoingHeaderMatcher(t *testing.T) {
t.Parallel()
msg := &pb.SimpleMessage{Id: "foo"}
for _, tc := range []struct {
msg *pb.SimpleMessage
name string
md runtime.ServerMetadata
headers http.Header
matcher runtime.HeaderMatcherFunc
}{
{
msg: &pb.SimpleMessage{Id: "foo"},
name: "default matcher",
md: runtime.ServerMetadata{
HeaderMD: metadata.Pairs(
Expand All @@ -346,6 +430,7 @@ func TestOutgoingHeaderMatcher(t *testing.T) {
},
},
{
msg: &pb.SimpleMessage{Id: "foo"},
name: "custom matcher",
md: runtime.ServerMetadata{
HeaderMD: metadata.Pairs(
Expand All @@ -367,6 +452,47 @@ func TestOutgoingHeaderMatcher(t *testing.T) {
}
},
},
{
msg: &pb.SimpleMessage{Id: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam rhoncus magna ante, sed malesuada nibh vehicula in nec."},
name: "default matcher, large message",
md: runtime.ServerMetadata{
HeaderMD: metadata.Pairs(
"foo", "bar",
"baz", "qux",
),
},
headers: http.Header{
"Content-Length": []string{"129"},
"Content-Type": []string{"application/json"},
"Grpc-Metadata-Foo": []string{"bar"},
"Grpc-Metadata-Baz": []string{"qux"},
"Etag": []string{"\"41bf5d28a47f59b2a649e44f2607b0ea\""},
},
},
{
msg: &pb.SimpleMessage{Id: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam rhoncus magna ante, sed malesuada nibh vehicula in nec."},
name: "custom matcher, large message",
md: runtime.ServerMetadata{
HeaderMD: metadata.Pairs(
"foo", "bar",
"baz", "qux",
),
},
headers: http.Header{
"Content-Length": []string{"129"},
"Content-Type": []string{"application/json"},
"Custom-Foo": []string{"bar"},
"Etag": []string{"\"41bf5d28a47f59b2a649e44f2607b0ea\""},
},
matcher: func(key string) (string, bool) {
switch key {
case "foo":
return "custom-foo", true
default:
return "", false
}
},
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
Expand All @@ -376,7 +502,7 @@ func TestOutgoingHeaderMatcher(t *testing.T) {
req := httptest.NewRequest("GET", "http://example.com/foo", nil)
resp := httptest.NewRecorder()

runtime.ForwardResponseMessage(ctx, runtime.NewServeMux(runtime.WithOutgoingHeaderMatcher(tc.matcher)), &runtime.JSONPb{}, resp, req, msg)
runtime.ForwardResponseMessage(ctx, runtime.NewServeMux(runtime.WithOutgoingHeaderMatcher(tc.matcher)), &runtime.JSONPb{}, resp, req, tc.msg)

w := resp.Result()
defer w.Body.Close()
Expand All @@ -393,8 +519,8 @@ func TestOutgoingHeaderMatcher(t *testing.T) {

func TestOutgoingTrailerMatcher(t *testing.T) {
t.Parallel()
msg := &pb.SimpleMessage{Id: "foo"}
for _, tc := range []struct {
msg *pb.SimpleMessage
name string
md runtime.ServerMetadata
caller http.Header
Expand All @@ -403,6 +529,7 @@ func TestOutgoingTrailerMatcher(t *testing.T) {
matcher runtime.HeaderMatcherFunc
}{
{
msg: &pb.SimpleMessage{Id: "foo"},
name: "default matcher, caller accepts",
md: runtime.ServerMetadata{
TrailerMD: metadata.Pairs(
Expand All @@ -416,14 +543,15 @@ func TestOutgoingTrailerMatcher(t *testing.T) {
headers: http.Header{
"Transfer-Encoding": []string{"chunked"},
"Content-Type": []string{"application/json"},
"Trailer": []string{"Grpc-Trailer-Foo", "Grpc-Trailer-Baz"},
"Trailer": []string{"Grpc-Trailer-Baz", "Grpc-Trailer-Foo"},
},
trailer: http.Header{
"Grpc-Trailer-Foo": []string{"bar"},
"Grpc-Trailer-Baz": []string{"qux"},
},
},
{
msg: &pb.SimpleMessage{Id: "foo"},
name: "default matcher, caller rejects",
md: runtime.ServerMetadata{
TrailerMD: metadata.Pairs(
Expand All @@ -437,6 +565,7 @@ func TestOutgoingTrailerMatcher(t *testing.T) {
},
},
{
msg: &pb.SimpleMessage{Id: "foo"},
name: "custom matcher",
md: runtime.ServerMetadata{
TrailerMD: metadata.Pairs(
Expand Down Expand Up @@ -464,6 +593,74 @@ func TestOutgoingTrailerMatcher(t *testing.T) {
}
},
},
{
msg: &pb.SimpleMessage{Id: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam rhoncus magna ante, sed malesuada nibh vehicula in nec."},
name: "default matcher, caller accepts, large message",
md: runtime.ServerMetadata{
TrailerMD: metadata.Pairs(
"foo", "bar",
"baz", "qux",
),
},
caller: http.Header{
"Te": []string{"trailers"},
},
headers: http.Header{
"Transfer-Encoding": []string{"chunked"},
"Content-Type": []string{"application/json"},
"Trailer": []string{"Grpc-Trailer-Baz", "Grpc-Trailer-Foo"},
"Etag": []string{"\"41bf5d28a47f59b2a649e44f2607b0ea\""},
},
trailer: http.Header{
"Grpc-Trailer-Foo": []string{"bar"},
"Grpc-Trailer-Baz": []string{"qux"},
},
},
{
msg: &pb.SimpleMessage{Id: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam rhoncus magna ante, sed malesuada nibh vehicula in nec."},
name: "default matcher, caller rejects, large message",
md: runtime.ServerMetadata{
TrailerMD: metadata.Pairs(
"foo", "bar",
"baz", "qux",
),
},
headers: http.Header{
"Content-Length": []string{"129"},
"Content-Type": []string{"application/json"},
"Etag": []string{"\"41bf5d28a47f59b2a649e44f2607b0ea\""},
},
},
{
msg: &pb.SimpleMessage{Id: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam rhoncus magna ante, sed malesuada nibh vehicula in nec."},
name: "custom matcher, large message",
md: runtime.ServerMetadata{
TrailerMD: metadata.Pairs(
"foo", "bar",
"baz", "qux",
),
},
caller: http.Header{
"Te": []string{"trailers"},
},
headers: http.Header{
"Transfer-Encoding": []string{"chunked"},
"Content-Type": []string{"application/json"},
"Trailer": []string{"Custom-Trailer-Foo"},
"Etag": []string{"\"41bf5d28a47f59b2a649e44f2607b0ea\""},
},
trailer: http.Header{
"Custom-Trailer-Foo": []string{"bar"},
},
matcher: func(key string) (string, bool) {
switch key {
case "foo":
return "custom-trailer-foo", true
default:
return "", false
}
},
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
Expand All @@ -474,7 +671,7 @@ func TestOutgoingTrailerMatcher(t *testing.T) {
req.Header = tc.caller
resp := httptest.NewRecorder()

runtime.ForwardResponseMessage(ctx, runtime.NewServeMux(runtime.WithOutgoingTrailerMatcher(tc.matcher)), &runtime.JSONPb{}, resp, req, msg)
runtime.ForwardResponseMessage(ctx, runtime.NewServeMux(runtime.WithOutgoingTrailerMatcher(tc.matcher)), &runtime.JSONPb{}, resp, req, tc.msg)

w := resp.Result()
_, _ = io.Copy(io.Discard, w.Body)
Expand All @@ -483,6 +680,9 @@ func TestOutgoingTrailerMatcher(t *testing.T) {
t.Fatalf("StatusCode %d want %d", w.StatusCode, http.StatusOK)
}

// Sort to the trailer headers to ensure the test is deterministic
sort.Strings(w.Header["Trailer"])

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