diff --git a/BUILD b/BUILD index 925e277cf77a1..cf147fcead1e3 100644 --- a/BUILD +++ b/BUILD @@ -1573,6 +1573,7 @@ grpc_cc_library( "src/core/ext/transport/chttp2/transport/frame_window_update.cc", "src/core/ext/transport/chttp2/transport/hpack_encoder.cc", "src/core/ext/transport/chttp2/transport/hpack_parser.cc", + "src/core/ext/transport/chttp2/transport/hpack_mapping.cc", "src/core/ext/transport/chttp2/transport/hpack_table.cc", "src/core/ext/transport/chttp2/transport/http2_settings.cc", "src/core/ext/transport/chttp2/transport/huffsyms.cc", @@ -1597,6 +1598,7 @@ grpc_cc_library( "src/core/ext/transport/chttp2/transport/frame_window_update.h", "src/core/ext/transport/chttp2/transport/hpack_encoder.h", "src/core/ext/transport/chttp2/transport/hpack_parser.h", + "src/core/ext/transport/chttp2/transport/hpack_mapping.h", "src/core/ext/transport/chttp2/transport/hpack_table.h", "src/core/ext/transport/chttp2/transport/http2_settings.h", "src/core/ext/transport/chttp2/transport/huffsyms.h", diff --git a/CMakeLists.txt b/CMakeLists.txt index a21bb8b5fae01..31c0887fab5ca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1103,6 +1103,7 @@ add_library(grpc src/core/ext/transport/chttp2/transport/frame_settings.cc src/core/ext/transport/chttp2/transport/frame_window_update.cc src/core/ext/transport/chttp2/transport/hpack_encoder.cc + src/core/ext/transport/chttp2/transport/hpack_mapping.cc src/core/ext/transport/chttp2/transport/hpack_parser.cc src/core/ext/transport/chttp2/transport/hpack_table.cc src/core/ext/transport/chttp2/transport/http2_settings.cc @@ -1514,6 +1515,7 @@ add_library(grpc_cronet src/core/ext/transport/chttp2/transport/frame_settings.cc src/core/ext/transport/chttp2/transport/frame_window_update.cc src/core/ext/transport/chttp2/transport/hpack_encoder.cc + src/core/ext/transport/chttp2/transport/hpack_mapping.cc src/core/ext/transport/chttp2/transport/hpack_parser.cc src/core/ext/transport/chttp2/transport/hpack_table.cc src/core/ext/transport/chttp2/transport/http2_settings.cc @@ -1927,6 +1929,7 @@ add_library(grpc_test_util src/core/ext/transport/chttp2/transport/frame_settings.cc src/core/ext/transport/chttp2/transport/frame_window_update.cc src/core/ext/transport/chttp2/transport/hpack_encoder.cc + src/core/ext/transport/chttp2/transport/hpack_mapping.cc src/core/ext/transport/chttp2/transport/hpack_parser.cc src/core/ext/transport/chttp2/transport/hpack_table.cc src/core/ext/transport/chttp2/transport/http2_settings.cc @@ -2237,6 +2240,7 @@ add_library(grpc_test_util_unsecure src/core/ext/transport/chttp2/transport/frame_settings.cc src/core/ext/transport/chttp2/transport/frame_window_update.cc src/core/ext/transport/chttp2/transport/hpack_encoder.cc + src/core/ext/transport/chttp2/transport/hpack_mapping.cc src/core/ext/transport/chttp2/transport/hpack_parser.cc src/core/ext/transport/chttp2/transport/hpack_table.cc src/core/ext/transport/chttp2/transport/http2_settings.cc @@ -2505,6 +2509,7 @@ add_library(grpc_unsecure src/core/ext/transport/chttp2/transport/frame_settings.cc src/core/ext/transport/chttp2/transport/frame_window_update.cc src/core/ext/transport/chttp2/transport/hpack_encoder.cc + src/core/ext/transport/chttp2/transport/hpack_mapping.cc src/core/ext/transport/chttp2/transport/hpack_parser.cc src/core/ext/transport/chttp2/transport/hpack_table.cc src/core/ext/transport/chttp2/transport/http2_settings.cc @@ -3171,6 +3176,7 @@ add_library(grpc++_cronet src/core/ext/transport/chttp2/transport/frame_settings.cc src/core/ext/transport/chttp2/transport/frame_window_update.cc src/core/ext/transport/chttp2/transport/hpack_encoder.cc + src/core/ext/transport/chttp2/transport/hpack_mapping.cc src/core/ext/transport/chttp2/transport/hpack_parser.cc src/core/ext/transport/chttp2/transport/hpack_table.cc src/core/ext/transport/chttp2/transport/http2_settings.cc diff --git a/Makefile b/Makefile index 96ea890bcb89a..e491e4bcd7650 100644 --- a/Makefile +++ b/Makefile @@ -3605,6 +3605,7 @@ LIBGRPC_SRC = \ src/core/ext/transport/chttp2/transport/frame_settings.cc \ src/core/ext/transport/chttp2/transport/frame_window_update.cc \ src/core/ext/transport/chttp2/transport/hpack_encoder.cc \ + src/core/ext/transport/chttp2/transport/hpack_mapping.cc \ src/core/ext/transport/chttp2/transport/hpack_parser.cc \ src/core/ext/transport/chttp2/transport/hpack_table.cc \ src/core/ext/transport/chttp2/transport/http2_settings.cc \ @@ -4015,6 +4016,7 @@ LIBGRPC_CRONET_SRC = \ src/core/ext/transport/chttp2/transport/frame_settings.cc \ src/core/ext/transport/chttp2/transport/frame_window_update.cc \ src/core/ext/transport/chttp2/transport/hpack_encoder.cc \ + src/core/ext/transport/chttp2/transport/hpack_mapping.cc \ src/core/ext/transport/chttp2/transport/hpack_parser.cc \ src/core/ext/transport/chttp2/transport/hpack_table.cc \ src/core/ext/transport/chttp2/transport/http2_settings.cc \ @@ -4426,6 +4428,7 @@ LIBGRPC_TEST_UTIL_SRC = \ src/core/ext/transport/chttp2/transport/frame_settings.cc \ src/core/ext/transport/chttp2/transport/frame_window_update.cc \ src/core/ext/transport/chttp2/transport/hpack_encoder.cc \ + src/core/ext/transport/chttp2/transport/hpack_mapping.cc \ src/core/ext/transport/chttp2/transport/hpack_parser.cc \ src/core/ext/transport/chttp2/transport/hpack_table.cc \ src/core/ext/transport/chttp2/transport/http2_settings.cc \ @@ -4727,6 +4730,7 @@ LIBGRPC_TEST_UTIL_UNSECURE_SRC = \ src/core/ext/transport/chttp2/transport/frame_settings.cc \ src/core/ext/transport/chttp2/transport/frame_window_update.cc \ src/core/ext/transport/chttp2/transport/hpack_encoder.cc \ + src/core/ext/transport/chttp2/transport/hpack_mapping.cc \ src/core/ext/transport/chttp2/transport/hpack_parser.cc \ src/core/ext/transport/chttp2/transport/hpack_table.cc \ src/core/ext/transport/chttp2/transport/http2_settings.cc \ @@ -4973,6 +4977,7 @@ LIBGRPC_UNSECURE_SRC = \ src/core/ext/transport/chttp2/transport/frame_settings.cc \ src/core/ext/transport/chttp2/transport/frame_window_update.cc \ src/core/ext/transport/chttp2/transport/hpack_encoder.cc \ + src/core/ext/transport/chttp2/transport/hpack_mapping.cc \ src/core/ext/transport/chttp2/transport/hpack_parser.cc \ src/core/ext/transport/chttp2/transport/hpack_table.cc \ src/core/ext/transport/chttp2/transport/http2_settings.cc \ @@ -5627,6 +5632,7 @@ LIBGRPC++_CRONET_SRC = \ src/core/ext/transport/chttp2/transport/frame_settings.cc \ src/core/ext/transport/chttp2/transport/frame_window_update.cc \ src/core/ext/transport/chttp2/transport/hpack_encoder.cc \ + src/core/ext/transport/chttp2/transport/hpack_mapping.cc \ src/core/ext/transport/chttp2/transport/hpack_parser.cc \ src/core/ext/transport/chttp2/transport/hpack_table.cc \ src/core/ext/transport/chttp2/transport/http2_settings.cc \ diff --git a/build.yaml b/build.yaml index a50a0a4ab6584..d14c19199debc 100644 --- a/build.yaml +++ b/build.yaml @@ -936,6 +936,7 @@ filegroups: - src/core/ext/transport/chttp2/transport/frame_settings.h - src/core/ext/transport/chttp2/transport/frame_window_update.h - src/core/ext/transport/chttp2/transport/hpack_encoder.h + - src/core/ext/transport/chttp2/transport/hpack_mapping.h - src/core/ext/transport/chttp2/transport/hpack_parser.h - src/core/ext/transport/chttp2/transport/hpack_table.h - src/core/ext/transport/chttp2/transport/http2_settings.h @@ -957,6 +958,7 @@ filegroups: - src/core/ext/transport/chttp2/transport/frame_settings.cc - src/core/ext/transport/chttp2/transport/frame_window_update.cc - src/core/ext/transport/chttp2/transport/hpack_encoder.cc + - src/core/ext/transport/chttp2/transport/hpack_mapping.cc - src/core/ext/transport/chttp2/transport/hpack_parser.cc - src/core/ext/transport/chttp2/transport/hpack_table.cc - src/core/ext/transport/chttp2/transport/http2_settings.cc diff --git a/config.m4 b/config.m4 index af3624cdd1b33..a6ce55e0a9d47 100644 --- a/config.m4 +++ b/config.m4 @@ -249,6 +249,7 @@ if test "$PHP_GRPC" != "no"; then src/core/ext/transport/chttp2/transport/frame_settings.cc \ src/core/ext/transport/chttp2/transport/frame_window_update.cc \ src/core/ext/transport/chttp2/transport/hpack_encoder.cc \ + src/core/ext/transport/chttp2/transport/hpack_mapping.cc \ src/core/ext/transport/chttp2/transport/hpack_parser.cc \ src/core/ext/transport/chttp2/transport/hpack_table.cc \ src/core/ext/transport/chttp2/transport/http2_settings.cc \ diff --git a/config.w32 b/config.w32 index ad91ee40bd7c0..333986c50a598 100644 --- a/config.w32 +++ b/config.w32 @@ -224,6 +224,7 @@ if (PHP_GRPC != "no") { "src\\core\\ext\\transport\\chttp2\\transport\\frame_settings.cc " + "src\\core\\ext\\transport\\chttp2\\transport\\frame_window_update.cc " + "src\\core\\ext\\transport\\chttp2\\transport\\hpack_encoder.cc " + + "src\\core\\ext\\transport\\chttp2\\transport\\hpack_mapping.cc " + "src\\core\\ext\\transport\\chttp2\\transport\\hpack_parser.cc " + "src\\core\\ext\\transport\\chttp2\\transport\\hpack_table.cc " + "src\\core\\ext\\transport\\chttp2\\transport\\http2_settings.cc " + diff --git a/gRPC-C++.podspec b/gRPC-C++.podspec index 581b9246bc567..f4cbd275645fd 100644 --- a/gRPC-C++.podspec +++ b/gRPC-C++.podspec @@ -251,6 +251,7 @@ Pod::Spec.new do |s| 'src/core/ext/transport/chttp2/transport/frame_settings.h', 'src/core/ext/transport/chttp2/transport/frame_window_update.h', 'src/core/ext/transport/chttp2/transport/hpack_encoder.h', + 'src/core/ext/transport/chttp2/transport/hpack_mapping.h', 'src/core/ext/transport/chttp2/transport/hpack_parser.h', 'src/core/ext/transport/chttp2/transport/hpack_table.h', 'src/core/ext/transport/chttp2/transport/http2_settings.h', diff --git a/gRPC-Core.podspec b/gRPC-Core.podspec index 5a82a4200a283..1efa71a19e0d4 100644 --- a/gRPC-Core.podspec +++ b/gRPC-Core.podspec @@ -263,6 +263,7 @@ Pod::Spec.new do |s| 'src/core/ext/transport/chttp2/transport/frame_settings.h', 'src/core/ext/transport/chttp2/transport/frame_window_update.h', 'src/core/ext/transport/chttp2/transport/hpack_encoder.h', + 'src/core/ext/transport/chttp2/transport/hpack_mapping.h', 'src/core/ext/transport/chttp2/transport/hpack_parser.h', 'src/core/ext/transport/chttp2/transport/hpack_table.h', 'src/core/ext/transport/chttp2/transport/http2_settings.h', @@ -681,6 +682,7 @@ Pod::Spec.new do |s| 'src/core/ext/transport/chttp2/transport/frame_settings.cc', 'src/core/ext/transport/chttp2/transport/frame_window_update.cc', 'src/core/ext/transport/chttp2/transport/hpack_encoder.cc', + 'src/core/ext/transport/chttp2/transport/hpack_mapping.cc', 'src/core/ext/transport/chttp2/transport/hpack_parser.cc', 'src/core/ext/transport/chttp2/transport/hpack_table.cc', 'src/core/ext/transport/chttp2/transport/http2_settings.cc', @@ -866,6 +868,7 @@ Pod::Spec.new do |s| 'src/core/ext/transport/chttp2/transport/frame_settings.h', 'src/core/ext/transport/chttp2/transport/frame_window_update.h', 'src/core/ext/transport/chttp2/transport/hpack_encoder.h', + 'src/core/ext/transport/chttp2/transport/hpack_mapping.h', 'src/core/ext/transport/chttp2/transport/hpack_parser.h', 'src/core/ext/transport/chttp2/transport/hpack_table.h', 'src/core/ext/transport/chttp2/transport/http2_settings.h', diff --git a/grpc.gemspec b/grpc.gemspec index c8e58faec9ebc..f5cbf796240c6 100644 --- a/grpc.gemspec +++ b/grpc.gemspec @@ -195,6 +195,7 @@ Gem::Specification.new do |s| s.files += %w( src/core/ext/transport/chttp2/transport/frame_settings.h ) s.files += %w( src/core/ext/transport/chttp2/transport/frame_window_update.h ) s.files += %w( src/core/ext/transport/chttp2/transport/hpack_encoder.h ) + s.files += %w( src/core/ext/transport/chttp2/transport/hpack_mapping.h ) s.files += %w( src/core/ext/transport/chttp2/transport/hpack_parser.h ) s.files += %w( src/core/ext/transport/chttp2/transport/hpack_table.h ) s.files += %w( src/core/ext/transport/chttp2/transport/http2_settings.h ) @@ -617,6 +618,7 @@ Gem::Specification.new do |s| s.files += %w( src/core/ext/transport/chttp2/transport/frame_settings.cc ) s.files += %w( src/core/ext/transport/chttp2/transport/frame_window_update.cc ) s.files += %w( src/core/ext/transport/chttp2/transport/hpack_encoder.cc ) + s.files += %w( src/core/ext/transport/chttp2/transport/hpack_mapping.cc ) s.files += %w( src/core/ext/transport/chttp2/transport/hpack_parser.cc ) s.files += %w( src/core/ext/transport/chttp2/transport/hpack_table.cc ) s.files += %w( src/core/ext/transport/chttp2/transport/http2_settings.cc ) diff --git a/grpc.gyp b/grpc.gyp index 654a531092306..15d20053f4d37 100644 --- a/grpc.gyp +++ b/grpc.gyp @@ -441,6 +441,7 @@ 'src/core/ext/transport/chttp2/transport/frame_settings.cc', 'src/core/ext/transport/chttp2/transport/frame_window_update.cc', 'src/core/ext/transport/chttp2/transport/hpack_encoder.cc', + 'src/core/ext/transport/chttp2/transport/hpack_mapping.cc', 'src/core/ext/transport/chttp2/transport/hpack_parser.cc', 'src/core/ext/transport/chttp2/transport/hpack_table.cc', 'src/core/ext/transport/chttp2/transport/http2_settings.cc', @@ -825,6 +826,7 @@ 'src/core/ext/transport/chttp2/transport/frame_settings.cc', 'src/core/ext/transport/chttp2/transport/frame_window_update.cc', 'src/core/ext/transport/chttp2/transport/hpack_encoder.cc', + 'src/core/ext/transport/chttp2/transport/hpack_mapping.cc', 'src/core/ext/transport/chttp2/transport/hpack_parser.cc', 'src/core/ext/transport/chttp2/transport/hpack_table.cc', 'src/core/ext/transport/chttp2/transport/http2_settings.cc', @@ -1060,6 +1062,7 @@ 'src/core/ext/transport/chttp2/transport/frame_settings.cc', 'src/core/ext/transport/chttp2/transport/frame_window_update.cc', 'src/core/ext/transport/chttp2/transport/hpack_encoder.cc', + 'src/core/ext/transport/chttp2/transport/hpack_mapping.cc', 'src/core/ext/transport/chttp2/transport/hpack_parser.cc', 'src/core/ext/transport/chttp2/transport/hpack_table.cc', 'src/core/ext/transport/chttp2/transport/http2_settings.cc', @@ -1252,6 +1255,7 @@ 'src/core/ext/transport/chttp2/transport/frame_settings.cc', 'src/core/ext/transport/chttp2/transport/frame_window_update.cc', 'src/core/ext/transport/chttp2/transport/hpack_encoder.cc', + 'src/core/ext/transport/chttp2/transport/hpack_mapping.cc', 'src/core/ext/transport/chttp2/transport/hpack_parser.cc', 'src/core/ext/transport/chttp2/transport/hpack_table.cc', 'src/core/ext/transport/chttp2/transport/http2_settings.cc', diff --git a/package.xml b/package.xml index 9c0e078e92ca3..1e8ae8bb6a9aa 100644 --- a/package.xml +++ b/package.xml @@ -200,6 +200,7 @@ + @@ -622,6 +623,7 @@ + diff --git a/src/core/ext/transport/chttp2/transport/hpack_encoder.cc b/src/core/ext/transport/chttp2/transport/hpack_encoder.cc index eff9b97a8e2da..920d52770fc81 100644 --- a/src/core/ext/transport/chttp2/transport/hpack_encoder.cc +++ b/src/core/ext/transport/chttp2/transport/hpack_encoder.cc @@ -525,7 +525,7 @@ static void hpack_enc(grpc_chttp2_hpack_compressor* c, grpc_mdelem elem, /* should this elem be in the table? */ size_t decoder_space_usage = - grpc_mdelem_get_size_in_hpack_table(elem, st->use_true_binary_metadata); + grpc_chttp2_get_size_in_hpack_table(elem, st->use_true_binary_metadata); bool should_add_elem = elem_interned && decoder_space_usage < MAX_DECODER_SPACE_USAGE && c->filter_elems[HASH_FRAGMENT_1(elem_hash)] >= @@ -689,7 +689,7 @@ void grpc_chttp2_encode_header(grpc_chttp2_hpack_compressor* c, } for (size_t i = 0; i < extra_headers_size; ++i) { grpc_mdelem md = *extra_headers[i]; - uint8_t static_index = grpc_mdelem_get_static_hpack_table_index(md); + uint8_t static_index = grpc_chttp2_get_static_hpack_table_index(md); if (static_index) { emit_indexed(c, static_index, &st); } else { @@ -698,7 +698,7 @@ void grpc_chttp2_encode_header(grpc_chttp2_hpack_compressor* c, } grpc_metadata_batch_assert_ok(metadata); for (grpc_linked_mdelem* l = metadata->list.head; l; l = l->next) { - uint8_t static_index = grpc_mdelem_get_static_hpack_table_index(l->md); + uint8_t static_index = grpc_chttp2_get_static_hpack_table_index(l->md); if (static_index) { emit_indexed(c, static_index, &st); } else { diff --git a/src/core/ext/transport/chttp2/transport/hpack_mapping.cc b/src/core/ext/transport/chttp2/transport/hpack_mapping.cc new file mode 100644 index 0000000000000..fd529f0fd4d67 --- /dev/null +++ b/src/core/ext/transport/chttp2/transport/hpack_mapping.cc @@ -0,0 +1,39 @@ +/* + * Copyright 2015 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * WARNING: Auto-generated code. + * + * To make changes to this file, change + * tools/codegen/core/gen_static_metadata.py, and then re-run it. + * + * This file contains the mapping from the index of each metadata element in the + * grpc static metadata table to the index of that element in the hpack static + * metadata table. If the element is not contained in the static hpack table, + * then the returned index is 0. + */ + +#include + +#include "src/core/ext/transport/chttp2/transport/hpack_mapping.h" + +const uint8_t grpc_hpack_static_mdelem_indices[GRPC_STATIC_MDELEM_COUNT] = { + 0, 0, 0, 0, 0, 0, 0, 0, 3, 8, 13, 6, 7, 0, 1, 2, 0, 4, + 5, 9, 10, 11, 12, 14, 15, 0, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, + 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, + 42, 43, 44, 0, 0, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, + 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +}; diff --git a/src/core/ext/transport/chttp2/transport/hpack_mapping.h b/src/core/ext/transport/chttp2/transport/hpack_mapping.h new file mode 100644 index 0000000000000..ebcd65bd9fe81 --- /dev/null +++ b/src/core/ext/transport/chttp2/transport/hpack_mapping.h @@ -0,0 +1,38 @@ +/* + * Copyright 2015 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * WARNING: Auto-generated code. + * + * To make changes to this file, change + * tools/codegen/core/gen_static_metadata.py, and then re-run it. + * + * This file contains the mapping from the index of each metadata element in the + * grpc static metadata table to the index of that element in the hpack static + * metadata table. If the element is not contained in the static hpack table, + * then the returned index is 0. + */ + +#ifndef GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_MAPPING_H +#define GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_MAPPING_H + +#include + +#include "src/core/lib/transport/static_metadata.h" + +extern const uint8_t grpc_hpack_static_mdelem_indices[GRPC_STATIC_MDELEM_COUNT]; + +#endif /* GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_MAPPING_H */ diff --git a/src/core/ext/transport/chttp2/transport/hpack_table.cc b/src/core/ext/transport/chttp2/transport/hpack_table.cc index 792925835675a..117679ab7513f 100644 --- a/src/core/ext/transport/chttp2/transport/hpack_table.cc +++ b/src/core/ext/transport/chttp2/transport/hpack_table.cc @@ -27,8 +27,10 @@ #include #include +#include "src/core/ext/transport/chttp2/transport/hpack_mapping.h" #include "src/core/lib/debug/trace.h" #include "src/core/lib/gpr/murmur_hash.h" +#include "src/core/lib/transport/static_metadata.h" extern grpc_core::TraceFlag grpc_http_trace; @@ -366,3 +368,30 @@ grpc_chttp2_hptbl_find_result grpc_chttp2_hptbl_find( return r; } + +static size_t get_base64_encoded_size(size_t raw_length) { + static const uint8_t tail_xtra[3] = {0, 2, 3}; + return raw_length / 3 * 4 + tail_xtra[raw_length % 3]; +} + +size_t grpc_chttp2_get_size_in_hpack_table(grpc_mdelem elem, + bool use_true_binary_metadata) { + size_t overhead_and_key = 32 + GRPC_SLICE_LENGTH(GRPC_MDKEY(elem)); + size_t value_len = GRPC_SLICE_LENGTH(GRPC_MDVALUE(elem)); + if (grpc_is_binary_header(GRPC_MDKEY(elem))) { + return overhead_and_key + (use_true_binary_metadata + ? value_len + 1 + : get_base64_encoded_size(value_len)); + } else { + return overhead_and_key + value_len; + } +} + +uint8_t grpc_chttp2_get_static_hpack_table_index(grpc_mdelem md) { + if (GRPC_MDELEM_STORAGE(md) == GRPC_MDELEM_STORAGE_STATIC) { + return grpc_hpack_static_mdelem_indices[GRPC_MDELEM_DATA(md) - + grpc_static_mdelem_table]; + } else { + return 0; + } +} diff --git a/src/core/ext/transport/chttp2/transport/hpack_table.h b/src/core/ext/transport/chttp2/transport/hpack_table.h index 98026a4ba4a2b..a0ffc6fab7707 100644 --- a/src/core/ext/transport/chttp2/transport/hpack_table.h +++ b/src/core/ext/transport/chttp2/transport/hpack_table.h @@ -83,6 +83,15 @@ grpc_mdelem grpc_chttp2_hptbl_lookup(const grpc_chttp2_hptbl* tbl, /* add a table entry to the index */ grpc_error* grpc_chttp2_hptbl_add(grpc_chttp2_hptbl* tbl, grpc_mdelem md) GRPC_MUST_USE_RESULT; + +size_t grpc_chttp2_get_size_in_hpack_table(grpc_mdelem elem, + bool use_true_binary_metadata); + +/* Returns the static hpack table index that corresponds to /a elem. Returns 0 + if /a elem is not statically stored or if it is not in the static hpack + table */ +uint8_t grpc_chttp2_get_static_hpack_table_index(grpc_mdelem md); + /* Find a key/value pair in the table... returns the index in the table of the most similar entry, or 0 if the value was not found */ typedef struct { diff --git a/src/core/lib/transport/metadata.cc b/src/core/lib/transport/metadata.cc index dfcd2e16d8a9f..d164502280acf 100644 --- a/src/core/lib/transport/metadata.cc +++ b/src/core/lib/transport/metadata.cc @@ -342,33 +342,6 @@ grpc_mdelem grpc_mdelem_from_grpc_metadata(grpc_metadata* metadata) { changed ? nullptr : reinterpret_cast(metadata)); } -static size_t get_base64_encoded_size(size_t raw_length) { - static const uint8_t tail_xtra[3] = {0, 2, 3}; - return raw_length / 3 * 4 + tail_xtra[raw_length % 3]; -} - -size_t grpc_mdelem_get_size_in_hpack_table(grpc_mdelem elem, - bool use_true_binary_metadata) { - size_t overhead_and_key = 32 + GRPC_SLICE_LENGTH(GRPC_MDKEY(elem)); - size_t value_len = GRPC_SLICE_LENGTH(GRPC_MDVALUE(elem)); - if (grpc_is_binary_header(GRPC_MDKEY(elem))) { - return overhead_and_key + (use_true_binary_metadata - ? value_len + 1 - : get_base64_encoded_size(value_len)); - } else { - return overhead_and_key + value_len; - } -} - -uint8_t grpc_mdelem_get_static_hpack_table_index(grpc_mdelem md) { - if (GRPC_MDELEM_STORAGE(md) == GRPC_MDELEM_STORAGE_STATIC) { - return grpc_hpack_static_mdelem_indices[GRPC_MDELEM_DATA(md) - - grpc_static_mdelem_table]; - } else { - return 0; - } -} - grpc_mdelem grpc_mdelem_ref(grpc_mdelem gmd DEBUG_ARGS) { switch (GRPC_MDELEM_STORAGE(gmd)) { case GRPC_MDELEM_STORAGE_EXTERNAL: diff --git a/src/core/lib/transport/metadata.h b/src/core/lib/transport/metadata.h index 37419c9d2f503..338082276cc50 100644 --- a/src/core/lib/transport/metadata.h +++ b/src/core/lib/transport/metadata.h @@ -125,14 +125,6 @@ grpc_mdelem grpc_mdelem_create( bool grpc_mdelem_eq(grpc_mdelem a, grpc_mdelem b); -size_t grpc_mdelem_get_size_in_hpack_table(grpc_mdelem elem, - bool use_true_binary_metadata); - -/* Returns the static hpack table index that corresponds to /a elem. Returns 0 - if /a elem is not statically stored or if it is not in the static hpack - table */ -uint8_t grpc_mdelem_get_static_hpack_table_index(grpc_mdelem md); - /* Mutator and accessor for grpc_mdelem user data. The destructor function is used as a type tag and is checked during user_data fetch. */ void* grpc_mdelem_get_user_data(grpc_mdelem md, void (*if_destroy_func)(void*)); diff --git a/src/core/lib/transport/static_metadata.cc b/src/core/lib/transport/static_metadata.cc index fdedd41036923..6a5144f21a2da 100644 --- a/src/core/lib/transport/static_metadata.cc +++ b/src/core/lib/transport/static_metadata.cc @@ -334,14 +334,6 @@ const grpc_slice grpc_static_slice_table[GRPC_STATIC_MDSTR_COUNT] = { {&grpc_static_metadata_refcounts[104], {{g_bytes + 1126, 21}}}, }; -const uint8_t grpc_hpack_static_mdelem_indices[GRPC_STATIC_MDELEM_COUNT] = { - 0, 0, 0, 0, 0, 0, 0, 0, 3, 8, 13, 6, 7, 0, 1, 2, 0, 4, - 5, 9, 10, 11, 12, 14, 15, 0, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, - 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, - 42, 43, 44, 0, 0, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, - 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -}; - uintptr_t grpc_static_mdelem_user_data[GRPC_STATIC_MDELEM_COUNT] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, diff --git a/src/core/lib/transport/static_metadata.h b/src/core/lib/transport/static_metadata.h index e5319c4c4d429..b3a10f58730ef 100644 --- a/src/core/lib/transport/static_metadata.h +++ b/src/core/lib/transport/static_metadata.h @@ -521,8 +521,6 @@ extern uintptr_t grpc_static_mdelem_user_data[GRPC_STATIC_MDELEM_COUNT]; #define GRPC_MDELEM_ACCEPT_ENCODING_IDENTITY_COMMA_GZIP \ (GRPC_MAKE_MDELEM(&grpc_static_mdelem_table[85], GRPC_MDELEM_STORAGE_STATIC)) -extern const uint8_t grpc_hpack_static_mdelem_indices[GRPC_STATIC_MDELEM_COUNT]; - grpc_mdelem grpc_static_mdelem_for_static_strings(int a, int b); typedef enum { GRPC_BATCH_PATH, diff --git a/src/python/grpcio/grpc_core_dependencies.py b/src/python/grpcio/grpc_core_dependencies.py index 0f68e823d7950..ceacc83e62624 100644 --- a/src/python/grpcio/grpc_core_dependencies.py +++ b/src/python/grpcio/grpc_core_dependencies.py @@ -223,6 +223,7 @@ 'src/core/ext/transport/chttp2/transport/frame_settings.cc', 'src/core/ext/transport/chttp2/transport/frame_window_update.cc', 'src/core/ext/transport/chttp2/transport/hpack_encoder.cc', + 'src/core/ext/transport/chttp2/transport/hpack_mapping.cc', 'src/core/ext/transport/chttp2/transport/hpack_parser.cc', 'src/core/ext/transport/chttp2/transport/hpack_table.cc', 'src/core/ext/transport/chttp2/transport/http2_settings.cc', diff --git a/test/core/transport/chttp2/hpack_encoder_test.cc b/test/core/transport/chttp2/hpack_encoder_test.cc index 2a57198ab64b5..ab819f9092363 100644 --- a/test/core/transport/chttp2/hpack_encoder_test.cc +++ b/test/core/transport/chttp2/hpack_encoder_test.cc @@ -202,7 +202,7 @@ static void verify_table_size_change_match_elem_size(const char* key, grpc_mdelem elem = grpc_mdelem_from_slices( grpc_slice_intern(grpc_slice_from_static_string(key)), grpc_slice_intern(grpc_slice_from_static_string(value))); - size_t elem_size = grpc_mdelem_get_size_in_hpack_table(elem, use_true_binary); + size_t elem_size = grpc_chttp2_get_size_in_hpack_table(elem, use_true_binary); size_t initial_table_size = g_compressor.table_size; grpc_linked_mdelem* e = static_cast(gpr_malloc(sizeof(*e))); diff --git a/test/core/transport/metadata_test.cc b/test/core/transport/metadata_test.cc index 4be34f72d9be5..307fcc6da3956 100644 --- a/test/core/transport/metadata_test.cc +++ b/test/core/transport/metadata_test.cc @@ -293,7 +293,7 @@ static void verify_ascii_header_size(const char* key, const char* value, grpc_mdelem elem = grpc_mdelem_from_slices( maybe_intern(grpc_slice_from_static_string(key), intern_key), maybe_intern(grpc_slice_from_static_string(value), intern_value)); - size_t elem_size = grpc_mdelem_get_size_in_hpack_table(elem, false); + size_t elem_size = grpc_chttp2_get_size_in_hpack_table(elem, false); size_t expected_size = 32 + strlen(key) + strlen(value); GPR_ASSERT(expected_size == elem_size); GRPC_MDELEM_UNREF(elem); @@ -307,7 +307,7 @@ static void verify_binary_header_size(const char* key, const uint8_t* value, maybe_intern(grpc_slice_from_static_buffer(value, value_len), intern_value)); GPR_ASSERT(grpc_is_binary_header(GRPC_MDKEY(elem))); - size_t elem_size = grpc_mdelem_get_size_in_hpack_table(elem, false); + size_t elem_size = grpc_chttp2_get_size_in_hpack_table(elem, false); grpc_slice value_slice = grpc_slice_from_copied_buffer( reinterpret_cast(value), value_len); grpc_slice base64_encoded = grpc_chttp2_base64_encode(value_slice); diff --git a/tools/codegen/core/gen_static_metadata.py b/tools/codegen/core/gen_static_metadata.py index 784e7540919fe..463047215afb9 100755 --- a/tools/codegen/core/gen_static_metadata.py +++ b/tools/codegen/core/gen_static_metadata.py @@ -326,6 +326,15 @@ def put_banner(files, banner): os.path.dirname(sys.argv[0]), '../../../test/core/end2end/fuzzers/hpack.dictionary'), 'w') +HPACK_H = open( + os.path.join( + os.path.dirname(sys.argv[0]), + '../../../src/core/ext/transport/chttp2/transport/hpack_mapping.h'), 'w') +HPACK_C = open( + os.path.join( + os.path.dirname(sys.argv[0]), + '../../../src/core/ext/transport/chttp2/transport/hpack_mapping.cc'), 'w') + # copy-paste copyright notice from this file with open(sys.argv[0]) as my_source: copyright = [] @@ -340,7 +349,7 @@ def put_banner(files, banner): if line[0] != '#': break copyright.append(line) - put_banner([H, C], [line[2:].rstrip() for line in copyright]) + put_banner([H, C, HPACK_H, HPACK_C], [line[2:].rstrip() for line in copyright]) hex_bytes = [ord(c) for c in 'abcdefABCDEF0123456789'] @@ -367,6 +376,17 @@ def esc_dict(line): an explanation of what's going on. """.splitlines()) +put_banner([HPACK_H, HPACK_C], """WARNING: Auto-generated code. + +To make changes to this file, change +tools/codegen/core/gen_static_metadata.py, and then re-run it. + +This file contains the mapping from the index of each metadata element in the +grpc static metadata table to the index of that element in the hpack static +metadata table. If the element is not contained in the static hpack table, then +the returned index is 0. +""".splitlines()) + print >> H, '#ifndef GRPC_CORE_LIB_TRANSPORT_STATIC_METADATA_H' print >> H, '#define GRPC_CORE_LIB_TRANSPORT_STATIC_METADATA_H' print >> H @@ -380,6 +400,20 @@ def esc_dict(line): print >> C print >> C, '#include "src/core/lib/slice/slice_internal.h"' print >> C +print >> HPACK_H, ('#ifndef GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_' + 'MAPPING_H') +print >> HPACK_H, ('#define GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_' + 'MAPPING_H') +print >> HPACK_H +print >> HPACK_H, '#include ' +print >> HPACK_H +print >> HPACK_H, '#include "src/core/lib/transport/static_metadata.h"' +print >> HPACK_H +print >> HPACK_C, '#include ' +print >> HPACK_C +print >> HPACK_C, ('#include ' + '"src/core/ext/transport/chttp2/transport/hpack_mapping.h"') +print >> HPACK_C str_ofs = 0 id2strofs = {} @@ -463,10 +497,10 @@ def slice_def(i): # Print out the chttp2 mapping between static mdelem index and the hpack static # table index -print >> H, ('extern const uint8_t grpc_hpack_static_mdelem_indices[' +print >> HPACK_H, ('extern const uint8_t grpc_hpack_static_mdelem_indices[' 'GRPC_STATIC_MDELEM_COUNT];') -print >> H -print >> C, ('const uint8_t grpc_hpack_static_mdelem_indices[' +print >> HPACK_H +print >> HPACK_C, ('const uint8_t grpc_hpack_static_mdelem_indices[' 'GRPC_STATIC_MDELEM_COUNT] = {') indices = '' for i, elem in enumerate(all_elems): @@ -474,9 +508,9 @@ def slice_def(i): if len(elem) == 3: index = elem[2] indices += '%d,' % index -print >> C, ' %s' % indices -print >> C, '};' -print >> C +print >> HPACK_C, ' %s' % indices +print >> HPACK_C, '};' +print >> HPACK_C print >> C, ('uintptr_t grpc_static_mdelem_user_data[GRPC_STATIC_MDELEM_COUNT] ' '= {') @@ -570,7 +604,7 @@ def f(i, p=p): print >> C print >> C, 'grpc_mdelem_data grpc_static_mdelem_table[GRPC_STATIC_MDELEM_COUNT] = {' -for i, elem in enumerate(all_elems): +for elem in all_elems: print >> C, '{%s,%s},' % (slice_def(str_idx(elem[0])), slice_def(str_idx(elem[1]))) print >> C, '};' @@ -624,5 +658,8 @@ def f(i, p=p): print >> H, '#endif /* GRPC_CORE_LIB_TRANSPORT_STATIC_METADATA_H */' +print >> HPACK_H, ('#endif /* GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_' + 'MAPPING_H */') + H.close() C.close() diff --git a/tools/doxygen/Doxyfile.core.internal b/tools/doxygen/Doxyfile.core.internal index 7cd1dc7bf3739..ed0e17a99efe0 100644 --- a/tools/doxygen/Doxyfile.core.internal +++ b/tools/doxygen/Doxyfile.core.internal @@ -1013,6 +1013,8 @@ src/core/ext/transport/chttp2/transport/frame_window_update.cc \ src/core/ext/transport/chttp2/transport/frame_window_update.h \ src/core/ext/transport/chttp2/transport/hpack_encoder.cc \ src/core/ext/transport/chttp2/transport/hpack_encoder.h \ +src/core/ext/transport/chttp2/transport/hpack_mapping.cc \ +src/core/ext/transport/chttp2/transport/hpack_mapping.h \ src/core/ext/transport/chttp2/transport/hpack_parser.cc \ src/core/ext/transport/chttp2/transport/hpack_parser.h \ src/core/ext/transport/chttp2/transport/hpack_table.cc \ diff --git a/tools/run_tests/generated/sources_and_headers.json b/tools/run_tests/generated/sources_and_headers.json index 8ea5126fdef18..698ead0c5a874 100644 --- a/tools/run_tests/generated/sources_and_headers.json +++ b/tools/run_tests/generated/sources_and_headers.json @@ -10661,6 +10661,7 @@ "src/core/ext/transport/chttp2/transport/frame_settings.h", "src/core/ext/transport/chttp2/transport/frame_window_update.h", "src/core/ext/transport/chttp2/transport/hpack_encoder.h", + "src/core/ext/transport/chttp2/transport/hpack_mapping.h", "src/core/ext/transport/chttp2/transport/hpack_parser.h", "src/core/ext/transport/chttp2/transport/hpack_table.h", "src/core/ext/transport/chttp2/transport/http2_settings.h", @@ -10698,6 +10699,8 @@ "src/core/ext/transport/chttp2/transport/frame_window_update.h", "src/core/ext/transport/chttp2/transport/hpack_encoder.cc", "src/core/ext/transport/chttp2/transport/hpack_encoder.h", + "src/core/ext/transport/chttp2/transport/hpack_mapping.cc", + "src/core/ext/transport/chttp2/transport/hpack_mapping.h", "src/core/ext/transport/chttp2/transport/hpack_parser.cc", "src/core/ext/transport/chttp2/transport/hpack_parser.h", "src/core/ext/transport/chttp2/transport/hpack_table.cc",