/* * * Copyright 2020 gRPC authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */
// Package metadata contains functions to set and get metadata from addresses.//// This package is experimental.
package metadataimport ()typemdKeyTypestringconstmdKey = mdKeyType("grpc.internal.address.metadata")typemdValuemetadata.MDfunc ( mdValue) ( any) bool { , := .(mdValue)if ! {returnfalse }iflen() != len() {returnfalse }for , := range { := []iflen() != len() {returnfalse }for , := range {if [] != {returnfalse } } }returntrue}// Get returns the metadata of addr.func ( resolver.Address) metadata.MD { := .Attributesif == nil {returnnil } , := .Value(mdKey).(mdValue)returnmetadata.MD()}// Set sets (overrides) the metadata in addr.//// When a SubConn is created with this address, the RPCs sent on it will all// have this metadata.func ( resolver.Address, metadata.MD) resolver.Address { .Attributes = .Attributes.WithValue(mdKey, mdValue())return}// Validate validates every pair in md with ValidatePair.func ( metadata.MD) error {for , := range {if := ValidatePair(, ...); != nil {return } }returnnil}// hasNotPrintable return true if msg contains any characters which are not in %x20-%x7Efunc ( string) bool {// for i that saving a conversion if not using for rangefor := 0; < len(); ++ {if [] < 0x20 || [] > 0x7E {returntrue } }returnfalse}// ValidateKey validates a key with the following rules (pseudo-headers are// skipped):// - the key must contain one or more characters.// - the characters in the key must be in [0-9 a-z _ - .].func ( string) error {// key should not be emptyif == "" {returnfmt.Errorf("there is an empty key in the header") }// pseudo-header will be ignoredif [0] == ':' {returnnil }// check key, for i that saving a conversion if not using for rangefor := 0; < len(); ++ { := []if !( >= 'a' && <= 'z') && !( >= '0' && <= '9') && != '.' && != '-' && != '_' {returnfmt.Errorf("header key %q contains illegal characters not in [0-9a-z-_.]", ) } }returnnil}// ValidatePair validates a key-value pair with the following rules// (pseudo-header are skipped):// - the key must contain one or more characters.// - the characters in the key must be in [0-9 a-z _ - .].// - if the key ends with a "-bin" suffix, no validation of the corresponding// value is performed.// - the characters in every value must be printable (in [%x20-%x7E]).func ( string, ...string) error {if := ValidateKey(); != nil {return }ifstrings.HasSuffix(, "-bin") {returnnil }// check valuefor , := range {ifhasNotPrintable() {returnfmt.Errorf("header key %q contains value with non-printable ASCII characters", ) } }returnnil}
The pages are generated with Goldsv0.7.6. (GOOS=linux GOARCH=amd64)