github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/Godeps/_workspace/src/google.golang.org/grpc/metadata/metadata.go (about) 1 /* 2 * 3 * Copyright 2014, Google Inc. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: 9 * 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * * Redistributions in binary form must reproduce the above 13 * copyright notice, this list of conditions and the following disclaimer 14 * in the documentation and/or other materials provided with the 15 * distribution. 16 * * Neither the name of Google Inc. nor the names of its 17 * contributors may be used to endorse or promote products derived from 18 * this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 * 32 */ 33 34 // Package metadata define the structure of the metadata supported by gRPC library. 35 package metadata 36 37 import ( 38 "encoding/base64" 39 "fmt" 40 "strings" 41 42 "golang.org/x/net/context" 43 ) 44 45 const ( 46 binHdrSuffix = "-bin" 47 ) 48 49 // encodeKeyValue encodes key and value qualified for transmission via gRPC. 50 // Transmitting binary headers violates HTTP/2 spec. 51 // TODO(zhaoq): Maybe check if k is ASCII also. 52 func encodeKeyValue(k, v string) (string, string) { 53 k = strings.ToLower(k) 54 if strings.HasSuffix(k, binHdrSuffix) { 55 val := base64.StdEncoding.EncodeToString([]byte(v)) 56 v = string(val) 57 } 58 return k, v 59 } 60 61 // DecodeKeyValue returns the original key and value corresponding to the 62 // encoded data in k, v. 63 func DecodeKeyValue(k, v string) (string, string, error) { 64 if !strings.HasSuffix(k, binHdrSuffix) { 65 return k, v, nil 66 } 67 val, err := base64.StdEncoding.DecodeString(v) 68 if err != nil { 69 return "", "", err 70 } 71 return k, string(val), nil 72 } 73 74 // MD is a mapping from metadata keys to values. Users should use the following 75 // two convenience functions New and Pairs to generate MD. 76 type MD map[string][]string 77 78 // New creates a MD from given key-value map. 79 func New(m map[string]string) MD { 80 md := MD{} 81 for k, v := range m { 82 key, val := encodeKeyValue(k, v) 83 md[key] = append(md[key], val) 84 } 85 return md 86 } 87 88 // Pairs returns an MD formed by the mapping of key, value ... 89 // Pairs panics if len(kv) is odd. 90 func Pairs(kv ...string) MD { 91 if len(kv)%2 == 1 { 92 panic(fmt.Sprintf("metadata: Pairs got the odd number of input pairs for metadata: %d", len(kv))) 93 } 94 md := MD{} 95 var k string 96 for i, s := range kv { 97 if i%2 == 0 { 98 k = s 99 continue 100 } 101 key, val := encodeKeyValue(k, s) 102 md[key] = append(md[key], val) 103 } 104 return md 105 } 106 107 // Len returns the number of items in md. 108 func (md MD) Len() int { 109 return len(md) 110 } 111 112 // Copy returns a copy of md. 113 func (md MD) Copy() MD { 114 out := MD{} 115 for k, v := range md { 116 for _, i := range v { 117 out[k] = append(out[k], i) 118 } 119 } 120 return out 121 } 122 123 type mdKey struct{} 124 125 // NewContext creates a new context with md attached. 126 func NewContext(ctx context.Context, md MD) context.Context { 127 return context.WithValue(ctx, mdKey{}, md) 128 } 129 130 // FromContext returns the MD in ctx if it exists. 131 func FromContext(ctx context.Context) (md MD, ok bool) { 132 md, ok = ctx.Value(mdKey{}).(MD) 133 return 134 }