Skip to content

Commit

Permalink
added CORS and other headers supports
Browse files Browse the repository at this point in the history
  • Loading branch information
sadlil authored and tamalsaha committed Jan 11, 2017
1 parent 2dbcd36 commit d3f3d34
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
27 changes: 25 additions & 2 deletions runtime/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@ import (
"google.golang.org/grpc/metadata"
)

// MetadataHeaderPrefix is prepended to HTTP headers in order to convert them to
// MetadataHeaderPrefix is prepended to HTTP headers in order to convert them to
// gRPC metadata for incoming requests processed by grpc-gateway
const MetadataHeaderPrefix = "Grpc-Metadata-"

// MetadataTrailerPrefix is prepended to gRPC metadata as it is converted to
// HTTP headers in a response handled by grpc-gateway
const MetadataTrailerPrefix = "Grpc-Trailer-"
const metadataGrpcTimeout = "Grpc-Timeout"

const xForwardedFor = "X-Forwarded-For"
const xForwardedHost = "X-Forwarded-Host"
const cookieHeader = "Cookie"
const csrfTokenHeader = "X-Phabricator-Csrf"
const corsHeaderPrefix = "access-control-"

var (
// DefaultContextTimeout is used for gRPC call context.WithTimeout whenever a Grpc-Timeout inbound
Expand All @@ -34,7 +38,6 @@ var (

/*
AnnotateContext adds context information such as metadata from the request.
At a minimum, the RemoteAddr is included in the fashion of "X-Forwarded-For",
except that the forwarded destination is not another HTTP service but rather
a gRPC service.
Expand All @@ -56,6 +59,18 @@ func AnnotateContext(ctx context.Context, req *http.Request) (context.Context, e
pairs = append(pairs, "authorization", val)
continue
}
if key == cookieHeader {
pairs = append(pairs, key, val)
continue
}
if strings.EqualFold(key, csrfTokenHeader) {
pairs = append(pairs, key, val)
continue
}
if strings.HasPrefix(strings.ToLower(key), corsHeaderPrefix) {
pairs = append(pairs, key, val)
continue
}
if strings.HasPrefix(key, MetadataHeaderPrefix) {
pairs = append(pairs, key[len(MetadataHeaderPrefix):], val)
}
Expand All @@ -79,6 +94,14 @@ func AnnotateContext(ctx context.Context, req *http.Request) (context.Context, e
}
}

// adding extra headers to metadata
pairs = append(pairs,
"http-request-method", req.Method,
"http-request-endpoint", req.RequestURI,
"http-request-host", req.Host,
"http-userAgent", req.UserAgent(),
)

if timeout != 0 {
ctx, _ = context.WithTimeout(ctx, timeout)
}
Expand Down
6 changes: 5 additions & 1 deletion runtime/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/http"
"net/textproto"
"strings"

"github.com/golang/protobuf/proto"
"github.com/grpc-ecosystem/grpc-gateway/runtime/internal"
Expand Down Expand Up @@ -67,7 +68,10 @@ func ForwardResponseStream(ctx context.Context, marshaler Marshaler, w http.Resp

func handleForwardResponseServerMetadata(w http.ResponseWriter, md ServerMetadata) {
for k, vs := range md.HeaderMD {
hKey := fmt.Sprintf("%s%s", MetadataHeaderPrefix, k)
hKey := k
if !strings.HasPrefix(strings.ToLower(k), corsHeaderPrefix) {
hKey = fmt.Sprintf("%s%s", MetadataHeaderPrefix, k)
}
for i := range vs {
w.Header().Add(hKey, vs[i])
}
Expand Down

0 comments on commit d3f3d34

Please sign in to comment.