1# Copyright 2017 gRPC authors. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import collections 16 17 18class InitialMetadataFlags: 19 used_mask = GRPC_INITIAL_METADATA_USED_MASK 20 wait_for_ready = GRPC_INITIAL_METADATA_WAIT_FOR_READY 21 wait_for_ready_explicitly_set = GRPC_INITIAL_METADATA_WAIT_FOR_READY_EXPLICITLY_SET 22 23 24_Metadatum = collections.namedtuple('_Metadatum', ('key', 'value',)) 25 26 27cdef void _store_c_metadata( 28 metadata, grpc_metadata **c_metadata, size_t *c_count) except *: 29 if metadata is None: 30 c_count[0] = 0 31 c_metadata[0] = NULL 32 else: 33 metadatum_count = len(metadata) 34 if metadatum_count == 0: 35 c_count[0] = 0 36 c_metadata[0] = NULL 37 else: 38 c_count[0] = metadatum_count 39 c_metadata[0] = <grpc_metadata *>gpr_malloc( 40 metadatum_count * sizeof(grpc_metadata)) 41 for index, (key, value) in enumerate(metadata): 42 encoded_key = _encode(key) 43 encoded_value = value if encoded_key[-4:] == b'-bin' else _encode(value) 44 if not isinstance(encoded_value, bytes): 45 raise TypeError('Binary metadata key="%s" expected bytes, got %s' % ( 46 key, 47 type(encoded_value) 48 )) 49 c_metadata[0][index].key = _slice_from_bytes(encoded_key) 50 c_metadata[0][index].value = _slice_from_bytes(encoded_value) 51 52 53cdef void _release_c_metadata(grpc_metadata *c_metadata, int count) except *: 54 if 0 < count: 55 for index in range(count): 56 grpc_slice_unref(c_metadata[index].key) 57 grpc_slice_unref(c_metadata[index].value) 58 gpr_free(c_metadata) 59 60 61cdef tuple _metadatum(grpc_slice key_slice, grpc_slice value_slice): 62 cdef bytes key = _slice_bytes(key_slice) 63 cdef bytes value = _slice_bytes(value_slice) 64 return <tuple>_Metadatum( 65 _decode(key), value if key[-4:] == b'-bin' else _decode(value)) 66 67 68cdef tuple _metadata(grpc_metadata_array *c_metadata_array): 69 return tuple( 70 _metadatum( 71 c_metadata_array.metadata[index].key, 72 c_metadata_array.metadata[index].value) 73 for index in range(c_metadata_array.count)) 74