grpc 1.54.0 → 1.54.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +2 -2
  3. data/src/core/ext/filters/client_channel/client_channel.cc +4 -6
  4. data/src/core/ext/filters/client_channel/client_channel.h +2 -0
  5. data/src/core/ext/filters/client_channel/lb_policy/rls/rls.cc +1 -1
  6. data/src/core/ext/gcp/metadata_query.cc +5 -10
  7. data/src/core/ext/gcp/metadata_query.h +10 -5
  8. data/src/core/ext/transport/chttp2/transport/bin_encoder.cc +12 -8
  9. data/src/core/ext/transport/chttp2/transport/bin_encoder.h +5 -1
  10. data/src/core/ext/transport/chttp2/transport/hpack_encoder.cc +118 -222
  11. data/src/core/ext/transport/chttp2/transport/hpack_encoder.h +295 -113
  12. data/src/core/ext/transport/chttp2/transport/hpack_encoder_table.cc +2 -0
  13. data/src/core/ext/transport/chttp2/transport/hpack_encoder_table.h +2 -0
  14. data/src/core/ext/transport/chttp2/transport/hpack_parser.cc +451 -277
  15. data/src/core/ext/transport/chttp2/transport/hpack_parser.h +3 -1
  16. data/src/core/ext/transport/chttp2/transport/hpack_parser_table.cc +14 -12
  17. data/src/core/ext/transport/chttp2/transport/hpack_parser_table.h +9 -1
  18. data/src/core/lib/event_engine/event_engine.cc +4 -4
  19. data/src/core/lib/event_engine/posix_engine/posix_engine.h +1 -0
  20. data/src/core/lib/event_engine/posix_engine/posix_engine_listener.cc +29 -0
  21. data/src/core/lib/event_engine/posix_engine/posix_engine_listener.h +3 -0
  22. data/src/core/lib/iomgr/tcp_server_posix.cc +39 -14
  23. data/src/core/lib/iomgr/tcp_server_utils_posix.h +12 -0
  24. data/src/core/lib/iomgr/tcp_server_utils_posix_common.cc +21 -0
  25. data/src/core/lib/surface/validate_metadata.cc +43 -42
  26. data/src/core/lib/surface/validate_metadata.h +9 -0
  27. data/src/core/lib/transport/metadata_batch.h +110 -2
  28. data/src/core/lib/transport/parsed_metadata.h +16 -6
  29. data/src/ruby/lib/grpc/version.rb +1 -1
  30. metadata +3 -3
@@ -109,33 +109,56 @@ void HPackCompressor::Frame(const EncodeHeaderOptions& options,
109
109
  }
110
110
  }
111
111
 
