gRPC  0.6.0
 All Classes Namespaces Functions Variables Enumerations Properties Pages
metadata.h
1 /*
2  *
3  * Copyright 2015, 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 /* Forward declarations */
65 typedef struct grpc_mdctx grpc_mdctx;
66 typedef struct grpc_mdstr grpc_mdstr;
67 typedef struct grpc_mdelem grpc_mdelem;
68 
69 /* if changing this, make identical changes in internal_string in metadata.c */
70 struct grpc_mdstr {
71  const gpr_slice slice;
72  const gpr_uint32 hash;
73  /* there is a private part to this in metadata.c */
74 };
75 
76 /* if changing this, make identical changes in internal_metadata in
77  metadata.c */
78 struct grpc_mdelem {
79  grpc_mdstr *const key;
80  grpc_mdstr *const value;
81  /* there is a private part to this in metadata.c */
82 };
83 
84 /* Create/orphan a metadata context */
85 grpc_mdctx *grpc_mdctx_create(void);
86 grpc_mdctx *grpc_mdctx_create_with_seed(gpr_uint32 seed);
87 void grpc_mdctx_ref(grpc_mdctx *mdctx);
88 void grpc_mdctx_unref(grpc_mdctx *mdctx);
89 
90 /* Test only accessors to internal state - only for testing this code - do not
91  rely on it outside of metadata_test.c */
92 size_t grpc_mdctx_get_mdtab_capacity_test_only(grpc_mdctx *mdctx);
93 size_t grpc_mdctx_get_mdtab_count_test_only(grpc_mdctx *mdctx);
94 size_t grpc_mdctx_get_mdtab_free_test_only(grpc_mdctx *mdctx);
95 
96 /* Constructors for grpc_mdstr instances; take a variety of data types that
97  clients may have handy */
98 grpc_mdstr *grpc_mdstr_from_string(grpc_mdctx *ctx, const char *str);
99 grpc_mdstr *grpc_mdstr_from_slice(grpc_mdctx *ctx, gpr_slice slice);
100 grpc_mdstr *grpc_mdstr_from_buffer(grpc_mdctx *ctx, const gpr_uint8 *str,
101  size_t length);
102 
103 /* Returns a borrowed slice from the mdstr with its contents base64 encoded
104  and huffman compressed */
105 gpr_slice grpc_mdstr_as_base64_encoded_and_huffman_compressed(grpc_mdstr *str);
106 
107 /* Constructors for grpc_mdelem instances; take a variety of data types that
108  clients may have handy */
109 grpc_mdelem *grpc_mdelem_from_metadata_strings(grpc_mdctx *ctx, grpc_mdstr *key,
110  grpc_mdstr *value);
111 grpc_mdelem *grpc_mdelem_from_strings(grpc_mdctx *ctx, const char *key,
112  const char *value);
113 grpc_mdelem *grpc_mdelem_from_slices(grpc_mdctx *ctx, gpr_slice key,
114  gpr_slice value);
115 grpc_mdelem *grpc_mdelem_from_string_and_buffer(grpc_mdctx *ctx,
116  const char *key,
117  const gpr_uint8 *value,
118  size_t value_length);
119 
120 /* Mutator and accessor for grpc_mdelem user data. The destructor function
121  is used as a type tag and is checked during user_data fetch. */
122 void *grpc_mdelem_get_user_data(grpc_mdelem *md,
123  void (*if_destroy_func)(void *));
124 void grpc_mdelem_set_user_data(grpc_mdelem *md, void (*destroy_func)(void *),
125  void *user_data);
126 
127 /* Reference counting */
128 grpc_mdstr *grpc_mdstr_ref(grpc_mdstr *s);
129 void grpc_mdstr_unref(grpc_mdstr *s);
130 
131 grpc_mdelem *grpc_mdelem_ref(grpc_mdelem *md);
132 void grpc_mdelem_unref(grpc_mdelem *md);
133 
134 /* Recover a char* from a grpc_mdstr. The returned string is null terminated.
135  Does not promise that the returned string has no embedded nulls however. */
136 const char *grpc_mdstr_as_c_string(grpc_mdstr *s);
137 
138 int grpc_mdstr_is_legal_header(grpc_mdstr *s);
139 int grpc_mdstr_is_bin_suffixed(grpc_mdstr *s);
140 
141 /* Batch mode metadata functions.
142  These API's have equivalents above, but allow taking the mdctx just once,
143  performing a bunch of work, and then leaving the mdctx. */
144 
145 /* Lock the metadata context: it's only safe to call _locked_ functions against
146  this context from the calling thread until grpc_mdctx_unlock is called */
147 void grpc_mdctx_lock(grpc_mdctx *ctx);
148 /* Unref a metadata element */
149 void grpc_mdctx_locked_mdelem_unref(grpc_mdctx *ctx, grpc_mdelem *elem);
150 /* Unlock the metadata context */
151 void grpc_mdctx_unlock(grpc_mdctx *ctx);
152 
153 #define GRPC_MDSTR_KV_HASH(k_hash, v_hash) (GPR_ROTL((k_hash), 2) ^ (v_hash))
154 
155 #endif /* GRPC_INTERNAL_CORE_TRANSPORT_METADATA_H */
Definition: metadata.h:70
Definition: metadata.c:83
Definition: metadata.h:78
Definition: slice.h:79