From 91c88f3f839acf7c55d2819c62515a6f99f60740 Mon Sep 17 00:00:00 2001 From: "Christian R. Vozar" Date: Wed, 10 Aug 2016 17:30:46 -0500 Subject: [PATCH] Pass Permanent Request Headers --- runtime/context.go | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/runtime/context.go b/runtime/context.go index ad42535662a..73a9da2fe24 100644 --- a/runtime/context.go +++ b/runtime/context.go @@ -15,7 +15,7 @@ import ( "google.golang.org/grpc/metadata" ) -const metadataHeaderPrefix = "Grpc-Metadata-" +const metadataHeaderPrefix = "GrpcGateway-" const metadataTrailerPrefix = "Grpc-Trailer-" const metadataGrpcTimeout = "Grpc-Timeout" @@ -48,8 +48,8 @@ func AnnotateContext(ctx context.Context, req *http.Request) (context.Context, e for key, vals := range req.Header { for _, val := range vals { - if key == "Authorization" { - pairs = append(pairs, "authorization", val) + if isPermanentHTTPHeader(key) { + pairs = append(pairs, strings.ToLower(fmt.Sprintf("%s%s", metadataHeaderPrefix, key)), val) continue } if strings.HasPrefix(key, metadataHeaderPrefix) { @@ -137,3 +137,38 @@ func timeoutUnitToDuration(u uint8) (d time.Duration, ok bool) { } return } + +// isPermanentHTTPHeader checks whether hdr belongs to the list of +// permenant request headers maintained by IANA. +// http://www.iana.org/assignments/message-headers/message-headers.xml +func isPermanentHTTPHeader(hdr string) bool { + switch hdr { + case + "Accept", + "Accept-Charset", + "Accept-Language", + "Accept-Ranges", + "Authorization", + "Cache-Control", + "Content-Type", + "Cookie", + "Date", + "Expect", + "From", + "Host", + "If-Match", + "If-Modified-Since", + "If-None-Match", + "If-Schedule-Tag-Match", + "If-Unmodified-Since", + "Max-Forwards", + "Origin", + "Pragma", + "Referer", + "User-Agent", + "Via", + "Warning": + return true + } + return false +}