OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, 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 #ifndef GRPC_INTERNAL_CORE_TRANSPORT_METADATA_H |
| 35 #define GRPC_INTERNAL_CORE_TRANSPORT_METADATA_H |
| 36 |
| 37 #include <grpc/support/slice.h> |
| 38 #include <grpc/support/useful.h> |
| 39 |
| 40 /* This file provides a mechanism for tracking metadata through the grpc stack. |
| 41 It's not intended for consumption outside of the library. |
| 42 |
| 43 Metadata is tracked in the context of a grpc_mdctx. For the time being there |
| 44 is one of these per-channel, avoiding cross channel interference with memory |
| 45 use and lock contention. |
| 46 |
| 47 The context tracks unique strings (grpc_mdstr) and pairs of strings |
| 48 (grpc_mdelem). Any of these objects can be checked for equality by comparing |
| 49 their pointers. These objects are reference counted. |
| 50 |
| 51 grpc_mdelem can additionally store a (non-NULL) user data pointer. This |
| 52 pointer is intended to be used to cache semantic meaning of a metadata |
| 53 element. For example, an OAuth token may cache the credentials it represents |
| 54 and the time at which it expires in the mdelem user data. |
| 55 |
| 56 Combining this metadata cache and the hpack compression table allows us to |
| 57 simply lookup complete preparsed objects quickly, incurring a few atomic |
| 58 ops per metadata element on the fast path. |
| 59 |
| 60 grpc_mdelem instances MAY live longer than their refcount implies, and are |
| 61 garbage collected periodically, meaning cached data can easily outlive a |
| 62 single request. |
| 63 |
| 64 STATIC METADATA: in static_metadata.h we declare a set of static metadata. |
| 65 These mdelems and mdstrs are available via pre-declared code generated macros |
| 66 and are available to code anywhere between grpc_init() and grpc_shutdown(). |
| 67 They are not refcounted, but can be passed to _ref and _unref functions |
| 68 declared here - in which case those functions are effectively no-ops. */ |
| 69 |
| 70 /* Forward declarations */ |
| 71 typedef struct grpc_mdstr grpc_mdstr; |
| 72 typedef struct grpc_mdelem grpc_mdelem; |
| 73 |
| 74 /* if changing this, make identical changes in internal_string in metadata.c */ |
| 75 struct grpc_mdstr { |
| 76 const gpr_slice slice; |
| 77 const uint32_t hash; |
| 78 /* there is a private part to this in metadata.c */ |
| 79 }; |
| 80 |
| 81 /* if changing this, make identical changes in internal_metadata in |
| 82 metadata.c */ |
| 83 struct grpc_mdelem { |
| 84 grpc_mdstr *const key; |
| 85 grpc_mdstr *const value; |
| 86 /* there is a private part to this in metadata.c */ |
| 87 }; |
| 88 |
| 89 void grpc_test_only_set_metadata_hash_seed(uint32_t seed); |
| 90 |
| 91 /* Constructors for grpc_mdstr instances; take a variety of data types that |
| 92 clients may have handy */ |
| 93 grpc_mdstr *grpc_mdstr_from_string(const char *str); |
| 94 /* Unrefs the slice. */ |
| 95 grpc_mdstr *grpc_mdstr_from_slice(gpr_slice slice); |
| 96 grpc_mdstr *grpc_mdstr_from_buffer(const uint8_t *str, size_t length); |
| 97 |
| 98 /* Returns a borrowed slice from the mdstr with its contents base64 encoded |
| 99 and huffman compressed */ |
| 100 gpr_slice grpc_mdstr_as_base64_encoded_and_huffman_compressed(grpc_mdstr *str); |
| 101 |
| 102 /* Constructors for grpc_mdelem instances; take a variety of data types that |
| 103 clients may have handy */ |
| 104 grpc_mdelem *grpc_mdelem_from_metadata_strings(grpc_mdstr *key, |
| 105 grpc_mdstr *value); |
| 106 grpc_mdelem *grpc_mdelem_from_strings(const char *key, const char *value); |
| 107 /* Unrefs the slices. */ |
| 108 grpc_mdelem *grpc_mdelem_from_slices(gpr_slice key, gpr_slice value); |
| 109 grpc_mdelem *grpc_mdelem_from_string_and_buffer(const char *key, |
| 110 const uint8_t *value, |
| 111 size_t value_length); |
| 112 |
| 113 /* Mutator and accessor for grpc_mdelem user data. The destructor function |
| 114 is used as a type tag and is checked during user_data fetch. */ |
| 115 void *grpc_mdelem_get_user_data(grpc_mdelem *md, |
| 116 void (*if_destroy_func)(void *)); |
| 117 void grpc_mdelem_set_user_data(grpc_mdelem *md, void (*destroy_func)(void *), |
| 118 void *user_data); |
| 119 |
| 120 /* Reference counting */ |
| 121 #ifdef GRPC_METADATA_REFCOUNT_DEBUG |
| 122 #define GRPC_MDSTR_REF(s) grpc_mdstr_ref((s), __FILE__, __LINE__) |
| 123 #define GRPC_MDSTR_UNREF(s) grpc_mdstr_unref((s), __FILE__, __LINE__) |
| 124 #define GRPC_MDELEM_REF(s) grpc_mdelem_ref((s), __FILE__, __LINE__) |
| 125 #define GRPC_MDELEM_UNREF(s) grpc_mdelem_unref((s), __FILE__, __LINE__) |
| 126 grpc_mdstr *grpc_mdstr_ref(grpc_mdstr *s, const char *file, int line); |
| 127 void grpc_mdstr_unref(grpc_mdstr *s, const char *file, int line); |
| 128 grpc_mdelem *grpc_mdelem_ref(grpc_mdelem *md, const char *file, int line); |
| 129 void grpc_mdelem_unref(grpc_mdelem *md, const char *file, int line); |
| 130 #else |
| 131 #define GRPC_MDSTR_REF(s) grpc_mdstr_ref((s)) |
| 132 #define GRPC_MDSTR_UNREF(s) grpc_mdstr_unref((s)) |
| 133 #define GRPC_MDELEM_REF(s) grpc_mdelem_ref((s)) |
| 134 #define GRPC_MDELEM_UNREF(s) grpc_mdelem_unref((s)) |
| 135 grpc_mdstr *grpc_mdstr_ref(grpc_mdstr *s); |
| 136 void grpc_mdstr_unref(grpc_mdstr *s); |
| 137 grpc_mdelem *grpc_mdelem_ref(grpc_mdelem *md); |
| 138 void grpc_mdelem_unref(grpc_mdelem *md); |
| 139 #endif |
| 140 |
| 141 /* Recover a char* from a grpc_mdstr. The returned string is null terminated. |
| 142 Does not promise that the returned string has no embedded nulls however. */ |
| 143 const char *grpc_mdstr_as_c_string(grpc_mdstr *s); |
| 144 |
| 145 #define GRPC_MDSTR_LENGTH(s) (GPR_SLICE_LENGTH(s->slice)) |
| 146 |
| 147 int grpc_mdstr_is_legal_header(grpc_mdstr *s); |
| 148 int grpc_mdstr_is_legal_nonbin_header(grpc_mdstr *s); |
| 149 int grpc_mdstr_is_bin_suffixed(grpc_mdstr *s); |
| 150 |
| 151 #define GRPC_MDSTR_KV_HASH(k_hash, v_hash) (GPR_ROTL((k_hash), 2) ^ (v_hash)) |
| 152 |
| 153 void grpc_mdctx_global_init(void); |
| 154 void grpc_mdctx_global_shutdown(void); |
| 155 |
| 156 #endif /* GRPC_INTERNAL_CORE_TRANSPORT_METADATA_H */ |
OLD | NEW |