112
- void HPackCompressor::Encoder::EmitIndexed(uint32_t elem_index) {
113
- VarintWriter<1> w(elem_index);
114
- w.Write(0x80, output_.AddTiny(w.length()));
112
+ void HPackCompressor::SetMaxUsableSize(uint32_t max_table_size) {
113
+ max_usable_size_ = max_table_size;
114
+ SetMaxTableSize(std::min(table_.max_size(), max_table_size));
115
+ }
116
+
117
+ void HPackCompressor::SetMaxTableSize(uint32_t max_table_size) {
118
+ if (table_.SetMaxSize(std::min(max_usable_size_, max_table_size))) {
119
+ advertise_table_size_change_ = true;
120
+ if (GRPC_TRACE_FLAG_ENABLED(grpc_http_trace)) {
121
+ gpr_log(GPR_INFO, "set max table size from encoder to %d",
122
+ max_table_size);
123
+ }
124
+ }
115
125
  }
116
126
 
127
+ namespace {
117
128
  struct WireValue {
118
129
  WireValue(uint8_t huffman_prefix, bool insert_null_before_wire_value,
119
130
  Slice slice)
120
131
  : data(std::move(slice)),
121
132
  huffman_prefix(huffman_prefix),
122
133
  insert_null_before_wire_value(insert_null_before_wire_value),
123
- length(data.length() + (insert_null_before_wire_value ? 1 : 0)) {}
134
+ length(data.length() + (insert_null_before_wire_value ? 1 : 0)),
135
+ hpack_length(length) {}
136
+ WireValue(uint8_t huffman_prefix, bool insert_null_before_wire_value,
137
+ Slice slice, size_t hpack_length)
138
+ : data(std::move(slice)),
139
+ huffman_prefix(huffman_prefix),
140
+ insert_null_before_wire_value(insert_null_before_wire_value),
141
+ length(data.length() + (insert_null_before_wire_value ? 1 : 0)),
142
+ hpack_length(hpack_length + (insert_null_before_wire_value ? 1 : 0)) {}
124
143
  Slice data;
125
144
  const uint8_t huffman_prefix;
126
145
  const bool insert_null_before_wire_value;
127
146
  const size_t length;
147
+ const size_t hpack_length;
128
148
  };
129
149
 
130
- static WireValue GetWireValue(Slice value, bool true_binary_enabled,
131
- bool is_bin_hdr) {
150
+ // Construct a wire value from a slice.
151
+ // true_binary_enabled => use the true binary system
152
+ // is_bin_hdr => the header is -bin suffixed
153
+ WireValue GetWireValue(Slice value, bool true_binary_enabled, bool is_bin_hdr) {
132
154
  if (is_bin_hdr) {
133
155
  if (true_binary_enabled) {
134
156
  return WireValue(0x00, true, std::move(value));
135
157
  } else {
136
- return WireValue(0x80, false,
137
- Slice(grpc_chttp2_base64_encode_and_huffman_compress(
138
- value.c_slice())));
158
+ uint32_t hpack_length;
159
+ Slice output(grpc_chttp2_base64_encode_and_huffman_compress(
160
+ value.c_slice(), &hpack_length));
161
+ return WireValue(0x80, false, std::move(output), hpack_length);
139
162
  }
140
163
  } else {
141
164
  // TODO(ctiller): opportunistically compress non-binary headers
@@ -175,6 +198,8 @@ class BinaryStringValue {
175
198
 
176
199
  Slice data() { return std::move(wire_value_.data); }
177
200
 
201
+ uint32_t hpack_length() { return wire_value_.hpack_length; }
202
+
178
203
  private:
179
204
  WireValue wire_value_;
180
205
  VarintWriter<1> len_val_;
@@ -214,19 +239,33 @@ class StringKey {
214
239
  Slice key_;
215
240
  VarintWriter<1> len_key_;
216
241
  };
242
+ } // namespace
217
243
 
218
- void HPackCompressor::Encoder::EmitLitHdrWithNonBinaryStringKeyIncIdx(
219
- Slice key_slice, Slice value_slice) {
244
+ namespace hpack_encoder_detail {
245
+ void Encoder::EmitIndexed(uint32_t elem_index) {
246
+ VarintWriter<1> w(elem_index);
247
+ w.Write(0x80, output_.AddTiny(w.length()));
248
+ }
249
+
250
+ uint32_t Encoder::EmitLitHdrWithNonBinaryStringKeyIncIdx(Slice key_slice,
251
+ Slice value_slice) {
252
+ auto key_len = key_slice.length();
253
+ auto value_len = value_slice.length();
220
254
  StringKey key(std::move(key_slice));
221
255
  key.WritePrefix(0x40, output_.AddTiny(key.prefix_length()));
222
256
  output_.Append(key.key());
223
257
  NonBinaryStringValue emit(std::move(value_slice));
224
258
  emit.WritePrefix(output_.AddTiny(emit.prefix_length()));
259
+ // Allocate an index in the hpack table for this newly emitted entry.
260
+ // (we do so here because we know the length of the key and value)
261
+ uint32_t index = compressor_->table_.AllocateIndex(
262
+ key_len + value_len + hpack_constants::kEntryOverhead);
225
263
  output_.Append(emit.data());
264
+ return index;
226
265
  }
227
266
 
228
- void HPackCompressor::Encoder::EmitLitHdrWithBinaryStringKeyNotIdx(
229
- Slice key_slice, Slice value_slice) {
267
+ void Encoder::EmitLitHdrWithBinaryStringKeyNotIdx(Slice key_slice,
268
+ Slice value_slice) {
230
269
  StringKey key(std::move(key_slice));
231
270
  key.WritePrefix(0x00, output_.AddTiny(key.prefix_length()));
232
271
  output_.Append(key.key());
@@ -235,18 +274,24 @@ void HPackCompressor::Encoder::EmitLitHdrWithBinaryStringKeyNotIdx(
235
274
  output_.Append(emit.data());
236
275
  }
237
276
 
238
- void HPackCompressor::Encoder::EmitLitHdrWithBinaryStringKeyIncIdx(
239
- Slice key_slice, Slice value_slice) {
277
+ uint32_t Encoder::EmitLitHdrWithBinaryStringKeyIncIdx(Slice key_slice,
278
+ Slice value_slice) {
279
+ auto key_len = key_slice.length();
240
280
  StringKey key(std::move(key_slice));
241
281
  key.WritePrefix(0x40, output_.AddTiny(key.prefix_length()));
242
282
  output_.Append(key.key());
243
283
  BinaryStringValue emit(std::move(value_slice), use_true_binary_metadata_);
244
284
  emit.WritePrefix(output_.AddTiny(emit.prefix_length()));
285
+ // Allocate an index in the hpack table for this newly emitted entry.
286
+ // (we do so here because we know the length of the key and value)
287
+ uint32_t index = compressor_->table_.AllocateIndex(
288
+ key_len + emit.hpack_length() + hpack_constants::kEntryOverhead);
245
289
  output_.Append(emit.data());
290
+ return index;
246
291
  }
247
292
 
248
- void HPackCompressor::Encoder::EmitLitHdrWithBinaryStringKeyNotIdx(
249
- uint32_t key_index, Slice value_slice) {
293
+ void Encoder::EmitLitHdrWithBinaryStringKeyNotIdx(uint32_t key_index,
294
+ Slice value_slice) {
250
295
  BinaryStringValue emit(std::move(value_slice), use_true_binary_metadata_);
251
296
  VarintWriter<4> key(key_index);
252
297
  uint8_t* data = output_.AddTiny(key.length() + emit.prefix_length());
@@ -255,8 +300,8 @@ void HPackCompressor::Encoder::EmitLitHdrWithBinaryStringKeyNotIdx(
255
300
  output_.Append(emit.data());
256
301
  }
257
302
 
258
- void HPackCompressor::Encoder::EmitLitHdrWithNonBinaryStringKeyNotIdx(
259
- Slice key_slice, Slice value_slice) {
303
+ void Encoder::EmitLitHdrWithNonBinaryStringKeyNotIdx(Slice key_slice,
304
+ Slice value_slice) {
260
305
  StringKey key(std::move(key_slice));
261
306
  key.WritePrefix(0x00, output_.AddTiny(key.prefix_length()));
262
307
  output_.Append(key.key());
@@ -265,14 +310,14 @@ void HPackCompressor::Encoder::EmitLitHdrWithNonBinaryStringKeyNotIdx(
265
310
  output_.Append(emit.data());
266
311
  }
267
312
 
268
- void HPackCompressor::Encoder::AdvertiseTableSizeChange() {
313
+ void Encoder::AdvertiseTableSizeChange() {
269
314
  VarintWriter<3> w(compressor_->table_.max_size());
270
315
  w.Write(0x20, output_.AddTiny(w.length()));
271
316
  }
272
317
 
273
- void HPackCompressor::SliceIndex::EmitTo(absl::string_view key,
274
- const Slice& value, Encoder* encoder) {
275
- auto& table = encoder->compressor_->table_;
318
+ void SliceIndex::EmitTo(absl::string_view key, const Slice& value,
319
+ Encoder* encoder) {
320
+ auto& table = encoder->hpack_table();
276
321
  using It = std::vector<ValueIndex>::iterator;
277
322
  It prev = values_.end();
278
323
  size_t transport_length =
@@ -291,8 +336,7 @@ void HPackCompressor::SliceIndex::EmitTo(absl::string_view key,
291
336
  encoder->EmitIndexed(table.DynamicIndex(it->index));
292
337
  } else {
293
338
  // Not current, emit a new literal and update the index.
294
- it->index = table.AllocateIndex(transport_length);
295
- encoder->EmitLitHdrWithNonBinaryStringKeyIncIdx(
339
+ it->index = encoder->EmitLitHdrWithNonBinaryStringKeyIncIdx(
296
340
  Slice::FromStaticString(key), value.Ref());
297
341
  }
298
342
  // Bubble this entry up if we can - ensures that the most used values end
@@ -310,13 +354,12 @@ void HPackCompressor::SliceIndex::EmitTo(absl::string_view key,
310
354
  prev = it;
311
355
  }
312
356
  // No hit, emit a new literal and add it to the index.
313
- uint32_t index = table.AllocateIndex(transport_length);
314
- encoder->EmitLitHdrWithNonBinaryStringKeyIncIdx(Slice::FromStaticString(key),
315
- value.Ref());
357
+ uint32_t index = encoder->EmitLitHdrWithNonBinaryStringKeyIncIdx(
358
+ Slice::FromStaticString(key), value.Ref());
316
359
  values_.emplace_back(value.Ref(), index);
317
360
  }
318
361
 
319
- void HPackCompressor::Encoder::Encode(const Slice& key, const Slice& value) {
362
+ void Encoder::Encode(const Slice& key, const Slice& value) {
320
363
  if (absl::EndsWith(key.as_string_view(), "-bin")) {
321
364
  EmitLitHdrWithBinaryStringKeyNotIdx(key.Ref(), value.Ref());
322
365
  } else {
@@ -324,43 +367,14 @@ void HPackCompressor::Encoder::Encode(const Slice& key, const Slice& value) {
324
367
  }
325
368
  }
326
369
 
327
- void HPackCompressor::Encoder::Encode(HttpPathMetadata, const Slice& value) {
328
- compressor_->path_index_.EmitTo(HttpPathMetadata::key(), value, this);
329
- }
330
-
331
- void HPackCompressor::Encoder::Encode(HttpAuthorityMetadata,
332
- const Slice& value) {
333
- compressor_->authority_index_.EmitTo(HttpAuthorityMetadata::key(), value,
334
- this);
335
- }
336
-
337
- void HPackCompressor::Encoder::Encode(TeMetadata, TeMetadata::ValueType value) {
338
- GPR_ASSERT(value == TeMetadata::ValueType::kTrailers);
339
- EncodeAlwaysIndexed(
340
- &compressor_->te_index_, "te", Slice::FromStaticString("trailers"),
341
- 2 /* te */ + 8 /* trailers */ + hpack_constants::kEntryOverhead);
342
- }
343
-
344
- void HPackCompressor::Encoder::Encode(ContentTypeMetadata,
345
- ContentTypeMetadata::ValueType value) {
346
- if (value != ContentTypeMetadata::ValueType::kApplicationGrpc) {
347
- gpr_log(GPR_ERROR, "Not encoding bad content-type header");
348
- return;
349
- }
350
- EncodeAlwaysIndexed(&compressor_->content_type_index_, "content-type",
351
- Slice::FromStaticString("application/grpc"),
352
- 12 /* content-type */ + 16 /* application/grpc */ +
353
- hpack_constants::kEntryOverhead);
354
- }
355
-
356
- void HPackCompressor::Encoder::Encode(HttpSchemeMetadata,
357
- HttpSchemeMetadata::ValueType value) {
370
+ void Compressor<HttpSchemeMetadata, HttpSchemeCompressor>::EncodeWith(
371
+ HttpSchemeMetadata, HttpSchemeMetadata::ValueType value, Encoder* encoder) {
358
372
  switch (value) {
359
373
  case HttpSchemeMetadata::ValueType::kHttp:
360
- EmitIndexed(6); // :scheme: http
374
+ encoder->EmitIndexed(6); // :scheme: http
361
375
  break;
362
376
  case HttpSchemeMetadata::ValueType::kHttps:
363
- EmitIndexed(7); // :scheme: https
377
+ encoder->EmitIndexed(7); // :scheme: https
364
378
  break;
365
379
  case HttpSchemeMetadata::ValueType::kInvalid:
366
380
  Crash("invalid http scheme encoding");
@@ -368,22 +382,10 @@ void HPackCompressor::Encoder::Encode(HttpSchemeMetadata,
368
382
  }
369
383
  }
370
384
 
371
- void HPackCompressor::Encoder::Encode(GrpcTraceBinMetadata,
372
- const Slice& slice) {
373
- EncodeRepeatingSliceValue(GrpcTraceBinMetadata::key(), slice,
374
- &compressor_->grpc_trace_bin_index_,
375
- HPackEncoderTable::MaxEntrySize());
376
- }
377
-
378
- void HPackCompressor::Encoder::Encode(GrpcTagsBinMetadata, const Slice& slice) {
379
- EncodeRepeatingSliceValue(GrpcTagsBinMetadata::key(), slice,
380
- &compressor_->grpc_tags_bin_index_,
381
- HPackEncoderTable::MaxEntrySize());
382
- }
383
-
384
- void HPackCompressor::Encoder::Encode(HttpStatusMetadata, uint32_t status) {
385
+ void Compressor<HttpStatusMetadata, HttpStatusCompressor>::EncodeWith(
386
+ HttpStatusMetadata, uint32_t status, Encoder* encoder) {
385
387
  if (status == 200) {
386
- EmitIndexed(8); // :status: 200
388
+ encoder->EmitIndexed(8); // :status: 200
387
389
  return;
388
390
  }
389
391
  uint8_t index = 0;
@@ -408,27 +410,28 @@ void HPackCompressor::Encoder::Encode(HttpStatusMetadata, uint32_t status) {
408
410
  break;
409
411
  }
410
412
  if (GPR_LIKELY(index != 0)) {
411
- EmitIndexed(index);
413
+ encoder->EmitIndexed(index);
412
414
  } else {
413
- EmitLitHdrWithNonBinaryStringKeyIncIdx(Slice::FromStaticString(":status"),
414
- Slice::FromInt64(status));
415
+ encoder->EmitLitHdrWithNonBinaryStringKeyNotIdx(
416
+ Slice::FromStaticString(":status"), Slice::FromInt64(status));
415
417
  }
416
418
  }
417
419
 
418
- void HPackCompressor::Encoder::Encode(HttpMethodMetadata,
419
- HttpMethodMetadata::ValueType method) {
420
+ void Compressor<HttpMethodMetadata, HttpMethodCompressor>::EncodeWith(
421
+ HttpMethodMetadata, HttpMethodMetadata::ValueType method,
422
+ Encoder* encoder) {
420
423
  switch (method) {
421
424
  case HttpMethodMetadata::ValueType::kPost:
422
- EmitIndexed(3); // :method: POST
425
+ encoder->EmitIndexed(3); // :method: POST
423
426
  break;
424
427
  case HttpMethodMetadata::ValueType::kGet:
425
- EmitIndexed(2); // :method: GET
428
+ encoder->EmitIndexed(2); // :method: GET
426
429
  break;
427
430
  case HttpMethodMetadata::ValueType::kPut:
428
431
  // Right now, we only emit PUT as a method for testing purposes, so it's
429
432
  // fine to not index it.
430
- EmitLitHdrWithNonBinaryStringKeyNotIdx(Slice::FromStaticString(":method"),
431
- Slice::FromStaticString("PUT"));
433
+ encoder->EmitLitHdrWithNonBinaryStringKeyNotIdx(
434
+ Slice::FromStaticString(":method"), Slice::FromStaticString("PUT"));
432
435
  break;
433
436
  case HttpMethodMetadata::ValueType::kInvalid:
434
437
  Crash("invalid http method encoding");
@@ -436,35 +439,31 @@ void HPackCompressor::Encoder::Encode(HttpMethodMetadata,
436
439
  }
437
440
  }
438
441
 
439
- void HPackCompressor::Encoder::EncodeAlwaysIndexed(uint32_t* index,
440
- absl::string_view key,
441
- Slice value,
442
- size_t transport_length) {
442
+ void Encoder::EncodeAlwaysIndexed(uint32_t* index, absl::string_view key,
443
+ Slice value, size_t) {
443
444
  if (compressor_->table_.ConvertableToDynamicIndex(*index)) {
444
445
  EmitIndexed(compressor_->table_.DynamicIndex(*index));
445
446
  } else {
446
- *index = compressor_->table_.AllocateIndex(transport_length);
447
- EmitLitHdrWithNonBinaryStringKeyIncIdx(Slice::FromStaticString(key),
448
- std::move(value));
447
+ *index = EmitLitHdrWithNonBinaryStringKeyIncIdx(
448
+ Slice::FromStaticString(key), std::move(value));
449
449
  }
450
450
  }
451
451
 
452
- void HPackCompressor::Encoder::EncodeIndexedKeyWithBinaryValue(
453
- uint32_t* index, absl::string_view key, Slice value) {
452
+ void Encoder::EncodeIndexedKeyWithBinaryValue(uint32_t* index,
453
+ absl::string_view key,
454
+ Slice value) {
454
455
  if (compressor_->table_.ConvertableToDynamicIndex(*index)) {
455
456
  EmitLitHdrWithBinaryStringKeyNotIdx(
456
457
  compressor_->table_.DynamicIndex(*index), std::move(value));
457
458
  } else {
458
- *index = compressor_->table_.AllocateIndex(key.length() + value.length() +
459
- hpack_constants::kEntryOverhead);
460
- EmitLitHdrWithBinaryStringKeyIncIdx(Slice::FromStaticString(key),
461
- std::move(value));
459
+ *index = EmitLitHdrWithBinaryStringKeyIncIdx(Slice::FromStaticString(key),
460
+ std::move(value));
462
461
  }
463
462
  }
464
463
 
465
- void HPackCompressor::Encoder::EncodeRepeatingSliceValue(
466
- const absl::string_view& key, const Slice& slice, uint32_t* index,
467
- size_t max_compression_size) {
464
+ void Encoder::EncodeRepeatingSliceValue(const absl::string_view& key,
465
+ const Slice& slice, uint32_t* index,
466
+ size_t max_compression_size) {
468
467
  if (hpack_constants::SizeForEntry(key.size(), slice.size()) >
469
468
  max_compression_size) {
470
469
  EmitLitHdrWithBinaryStringKeyNotIdx(Slice::FromStaticString(key),
@@ -474,141 +473,37 @@ void HPackCompressor::Encoder::EncodeRepeatingSliceValue(
474
473
  }
475
474
  }
476
475
 
477
- void HPackCompressor::Encoder::Encode(GrpcTimeoutMetadata, Timestamp deadline) {
476
+ void TimeoutCompressorImpl::EncodeWith(absl::string_view key,
477
+ Timestamp deadline, Encoder* encoder) {
478
478
  Timeout timeout = Timeout::FromDuration(deadline - Timestamp::Now());
479
- for (auto it = compressor_->previous_timeouts_.begin();
480
- it != compressor_->previous_timeouts_.end(); ++it) {
479
+ auto& table = encoder->hpack_table();
480
+ for (auto it = previous_timeouts_.begin(); it != previous_timeouts_.end();
481
+ ++it) {
481
482
  double ratio = timeout.RatioVersus(it->timeout);
482
483
  // If the timeout we're sending is shorter than a previous timeout, but
483
484
  // within 3% of it, we'll consider sending it.
484
485
  if (ratio > -3 && ratio <= 0 &&
485
- compressor_->table_.ConvertableToDynamicIndex(it->index)) {
486
- EmitIndexed(compressor_->table_.DynamicIndex(it->index));
486
+ table.ConvertableToDynamicIndex(it->index)) {
487
+ encoder->EmitIndexed(table.DynamicIndex(it->index));
487
488
  // Put this timeout to the front of the queue - forces common timeouts to
488
489
  // be considered earlier.
489
- std::swap(*it, *compressor_->previous_timeouts_.begin());
490
+ std::swap(*it, *previous_timeouts_.begin());
490
491
  return;
491
492
  }
492
493
  }
493
494
  // Clean out some expired timeouts.
494
- while (!compressor_->previous_timeouts_.empty() &&
495
- !compressor_->table_.ConvertableToDynamicIndex(
496
- compressor_->previous_timeouts_.back().index)) {
497
- compressor_->previous_timeouts_.pop_back();
495
+ while (!previous_timeouts_.empty() &&
496
+ !table.ConvertableToDynamicIndex(previous_timeouts_.back().index)) {
497
+ previous_timeouts_.pop_back();
498
498
  }
499
499
  Slice encoded = timeout.Encode();
500
- uint32_t index = compressor_->table_.AllocateIndex(
501
- GrpcTimeoutMetadata::key().length() + encoded.length() +
502
- hpack_constants::kEntryOverhead);
503
- compressor_->previous_timeouts_.push_back(PreviousTimeout{timeout, index});
504
- EmitLitHdrWithNonBinaryStringKeyIncIdx(
505
- Slice::FromStaticString(GrpcTimeoutMetadata::key()), std::move(encoded));
506
- }
507
-
508
- void HPackCompressor::Encoder::Encode(UserAgentMetadata, const Slice& slice) {
509
- if (hpack_constants::SizeForEntry(UserAgentMetadata::key().size(),
510
- slice.size()) >
511
- HPackEncoderTable::MaxEntrySize()) {
512
- EmitLitHdrWithNonBinaryStringKeyNotIdx(
513
- Slice::FromStaticString(UserAgentMetadata::key()), slice.Ref());
514
- return;
515
- }
516
- if (!slice.is_equivalent(compressor_->user_agent_)) {
517
- compressor_->user_agent_ = slice.Ref();
518
- compressor_->user_agent_index_ = 0;
519
- }
520
- EncodeAlwaysIndexed(&compressor_->user_agent_index_, UserAgentMetadata::key(),
521
- slice.Ref(),
522
- hpack_constants::SizeForEntry(
523
- UserAgentMetadata::key().size(), slice.size()));
524
- }
525
-
526
- void HPackCompressor::Encoder::Encode(GrpcStatusMetadata,
527
- grpc_status_code status) {
528
- const uint32_t code = static_cast<uint32_t>(status);
529
- uint32_t* index = nullptr;
530
- if (code < kNumCachedGrpcStatusValues) {
531
- index = &compressor_->cached_grpc_status_[code];
532
- if (compressor_->table_.ConvertableToDynamicIndex(*index)) {
533
- EmitIndexed(compressor_->table_.DynamicIndex(*index));
534
- return;
535
- }
536
- }
537
- Slice key = Slice::FromStaticString(GrpcStatusMetadata::key());
538
- Slice value = Slice::FromInt64(code);
539
- const size_t transport_length =
540
- key.length() + value.length() + hpack_constants::kEntryOverhead;
541
- if (index != nullptr) {
542
- *index = compressor_->table_.AllocateIndex(transport_length);
543
- EmitLitHdrWithNonBinaryStringKeyIncIdx(std::move(key), std::move(value));
544
- } else {
545
- EmitLitHdrWithNonBinaryStringKeyNotIdx(std::move(key), std::move(value));
546
- }
547
- }
548
-
549
- void HPackCompressor::Encoder::Encode(GrpcEncodingMetadata,
550
- grpc_compression_algorithm value) {
551
- uint32_t* index = nullptr;
552
- if (value < GRPC_COMPRESS_ALGORITHMS_COUNT) {
553
- index = &compressor_->cached_grpc_encoding_[static_cast<uint32_t>(value)];
554
- if (compressor_->table_.ConvertableToDynamicIndex(*index)) {
555
- EmitIndexed(compressor_->table_.DynamicIndex(*index));
556
- return;
557
- }
558
- }
559
- auto key = Slice::FromStaticString(GrpcEncodingMetadata::key());
560
- auto encoded_value = GrpcEncodingMetadata::Encode(value);
561
- size_t transport_length =
562
- key.length() + encoded_value.length() + hpack_constants::kEntryOverhead;
563
- if (index != nullptr) {
564
- *index = compressor_->table_.AllocateIndex(transport_length);
565
- EmitLitHdrWithNonBinaryStringKeyIncIdx(std::move(key),
566
- std::move(encoded_value));
567
- } else {
568
- EmitLitHdrWithNonBinaryStringKeyNotIdx(std::move(key),
569
- std::move(encoded_value));
570
- }
571
- }
572
-
573
- void HPackCompressor::Encoder::Encode(GrpcAcceptEncodingMetadata,
574
- CompressionAlgorithmSet value) {
575
- if (compressor_->grpc_accept_encoding_index_ != 0 &&
576
- value == compressor_->grpc_accept_encoding_ &&
577
- compressor_->table_.ConvertableToDynamicIndex(
578
- compressor_->grpc_accept_encoding_index_)) {
579
- EmitIndexed(compressor_->table_.DynamicIndex(
580
- compressor_->grpc_accept_encoding_index_));
581
- return;
582
- }
583
- auto key = Slice::FromStaticString(GrpcAcceptEncodingMetadata::key());
584
- auto encoded_value = GrpcAcceptEncodingMetadata::Encode(value);
585
- size_t transport_length =
586
- key.length() + encoded_value.length() + hpack_constants::kEntryOverhead;
587
- compressor_->grpc_accept_encoding_index_ =
588
- compressor_->table_.AllocateIndex(transport_length);
589
- compressor_->grpc_accept_encoding_ = value;
590
- EmitLitHdrWithNonBinaryStringKeyIncIdx(std::move(key),
591
- std::move(encoded_value));
592
- }
593
-
594
- void HPackCompressor::SetMaxUsableSize(uint32_t max_table_size) {
595
- max_usable_size_ = max_table_size;
596
- SetMaxTableSize(std::min(table_.max_size(), max_table_size));
597
- }
598
-
599
- void HPackCompressor::SetMaxTableSize(uint32_t max_table_size) {
600
- if (table_.SetMaxSize(std::min(max_usable_size_, max_table_size))) {
601
- advertise_table_size_change_ = true;
602
- if (GRPC_TRACE_FLAG_ENABLED(grpc_http_trace)) {
603
- gpr_log(GPR_INFO, "set max table size from encoder to %d",
604
- max_table_size);
605
- }
606
- }
500
+ uint32_t index = encoder->EmitLitHdrWithNonBinaryStringKeyIncIdx(
501
+ Slice::FromStaticString(key), std::move(encoded));
502
+ previous_timeouts_.push_back(PreviousTimeout{timeout, index});
607
503
  }
608
504
 
609
- HPackCompressor::Encoder::Encoder(HPackCompressor* compressor,
610
- bool use_true_binary_metadata,
611
- SliceBuffer& output)
505
+ Encoder::Encoder(HPackCompressor* compressor, bool use_true_binary_metadata,
506
+ SliceBuffer& output)
612
507
  : use_true_binary_metadata_(use_true_binary_metadata),
613
508
  compressor_(compressor),
614
509
  output_(output) {
@@ -617,4 +512,5 @@ HPackCompressor::Encoder::Encoder(HPackCompressor* compressor,
617
512
  }
618
513
  }
619
514
 
515
+ } // namespace hpack_encoder_detail
620
516
  } // namespace grpc_core