Skip to content

Commit

Permalink
Receive GRPC metadata from HTTP headers.
Browse files Browse the repository at this point in the history
* Add support for X-Grpc-Metadata-(varname) headers
* Modify codegen to do metadata annotation
  • Loading branch information
James Crasta committed Jun 9, 2015
1 parent f748a47 commit e56b661
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion protoc-gen-grpc-gateway/gengateway/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func Register{{$svc.GetName}}Handler(ctx context.Context, mux *runtime.ServeMux,
{{range $m := $svc.Methods}}
{{range $b := $m.Bindings}}
mux.Handle({{$b.HTTPMethod | printf "%q"}}, pattern_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
resp, err := request_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}(ctx, client, req, pathParams)
resp, err := request_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}(runtime.AnnotateContext(ctx, req), client, req, pathParams)
if err != nil {
runtime.HTTPError(w, err)
return
Expand Down
30 changes: 30 additions & 0 deletions runtime/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package runtime

import (
"golang.org/x/net/context"
"google.golang.org/grpc/metadata"
"net/http"
"strings"
)

const metadataHeaderPrefix = "X-Grpc-Metadata-"

/*
Add context information such as metadata from the request.
If there are no metadata headers in the request, then the context returned
will be the same context.
*/
func AnnotateContext(ctx context.Context, req *http.Request) context.Context {
var pairs []string
for key, val := range req.Header {
if strings.HasPrefix(key, metadataHeaderPrefix) {
pairs = append(pairs, key[len(metadataHeaderPrefix):], val[0])
}
}

if len(pairs) != 0 {
ctx = metadata.NewContext(ctx, metadata.Pairs(pairs...))
}
return ctx
}
28 changes: 28 additions & 0 deletions runtime/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package runtime_test

import (
"github.com/gengo/grpc-gateway/runtime"
"google.golang.org/grpc/metadata"

"golang.org/x/net/context"
"net/http"
"testing"
)

func TestAnnotateContext(t *testing.T) {
ctx := context.Background()

request, _ := http.NewRequest("GET", "http://localhost", nil)
request.Header = http.Header{}
annotated := runtime.AnnotateContext(ctx, request)
if annotated != ctx {
t.Errorf("Annotation did not return proper context: %v != %v", ctx, annotated)
}
request.Header.Add("X-Grpc-Metadata-FooBar", "Value1")
request.Header.Add("X-Grpc-Metadata-Foo-BAZ", "Value2")
annotated = runtime.AnnotateContext(ctx, request)
md, ok := metadata.FromContext(annotated)
if !ok || len(md) != 2 || md["Foobar"] != "Value1" || md["Foo-Baz"] != "Value2" {
t.Errorf("Could not find metadata or incorrect metadata: %v", md)
}
}

0 comments on commit e56b661

Please sign in to comment.