forked from grpc-ecosystem/grpc-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
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 Grpc-Metadata-(varname) headers * Modify codegen to do metadata annotation
- Loading branch information
James Crasta
committed
Jun 10, 2015
1 parent
f748a47
commit e5d20af
Showing
3 changed files
with
66 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,31 @@ | ||
package runtime | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"golang.org/x/net/context" | ||
"google.golang.org/grpc/metadata" | ||
) | ||
|
||
const metadataHeaderPrefix = "Grpc-Metadata-" | ||
|
||
/* | ||
AnnotateContext adds 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,34 @@ | ||
package runtime_test | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/gengo/grpc-gateway/runtime" | ||
"golang.org/x/net/context" | ||
"google.golang.org/grpc/metadata" | ||
) | ||
|
||
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("AnnotateContext(ctx, request) = %v; want %v", annotated, ctx) | ||
} | ||
request.Header.Add("Grpc-Metadata-FooBar", "Value1") | ||
request.Header.Add("Grpc-Metadata-Foo-BAZ", "Value2") | ||
annotated = runtime.AnnotateContext(ctx, request) | ||
md, ok := metadata.FromContext(annotated) | ||
if !ok || len(md) != 2 { | ||
t.Errorf("Expected 2 metadata items in context; got %v", md) | ||
} | ||
if md["Foobar"] != "Value1" { | ||
t.Errorf("md[\"Foobar\"] = %v; want %v", md["Foobar"], "Value1") | ||
} | ||
if md["Foo-Baz"] != "Value2" { | ||
t.Errorf("md[\"Foo-Baz\"] = %v want %v", md["Foo-Baz"], "Value2") | ||
} | ||
} |