blob: 713d9e6782ca293122bb216d35e3e5143ec5c14c [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
Craig Tiller9a4dddd2016-03-25 17:08:13 -070034#ifndef GRPC_CORE_LIB_TRANSPORT_METADATA_H
35#define GRPC_CORE_LIB_TRANSPORT_METADATA_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
37#include <grpc/support/slice.h>
ctillerfb93d192014-12-15 10:40:05 -080038#include <grpc/support/useful.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039
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
Craig Tiller70b080d2015-11-19 08:04:48 -080062 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. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080069
70/* Forward declarations */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080071typedef struct grpc_mdstr grpc_mdstr;
72typedef struct grpc_mdelem grpc_mdelem;
73
74/* if changing this, make identical changes in internal_string in metadata.c */
75struct grpc_mdstr {
76 const gpr_slice slice;
Craig Tiller7536af02015-12-22 13:49:30 -080077 const uint32_t hash;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080078 /* 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 */
83struct 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
Craig Tiller7536af02015-12-22 13:49:30 -080089void grpc_test_only_set_metadata_hash_seed(uint32_t seed);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080090
91/* Constructors for grpc_mdstr instances; take a variety of data types that
92 clients may have handy */
Craig Tillerb2b42612015-11-20 12:02:17 -080093grpc_mdstr *grpc_mdstr_from_string(const char *str);
Julien Boeuf75c9b6f2015-05-29 13:12:12 -070094/* Unrefs the slice. */
Craig Tillerb2b42612015-11-20 12:02:17 -080095grpc_mdstr *grpc_mdstr_from_slice(gpr_slice slice);
Craig Tiller7536af02015-12-22 13:49:30 -080096grpc_mdstr *grpc_mdstr_from_buffer(const uint8_t *str, size_t length);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080097
ctiller430c4992014-12-11 09:15:41 -080098/* Returns a borrowed slice from the mdstr with its contents base64 encoded
99 and huffman compressed */
100gpr_slice grpc_mdstr_as_base64_encoded_and_huffman_compressed(grpc_mdstr *str);
101
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800102/* Constructors for grpc_mdelem instances; take a variety of data types that
103 clients may have handy */
Craig Tillerb2b42612015-11-20 12:02:17 -0800104grpc_mdelem *grpc_mdelem_from_metadata_strings(grpc_mdstr *key,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800105 grpc_mdstr *value);
Craig Tillerb2b42612015-11-20 12:02:17 -0800106grpc_mdelem *grpc_mdelem_from_strings(const char *key, const char *value);
Julien Boeuf75c9b6f2015-05-29 13:12:12 -0700107/* Unrefs the slices. */
Craig Tillerb2b42612015-11-20 12:02:17 -0800108grpc_mdelem *grpc_mdelem_from_slices(gpr_slice key, gpr_slice value);
109grpc_mdelem *grpc_mdelem_from_string_and_buffer(const char *key,
Craig Tiller7536af02015-12-22 13:49:30 -0800110 const uint8_t *value,
Craig Tiller4dbdd6a2015-09-25 15:12:16 -0700111 size_t value_length);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800112
yang-gcb7a8022016-03-30 14:58:53 -0700113size_t grpc_mdelem_get_size_in_hpack_table(grpc_mdelem *elem);
114
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800115/* Mutator and accessor for grpc_mdelem user data. The destructor function
116 is used as a type tag and is checked during user_data fetch. */
117void *grpc_mdelem_get_user_data(grpc_mdelem *md,
118 void (*if_destroy_func)(void *));
119void grpc_mdelem_set_user_data(grpc_mdelem *md, void (*destroy_func)(void *),
120 void *user_data);
121
122/* Reference counting */
Craig Tillera92ebc82016-04-24 11:43:24 -0700123//#define GRPC_METADATA_REFCOUNT_DEBUG
Craig Tiller1a65a232015-07-06 10:22:32 -0700124#ifdef GRPC_METADATA_REFCOUNT_DEBUG
125#define GRPC_MDSTR_REF(s) grpc_mdstr_ref((s), __FILE__, __LINE__)
126#define GRPC_MDSTR_UNREF(s) grpc_mdstr_unref((s), __FILE__, __LINE__)
127#define GRPC_MDELEM_REF(s) grpc_mdelem_ref((s), __FILE__, __LINE__)
128#define GRPC_MDELEM_UNREF(s) grpc_mdelem_unref((s), __FILE__, __LINE__)
129grpc_mdstr *grpc_mdstr_ref(grpc_mdstr *s, const char *file, int line);
130void grpc_mdstr_unref(grpc_mdstr *s, const char *file, int line);
131grpc_mdelem *grpc_mdelem_ref(grpc_mdelem *md, const char *file, int line);
132void grpc_mdelem_unref(grpc_mdelem *md, const char *file, int line);
133#else
134#define GRPC_MDSTR_REF(s) grpc_mdstr_ref((s))
135#define GRPC_MDSTR_UNREF(s) grpc_mdstr_unref((s))
136#define GRPC_MDELEM_REF(s) grpc_mdelem_ref((s))
137#define GRPC_MDELEM_UNREF(s) grpc_mdelem_unref((s))
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800138grpc_mdstr *grpc_mdstr_ref(grpc_mdstr *s);
139void grpc_mdstr_unref(grpc_mdstr *s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800140grpc_mdelem *grpc_mdelem_ref(grpc_mdelem *md);
141void grpc_mdelem_unref(grpc_mdelem *md);
Craig Tiller1a65a232015-07-06 10:22:32 -0700142#endif
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800143
144/* Recover a char* from a grpc_mdstr. The returned string is null terminated.
145 Does not promise that the returned string has no embedded nulls however. */
146const char *grpc_mdstr_as_c_string(grpc_mdstr *s);
147
murgatroid99c3910ca2016-01-06 13:14:23 -0800148#define GRPC_MDSTR_LENGTH(s) (GPR_SLICE_LENGTH(s->slice))
149
Craig Tillerb96d0012015-05-06 15:33:23 -0700150int grpc_mdstr_is_legal_header(grpc_mdstr *s);
Craig Tiller49772e02015-08-21 08:08:37 -0700151int grpc_mdstr_is_legal_nonbin_header(grpc_mdstr *s);
Craig Tillerb96d0012015-05-06 15:33:23 -0700152int grpc_mdstr_is_bin_suffixed(grpc_mdstr *s);
153
ctillerfb93d192014-12-15 10:40:05 -0800154#define GRPC_MDSTR_KV_HASH(k_hash, v_hash) (GPR_ROTL((k_hash), 2) ^ (v_hash))
155
Craig Tiller0e72ede2015-11-19 07:48:53 -0800156void grpc_mdctx_global_init(void);
157void grpc_mdctx_global_shutdown(void);
158
Craig Tillerf82ddc42016-04-05 17:15:07 -0700159/* Implementation provided by chttp2_transport */
160extern gpr_slice (*grpc_chttp2_base64_encode_and_huffman_compress)(
161 gpr_slice input);
162
Craig Tiller9a4dddd2016-03-25 17:08:13 -0700163#endif /* GRPC_CORE_LIB_TRANSPORT_METADATA_H */