Skip to content

Commit

Permalink
improve {incoming,outgoing}HeaderMatcher logic (#408)
Browse files Browse the repository at this point in the history
* default implementation over nil-check
* really exclude header when outgoingHeaderMatcher returns false
  • Loading branch information
flisky authored and tamalsaha committed Jun 6, 2017
1 parent 2a40dd7 commit c6f7a5a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
6 changes: 2 additions & 4 deletions runtime/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@ func AnnotateContext(ctx context.Context, mux *ServeMux, req *http.Request) (con
if strings.ToLower(key) == "authorization" {
pairs = append(pairs, "authorization", val)
}
if mux.incomingHeaderMatcher != nil {
if h, ok := mux.incomingHeaderMatcher(key); ok {
pairs = append(pairs, h, val)
}
if h, ok := mux.incomingHeaderMatcher(key); ok {
pairs = append(pairs, h, val)
}
}
}
Expand Down
14 changes: 5 additions & 9 deletions runtime/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,11 @@ func ForwardResponseStream(ctx context.Context, mux *ServeMux, marshaler Marshal

func handleForwardResponseServerMetadata(w http.ResponseWriter, mux *ServeMux, md ServerMetadata) {
for k, vs := range md.HeaderMD {
hKey := fmt.Sprintf("%s%s", MetadataHeaderPrefix, k)
if mux.outgoingHeaderMatcher != nil {
if h, ok := mux.outgoingHeaderMatcher(k); ok {
hKey = h
if h, ok := mux.outgoingHeaderMatcher(k); ok {
for _, v := range vs {
w.Header().Add(h, v)
}
}
for i := range vs {
w.Header().Add(hKey, vs[i])
}
}
}

Expand All @@ -90,8 +86,8 @@ func handleForwardResponseTrailerHeader(w http.ResponseWriter, md ServerMetadata
func handleForwardResponseTrailer(w http.ResponseWriter, md ServerMetadata) {
for k, vs := range md.TrailerMD {
tKey := fmt.Sprintf("%s%s", MetadataTrailerPrefix, k)
for i := range vs {
w.Header().Add(tKey, vs[i])
for _, v := range vs {
w.Header().Add(tKey, v)
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion runtime/mux.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runtime

import (
"fmt"
"net/http"
"net/textproto"
"strings"
Expand Down Expand Up @@ -107,7 +108,6 @@ func NewServeMux(opts ...ServeMuxOption) *ServeMux {
handlers: make(map[string][]handler),
forwardResponseOptions: make([]func(context.Context, http.ResponseWriter, proto.Message) error, 0),
marshalers: makeMarshalerMIMERegistry(),
incomingHeaderMatcher: DefaultHeaderMatcher,
}

for _, opt := range opts {
Expand All @@ -126,6 +126,16 @@ func NewServeMux(opts ...ServeMuxOption) *ServeMux {
}
}

if serveMux.incomingHeaderMatcher == nil {
serveMux.incomingHeaderMatcher = DefaultHeaderMatcher
}

if serveMux.outgoingHeaderMatcher == nil {
serveMux.outgoingHeaderMatcher = func(key string) (string, bool) {
return fmt.Sprintf("%s%s", MetadataHeaderPrefix, key), true
}
}

return serveMux
}

Expand Down

0 comments on commit c6f7a5a

Please sign in to comment.