Skip to content
Snippets Groups Projects
Commit e52bbb1d authored by Craig Tiller's avatar Craig Tiller
Browse files

Initial elimination of grpc_mdstr from metadata.{h,c}

parent 515a33d6
No related branches found
No related tags found
No related merge requests found
...@@ -127,6 +127,8 @@ GPRAPI grpc_slice gpr_empty_slice(void); ...@@ -127,6 +127,8 @@ GPRAPI grpc_slice gpr_empty_slice(void);
GPRAPI int grpc_slice_cmp(grpc_slice a, grpc_slice b); GPRAPI int grpc_slice_cmp(grpc_slice a, grpc_slice b);
GPRAPI int grpc_slice_str_cmp(grpc_slice a, const char *b); GPRAPI int grpc_slice_str_cmp(grpc_slice a, const char *b);
GPRAPI uint32_t grpc_slice_hash(grpc_slice s);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
/*
*
* Copyright 2016, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef SLICE_TRAITS_H
#define SLICE_TRAITS_H
#include <grpc/slice.h>
#include <stdbool.h>
bool grpc_slice_is_legal_header(grpc_slice s);
bool grpc_slice_is_legal_nonbin_header(grpc_slice s);
bool grpc_slice_is_bin_suffixed(grpc_slice s);
#endif
This diff is collapsed.
...@@ -74,51 +74,19 @@ extern "C" { ...@@ -74,51 +74,19 @@ extern "C" {
declared here - in which case those functions are effectively no-ops. */ declared here - in which case those functions are effectively no-ops. */
/* Forward declarations */ /* Forward declarations */
typedef struct grpc_mdstr grpc_mdstr;
typedef struct grpc_mdelem grpc_mdelem; typedef struct grpc_mdelem grpc_mdelem;
/* if changing this, make identical changes in internal_string in metadata.c */
struct grpc_mdstr {
const grpc_slice slice;
const uint32_t hash;
/* there is a private part to this in metadata.c */
};
/* if changing this, make identical changes in internal_metadata in /* if changing this, make identical changes in internal_metadata in
metadata.c */ metadata.c */
struct grpc_mdelem { struct grpc_mdelem {
grpc_mdstr *const key; const grpc_slice key;
grpc_mdstr *const value; const grpc_slice value;
/* there is a private part to this in metadata.c */ /* there is a private part to this in metadata.c */
}; };
void grpc_test_only_set_metadata_hash_seed(uint32_t seed);
/* Constructors for grpc_mdstr instances; take a variety of data types that
clients may have handy */
grpc_mdstr *grpc_mdstr_from_string(const char *str);
/* Unrefs the slice. */
grpc_mdstr *grpc_mdstr_from_slice(grpc_exec_ctx *exec_ctx, grpc_slice slice);
grpc_mdstr *grpc_mdstr_from_buffer(const uint8_t *str, size_t length);
/* Returns a borrowed slice from the mdstr with its contents base64 encoded
and huffman compressed */
grpc_slice grpc_mdstr_as_base64_encoded_and_huffman_compressed(grpc_mdstr *str);
/* Constructors for grpc_mdelem instances; take a variety of data types that
clients may have handy */
grpc_mdelem *grpc_mdelem_from_metadata_strings(grpc_exec_ctx *exec_ctx,
grpc_mdstr *key,
grpc_mdstr *value);
grpc_mdelem *grpc_mdelem_from_strings(grpc_exec_ctx *exec_ctx, const char *key,
const char *value);
/* Unrefs the slices. */ /* Unrefs the slices. */
grpc_mdelem *grpc_mdelem_from_slices(grpc_exec_ctx *exec_ctx, grpc_slice key, grpc_mdelem *grpc_mdelem_from_slices(grpc_exec_ctx *exec_ctx, grpc_slice key,
grpc_slice value); grpc_slice value);
grpc_mdelem *grpc_mdelem_from_string_and_buffer(grpc_exec_ctx *exec_ctx,
const char *key,
const uint8_t *value,
size_t value_length);
size_t grpc_mdelem_get_size_in_hpack_table(grpc_mdelem *elem); size_t grpc_mdelem_get_size_in_hpack_table(grpc_mdelem *elem);
...@@ -132,52 +100,30 @@ void grpc_mdelem_set_user_data(grpc_mdelem *md, void (*destroy_func)(void *), ...@@ -132,52 +100,30 @@ void grpc_mdelem_set_user_data(grpc_mdelem *md, void (*destroy_func)(void *),
/* Reference counting */ /* Reference counting */
//#define GRPC_METADATA_REFCOUNT_DEBUG //#define GRPC_METADATA_REFCOUNT_DEBUG
#ifdef GRPC_METADATA_REFCOUNT_DEBUG #ifdef GRPC_METADATA_REFCOUNT_DEBUG
#define GRPC_MDSTR_REF(s) grpc_mdstr_ref((s), __FILE__, __LINE__)
#define GRPC_MDSTR_UNREF(exec_ctx, s) \
grpc_mdstr_unref((exec_ctx), (s), __FILE__, __LINE__)
#define GRPC_MDELEM_REF(s) grpc_mdelem_ref((s), __FILE__, __LINE__) #define GRPC_MDELEM_REF(s) grpc_mdelem_ref((s), __FILE__, __LINE__)
#define GRPC_MDELEM_UNREF(exec_ctx, s) \ #define GRPC_MDELEM_UNREF(exec_ctx, s) \
grpc_mdelem_unref((exec_ctx), (s), __FILE__, __LINE__) grpc_mdelem_unref((exec_ctx), (s), __FILE__, __LINE__)
grpc_mdstr *grpc_mdstr_ref(grpc_mdstr *s, const char *file, int line);
void grpc_mdstr_unref(grpc_exec_ctx *exec_ctx, grpc_mdstr *s, const char *file,
int line);
grpc_mdelem *grpc_mdelem_ref(grpc_mdelem *md, const char *file, int line); grpc_mdelem *grpc_mdelem_ref(grpc_mdelem *md, const char *file, int line);
void grpc_mdelem_unref(grpc_exec_ctx *exec_ctx, grpc_mdelem *md, void grpc_mdelem_unref(grpc_exec_ctx *exec_ctx, grpc_mdelem *md,
const char *file, int line); const char *file, int line);
#else #else
#define GRPC_MDSTR_REF(s) grpc_mdstr_ref((s))
#define GRPC_MDSTR_UNREF(exec_ctx, s) grpc_mdstr_unref((exec_ctx), (s))
#define GRPC_MDELEM_REF(s) grpc_mdelem_ref((s)) #define GRPC_MDELEM_REF(s) grpc_mdelem_ref((s))
#define GRPC_MDELEM_UNREF(exec_ctx, s) grpc_mdelem_unref((exec_ctx), (s)) #define GRPC_MDELEM_UNREF(exec_ctx, s) grpc_mdelem_unref((exec_ctx), (s))
grpc_mdstr *grpc_mdstr_ref(grpc_mdstr *s);
void grpc_mdstr_unref(grpc_exec_ctx *exec_ctx, grpc_mdstr *s);
grpc_mdelem *grpc_mdelem_ref(grpc_mdelem *md); grpc_mdelem *grpc_mdelem_ref(grpc_mdelem *md);
void grpc_mdelem_unref(grpc_exec_ctx *exec_ctx, grpc_mdelem *md); void grpc_mdelem_unref(grpc_exec_ctx *exec_ctx, grpc_mdelem *md);
#endif #endif
/* Recover a char* from a grpc_mdstr. The returned string is null terminated.
Does not promise that the returned string has no embedded nulls however. */
const char *grpc_mdstr_as_c_string(const grpc_mdstr *s);
#define GRPC_MDSTR_LENGTH(s) (GRPC_SLICE_LENGTH(s->slice)) #define GRPC_MDSTR_LENGTH(s) (GRPC_SLICE_LENGTH(s->slice))
/* We add 32 bytes of padding as per RFC-7540 section 6.5.2. */ /* We add 32 bytes of padding as per RFC-7540 section 6.5.2. */
#define GRPC_MDELEM_LENGTH(e) \ #define GRPC_MDELEM_LENGTH(e) \
(GRPC_MDSTR_LENGTH((e)->key) + GRPC_MDSTR_LENGTH((e)->value) + 32) (GRPC_MDSTR_LENGTH((e)->key) + GRPC_MDSTR_LENGTH((e)->value) + 32)
int grpc_mdstr_is_legal_header(grpc_mdstr *s);
int grpc_mdstr_is_legal_nonbin_header(grpc_mdstr *s);
int grpc_mdstr_is_bin_suffixed(grpc_mdstr *s);
#define GRPC_MDSTR_KV_HASH(k_hash, v_hash) (GPR_ROTL((k_hash), 2) ^ (v_hash)) #define GRPC_MDSTR_KV_HASH(k_hash, v_hash) (GPR_ROTL((k_hash), 2) ^ (v_hash))
void grpc_mdctx_global_init(void); void grpc_mdctx_global_init(void);
void grpc_mdctx_global_shutdown(grpc_exec_ctx *exec_ctx); void grpc_mdctx_global_shutdown(grpc_exec_ctx *exec_ctx);
/* Implementation provided by chttp2_transport */
extern grpc_slice (*grpc_chttp2_base64_encode_and_huffman_compress)(
grpc_slice input);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
This diff is collapsed.
...@@ -45,194 +45,196 @@ ...@@ -45,194 +45,196 @@
#include "src/core/lib/transport/metadata.h" #include "src/core/lib/transport/metadata.h"
#define GRPC_STATIC_MDSTR_COUNT 93 #define GRPC_STATIC_MDSTR_COUNT 93
extern grpc_mdstr grpc_static_mdstr_table[GRPC_STATIC_MDSTR_COUNT]; extern const grpc_slice grpc_static_slice_table[GRPC_STATIC_MDSTR_COUNT];
/* "0" */ /* "0" */
#define GRPC_MDSTR_0 (&grpc_static_mdstr_table[0]) #define GRPC_MDSTR_0 (&grpc_static_slice_table[0])
/* "1" */ /* "1" */
#define GRPC_MDSTR_1 (&grpc_static_mdstr_table[1]) #define GRPC_MDSTR_1 (&grpc_static_slice_table[1])
/* "2" */ /* "2" */
#define GRPC_MDSTR_2 (&grpc_static_mdstr_table[2]) #define GRPC_MDSTR_2 (&grpc_static_slice_table[2])
/* "200" */ /* "200" */
#define GRPC_MDSTR_200 (&grpc_static_mdstr_table[3]) #define GRPC_MDSTR_200 (&grpc_static_slice_table[3])
/* "204" */ /* "204" */
#define GRPC_MDSTR_204 (&grpc_static_mdstr_table[4]) #define GRPC_MDSTR_204 (&grpc_static_slice_table[4])
/* "206" */ /* "206" */
#define GRPC_MDSTR_206 (&grpc_static_mdstr_table[5]) #define GRPC_MDSTR_206 (&grpc_static_slice_table[5])
/* "304" */ /* "304" */
#define GRPC_MDSTR_304 (&grpc_static_mdstr_table[6]) #define GRPC_MDSTR_304 (&grpc_static_slice_table[6])
/* "400" */ /* "400" */
#define GRPC_MDSTR_400 (&grpc_static_mdstr_table[7]) #define GRPC_MDSTR_400 (&grpc_static_slice_table[7])
/* "404" */ /* "404" */
#define GRPC_MDSTR_404 (&grpc_static_mdstr_table[8]) #define GRPC_MDSTR_404 (&grpc_static_slice_table[8])
/* "500" */ /* "500" */
#define GRPC_MDSTR_500 (&grpc_static_mdstr_table[9]) #define GRPC_MDSTR_500 (&grpc_static_slice_table[9])
/* "accept" */ /* "accept" */
#define GRPC_MDSTR_ACCEPT (&grpc_static_mdstr_table[10]) #define GRPC_MDSTR_ACCEPT (&grpc_static_slice_table[10])
/* "accept-charset" */ /* "accept-charset" */
#define GRPC_MDSTR_ACCEPT_CHARSET (&grpc_static_mdstr_table[11]) #define GRPC_MDSTR_ACCEPT_CHARSET (&grpc_static_slice_table[11])
/* "accept-encoding" */ /* "accept-encoding" */
#define GRPC_MDSTR_ACCEPT_ENCODING (&grpc_static_mdstr_table[12]) #define GRPC_MDSTR_ACCEPT_ENCODING (&grpc_static_slice_table[12])
/* "accept-language" */ /* "accept-language" */
#define GRPC_MDSTR_ACCEPT_LANGUAGE (&grpc_static_mdstr_table[13]) #define GRPC_MDSTR_ACCEPT_LANGUAGE (&grpc_static_slice_table[13])
/* "accept-ranges" */ /* "accept-ranges" */
#define GRPC_MDSTR_ACCEPT_RANGES (&grpc_static_mdstr_table[14]) #define GRPC_MDSTR_ACCEPT_RANGES (&grpc_static_slice_table[14])
/* "access-control-allow-origin" */ /* "access-control-allow-origin" */
#define GRPC_MDSTR_ACCESS_CONTROL_ALLOW_ORIGIN (&grpc_static_mdstr_table[15]) #define GRPC_MDSTR_ACCESS_CONTROL_ALLOW_ORIGIN (&grpc_static_slice_table[15])
/* "age" */ /* "age" */
#define GRPC_MDSTR_AGE (&grpc_static_mdstr_table[16]) #define GRPC_MDSTR_AGE (&grpc_static_slice_table[16])
/* "allow" */ /* "allow" */
#define GRPC_MDSTR_ALLOW (&grpc_static_mdstr_table[17]) #define GRPC_MDSTR_ALLOW (&grpc_static_slice_table[17])
/* "application/grpc" */ /* "application/grpc" */
#define GRPC_MDSTR_APPLICATION_SLASH_GRPC (&grpc_static_mdstr_table[18]) #define GRPC_MDSTR_APPLICATION_SLASH_GRPC (&grpc_static_slice_table[18])
/* ":authority" */ /* ":authority" */
#define GRPC_MDSTR_AUTHORITY (&grpc_static_mdstr_table[19]) #define GRPC_MDSTR_AUTHORITY (&grpc_static_slice_table[19])
/* "authorization" */ /* "authorization" */
#define GRPC_MDSTR_AUTHORIZATION (&grpc_static_mdstr_table[20]) #define GRPC_MDSTR_AUTHORIZATION (&grpc_static_slice_table[20])
/* "cache-control" */ /* "cache-control" */
#define GRPC_MDSTR_CACHE_CONTROL (&grpc_static_mdstr_table[21]) #define GRPC_MDSTR_CACHE_CONTROL (&grpc_static_slice_table[21])
/* "content-disposition" */ /* "content-disposition" */
#define GRPC_MDSTR_CONTENT_DISPOSITION (&grpc_static_mdstr_table[22]) #define GRPC_MDSTR_CONTENT_DISPOSITION (&grpc_static_slice_table[22])
/* "content-encoding" */ /* "content-encoding" */
#define GRPC_MDSTR_CONTENT_ENCODING (&grpc_static_mdstr_table[23]) #define GRPC_MDSTR_CONTENT_ENCODING (&grpc_static_slice_table[23])
/* "content-language" */ /* "content-language" */
#define GRPC_MDSTR_CONTENT_LANGUAGE (&grpc_static_mdstr_table[24]) #define GRPC_MDSTR_CONTENT_LANGUAGE (&grpc_static_slice_table[24])
/* "content-length" */ /* "content-length" */
#define GRPC_MDSTR_CONTENT_LENGTH (&grpc_static_mdstr_table[25]) #define GRPC_MDSTR_CONTENT_LENGTH (&grpc_static_slice_table[25])
/* "content-location" */ /* "content-location" */
#define GRPC_MDSTR_CONTENT_LOCATION (&grpc_static_mdstr_table[26]) #define GRPC_MDSTR_CONTENT_LOCATION (&grpc_static_slice_table[26])
/* "content-range" */ /* "content-range" */
#define GRPC_MDSTR_CONTENT_RANGE (&grpc_static_mdstr_table[27]) #define GRPC_MDSTR_CONTENT_RANGE (&grpc_static_slice_table[27])
/* "content-type" */ /* "content-type" */
#define GRPC_MDSTR_CONTENT_TYPE (&grpc_static_mdstr_table[28]) #define GRPC_MDSTR_CONTENT_TYPE (&grpc_static_slice_table[28])
/* "cookie" */ /* "cookie" */
#define GRPC_MDSTR_COOKIE (&grpc_static_mdstr_table[29]) #define GRPC_MDSTR_COOKIE (&grpc_static_slice_table[29])
/* "date" */ /* "date" */
#define GRPC_MDSTR_DATE (&grpc_static_mdstr_table[30]) #define GRPC_MDSTR_DATE (&grpc_static_slice_table[30])
/* "deflate" */ /* "deflate" */
#define GRPC_MDSTR_DEFLATE (&grpc_static_mdstr_table[31]) #define GRPC_MDSTR_DEFLATE (&grpc_static_slice_table[31])
/* "deflate,gzip" */ /* "deflate,gzip" */
#define GRPC_MDSTR_DEFLATE_COMMA_GZIP (&grpc_static_mdstr_table[32]) #define GRPC_MDSTR_DEFLATE_COMMA_GZIP (&grpc_static_slice_table[32])
/* "" */ /* "" */
#define GRPC_MDSTR_EMPTY (&grpc_static_mdstr_table[33]) #define GRPC_MDSTR_EMPTY (&grpc_static_slice_table[33])
/* "etag" */ /* "etag" */
#define GRPC_MDSTR_ETAG (&grpc_static_mdstr_table[34]) #define GRPC_MDSTR_ETAG (&grpc_static_slice_table[34])
/* "expect" */ /* "expect" */
#define GRPC_MDSTR_EXPECT (&grpc_static_mdstr_table[35]) #define GRPC_MDSTR_EXPECT (&grpc_static_slice_table[35])
/* "expires" */ /* "expires" */
#define GRPC_MDSTR_EXPIRES (&grpc_static_mdstr_table[36]) #define GRPC_MDSTR_EXPIRES (&grpc_static_slice_table[36])
/* "from" */ /* "from" */
#define GRPC_MDSTR_FROM (&grpc_static_mdstr_table[37]) #define GRPC_MDSTR_FROM (&grpc_static_slice_table[37])
/* "GET" */ /* "GET" */
#define GRPC_MDSTR_GET (&grpc_static_mdstr_table[38]) #define GRPC_MDSTR_GET (&grpc_static_slice_table[38])
/* "grpc" */ /* "grpc" */
#define GRPC_MDSTR_GRPC (&grpc_static_mdstr_table[39]) #define GRPC_MDSTR_GRPC (&grpc_static_slice_table[39])
/* "grpc-accept-encoding" */ /* "grpc-accept-encoding" */
#define GRPC_MDSTR_GRPC_ACCEPT_ENCODING (&grpc_static_mdstr_table[40]) #define GRPC_MDSTR_GRPC_ACCEPT_ENCODING (&grpc_static_slice_table[40])
/* "grpc-encoding" */ /* "grpc-encoding" */
#define GRPC_MDSTR_GRPC_ENCODING (&grpc_static_mdstr_table[41]) #define GRPC_MDSTR_GRPC_ENCODING (&grpc_static_slice_table[41])
/* "grpc-internal-encoding-request" */ /* "grpc-internal-encoding-request" */
#define GRPC_MDSTR_GRPC_INTERNAL_ENCODING_REQUEST (&grpc_static_mdstr_table[42]) #define GRPC_MDSTR_GRPC_INTERNAL_ENCODING_REQUEST (&grpc_static_slice_table[42])
/* "grpc-message" */ /* "grpc-message" */
#define GRPC_MDSTR_GRPC_MESSAGE (&grpc_static_mdstr_table[43]) #define GRPC_MDSTR_GRPC_MESSAGE (&grpc_static_slice_table[43])
/* "grpc-payload-bin" */ /* "grpc-payload-bin" */
#define GRPC_MDSTR_GRPC_PAYLOAD_BIN (&grpc_static_mdstr_table[44]) #define GRPC_MDSTR_GRPC_PAYLOAD_BIN (&grpc_static_slice_table[44])
/* "grpc-stats-bin" */ /* "grpc-stats-bin" */
#define GRPC_MDSTR_GRPC_STATS_BIN (&grpc_static_mdstr_table[45]) #define GRPC_MDSTR_GRPC_STATS_BIN (&grpc_static_slice_table[45])
/* "grpc-status" */ /* "grpc-status" */
#define GRPC_MDSTR_GRPC_STATUS (&grpc_static_mdstr_table[46]) #define GRPC_MDSTR_GRPC_STATUS (&grpc_static_slice_table[46])
/* "grpc-timeout" */ /* "grpc-timeout" */
#define GRPC_MDSTR_GRPC_TIMEOUT (&grpc_static_mdstr_table[47]) #define GRPC_MDSTR_GRPC_TIMEOUT (&grpc_static_slice_table[47])
/* "grpc-tracing-bin" */ /* "grpc-tracing-bin" */
#define GRPC_MDSTR_GRPC_TRACING_BIN (&grpc_static_mdstr_table[48]) #define GRPC_MDSTR_GRPC_TRACING_BIN (&grpc_static_slice_table[48])
/* "gzip" */ /* "gzip" */
#define GRPC_MDSTR_GZIP (&grpc_static_mdstr_table[49]) #define GRPC_MDSTR_GZIP (&grpc_static_slice_table[49])
/* "gzip, deflate" */ /* "gzip, deflate" */
#define GRPC_MDSTR_GZIP_COMMA_DEFLATE (&grpc_static_mdstr_table[50]) #define GRPC_MDSTR_GZIP_COMMA_DEFLATE (&grpc_static_slice_table[50])
/* "host" */ /* "host" */
#define GRPC_MDSTR_HOST (&grpc_static_mdstr_table[51]) #define GRPC_MDSTR_HOST (&grpc_static_slice_table[51])
/* "http" */ /* "http" */
#define GRPC_MDSTR_HTTP (&grpc_static_mdstr_table[52]) #define GRPC_MDSTR_HTTP (&grpc_static_slice_table[52])
/* "https" */ /* "https" */
#define GRPC_MDSTR_HTTPS (&grpc_static_mdstr_table[53]) #define GRPC_MDSTR_HTTPS (&grpc_static_slice_table[53])
/* "identity" */ /* "identity" */
#define GRPC_MDSTR_IDENTITY (&grpc_static_mdstr_table[54]) #define GRPC_MDSTR_IDENTITY (&grpc_static_slice_table[54])
/* "identity,deflate" */ /* "identity,deflate" */
#define GRPC_MDSTR_IDENTITY_COMMA_DEFLATE (&grpc_static_mdstr_table[55]) #define GRPC_MDSTR_IDENTITY_COMMA_DEFLATE (&grpc_static_slice_table[55])
/* "identity,deflate,gzip" */ /* "identity,deflate,gzip" */
#define GRPC_MDSTR_IDENTITY_COMMA_DEFLATE_COMMA_GZIP \ #define GRPC_MDSTR_IDENTITY_COMMA_DEFLATE_COMMA_GZIP \
(&grpc_static_mdstr_table[56]) (&grpc_static_slice_table[56])
/* "identity,gzip" */ /* "identity,gzip" */
#define GRPC_MDSTR_IDENTITY_COMMA_GZIP (&grpc_static_mdstr_table[57]) #define GRPC_MDSTR_IDENTITY_COMMA_GZIP (&grpc_static_slice_table[57])
/* "if-match" */ /* "if-match" */
#define GRPC_MDSTR_IF_MATCH (&grpc_static_mdstr_table[58]) #define GRPC_MDSTR_IF_MATCH (&grpc_static_slice_table[58])
/* "if-modified-since" */ /* "if-modified-since" */
#define GRPC_MDSTR_IF_MODIFIED_SINCE (&grpc_static_mdstr_table[59]) #define GRPC_MDSTR_IF_MODIFIED_SINCE (&grpc_static_slice_table[59])
/* "if-none-match" */ /* "if-none-match" */
#define GRPC_MDSTR_IF_NONE_MATCH (&grpc_static_mdstr_table[60]) #define GRPC_MDSTR_IF_NONE_MATCH (&grpc_static_slice_table[60])
/* "if-range" */ /* "if-range" */
#define GRPC_MDSTR_IF_RANGE (&grpc_static_mdstr_table[61]) #define GRPC_MDSTR_IF_RANGE (&grpc_static_slice_table[61])
/* "if-unmodified-since" */ /* "if-unmodified-since" */
#define GRPC_MDSTR_IF_UNMODIFIED_SINCE (&grpc_static_mdstr_table[62]) #define GRPC_MDSTR_IF_UNMODIFIED_SINCE (&grpc_static_slice_table[62])
/* "last-modified" */ /* "last-modified" */
#define GRPC_MDSTR_LAST_MODIFIED (&grpc_static_mdstr_table[63]) #define GRPC_MDSTR_LAST_MODIFIED (&grpc_static_slice_table[63])
/* "lb-cost-bin" */ /* "lb-cost-bin" */
#define GRPC_MDSTR_LB_COST_BIN (&grpc_static_mdstr_table[64]) #define GRPC_MDSTR_LB_COST_BIN (&grpc_static_slice_table[64])
/* "lb-token" */ /* "lb-token" */
#define GRPC_MDSTR_LB_TOKEN (&grpc_static_mdstr_table[65]) #define GRPC_MDSTR_LB_TOKEN (&grpc_static_slice_table[65])
/* "link" */ /* "link" */
#define GRPC_MDSTR_LINK (&grpc_static_mdstr_table[66]) #define GRPC_MDSTR_LINK (&grpc_static_slice_table[66])
/* "location" */ /* "location" */
#define GRPC_MDSTR_LOCATION (&grpc_static_mdstr_table[67]) #define GRPC_MDSTR_LOCATION (&grpc_static_slice_table[67])
/* "max-forwards" */ /* "max-forwards" */
#define GRPC_MDSTR_MAX_FORWARDS (&grpc_static_mdstr_table[68]) #define GRPC_MDSTR_MAX_FORWARDS (&grpc_static_slice_table[68])
/* ":method" */ /* ":method" */
#define GRPC_MDSTR_METHOD (&grpc_static_mdstr_table[69]) #define GRPC_MDSTR_METHOD (&grpc_static_slice_table[69])
/* ":path" */ /* ":path" */
#define GRPC_MDSTR_PATH (&grpc_static_mdstr_table[70]) #define GRPC_MDSTR_PATH (&grpc_static_slice_table[70])
/* "POST" */ /* "POST" */
#define GRPC_MDSTR_POST (&grpc_static_mdstr_table[71]) #define GRPC_MDSTR_POST (&grpc_static_slice_table[71])
/* "proxy-authenticate" */ /* "proxy-authenticate" */
#define GRPC_MDSTR_PROXY_AUTHENTICATE (&grpc_static_mdstr_table[72]) #define GRPC_MDSTR_PROXY_AUTHENTICATE (&grpc_static_slice_table[72])
/* "proxy-authorization" */ /* "proxy-authorization" */
#define GRPC_MDSTR_PROXY_AUTHORIZATION (&grpc_static_mdstr_table[73]) #define GRPC_MDSTR_PROXY_AUTHORIZATION (&grpc_static_slice_table[73])
/* "PUT" */ /* "PUT" */
#define GRPC_MDSTR_PUT (&grpc_static_mdstr_table[74]) #define GRPC_MDSTR_PUT (&grpc_static_slice_table[74])
/* "range" */ /* "range" */
#define GRPC_MDSTR_RANGE (&grpc_static_mdstr_table[75]) #define GRPC_MDSTR_RANGE (&grpc_static_slice_table[75])
/* "referer" */ /* "referer" */
#define GRPC_MDSTR_REFERER (&grpc_static_mdstr_table[76]) #define GRPC_MDSTR_REFERER (&grpc_static_slice_table[76])
/* "refresh" */ /* "refresh" */
#define GRPC_MDSTR_REFRESH (&grpc_static_mdstr_table[77]) #define GRPC_MDSTR_REFRESH (&grpc_static_slice_table[77])
/* "retry-after" */ /* "retry-after" */
#define GRPC_MDSTR_RETRY_AFTER (&grpc_static_mdstr_table[78]) #define GRPC_MDSTR_RETRY_AFTER (&grpc_static_slice_table[78])
/* ":scheme" */ /* ":scheme" */
#define GRPC_MDSTR_SCHEME (&grpc_static_mdstr_table[79]) #define GRPC_MDSTR_SCHEME (&grpc_static_slice_table[79])
/* "server" */ /* "server" */
#define GRPC_MDSTR_SERVER (&grpc_static_mdstr_table[80]) #define GRPC_MDSTR_SERVER (&grpc_static_slice_table[80])
/* "set-cookie" */ /* "set-cookie" */
#define GRPC_MDSTR_SET_COOKIE (&grpc_static_mdstr_table[81]) #define GRPC_MDSTR_SET_COOKIE (&grpc_static_slice_table[81])
/* "/" */ /* "/" */
#define GRPC_MDSTR_SLASH (&grpc_static_mdstr_table[82]) #define GRPC_MDSTR_SLASH (&grpc_static_slice_table[82])
/* "/index.html" */ /* "/index.html" */
#define GRPC_MDSTR_SLASH_INDEX_DOT_HTML (&grpc_static_mdstr_table[83]) #define GRPC_MDSTR_SLASH_INDEX_DOT_HTML (&grpc_static_slice_table[83])
/* ":status" */ /* ":status" */
#define GRPC_MDSTR_STATUS (&grpc_static_mdstr_table[84]) #define GRPC_MDSTR_STATUS (&grpc_static_slice_table[84])
/* "strict-transport-security" */ /* "strict-transport-security" */
#define GRPC_MDSTR_STRICT_TRANSPORT_SECURITY (&grpc_static_mdstr_table[85]) #define GRPC_MDSTR_STRICT_TRANSPORT_SECURITY (&grpc_static_slice_table[85])
/* "te" */ /* "te" */
#define GRPC_MDSTR_TE (&grpc_static_mdstr_table[86]) #define GRPC_MDSTR_TE (&grpc_static_slice_table[86])
/* "trailers" */ /* "trailers" */
#define GRPC_MDSTR_TRAILERS (&grpc_static_mdstr_table[87]) #define GRPC_MDSTR_TRAILERS (&grpc_static_slice_table[87])
/* "transfer-encoding" */ /* "transfer-encoding" */
#define GRPC_MDSTR_TRANSFER_ENCODING (&grpc_static_mdstr_table[88]) #define GRPC_MDSTR_TRANSFER_ENCODING (&grpc_static_slice_table[88])
/* "user-agent" */ /* "user-agent" */
#define GRPC_MDSTR_USER_AGENT (&grpc_static_mdstr_table[89]) #define GRPC_MDSTR_USER_AGENT (&grpc_static_slice_table[89])
/* "vary" */ /* "vary" */
#define GRPC_MDSTR_VARY (&grpc_static_mdstr_table[90]) #define GRPC_MDSTR_VARY (&grpc_static_slice_table[90])
/* "via" */ /* "via" */
#define GRPC_MDSTR_VIA (&grpc_static_mdstr_table[91]) #define GRPC_MDSTR_VIA (&grpc_static_slice_table[91])
/* "www-authenticate" */ /* "www-authenticate" */
#define GRPC_MDSTR_WWW_AUTHENTICATE (&grpc_static_mdstr_table[92]) #define GRPC_MDSTR_WWW_AUTHENTICATE (&grpc_static_slice_table[92])
bool grpc_is_static_metadata_string(grpc_slice slice);
#define GRPC_STATIC_MDELEM_COUNT 81 #define GRPC_STATIC_MDELEM_COUNT 81
extern grpc_mdelem grpc_static_mdelem_table[GRPC_STATIC_MDELEM_COUNT]; extern grpc_mdelem grpc_static_mdelem_table[GRPC_STATIC_MDELEM_COUNT];
...@@ -409,9 +411,9 @@ extern uintptr_t grpc_static_mdelem_user_data[GRPC_STATIC_MDELEM_COUNT]; ...@@ -409,9 +411,9 @@ extern uintptr_t grpc_static_mdelem_user_data[GRPC_STATIC_MDELEM_COUNT];
/* "www-authenticate": "" */ /* "www-authenticate": "" */
#define GRPC_MDELEM_WWW_AUTHENTICATE_EMPTY (&grpc_static_mdelem_table[80]) #define GRPC_MDELEM_WWW_AUTHENTICATE_EMPTY (&grpc_static_mdelem_table[80])
grpc_mdelem *grpc_static_mdelem_for_static_strings(int a, int b);
extern const uint8_t extern const uint8_t
grpc_static_metadata_elem_indices[GRPC_STATIC_MDELEM_COUNT * 2]; grpc_static_metadata_elem_indices[GRPC_STATIC_MDELEM_COUNT * 2];
extern const char *const grpc_static_metadata_strings[GRPC_STATIC_MDSTR_COUNT];
extern const uint8_t grpc_static_accept_encoding_metadata[8]; extern const uint8_t grpc_static_accept_encoding_metadata[8];
#define GRPC_MDELEM_ACCEPT_ENCODING_FOR_ALGORITHMS(algs) \ #define GRPC_MDELEM_ACCEPT_ENCODING_FOR_ALGORITHMS(algs) \
(&grpc_static_mdelem_table[grpc_static_accept_encoding_metadata[(algs)]]) (&grpc_static_mdelem_table[grpc_static_accept_encoding_metadata[(algs)]])
......
...@@ -31,8 +31,11 @@ ...@@ -31,8 +31,11 @@
import hashlib import hashlib
import itertools import itertools
import collections
import os import os
import sys import sys
import subprocess
import re
# configuration: a list of either strings or 2-tuples of strings # configuration: a list of either strings or 2-tuples of strings
# a single string represents a static grpc_mdstr # a single string represents a static grpc_mdstr
...@@ -281,12 +284,43 @@ print >>C, '#include "src/core/lib/transport/static_metadata.h"' ...@@ -281,12 +284,43 @@ print >>C, '#include "src/core/lib/transport/static_metadata.h"'
print >>C print >>C
print >>H, '#define GRPC_STATIC_MDSTR_COUNT %d' % len(all_strs) print >>H, '#define GRPC_STATIC_MDSTR_COUNT %d' % len(all_strs)
print >>H, 'extern grpc_mdstr grpc_static_mdstr_table[GRPC_STATIC_MDSTR_COUNT];' print >>H, 'extern const grpc_slice grpc_static_slice_table[GRPC_STATIC_MDSTR_COUNT];'
for i, elem in enumerate(all_strs): for i, elem in enumerate(all_strs):
print >>H, '/* "%s" */' % elem print >>H, '/* "%s" */' % elem
print >>H, '#define %s (&grpc_static_mdstr_table[%d])' % (mangle(elem).upper(), i) print >>H, '#define %s (&grpc_static_slice_table[%d])' % (mangle(elem).upper(), i)
print >>H print >>H
print >>C, 'grpc_mdstr grpc_static_mdstr_table[GRPC_STATIC_MDSTR_COUNT];' print >>H, 'bool grpc_is_static_metadata_string(grpc_slice slice);'
print >>H
print >>C, 'static uint8_t g_raw_bytes[] = {%s};' % (','.join('%d' % ord(c) for c in ''.join(all_strs)))
print >>C
print >>C, 'static void static_ref(void *unused) {}'
print >>C, 'static void static_unref(grpc_exec_ctx *exec_ctx, void *unused) {}'
print >>C, 'static grpc_slice_refcount g_refcnt = {static_ref, static_unref};'
print >>C
print >>C, 'bool grpc_is_static_metadata_string(grpc_slice slice) {'
print >>C, ' return slice.refcount != NULL && slice.refcount->ref == static_ref;'
print >>C, '};'
print >>C
print >>C, 'const grpc_slice grpc_static_slice_table[GRPC_STATIC_MDSTR_COUNT] = {'
str_ofs = 0
revmap = {}
zero_length_idx = None
for i, elem in enumerate(all_strs):
print >>C, '{.refcount = &g_refcnt, .data.refcounted = {.bytes = g_raw_bytes+%d, .length=%d}},' % (str_ofs, len(elem))
revmap[str_ofs] = i
if len(elem) == 0: zero_length_idx = i
str_ofs += len(elem);
print >>C, '};'
print >>C
print >>C, 'static const uint8_t g_revmap[] = {%s};' % ','.join('%d' % (revmap[i] if i in revmap else 255) for i in range(0, str_ofs))
print >>C
print >>C, 'int grpc_static_metadata_index(grpc_slice slice) {'
print >>C, ' if (GRPC_SLICE_LENGTH(slice) == 0) return %d;' % zero_length_idx
print >>C, ' size_t ofs = (size_t)(GRPC_SLICE_START_PTR(slice) - g_raw_bytes);'
print >>C, ' if (ofs > sizeof(g_revmap)) return -1;'
print >>C, ' uint8_t id = g_revmap[ofs];'
print >>C, ' return id == 255 ? -1 : id;'
print >>C, '};'
print >>C print >>C
print >>D, '# hpack fuzzing dictionary' print >>D, '# hpack fuzzing dictionary'
...@@ -319,18 +353,72 @@ def md_idx(m): ...@@ -319,18 +353,72 @@ def md_idx(m):
if m == m2: if m == m2:
return i return i
def perfect_hash(keys, name):
tmp = open('/tmp/keys.txt', 'w')
tmp.write(''.join('%d\n' % (x - min(keys)) for x in keys))
tmp.close()
cmd = '%s/perfect/run.sh %s -ds' % (os.path.dirname(sys.argv[0]), tmp.name)
subprocess.check_call(cmd, shell=True)
code = ''
results = {}
with open('%s/perfect/phash.h' % os.path.dirname(sys.argv[0])) as f:
txt = f.read()
for var in ('PHASHLEN', 'PHASHNKEYS', 'PHASHRANGE', 'PHASHSALT'):
val = re.search(r'#define %s ([0-9a-zA-Z]+)' % var, txt).group(1)
code += '#define %s_%s %s\n' % (name.upper(), var, val)
results[var] = val
code += '\n'
pycode = 'def f(val):\n'
pycode += ' val -= %d\n' % min(keys)
with open('%s/perfect/phash.c' % os.path.dirname(sys.argv[0])) as f:
txt = f.read()
tabdata = re.search(r'ub1 tab\[\] = \{([^}]+)\}', txt, re.MULTILINE).group(1)
code += 'static const uint8_t %s_tab[] = {%s};\n\n' % (name, tabdata)
func_body = re.search(r'ub4 phash\(val\)\nub4 val;\n\{([^}]+)\}', txt, re.MULTILINE).group(1).replace('ub4', 'uint32_t')
code += 'static uint32_t %s_phash(uint32_t val) {\nval -= %d;\n%s}\n' % (name,
min(keys), func_body.replace('tab', '%s_tab' % name))
pycode += ' tab=(%s)' % tabdata.replace('\n', '')
pycode += '\n'.join(' %s' % s.strip() for s in func_body.splitlines()[2:])
g = {}
exec pycode in g
pyfunc = g['f']
results['code'] = code
results['pyfunc'] = pyfunc
return results
elem_keys = [str_idx(elem[0]) * len(all_strs) + str_idx(elem[1]) for elem in all_elems]
elem_hash = perfect_hash(elem_keys, "elems")
print >>C, elem_hash['code']
keys = [0] * int(elem_hash['PHASHRANGE'])
idxs = [-1] * int(elem_hash['PHASHNKEYS'])
for i, k in enumerate(elem_keys):
h = elem_hash['pyfunc'](k)
assert keys[h] == 0
keys[h] = k
idxs[h] = i
print >>C, 'static const uint16_t elem_keys[] = {%s};' % ','.join('%d' % k for k in keys)
print >>C, 'static const uint8_t elem_idxs[] = {%s};' % ','.join('%d' % i for i in idxs)
print >>C
print >>H, 'grpc_mdelem *grpc_static_mdelem_for_static_strings(int a, int b);'
print >>C, 'grpc_mdelem *grpc_static_mdelem_for_static_strings(int a, int b) {'
print >>C, ' if (a == -1 || b == -1) return NULL;'
print >>C, ' uint32_t k = (uint32_t)(a * %d + b);' % len(all_strs)
print >>C, ' uint32_t h = elems_phash(k);'
print >>C, ' return elem_keys[h] == k ? &grpc_static_mdelem_table[elem_idxs[h]] : NULL;'
print >>C, '}'
print >>C
print >>H, 'extern const uint8_t grpc_static_metadata_elem_indices[GRPC_STATIC_MDELEM_COUNT*2];' print >>H, 'extern const uint8_t grpc_static_metadata_elem_indices[GRPC_STATIC_MDELEM_COUNT*2];'
print >>C, 'const uint8_t grpc_static_metadata_elem_indices[GRPC_STATIC_MDELEM_COUNT*2] = {' print >>C, 'const uint8_t grpc_static_metadata_elem_indices[GRPC_STATIC_MDELEM_COUNT*2] = {'
print >>C, ','.join('%d' % str_idx(x) for x in itertools.chain.from_iterable([a,b] for a, b in all_elems)) print >>C, ','.join('%d' % str_idx(x) for x in itertools.chain.from_iterable([a,b] for a, b in all_elems))
print >>C, '};' print >>C, '};'
print >>C print >>C
print >>H, 'extern const char *const grpc_static_metadata_strings[GRPC_STATIC_MDSTR_COUNT];'
print >>C, 'const char *const grpc_static_metadata_strings[GRPC_STATIC_MDSTR_COUNT] = {'
print >>C, '%s' % ',\n'.join(' "%s"' % s for s in all_strs)
print >>C, '};'
print >>C
print >>H, 'extern const uint8_t grpc_static_accept_encoding_metadata[%d];' % (1 << len(COMPRESSION_ALGORITHMS)) print >>H, 'extern const uint8_t grpc_static_accept_encoding_metadata[%d];' % (1 << len(COMPRESSION_ALGORITHMS))
print >>C, 'const uint8_t grpc_static_accept_encoding_metadata[%d] = {' % (1 << len(COMPRESSION_ALGORITHMS)) print >>C, 'const uint8_t grpc_static_accept_encoding_metadata[%d] = {' % (1 << len(COMPRESSION_ALGORITHMS))
print >>C, '0,%s' % ','.join('%d' % md_idx(elem) for elem in compression_elems) print >>C, '0,%s' % ','.join('%d' % md_idx(elem) for elem in compression_elems)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment