Skip to content

Commit c84a5de

Browse files
authored
transport/server: add :method POST to incoming metadata (#4770)
* transport/server: add :method POST to incoming metadata
1 parent 98ccf47 commit c84a5de

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

binarylog/binarylog_end2end_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -850,11 +850,27 @@ func equalLogEntry(entries ...*pb.GrpcLogEntry) (equal bool) {
850850
tmp := append(h.Metadata.Entry[:0], h.Metadata.Entry...)
851851
h.Metadata.Entry = tmp
852852
sort.Slice(h.Metadata.Entry, func(i, j int) bool { return h.Metadata.Entry[i].Key < h.Metadata.Entry[j].Key })
853+
// Delete headers that have POST values here since we cannot control
854+
// this.
855+
for i, entry := range h.Metadata.Entry {
856+
if entry.Key == ":method" {
857+
h.Metadata.Entry = append(h.Metadata.Entry[:i], h.Metadata.Entry[i+1:]...)
858+
break
859+
}
860+
}
853861
}
854862
if h := e.GetServerHeader(); h != nil {
855863
tmp := append(h.Metadata.Entry[:0], h.Metadata.Entry...)
856864
h.Metadata.Entry = tmp
857865
sort.Slice(h.Metadata.Entry, func(i, j int) bool { return h.Metadata.Entry[i].Key < h.Metadata.Entry[j].Key })
866+
// Delete headers that have POST values here since we cannot control
867+
// this.
868+
for i, entry := range h.Metadata.Entry {
869+
if entry.Key == ":method" {
870+
h.Metadata.Entry = append(h.Metadata.Entry[:i], h.Metadata.Entry[i+1:]...)
871+
break
872+
}
873+
}
858874
}
859875
if h := e.GetTrailer(); h != nil {
860876
sort.Slice(h.Metadata.Entry, func(i, j int) bool { return h.Metadata.Entry[i].Key < h.Metadata.Entry[j].Key })

internal/transport/http2_server.go

+1
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ func (t *http2Server) operateHeaders(frame *http2.MetaHeadersFrame, handle func(
380380
s.recvCompress = hf.Value
381381
case ":method":
382382
httpMethod = hf.Value
383+
mdata[":method"] = append(mdata[":method"], hf.Value)
383384
case ":path":
384385
s.method = hf.Value
385386
case "grpc-timeout":

test/end2end_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -7847,3 +7847,40 @@ func (s) TestStreamingServerInterceptorGetsConnection(t *testing.T) {
78477847
t.Fatalf("ss.Client.StreamingInputCall(_) = _, %v, want _, %v", err, io.EOF)
78487848
}
78497849
}
7850+
7851+
func unaryInterceptorVerifyPost(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
7852+
md, ok := metadata.FromIncomingContext(ctx)
7853+
if !ok {
7854+
return nil, status.Error(codes.NotFound, "metadata was not in context")
7855+
}
7856+
method := md.Get(":method")
7857+
if len(method) != 1 {
7858+
return nil, status.Error(codes.InvalidArgument, ":method value had more than one value")
7859+
}
7860+
if method[0] != "POST" {
7861+
return nil, status.Error(codes.InvalidArgument, ":method value was not post")
7862+
}
7863+
return handler(ctx, req)
7864+
}
7865+
7866+
// TestUnaryInterceptorGetsPost verifies that the server transport adds a
7867+
// :method POST header to metadata, and that that added Header is visibile at
7868+
// the grpc layer.
7869+
func (s) TestUnaryInterceptorGetsPost(t *testing.T) {
7870+
ss := &stubserver.StubServer{
7871+
EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
7872+
return &testpb.Empty{}, nil
7873+
},
7874+
}
7875+
if err := ss.Start([]grpc.ServerOption{grpc.UnaryInterceptor(unaryInterceptorVerifyPost)}); err != nil {
7876+
t.Fatalf("Error starting endpoint server: %v", err)
7877+
}
7878+
defer ss.Stop()
7879+
7880+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
7881+
defer cancel()
7882+
7883+
if _, err := ss.Client.EmptyCall(ctx, &testpb.Empty{}); status.Code(err) != codes.OK {
7884+
t.Fatalf("ss.Client.EmptyCall(_, _) = _, %v, want _, error code %s", err, codes.OK)
7885+
}
7886+
}

0 commit comments

Comments
 (0)