QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/third_party/quiche/src/quic/core/qpack/qpack_encoder.h" |
| 6 | |
bnc | 95734b2 | 2019-07-17 07:06:53 -0700 | [diff] [blame] | 7 | #include <list> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 8 | |
bnc | 5f88b0c | 2019-06-21 05:11:20 -0700 | [diff] [blame] | 9 | #include "net/third_party/quiche/src/quic/core/qpack/qpack_constants.h" |
| 10 | #include "net/third_party/quiche/src/quic/core/qpack/qpack_instruction_encoder.h" |
bnc | 8873e57 | 2019-07-25 08:45:42 -0700 | [diff] [blame^] | 11 | #include "net/third_party/quiche/src/quic/core/qpack/qpack_required_insert_count.h" |
bnc | 5f88b0c | 2019-06-21 05:11:20 -0700 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/quic/core/qpack/value_splitting_header_list.h" |
vasilvv | 0fb4443 | 2019-03-13 22:47:36 -0700 | [diff] [blame] | 13 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 14 | #include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 15 | |
| 16 | namespace quic { |
| 17 | |
| 18 | QpackEncoder::QpackEncoder( |
| 19 | DecoderStreamErrorDelegate* decoder_stream_error_delegate, |
renjietang | c2aa5cb | 2019-06-20 12:22:53 -0700 | [diff] [blame] | 20 | QpackStreamSenderDelegate* encoder_stream_sender_delegate) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 21 | : decoder_stream_error_delegate_(decoder_stream_error_delegate), |
| 22 | decoder_stream_receiver_(this), |
| 23 | encoder_stream_sender_(encoder_stream_sender_delegate) { |
| 24 | DCHECK(decoder_stream_error_delegate_); |
| 25 | DCHECK(encoder_stream_sender_delegate); |
| 26 | } |
| 27 | |
| 28 | QpackEncoder::~QpackEncoder() {} |
| 29 | |
bnc | f21c1ad | 2019-06-20 20:09:50 -0700 | [diff] [blame] | 30 | std::string QpackEncoder::EncodeHeaderList( |
bnc | 5f88b0c | 2019-06-21 05:11:20 -0700 | [diff] [blame] | 31 | QuicStreamId /* stream_id */, |
bnc | f21c1ad | 2019-06-20 20:09:50 -0700 | [diff] [blame] | 32 | const spdy::SpdyHeaderBlock* header_list) { |
bnc | 95734b2 | 2019-07-17 07:06:53 -0700 | [diff] [blame] | 33 | // First pass. |
bnc | 5f88b0c | 2019-06-21 05:11:20 -0700 | [diff] [blame] | 34 | |
bnc | 95734b2 | 2019-07-17 07:06:53 -0700 | [diff] [blame] | 35 | // Encode into |instructions| which will be serialized during the second pass. |
| 36 | std::list<InstructionWithValues> instructions; |
bnc | 5f88b0c | 2019-06-21 05:11:20 -0700 | [diff] [blame] | 37 | |
| 38 | for (const auto& header : ValueSplittingHeaderList(header_list)) { |
| 39 | QuicStringPiece name = header.first; |
| 40 | QuicStringPiece value = header.second; |
| 41 | |
| 42 | bool is_static; |
| 43 | uint64_t index; |
| 44 | |
| 45 | auto match_type = |
| 46 | header_table_.FindHeaderField(name, value, &is_static, &index); |
| 47 | |
| 48 | switch (match_type) { |
| 49 | case QpackHeaderTable::MatchType::kNameAndValue: |
| 50 | DCHECK(is_static) << "Dynamic table entries not supported yet."; |
| 51 | |
bnc | 95734b2 | 2019-07-17 07:06:53 -0700 | [diff] [blame] | 52 | instructions.push_back({QpackIndexedHeaderFieldInstruction(), {}}); |
| 53 | instructions.back().values.s_bit = is_static; |
| 54 | instructions.back().values.varint = index; |
bnc | 5f88b0c | 2019-06-21 05:11:20 -0700 | [diff] [blame] | 55 | |
| 56 | break; |
| 57 | case QpackHeaderTable::MatchType::kName: |
| 58 | DCHECK(is_static) << "Dynamic table entries not supported yet."; |
| 59 | |
bnc | 95734b2 | 2019-07-17 07:06:53 -0700 | [diff] [blame] | 60 | instructions.push_back( |
| 61 | {QpackLiteralHeaderFieldNameReferenceInstruction(), {}}); |
| 62 | instructions.back().values.s_bit = is_static; |
| 63 | instructions.back().values.varint = index; |
| 64 | instructions.back().values.value = value; |
bnc | 5f88b0c | 2019-06-21 05:11:20 -0700 | [diff] [blame] | 65 | |
| 66 | break; |
| 67 | case QpackHeaderTable::MatchType::kNoMatch: |
bnc | 95734b2 | 2019-07-17 07:06:53 -0700 | [diff] [blame] | 68 | instructions.push_back({QpackLiteralHeaderFieldInstruction(), {}}); |
| 69 | instructions.back().values.name = name; |
| 70 | instructions.back().values.value = value; |
bnc | 5f88b0c | 2019-06-21 05:11:20 -0700 | [diff] [blame] | 71 | |
| 72 | break; |
| 73 | } |
bnc | f21c1ad | 2019-06-20 20:09:50 -0700 | [diff] [blame] | 74 | } |
bnc | 5f88b0c | 2019-06-21 05:11:20 -0700 | [diff] [blame] | 75 | |
bnc | 8873e57 | 2019-07-25 08:45:42 -0700 | [diff] [blame^] | 76 | // TODO(bnc): Implement dynamic entries and set Required Insert Count |
| 77 | // accordingly. |
| 78 | const uint64_t required_insert_count = 0; |
| 79 | |
bnc | 95734b2 | 2019-07-17 07:06:53 -0700 | [diff] [blame] | 80 | // Second pass. |
| 81 | QpackInstructionEncoder instruction_encoder; |
| 82 | std::string encoded_headers; |
| 83 | |
| 84 | // Header block prefix. |
bnc | 95734b2 | 2019-07-17 07:06:53 -0700 | [diff] [blame] | 85 | QpackInstructionEncoder::Values values; |
bnc | 8873e57 | 2019-07-25 08:45:42 -0700 | [diff] [blame^] | 86 | values.varint = QpackEncodeRequiredInsertCount(required_insert_count, |
| 87 | header_table_.max_entries()); |
bnc | 95734b2 | 2019-07-17 07:06:53 -0700 | [diff] [blame] | 88 | values.varint2 = 0; // Delta Base. |
| 89 | values.s_bit = false; // Delta Base sign. |
| 90 | |
| 91 | instruction_encoder.Encode(QpackPrefixInstruction(), values, |
| 92 | &encoded_headers); |
| 93 | |
| 94 | for (const auto& instruction : instructions) { |
| 95 | instruction_encoder.Encode(instruction.instruction, instruction.values, |
| 96 | &encoded_headers); |
| 97 | } |
| 98 | |
bnc | f21c1ad | 2019-06-20 20:09:50 -0700 | [diff] [blame] | 99 | return encoded_headers; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void QpackEncoder::DecodeDecoderStreamData(QuicStringPiece data) { |
| 103 | decoder_stream_receiver_.Decode(data); |
| 104 | } |
| 105 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 106 | void QpackEncoder::OnInsertCountIncrement(uint64_t /*increment*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 107 | // TODO(bnc): Implement dynamic table management for encoding. |
| 108 | } |
| 109 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 110 | void QpackEncoder::OnHeaderAcknowledgement(QuicStreamId /*stream_id*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 111 | // TODO(bnc): Implement dynamic table management for encoding. |
| 112 | } |
| 113 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 114 | void QpackEncoder::OnStreamCancellation(QuicStreamId /*stream_id*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 115 | // TODO(bnc): Implement dynamic table management for encoding. |
| 116 | } |
| 117 | |
| 118 | void QpackEncoder::OnErrorDetected(QuicStringPiece error_message) { |
| 119 | decoder_stream_error_delegate_->OnDecoderStreamError(error_message); |
| 120 | } |
| 121 | |
| 122 | } // namespace quic |