Skip to content

Commit

Permalink
Merge branch 'master' into concurrent_forward
Browse files Browse the repository at this point in the history
  • Loading branch information
tmc committed Jun 7, 2016
2 parents 363678e + 9b71cfe commit 2a02af8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
15 changes: 9 additions & 6 deletions runtime/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,17 @@ func AnnotateContext(ctx context.Context, req *http.Request) (context.Context, e
} else if req.Host != "" {
pairs = append(pairs, strings.ToLower(xForwardedHost), req.Host)
}
if remoteIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
if fwd := req.Header.Get(xForwardedFor); fwd == "" {
pairs = append(pairs, strings.ToLower(xForwardedFor), remoteIP)

if addr := req.RemoteAddr; addr != "" {
if remoteIP, _, err := net.SplitHostPort(addr); err == nil {
if fwd := req.Header.Get(xForwardedFor); fwd == "" {
pairs = append(pairs, strings.ToLower(xForwardedFor), remoteIP)
} else {
pairs = append(pairs, strings.ToLower(xForwardedFor), fmt.Sprintf("%s, %s", fwd, remoteIP))
}
} else {
pairs = append(pairs, strings.ToLower(xForwardedFor), fmt.Sprintf("%s, %s", fwd, remoteIP))
grpclog.Printf("invalid remote addr: %s", addr)
}
} else {
grpclog.Printf("invalid remote addr: %s", req.RemoteAddr)
}

if timeout != 0 {
Expand Down
12 changes: 5 additions & 7 deletions runtime/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

const (
emptyForwardMetaCount = 2
emptyForwardMetaCount = 1
)

func TestAnnotateContext_WorksWithEmpty(t *testing.T) {
Expand All @@ -22,7 +22,6 @@ func TestAnnotateContext_WorksWithEmpty(t *testing.T) {
if err != nil {
t.Fatalf("http.NewRequest(%q, %q, nil) failed with %v; want success", "GET", "http://www.example.com", err)
}
request.RemoteAddr = "192.0.2.1:12345"
request.Header.Add("Some-Irrelevant-Header", "some value")
annotated, err := runtime.AnnotateContext(ctx, request)
if err != nil {
Expand All @@ -31,7 +30,7 @@ func TestAnnotateContext_WorksWithEmpty(t *testing.T) {
}
md, ok := metadata.FromContext(annotated)
if !ok || len(md) != emptyForwardMetaCount {
t.Errorf("Expected 2 metadata items in context; got %v", md)
t.Errorf("Expected %d metadata items in context; got %v", emptyForwardMetaCount, md)
}
}

Expand All @@ -41,7 +40,6 @@ func TestAnnotateContext_ForwardsGrpcMetadata(t *testing.T) {
if err != nil {
t.Fatalf("http.NewRequest(%q, %q, nil) failed with %v; want success", "GET", "http://www.example.com", err)
}
request.RemoteAddr = "192.168.0.1:12345"
request.Header.Add("Some-Irrelevant-Header", "some value")
request.Header.Add("Grpc-Metadata-FooBar", "Value1")
request.Header.Add("Grpc-Metadata-Foo-BAZ", "Value2")
Expand All @@ -54,7 +52,7 @@ func TestAnnotateContext_ForwardsGrpcMetadata(t *testing.T) {
}
md, ok := metadata.FromContext(annotated)
if !ok || len(md) != emptyForwardMetaCount+3 {
t.Errorf("Expected 5 metadata items in context; got %v", md)
t.Errorf("Expected %d metadata items in context; got %v", md)
}
if got, want := md["foobar"], []string{"Value1"}; !reflect.DeepEqual(got, want) {
t.Errorf(`md["foobar"] = %q; want %q`, got, want)
Expand Down Expand Up @@ -82,8 +80,8 @@ func TestAnnotateContext_XForwardedFor(t *testing.T) {
return
}
md, ok := metadata.FromContext(annotated)
if !ok || len(md) != emptyForwardMetaCount {
t.Errorf("Expected 2 metadata items in context; got %v", md)
if !ok || len(md) != emptyForwardMetaCount+1 {
t.Errorf("Expected %d metadata items in context; got %v", emptyForwardMetaCount+1, md)
}
if got, want := md["x-forwarded-host"], []string{"bar.foo.example.com"}; !reflect.DeepEqual(got, want) {
t.Errorf(`md["host"] = %v; want %v`, got, want)
Expand Down

0 comments on commit 2a02af8

Please sign in to comment.