-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Receive GRPC metadata from HTTP headers.
* 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
Showing
3 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |