You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by ma...@apache.org on 2018/08/27 07:05:50 UTC
[trafficserver] branch quic-latest updated (ea32638 -> 4c0dc17)
This is an automated email from the ASF dual-hosted git repository.
maskit pushed a change to branch quic-latest
in repository https://gitbox.apache.org/repos/asf/trafficserver.git.
from ea32638 Return STREAM_BLOCKED frame when it is generated
new 8b8d9a0 Add QUICStreamIO::peek
new 062b8f4 Separate out some of HPACK logic into hdrs/
new e755cc8 Preserve the prefix bits when encoding integer with XPACK
new e4cbf6b ADD QPACK module
new 4c0dc17 Add tests for QPACK
The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.gitignore | 2 +-
iocore/eventsystem/I_Event.h | 1 +
iocore/net/quic/QUICApplication.cc | 14 +-
iocore/net/quic/QUICApplication.h | 2 +
proxy/{http2 => hdrs}/HuffmanCodec.cc | 0
proxy/{http2 => hdrs}/HuffmanCodec.h | 0
proxy/hdrs/Makefile.am | 23 +-
proxy/hdrs/XPACK.cc | 202 +++
.../net/quic/QUICIntUtil.h => proxy/hdrs/XPACK.h | 26 +-
proxy/{http2 => hdrs}/test_Huffmancode.cc | 0
proxy/hq/Makefile.am | 32 +-
proxy/hq/QPACK.cc | 1608 ++++++++++++++++++++
proxy/hq/QPACK.h | 322 ++++
.../hq/test/main_qpack.cc | 40 +-
{iocore/net/quic => proxy/hq}/test/stub.cc | 37 +
proxy/hq/test/test_QPACK.cc | 433 ++++++
proxy/http2/HPACK.cc | 215 +--
proxy/http2/HPACK.h | 5 +-
proxy/http2/HTTP2.cc | 1 -
proxy/http2/Makefile.am | 16 +-
proxy/http2/RegressionHPACK.cc | 18 +-
21 files changed, 2754 insertions(+), 243 deletions(-)
rename proxy/{http2 => hdrs}/HuffmanCodec.cc (100%)
rename proxy/{http2 => hdrs}/HuffmanCodec.h (100%)
create mode 100644 proxy/hdrs/XPACK.cc
copy iocore/net/quic/QUICIntUtil.h => proxy/hdrs/XPACK.h (57%)
rename proxy/{http2 => hdrs}/test_Huffmancode.cc (100%)
create mode 100644 proxy/hq/QPACK.cc
create mode 100644 proxy/hq/QPACK.h
copy iocore/net/quic/test/event_processor_main.cc => proxy/hq/test/main_qpack.cc (63%)
copy {iocore/net/quic => proxy/hq}/test/stub.cc (77%)
create mode 100644 proxy/hq/test/test_QPACK.cc
[trafficserver] 04/05: ADD QPACK module
Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
maskit pushed a commit to branch quic-latest
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
commit e4cbf6b1a984fdd1dfdd1ef3fcf0f90166209bd2
Author: Masakazu Kitajo <ma...@apache.org>
AuthorDate: Tue Jul 24 11:39:36 2018 +0900
ADD QPACK module
---
iocore/eventsystem/I_Event.h | 1 +
proxy/hq/Makefile.am | 1 +
proxy/hq/QPACK.cc | 1608 ++++++++++++++++++++++++++++++++++++++++++
proxy/hq/QPACK.h | 322 +++++++++
4 files changed, 1932 insertions(+)
diff --git a/iocore/eventsystem/I_Event.h b/iocore/eventsystem/I_Event.h
index 4e1938f..36239d3 100644
--- a/iocore/eventsystem/I_Event.h
+++ b/iocore/eventsystem/I_Event.h
@@ -75,6 +75,7 @@
#define HTTP_SCH_UPDATE_EVENTS_START 2400
#define QUIC_EVENT_EVENTS_START 2500
#define HQ_SESSION_EVENTS_START 2600
+#define QPACK_EVENT_EVENTS_START 2700
#define NT_ASYNC_CONNECT_EVENT_EVENTS_START 3000
#define NT_ASYNC_IO_EVENT_EVENTS_START 3100
#define RAFT_EVENT_EVENTS_START 3200
diff --git a/proxy/hq/Makefile.am b/proxy/hq/Makefile.am
index 3276530..c8dac9a 100644
--- a/proxy/hq/Makefile.am
+++ b/proxy/hq/Makefile.am
@@ -45,6 +45,7 @@ libhq_a_SOURCES = \
HQDataFramer.cc \
HQHeaderVIOAdaptor.cc \
HQStreamDataVIOAdaptor.cc \
+ QPACK.cc \
QUICSimpleApp.cc
#
diff --git a/proxy/hq/QPACK.cc b/proxy/hq/QPACK.cc
new file mode 100644
index 0000000..127e305
--- /dev/null
+++ b/proxy/hq/QPACK.cc
@@ -0,0 +1,1608 @@
+/** @file
+ *
+ * A brief file description
+ *
+ * @section license License
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+#include "HTTP.h"
+#include "XPACK.h"
+#include "QPACK.h"
+#include "ts/ink_defs.h"
+#include "ts/ink_memory.h"
+
+#define QPACKDebug(fmt, ...) Debug("qpack", "[%s] " fmt, this->_qc->cids().data(), ##__VA_ARGS__)
+
+const QPACK::Header QPACK::StaticTable::STATIC_HEADER_FIELDS[] = {
+ {"", ""}, // Index 0 is invalid
+ {"static_1", "svalue1"},
+ {"static_2", ""},
+};
+
+QPACK::QPACK(QUICConnection *qc, size_t maximum_size) : QUICApplication(qc), _dynamic_table(SETTINGS_HEADER_TABLE_SIZE)
+{
+ SET_HANDLER(&QPACK::event_handler);
+
+ this->_encoder_stream_sending_instructions = new_MIOBuffer(BUFFER_SIZE_INDEX_1K);
+ this->_decoder_stream_sending_instructions = new_MIOBuffer(BUFFER_SIZE_INDEX_1K);
+ this->_encoder_stream_sending_instructions_reader = this->_encoder_stream_sending_instructions->alloc_reader();
+ this->_decoder_stream_sending_instructions_reader = this->_decoder_stream_sending_instructions->alloc_reader();
+}
+
+QPACK::~QPACK() {}
+
+int
+QPACK::event_handler(int event, Event *data)
+{
+ VIO *vio = reinterpret_cast<VIO *>(data);
+ QUICStreamIO *stream_io = this->_find_stream_io(vio);
+ int ret;
+
+ switch (event) {
+ case VC_EVENT_READ_READY:
+ ret = this->_on_read_ready(*stream_io);
+ break;
+ case VC_EVENT_READ_COMPLETE:
+ ret = EVENT_DONE;
+ break;
+ case VC_EVENT_WRITE_READY:
+ ret = this->_on_write_ready(*stream_io);
+ case VC_EVENT_WRITE_COMPLETE:
+ ret = EVENT_DONE;
+ break;
+ default:
+ ret = EVENT_DONE;
+ }
+ return ret;
+}
+
+int
+QPACK::encode(uint64_t stream_id, HTTPHdr &header_set, MIOBuffer *header_block)
+{
+ if (!header_block) {
+ return -1;
+ }
+
+ uint16_t base_index = this->_largest_known_received_index;
+
+ // Compress headers and record the largest reference
+ uint16_t referred_index = 0;
+ uint16_t largest_reference = 0;
+ uint16_t smallest_reference = 0;
+ IOBufferBlock *compressed_headers = new_IOBufferBlock();
+ compressed_headers->alloc(BUFFER_SIZE_INDEX_2K);
+
+ MIMEFieldIter field_iter;
+ for (MIMEField *field = header_set.iter_get_first(&field_iter); field != nullptr; field = header_set.iter_get_next(&field_iter)) {
+ int ret = this->_encode_header(*field, base_index, compressed_headers, referred_index);
+ largest_reference = std::max(largest_reference, referred_index);
+ smallest_reference = std::min(smallest_reference, referred_index);
+ if (ret < 0) {
+ compressed_headers->free();
+ return ret;
+ }
+ }
+ struct EntryReference eref = {smallest_reference, largest_reference};
+ this->_references.emplace(stream_id, eref);
+
+ // Make an IOBufferBlock for Header Data Prefix
+ IOBufferBlock *header_data_prefix = new_IOBufferBlock();
+ header_data_prefix->alloc(BUFFER_SIZE_INDEX_128);
+ this->_encode_prefix(largest_reference, base_index, header_data_prefix);
+
+ header_block->append_block(header_data_prefix);
+ header_block->append_block(compressed_headers);
+
+ return 0;
+}
+
+int
+QPACK::decode(uint64_t stream_id, const uint8_t *header_block, size_t header_block_len, HTTPHdr &hdr, Continuation *cont,
+ EThread *thread)
+{
+ if (!cont || !header_block) {
+ return -1;
+ }
+
+ if (this->_invalid) {
+ thread->schedule_imm(cont, QPACK_EVENT_DECODE_FAILED, nullptr);
+ return 0;
+ }
+
+ uint64_t tmp = 0;
+ int64_t ret = xpack_decode_integer(tmp, header_block, header_block + header_block_len, 8);
+ if (ret < 0 && tmp > 0xFFFF) {
+ return -1;
+ }
+ uint16_t largest_reference = tmp;
+
+ if (this->_dynamic_table.largest_index() < largest_reference) {
+ // Blocked
+ if (this->_add_to_blocked_list(
+ new DecodeRequest(largest_reference, thread, cont, stream_id, header_block, header_block_len, hdr))) {
+ return 1;
+ } else {
+ // Number of blocked streams exceed the limit
+ return -2;
+ }
+ }
+
+ this->_decode(thread, cont, stream_id, header_block, header_block_len, hdr);
+
+ return 0;
+}
+
+int
+QPACK::_encode_prefix(uint16_t largest_reference, uint16_t base_index, IOBufferBlock *prefix)
+{
+ int ret;
+ if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(prefix->end()),
+ reinterpret_cast<uint8_t *>(prefix->end() + prefix->write_avail()), largest_reference, 8)) < 0) {
+ return -1;
+ }
+ prefix->fill(ret);
+
+ uint16_t delta;
+ if (base_index < largest_reference) {
+ prefix->end()[0] |= 0x80;
+ delta = largest_reference - base_index;
+ } else {
+ delta = base_index - largest_reference;
+ }
+ if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(prefix->end()),
+ reinterpret_cast<uint8_t *>(prefix->end() + prefix->write_avail()), delta, 7)) < 0) {
+ return -1;
+ }
+ prefix->fill(ret);
+
+ QPACKDebug("Encoded Header Data Prefix: largest_ref=%d, base_index=%d, delta=%d", largest_reference, base_index, delta);
+
+ return 0;
+}
+
+int
+QPACK::_encode_header(const MIMEField &field, uint16_t base_index, IOBufferBlock *compressed_header, uint16_t &referred_index)
+{
+ int name_len;
+ const char *name = field.name_get(&name_len);
+ int value_len;
+ const char *value = field.value_get(&value_len);
+
+ // TODO Set never_index flag on/off according to encoding headers
+ bool never_index = false;
+
+ // Find from tables, and insert / duplicate a entry prior to encode it
+ LookupResult lookup_result_static;
+ LookupResult lookup_result_dynamic;
+ lookup_result_static = StaticTable::lookup(name, name_len, value, value_len);
+ if (lookup_result_static.match_type != LookupResult::MatchType::EXACT) {
+ lookup_result_dynamic = this->_dynamic_table.lookup(name, name_len, value, value_len);
+ if (lookup_result_dynamic.match_type == LookupResult::MatchType::EXACT) {
+ if (this->_dynamic_table.should_duplicate(lookup_result_dynamic.index)) {
+ // Duplicate an entry and use the new entry
+ uint16_t current_index = lookup_result_dynamic.index;
+ lookup_result_dynamic = this->_dynamic_table.duplicate_entry(current_index);
+ if (lookup_result_dynamic.match_type != LookupResult::MatchType::NONE) {
+ this->_write_duplicate(current_index);
+ QPACKDebug("Wrote Duplicate: current_index=%d", current_index);
+ }
+ }
+ } else if (lookup_result_static.match_type == LookupResult::MatchType::NAME) {
+ if (never_index) {
+ // Name in static table is always available. Do nothing.
+ } else {
+ // Insert both the name and the value
+ lookup_result_dynamic = this->_dynamic_table.insert_entry(name, name_len, value, value_len);
+ if (lookup_result_dynamic.match_type != LookupResult::MatchType::NONE) {
+ this->_write_insert_without_name_ref(name, name_len, value, value_len);
+ QPACKDebug("Wrote Insert Without Name Ref: name=%.*s value=%.*s", name_len, name, value_len, value);
+ }
+ }
+ } else if (lookup_result_dynamic.match_type == LookupResult::MatchType::NAME) {
+ if (never_index) {
+ if (this->_dynamic_table.should_duplicate(lookup_result_dynamic.index)) {
+ // Duplicate an entry and use the new entry
+ uint16_t current_index = lookup_result_dynamic.index;
+ lookup_result_dynamic = this->_dynamic_table.duplicate_entry(current_index);
+ if (lookup_result_dynamic.match_type != LookupResult::MatchType::NONE) {
+ this->_write_duplicate(current_index);
+ QPACKDebug("Wrote Duplicate: current_index=%d", current_index);
+ }
+ }
+ } else {
+ if (this->_dynamic_table.should_duplicate(lookup_result_dynamic.index)) {
+ // Duplicate an entry and use the new entry
+ uint16_t current_index = lookup_result_dynamic.index;
+ lookup_result_dynamic = this->_dynamic_table.duplicate_entry(current_index);
+ if (lookup_result_dynamic.match_type != LookupResult::MatchType::NONE) {
+ this->_write_duplicate(current_index);
+ QPACKDebug("Wrote Duplicate: current_index=%d", current_index);
+ }
+ } else {
+ // Insert both the name and the value
+ lookup_result_dynamic = this->_dynamic_table.insert_entry(name, name_len, value, value_len);
+ if (lookup_result_dynamic.match_type != LookupResult::MatchType::NONE) {
+ this->_write_insert_without_name_ref(name, name_len, value, value_len);
+ QPACKDebug("Wrote Insert Without Name Ref: name=%.*s value=%.*s", name_len, name, value_len, value);
+ }
+ }
+ }
+ } else {
+ if (never_index) {
+ // Insert only the name
+ lookup_result_dynamic = this->_dynamic_table.insert_entry(name, name_len, "", 0);
+ if (lookup_result_dynamic.match_type != LookupResult::MatchType::NONE) {
+ this->_write_insert_without_name_ref(name, name_len, "", 0);
+ QPACKDebug("Wrote Insert Without Name Ref: name=%.*s value=%.*s", name_len, name, 0, "");
+ }
+ } else {
+ // Insert both the name and the value
+ lookup_result_dynamic = this->_dynamic_table.insert_entry(name, name_len, value, value_len);
+ if (lookup_result_dynamic.match_type != LookupResult::MatchType::NONE) {
+ this->_write_insert_without_name_ref(name, name_len, value, value_len);
+ QPACKDebug("Wrote Insert Without Name Ref: name=%.*s value=%.*s", name_len, name, value_len, value);
+ }
+ }
+ }
+ }
+
+ // Encode
+ if (lookup_result_static.match_type == LookupResult::MatchType::EXACT) {
+ this->_encode_indexed_header_field(lookup_result_static.index, base_index, false, compressed_header);
+ QPACKDebug("Encoded Indexed Header Field: abs_index=%d, base_index=%d, dynamic_table=%d", lookup_result_static.index,
+ base_index, false);
+ referred_index = lookup_result_static.index;
+ } else if (lookup_result_dynamic.match_type == LookupResult::MatchType::EXACT) {
+ if (lookup_result_dynamic.index < this->_largest_known_received_index) {
+ this->_encode_indexed_header_field(lookup_result_dynamic.index, base_index, true, compressed_header);
+ QPACKDebug("Encoded Indexed Header Field: abs_index=%d, base_index=%d, dynamic_table=%d", lookup_result_dynamic.index,
+ base_index, true);
+ } else {
+ this->_encode_indexed_header_field_with_postbase_index(lookup_result_dynamic.index, base_index, never_index,
+ compressed_header);
+ QPACKDebug("Encoded Indexed Header With Postbase Index: abs_index=%d, base_index=%d, never_index=%d",
+ lookup_result_dynamic.index, base_index, never_index);
+ }
+ this->_dynamic_table.ref_entry(lookup_result_dynamic.index);
+ referred_index = lookup_result_dynamic.index;
+ } else if (lookup_result_static.match_type == LookupResult::MatchType::NAME) {
+ this->_encode_literal_header_field_with_name_ref(lookup_result_static.index, false, base_index, value, value_len, never_index,
+ compressed_header);
+ QPACKDebug(
+ "Encoded Literal Header Field With Name Ref: abs_index=%d, base_index=%d, dynamic_table=%d, value=%.*s, never_index=%d",
+ lookup_result_static.index, base_index, false, value_len, value, never_index);
+ referred_index = lookup_result_static.index;
+ } else if (lookup_result_dynamic.match_type == LookupResult::MatchType::NAME) {
+ if (lookup_result_dynamic.index <= this->_largest_known_received_index) {
+ this->_encode_literal_header_field_with_name_ref(lookup_result_dynamic.index, true, base_index, value, value_len, never_index,
+ compressed_header);
+ QPACKDebug(
+ "Encoded Literal Header Field With Name Ref: abs_index=%d, base_index=%d, dynamic_table=%d, value=%.*s, never_index=%d",
+ lookup_result_dynamic.index, base_index, true, value_len, value, never_index);
+ } else {
+ this->_encode_literal_header_field_with_postbase_name_ref(lookup_result_dynamic.index, base_index, value, value_len,
+ never_index, compressed_header);
+ QPACKDebug("Encoded Literal Header Field With Postbase Name Ref: abs_index=%d, base_index=%d, value=%.*s, never_index=%d",
+ lookup_result_dynamic.index, base_index, value_len, value, never_index);
+ }
+ this->_dynamic_table.ref_entry(lookup_result_dynamic.index);
+ referred_index = lookup_result_dynamic.index;
+ } else {
+ this->_encode_literal_header_field_without_name_ref(name, name_len, value, value_len, never_index, compressed_header);
+ QPACKDebug("Encoded Literal Header Field Without Name Ref: name=%.*s, value=%.*s, never_index=%d", name_len, name, value_len,
+ value, never_index);
+ }
+
+ return 0;
+}
+
+int
+QPACK::_encode_indexed_header_field(uint16_t index, uint16_t base_index, bool dynamic_table, IOBufferBlock *compressed_header)
+{
+ char *buf = compressed_header->end();
+ char *buf_end = buf + compressed_header->write_avail();
+ int written = 0;
+
+ // Indexed Header Field
+ buf[0] = 0x80;
+
+ // References static table or not
+ if (dynamic_table) {
+ // Use relative index if we refer Dynamic Table
+ index = this->_calc_relative_index_from_absolute_index(base_index, index);
+ } else {
+ buf[0] |= 0x40;
+ }
+
+ // Index
+ int ret;
+ if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), index, 6)) <
+ 0) {
+ return ret;
+ }
+ written += ret;
+
+ // Finalize and Schedule to send
+ compressed_header->fill(written);
+
+ return 0;
+}
+
+int
+QPACK::_encode_indexed_header_field_with_postbase_index(uint16_t index, uint16_t base_index, bool never_index,
+ IOBufferBlock *compressed_header)
+{
+ char *buf = compressed_header->end();
+ char *buf_end = buf + compressed_header->write_avail();
+ int written = 0;
+
+ // Indexed Header Field with Post-Base Index
+ buf[0] = 0x10;
+
+ // Index
+ int ret;
+ if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end),
+ this->_calc_postbase_index_from_absolute_index(base_index, index), 4)) < 0) {
+ return ret;
+ }
+ written += ret;
+
+ // Finalize and Schedule to send
+ compressed_header->fill(written);
+
+ return 0;
+}
+
+int
+QPACK::_encode_literal_header_field_with_name_ref(uint16_t index, bool dynamic_table, uint16_t base_index, const char *value,
+ int value_len, bool never_index, IOBufferBlock *compressed_header)
+{
+ char *buf = compressed_header->end();
+ char *buf_end = buf + compressed_header->write_avail();
+ int written = 0;
+
+ // Literal Header Field With Name Reference
+ buf[0] = 0x40;
+
+ if (never_index) {
+ buf[0] |= 0x20;
+ }
+
+ // References static table or not
+ if (dynamic_table) {
+ // Use relative index if we refer Dynamic Table
+ index = this->_calc_relative_index_from_absolute_index(base_index, index);
+ } else {
+ buf[0] |= 0x10;
+ }
+
+ // Index
+ int ret;
+ if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), index, 4)) <
+ 0) {
+ return ret;
+ }
+ written += ret;
+
+ // Value
+ if ((ret = xpack_encode_string(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), value,
+ value_len)) < 0) {
+ return ret;
+ }
+ written += ret;
+
+ // Finalize and Schedule to send
+ compressed_header->fill(written);
+
+ return 0;
+}
+
+int
+QPACK::_encode_literal_header_field_without_name_ref(const char *name, int name_len, const char *value, int value_len,
+ bool never_index, IOBufferBlock *compressed_header)
+{
+ char *buf = compressed_header->end();
+ char *buf_end = buf + compressed_header->write_avail();
+ int written = 0;
+
+ // Literal Header Field Without Name Reference
+ buf[0] = 0x20;
+
+ if (never_index) {
+ buf[0] |= 0x10;
+ }
+
+ // Name
+ int ret;
+ if ((ret = xpack_encode_string(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), name, name_len,
+ 3)) < 0) {
+ return ret;
+ }
+ written += ret;
+
+ // Value
+ if ((ret = xpack_encode_string(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), value, value_len,
+ 7)) < 0) {
+ return ret;
+ }
+ written += ret;
+
+ // Finalize and Schedule to send
+ compressed_header->fill(written);
+
+ return 0;
+}
+
+int
+QPACK::_encode_literal_header_field_with_postbase_name_ref(uint16_t index, uint16_t base_index, const char *value, int value_len,
+ bool never_index, IOBufferBlock *compressed_header)
+{
+ char *buf = compressed_header->end();
+ char *buf_end = buf + compressed_header->write_avail();
+ int written = 0;
+
+ // Literal Header Field With Post-Base Name Reference
+ buf[0] = 0x00;
+
+ if (never_index) {
+ buf[0] |= 0x08;
+ }
+
+ // Index
+ int ret;
+ if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end),
+ this->_calc_postbase_index_from_absolute_index(base_index, index), 3)) < 0) {
+ return ret;
+ }
+ written += ret;
+
+ // Value
+ if ((ret = xpack_encode_string(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), value, value_len,
+ 7)) < 0) {
+ return ret;
+ }
+ written += ret;
+
+ // Finalize and Schedule to send
+ compressed_header->fill(written);
+
+ return 0;
+}
+
+int
+QPACK::_decode_indexed_header_field(int16_t base_index, const uint8_t *buf, size_t buf_len, HTTPHdr &hdr)
+{
+ // Read index field
+ int len = 0;
+ uint64_t index;
+ int ret = xpack_decode_integer(index, buf, buf + buf_len, 6);
+ if (ret < 0) {
+ return -1;
+ }
+ len += ret;
+
+ // Lookup a table
+ const char *name = nullptr;
+ int name_len = 0;
+ const char *value = nullptr;
+ int value_len = 0;
+ QPACK::LookupResult result;
+
+ if (buf[0] & 0x40) { // Static table
+ result = StaticTable::lookup(index, &name, &name_len, &value, &value_len);
+ } else { // Dynamic table
+ result = this->_dynamic_table.lookup(this->_calc_absolute_index_from_relative_index(base_index, index), &name, &name_len,
+ &value, &value_len);
+ }
+ if (result.match_type != QPACK::LookupResult::MatchType::EXACT) {
+ return -1;
+ }
+
+ // Create and attach a header
+ MIMEField *new_field = hdr.field_create(name, name_len);
+ new_field->value_set(hdr.m_heap, hdr.m_mime, value, value_len);
+ hdr.field_attach(new_field);
+
+ QPACKDebug("Decoded Indexed Header Field: base_index=%d, abs_index=%d, name=%.*s, value=%.*s", base_index, result.index, name_len,
+ name, value_len, value);
+
+ return len;
+}
+
+int
+QPACK::_decode_literal_header_field_with_name_ref(int16_t base_index, const uint8_t *buf, size_t buf_len, HTTPHdr &hdr)
+{
+ int read_len = 0;
+
+ // Never index field
+ bool never_index = false;
+ if (buf[0] & 0x40) {
+ never_index = true;
+ }
+
+ // Read name index field
+ uint64_t index;
+ int ret = xpack_decode_integer(index, buf, buf + buf_len, 4);
+ if (ret < 0) {
+ return -1;
+ }
+ read_len += ret;
+
+ // Lookup the name
+ const char *name = nullptr;
+ int name_len = 0;
+ const char *dummy = nullptr;
+ int dummy_len = 0;
+ QPACK::LookupResult result;
+
+ if (buf[0] & 0x10) { // Static table
+ result = StaticTable::lookup(index, &name, &name_len, &dummy, &dummy_len);
+ } else { // Dynamic table
+ result = this->_dynamic_table.lookup(this->_calc_absolute_index_from_relative_index(base_index, index), &name, &name_len,
+ &dummy, &dummy_len);
+ }
+ if (result.match_type != QPACK::LookupResult::MatchType::EXACT) {
+ return -1;
+ }
+
+ // Read value
+ Arena arena;
+ char *value;
+ uint64_t value_len;
+ if ((ret = xpack_decode_string(arena, &value, value_len, buf + read_len, buf + buf_len - read_len, 7)) < 0) {
+ return -1;
+ }
+ read_len += ret;
+
+ // Create and attach a header
+ this->_attach_header(hdr, name, name_len, value, value_len);
+
+ QPACKDebug("Decoded Literal Header Field With Name Ref: base_index=%d, abs_index=%d, name=%.*s, value=%.*s", base_index,
+ result.index, name_len, name, static_cast<int>(value_len), value);
+
+ return read_len;
+}
+
+int
+QPACK::_decode_literal_header_field_without_name_ref(const uint8_t *buf, size_t buf_len, HTTPHdr &hdr)
+{
+ int read_len = 0;
+
+ // Never index field
+ bool never_index = false;
+ if (buf[0] & 0x10) {
+ never_index = true;
+ }
+
+ // Read name and value
+ Arena arena;
+ int64_t ret;
+ char *name;
+ uint64_t name_len;
+ if ((ret = xpack_decode_string(arena, &name, name_len, buf, buf + buf_len, 3)) < 0) {
+ return -1;
+ }
+ read_len += ret;
+
+ char *value;
+ uint64_t value_len;
+ if ((ret = xpack_decode_string(arena, &value, value_len, buf + read_len, buf + buf_len - read_len, 7)) < 0) {
+ return -1;
+ }
+ read_len += ret;
+
+ // Create and attach a header
+ this->_attach_header(hdr, name, name_len, value, value_len);
+
+ QPACKDebug("Decoded Literal Header Field Without Name Ref: name=%.*s, value=%.*s", static_cast<uint16_t>(name_len), name,
+ static_cast<uint16_t>(value_len), value);
+
+ return read_len;
+}
+
+int
+QPACK::_decode_indexed_header_field_with_postbase_index(int16_t base_index, const uint8_t *buf, size_t buf_len, HTTPHdr &hdr)
+{
+ // Read index field
+ int len = 0;
+ uint64_t index;
+ int ret = xpack_decode_integer(index, buf, buf + buf_len, 4);
+ if (ret < 0) {
+ return -1;
+ }
+ len += ret;
+
+ // Lookup a table
+ const char *name = nullptr;
+ int name_len = 0;
+ const char *value = nullptr;
+ int value_len = 0;
+ QPACK::LookupResult result;
+
+ result = this->_dynamic_table.lookup(this->_calc_absolute_index_from_postbase_index(base_index, index), &name, &name_len, &value,
+ &value_len);
+ if (result.match_type != QPACK::LookupResult::MatchType::EXACT) {
+ return -1;
+ }
+
+ // Create and attach a header
+ this->_attach_header(hdr, name, name_len, value, value_len);
+
+ QPACKDebug("Decoded Indexed Header Field With Postbase Index: base_index=%d, abs_index=%d, name=%.*s, value=%.*s", base_index,
+ result.index, name_len, name, value_len, value);
+
+ return len;
+}
+
+int
+QPACK::_decode_literal_header_field_with_postbase_name_ref(int16_t base_index, const uint8_t *buf, size_t buf_len, HTTPHdr &hdr)
+{
+ int read_len = 0;
+
+ // Never index field
+ bool never_index = false;
+ if (buf[0] & 0x08) {
+ never_index = true;
+ }
+
+ // Read name index field
+ uint64_t index;
+ int ret = xpack_decode_integer(index, buf, buf + buf_len, 3);
+ if (ret < 0) {
+ return -1;
+ }
+ read_len += ret;
+
+ // Lookup the name
+ const char *name = nullptr;
+ int name_len = 0;
+ const char *dummy = nullptr;
+ int dummy_len = 0;
+ QPACK::LookupResult result;
+
+ result = this->_dynamic_table.lookup(this->_calc_absolute_index_from_postbase_index(base_index, index), &name, &name_len, &dummy,
+ &dummy_len);
+ if (result.match_type != QPACK::LookupResult::MatchType::EXACT) {
+ return -1;
+ }
+
+ // Read value
+ Arena arena;
+ char *value;
+ uint64_t value_len;
+ if ((ret = xpack_decode_string(arena, &value, value_len, buf + read_len, buf + buf_len - read_len, 7)) < 0) {
+ return -1;
+ }
+ read_len += ret;
+
+ // Create and attach a header
+ this->_attach_header(hdr, name, name_len, value, value_len);
+
+ QPACKDebug("Decoded Literal Header Field With Postbase Name Ref: base_index=%d, abs_index=%d, name=%.*s, value=%.*s", base_index,
+ static_cast<uint16_t>(index), name_len, name, static_cast<int>(value_len), value);
+
+ return read_len;
+}
+
+int
+QPACK::_decode_header(const uint8_t *header_block, size_t header_block_len, HTTPHdr &hdr)
+{
+ const uint8_t *pos = header_block;
+ size_t remain_len = header_block_len;
+ int64_t ret;
+
+ // Decode Header Data Prefix
+ uint64_t tmp;
+ if ((ret = xpack_decode_integer(tmp, pos, pos + remain_len, 8)) < 0 && tmp > 0xFFFF) {
+ return -1;
+ }
+ pos += ret;
+ uint16_t largest_reference = tmp;
+
+ uint64_t delta_base_index;
+ uint16_t base_index;
+ if ((ret = xpack_decode_integer(delta_base_index, pos, pos + remain_len, 7)) < 0 && delta_base_index < 0xFFFF) {
+ return -1;
+ }
+ if (pos[0] & 0x80) {
+ if (delta_base_index == 0) {
+ return -1;
+ }
+ base_index = largest_reference - delta_base_index;
+ } else {
+ base_index = largest_reference + delta_base_index;
+ }
+ pos += ret;
+
+ // Decode Instructions
+ while (pos < header_block + header_block_len) {
+ if (pos[0] & 0x80) { // Index Header Field
+ ret = this->_decode_indexed_header_field(base_index, pos, remain_len, hdr);
+ } else if (pos[0] & 0x40) { // Literal Header Field With Name Reference
+ ret = this->_decode_literal_header_field_with_name_ref(base_index, pos, remain_len, hdr);
+ } else if (pos[0] & 0x20) { // Literal Header Field Without Name Reference
+ ret = this->_decode_literal_header_field_without_name_ref(pos, remain_len, hdr);
+ } else if (pos[0] & 0x10) { // Indexed Header Field With Post-Base Index
+ ret = this->_decode_indexed_header_field_with_postbase_index(base_index, pos, remain_len, hdr);
+ } else { // Literal Header Field With Post-Base Name Reference
+ ret = this->_decode_literal_header_field_with_postbase_name_ref(base_index, pos, remain_len, hdr);
+ }
+ if (ret < 0) {
+ break;
+ }
+ pos += ret;
+ }
+
+ return ret;
+}
+
+void
+QPACK::_decode(EThread *ethread, Continuation *cont, uint64_t stream_id, const uint8_t *header_block, size_t header_block_len,
+ HTTPHdr &hdr)
+{
+ int event;
+ if (this->_decode_header(header_block, header_block_len, hdr) < 0) {
+ event = QPACK_EVENT_DECODE_FAILED;
+ } else {
+ event = QPACK_EVENT_DECODE_COMPLETE;
+ this->_write_header_acknowledgement(stream_id);
+ }
+ ethread->schedule_imm(cont, event, &hdr);
+}
+
+bool
+QPACK::_add_to_blocked_list(DecodeRequest *decode_request)
+{
+ if (this->_blocked_list.count() >= SETTINGS_QPACK_BLOCKED_STREAMS) {
+ return false;
+ }
+
+ this->_blocked_list.append(decode_request);
+ return true;
+}
+
+void
+QPACK::_update_largest_known_received_index_by_insert_count(uint16_t insert_count)
+{
+ this->_largest_known_received_index += insert_count;
+}
+
+void
+QPACK::_update_largest_known_received_index_by_stream_id(uint64_t stream_id)
+{
+ uint16_t largest_ref_index = this->_references[stream_id].largest;
+ if (largest_ref_index > this->_largest_known_received_index) {
+ this->_largest_known_received_index = largest_ref_index;
+ }
+}
+
+void
+QPACK::_update_reference_counts(uint64_t stream_id)
+{
+ uint16_t smallest_ref_index = this->_references[stream_id].smallest;
+ this->_dynamic_table.unref_entry(smallest_ref_index);
+}
+
+void
+QPACK::_resume_decode()
+{
+ DecodeRequest *r = this->_blocked_list.head();
+ while (r) {
+ if (this->_largest_known_received_index >= r->largest_reference()) {
+ this->_decode(r->thread(), r->continuation(), r->stream_id(), r->header_block(), r->header_block_len(), r->hdr());
+ DecodeRequest *tmp = r;
+ r = DecodeRequest::Linkage::next_ptr(r);
+ this->_blocked_list.erase(tmp);
+ delete tmp;
+ } else {
+ r = DecodeRequest::Linkage::next_ptr(r);
+ }
+ }
+}
+
+void
+QPACK::_abort_decode()
+{
+ this->_invalid = true;
+
+ DecodeRequest *r = this->_blocked_list.head();
+ while (r) {
+ if (this->_largest_known_received_index >= r->largest_reference()) {
+ r->thread()->schedule_imm(r->continuation(), QPACK_EVENT_DECODE_FAILED, nullptr);
+ DecodeRequest *tmp = r;
+ r = DecodeRequest::Linkage::next_ptr(r);
+ this->_blocked_list.erase(tmp);
+ delete tmp;
+ } else {
+ r = DecodeRequest::Linkage::next_ptr(r);
+ }
+ }
+}
+
+int
+QPACK::_on_read_ready(QUICStreamIO &stream_io)
+{
+ QUICStreamId stream_id = stream_io.stream_id();
+ if (stream_id == this->_decoder_stream_id) {
+ return this->_on_decoder_stream_read_ready(stream_io);
+ } else if (stream_id == this->_encoder_stream_id) {
+ return this->_on_encoder_stream_read_ready(stream_io);
+ } else {
+ ink_assert(!"The stream ID must match either encoder stream id or decoder stream id");
+ return EVENT_DONE;
+ }
+}
+
+int
+QPACK::_on_write_ready(QUICStreamIO &stream_io)
+{
+ QUICStreamId stream_id = stream_io.stream_id();
+ if (stream_id == this->_decoder_stream_id) {
+ return this->_on_decoder_write_ready(stream_io);
+ } else if (stream_id == this->_encoder_stream_id) {
+ return this->_on_encoder_write_ready(stream_io);
+ } else {
+ ink_assert(!"The stream ID must match either decoder stream id or decoder stream id");
+ return EVENT_DONE;
+ }
+}
+
+int
+QPACK::_on_decoder_stream_read_ready(QUICStreamIO &stream_io)
+{
+ uint8_t buf;
+ if (stream_io.peek(&buf, 1) > 0) {
+ if (buf & 0x80) { // Header Acknowledgement
+ uint64_t stream_id;
+ if (this->_read_header_acknowledgement(stream_io, stream_id) >= 0) {
+ QPACKDebug("Received Header Acknowledgement: stream_id=%" PRIu64, stream_id);
+ this->_update_largest_known_received_index_by_stream_id(stream_id);
+ this->_update_reference_counts(stream_id);
+ this->_references.erase(stream_id);
+ }
+ } else if (buf & 0x40) { // Stream Cancellation
+ uint64_t stream_id;
+ if (this->_read_stream_cancellation(stream_io, stream_id) >= 0) {
+ QPACKDebug("Received Stream Cancellation: stream_id=%" PRIu64, stream_id);
+ this->_update_reference_counts(stream_id);
+ this->_references.erase(stream_id);
+ }
+ } else { // Table State Synchronize
+ uint16_t insert_count;
+ if (this->_read_table_state_synchronize(stream_io, insert_count) >= 0) {
+ QPACKDebug("Received Table State Synchronize: inserted_count=%d", insert_count);
+ this->_update_largest_known_received_index_by_insert_count(insert_count);
+ }
+ }
+ }
+
+ return EVENT_DONE;
+}
+
+int
+QPACK::_on_encoder_stream_read_ready(QUICStreamIO &stream_io)
+{
+ uint8_t buf;
+
+ while (stream_io.peek(&buf, 1) > 0) {
+ if (buf & 0x80) { // Insert With Name Reference
+ bool is_static;
+ uint16_t index;
+ Arena arena;
+ char *value;
+ uint16_t value_len;
+ if (this->_read_insert_with_name_ref(stream_io, is_static, index, arena, &value, value_len) < 0) {
+ this->_abort_decode();
+ return EVENT_DONE;
+ }
+ QPACKDebug("Received Insert With Name Ref: is_static=%d, index=%d, value=%.*s", is_static, index, value_len, value);
+ this->_dynamic_table.insert_entry(is_static, index, value, value_len);
+ } else if (buf & 0x40) { // Insert Without Name Reference
+ Arena arena;
+ char *name;
+ uint16_t name_len;
+ char *value;
+ uint16_t value_len;
+ if (this->_read_insert_without_name_ref(stream_io, arena, &name, name_len, &value, value_len) < 0) {
+ this->_abort_decode();
+ return EVENT_DONE;
+ }
+ QPACKDebug("Received Insert Without Name Ref: name=%.*s, value=%.*s", name_len, name, value_len, value);
+ this->_dynamic_table.insert_entry(name, name_len, value, value_len);
+ } else if (buf & 0x20) { // Dynamic Table Size Update
+ uint16_t max_size;
+ if (this->_read_dynamic_table_size_update(stream_io, max_size) < 0) {
+ this->_abort_decode();
+ return EVENT_DONE;
+ }
+ QPACKDebug("Received Dynamic Table Size Update: max_size=%d", max_size);
+ this->_dynamic_table.update_size(max_size);
+ } else { // Duplicatex
+ uint16_t index;
+ if (this->_read_duplicate(stream_io, index) < 0) {
+ this->_abort_decode();
+ return EVENT_DONE;
+ }
+ QPACKDebug("Received Duplicate: index=%d", index);
+ this->_dynamic_table.duplicate_entry(index);
+ }
+
+ this->_resume_decode();
+ }
+
+ return EVENT_DONE;
+}
+
+int
+QPACK::_on_decoder_write_ready(QUICStreamIO &stream_io)
+{
+ int64_t written_len = stream_io.write(this->_decoder_stream_sending_instructions_reader, INT64_MAX);
+ this->_decoder_stream_sending_instructions_reader->consume(written_len);
+ return written_len;
+}
+
+int
+QPACK::_on_encoder_write_ready(QUICStreamIO &stream_io)
+{
+ int64_t written_len = stream_io.write(this->_encoder_stream_sending_instructions_reader, INT64_MAX);
+ this->_encoder_stream_sending_instructions_reader->consume(written_len);
+ return written_len;
+}
+
+size_t
+QPACK::estimate_header_block_size(const HTTPHdr &hdr)
+{
+ // FIXME Estimate it
+ return 128 * 1024 * 1024;
+}
+
+const QPACK::LookupResult
+QPACK::StaticTable::lookup(uint16_t index, const char **name, int *name_len, const char **value, int *value_len)
+{
+ const Header &header = STATIC_HEADER_FIELDS[index];
+ *name = header.name;
+ *name_len = header.name_len;
+ *value = header.value;
+ *value_len = header.value_len;
+ return {index, QPACK::LookupResult::MatchType::EXACT};
+}
+
+const QPACK::LookupResult
+QPACK::StaticTable::lookup(const char *name, int name_len, const char *value, int value_len)
+{
+ QPACK::LookupResult::MatchType match_type = QPACK::LookupResult::MatchType::NONE;
+ uint16_t i = 0;
+ int candidate_index = 0;
+ int n = countof(STATIC_HEADER_FIELDS);
+
+ for (; i < n; ++i) {
+ const Header &h = STATIC_HEADER_FIELDS[i];
+ if (h.name_len == name_len) {
+ if (memcmp(name, h.name, name_len) == 0) {
+ if (value_len == h.value_len && memcmp(value, h.value, value_len) == 0) {
+ // Exact match
+ match_type = QPACK::LookupResult::MatchType::EXACT;
+ break;
+ } else {
+ // Name match -- Keep it for no exact matchs
+ match_type = QPACK::LookupResult::MatchType::NAME;
+ }
+ candidate_index = i;
+ }
+ }
+ }
+ return {i, match_type};
+}
+
+uint16_t
+QPACK::_calc_absolute_index_from_relative_index(uint16_t base_index, uint16_t relative_index)
+{
+ return base_index - relative_index;
+}
+
+uint16_t
+QPACK::_calc_absolute_index_from_postbase_index(uint16_t base_index, uint16_t postbase_index)
+{
+ return base_index + postbase_index + 1;
+}
+
+uint16_t
+QPACK::_calc_relative_index_from_absolute_index(uint16_t base_index, uint16_t absolute_index)
+{
+ return base_index - absolute_index;
+}
+
+uint16_t
+QPACK::_calc_postbase_index_from_absolute_index(uint16_t base_index, uint16_t absolute_index)
+{
+ return absolute_index - base_index - 1;
+}
+
+void
+QPACK::_attach_header(HTTPHdr &hdr, const char *name, int name_len, const char *value, int value_len)
+{
+ MIMEField *new_field = hdr.field_create(name, name_len);
+ new_field->value_set(hdr.m_heap, hdr.m_mime, value, value_len);
+ hdr.field_attach(new_field);
+}
+
+//
+// DynamicTable
+//
+QPACK::DynamicTable::DynamicTable(uint16_t size) : _available(size), _storage(new DynamicTableStorage(size)) {}
+
+QPACK::DynamicTable::~DynamicTable()
+{
+ if (this->_storage) {
+ delete this->_storage;
+ this->_storage = nullptr;
+ }
+}
+
+const QPACK::LookupResult
+QPACK::DynamicTable::lookup(uint16_t index, const char **name, int *name_len, const char **value, int *value_len)
+{
+ uint16_t pos = (this->_entries_head + (index - this->_entries[this->_entries_head].index)) % countof(this->_entries);
+ *name_len = this->_entries[pos].name_len;
+ *value_len = this->_entries[pos].value_len;
+ this->_storage->read(this->_entries[pos].offset, name, *name_len, value, *value_len);
+ return {index, QPACK::LookupResult::MatchType::EXACT};
+}
+
+const QPACK::LookupResult
+QPACK::DynamicTable::lookup(const char *name, int name_len, const char *value, int value_len)
+{
+ QPACK::LookupResult::MatchType match_type = QPACK::LookupResult::MatchType::NONE;
+ uint16_t i = 0;
+ uint16_t candidate_index = 0;
+ const char *tmp_name = nullptr;
+ const char *tmp_value = nullptr;
+ int n = countof(this->_entries);
+
+ // TODO Use a tree for better perfomance
+ for (; i < n; ++i) {
+ if (name_len != 0 && this->_entries[i].name_len == name_len) {
+ this->_storage->read(this->_entries[i].offset, &tmp_name, this->_entries[i].name_len, &tmp_value,
+ this->_entries[i].value_len);
+ if (memcmp(name, tmp_name, name_len) == 0) {
+ candidate_index = this->_entries[i].index;
+ if (value_len == this->_entries[i].value_len && memcmp(value, tmp_value, value_len) == 0) {
+ // Exact match
+ match_type = QPACK::LookupResult::MatchType::EXACT;
+ break;
+ } else {
+ // Name match -- Keep it for no exact matchs
+ match_type = QPACK::LookupResult::MatchType::NAME;
+ }
+ }
+ }
+ }
+ return {candidate_index, match_type};
+}
+
+const QPACK::LookupResult
+QPACK::DynamicTable::insert_entry(bool is_static, uint16_t index, const char *value, uint16_t value_len)
+{
+ // TODO Implement it.
+ ink_assert(!"not implemented");
+ return {};
+}
+
+const QPACK::LookupResult
+QPACK::DynamicTable::insert_entry(const char *name, uint16_t name_len, const char *value, uint16_t value_len)
+{
+ // Check if we can make enough space to insert a new entry
+ uint16_t required_len = name_len + value_len;
+ uint16_t available = this->_available;
+ uint16_t tail = this->_entries_tail;
+ while (available < required_len) {
+ if (this->_entries[tail].ref_count) {
+ break;
+ }
+ available += this->_entries[tail].name_len + this->_entries[tail].value_len;
+ tail = (tail + 1) % countof(this->_entries);
+ }
+ if (available < required_len) {
+ // We can't insert a new entry because some stream(s) refer an entry that need to be evicted
+ return {UINT16_C(0), QPACK::LookupResult::MatchType::NONE};
+ }
+
+ // Evict
+ this->_available = available;
+ this->_entries_tail = tail;
+
+ // Insert
+ this->_entries_head = (this->_entries_head + 1) % countof(this->_entries);
+ this->_entries[this->_entries_head] = {++this->_entries_inserted, this->_storage->write(name, name_len, value, value_len),
+ name_len, value_len, 0};
+ this->_available -= required_len;
+
+ return {this->_entries_inserted, value_len ? LookupResult::MatchType::EXACT : LookupResult::MatchType::NAME};
+}
+
+const QPACK::LookupResult
+QPACK::DynamicTable::duplicate_entry(uint16_t current_index)
+{
+ const char *name;
+ int name_len;
+ const char *value;
+ int value_len;
+
+ this->lookup(current_index, &name, &name_len, &value, &value_len);
+ return this->insert_entry(name, name_len, value, value_len);
+}
+
+bool
+QPACK::DynamicTable::should_duplicate(uint16_t index)
+{
+ // TODO: Check whether a specified entry should be duplicated
+ // Just return false for now
+ return false;
+}
+
+void
+QPACK::DynamicTable::update_size(uint16_t max_size)
+{
+ // TODO Implement it
+}
+
+void
+QPACK::DynamicTable::ref_entry(uint16_t index)
+{
+ ++this->_entries[index].ref_count;
+}
+
+void
+QPACK::DynamicTable::unref_entry(uint16_t index)
+{
+ --this->_entries[index].ref_count;
+}
+
+uint16_t
+QPACK::DynamicTable::largest_index()
+{
+ return this->_entries_inserted;
+}
+
+int
+QPACK::_write_insert_with_name_ref(uint16_t index, bool dynamic, const char *value, uint16_t value_len)
+{
+ IOBufferBlock *instruction = new_IOBufferBlock();
+ instruction->alloc(TS_IOBUFFER_SIZE_INDEX_2K);
+
+ char *buf = instruction->end();
+ char *buf_end = buf + instruction->write_avail();
+ int written = 0;
+
+ // Insert With Name Reference
+ buf[0] = 0x80;
+
+ // References static table or not
+ if (!dynamic) {
+ buf[0] |= 0x40;
+ }
+
+ // Name Index
+ int ret;
+ if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), index, 6)) <
+ 0) {
+ return ret;
+ }
+ written += ret;
+
+ // Value
+ if ((ret = xpack_encode_string(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), value, value_len,
+ 7)) < 0) {
+ return ret;
+ }
+ written += ret;
+
+ // Finalize and Schedule to send
+ instruction->fill(written);
+ this->_encoder_stream_sending_instructions->append_block(instruction);
+
+ return 0;
+}
+
+int
+QPACK::_write_insert_without_name_ref(const char *name, int name_len, const char *value, uint16_t value_len)
+{
+ IOBufferBlock *instruction = new_IOBufferBlock();
+ instruction->alloc(TS_IOBUFFER_SIZE_INDEX_2K);
+
+ char *buf = instruction->end();
+ char *buf_end = buf + instruction->write_avail();
+ int written = 0;
+
+ // Insert Without Name Reference
+ buf[0] = 0x40;
+
+ // Name
+ int ret;
+ if ((ret = xpack_encode_string(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), name, name_len,
+ 5)) < 0) {
+ return ret;
+ }
+ written += ret;
+
+ // Value
+ if ((ret = xpack_encode_string(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), value, value_len,
+ 7)) < 0) {
+ return ret;
+ }
+ written += ret;
+
+ // Finalize and Schedule to send
+ instruction->fill(written);
+ this->_encoder_stream_sending_instructions->append_block(instruction);
+
+ return 0;
+}
+
+int
+QPACK::_write_duplicate(uint16_t index)
+{
+ IOBufferBlock *instruction = new_IOBufferBlock();
+ instruction->alloc(TS_IOBUFFER_SIZE_INDEX_2K);
+
+ char *buf = instruction->end();
+ char *buf_end = buf + instruction->write_avail();
+ int written = 0;
+
+ // Index
+ int ret;
+ if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), index, 5)) <
+ 0) {
+ return ret;
+ }
+ written += ret;
+
+ // Finalize and Schedule to send
+ instruction->fill(written);
+ this->_encoder_stream_sending_instructions->append_block(instruction);
+
+ return 0;
+}
+
+int
+QPACK::_write_dynamic_table_size_update(uint16_t max_size)
+{
+ IOBufferBlock *instruction = new_IOBufferBlock();
+ instruction->alloc(TS_IOBUFFER_SIZE_INDEX_128);
+
+ char *buf = instruction->end();
+ char *buf_end = buf + instruction->write_avail();
+ int written = 0;
+
+ // Dynamic Table Size Update
+ buf[0] = 0x20;
+
+ // Max Size
+ int ret;
+ if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), max_size, 5)) <
+ 0) {
+ return ret;
+ }
+ written += ret;
+
+ // Finalize and Schedule to send
+ instruction->fill(written);
+ this->_encoder_stream_sending_instructions->append_block(instruction);
+
+ return 0;
+}
+
+int
+QPACK::_write_table_state_syncrhonize(uint16_t insert_count)
+{
+ IOBufferBlock *instruction = new_IOBufferBlock();
+ instruction->alloc(TS_IOBUFFER_SIZE_INDEX_128);
+
+ char *buf = instruction->end();
+ char *buf_end = buf + instruction->write_avail();
+ int written = 0;
+
+ // Insert Count
+ int ret;
+ if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), insert_count,
+ 6)) < 0) {
+ return ret;
+ }
+ written += ret;
+
+ // Finalize and Schedule to send
+ instruction->fill(written);
+ this->_encoder_stream_sending_instructions->append_block(instruction);
+
+ return 0;
+}
+
+int
+QPACK::_write_header_acknowledgement(uint64_t stream_id)
+{
+ IOBufferBlock *instruction = new_IOBufferBlock();
+ instruction->alloc(TS_IOBUFFER_SIZE_INDEX_128);
+
+ char *buf = instruction->end();
+ char *buf_end = buf + instruction->write_avail();
+ int written = 0;
+
+ // Header Acknowledgement
+ buf[0] = 0x80;
+
+ // Stream ID
+ int ret;
+ if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), stream_id, 7)) <
+ 0) {
+ return ret;
+ }
+ written += ret;
+
+ // Finalize and Schedule to send
+ instruction->fill(written);
+ this->_encoder_stream_sending_instructions->append_block(instruction);
+
+ return 0;
+}
+
+int
+QPACK::_write_stream_cancellation(uint64_t stream_id)
+{
+ IOBufferBlock *instruction = new_IOBufferBlock();
+ instruction->alloc(TS_IOBUFFER_SIZE_INDEX_128);
+
+ char *buf = instruction->end();
+ char *buf_end = buf + instruction->write_avail();
+ int written = 0;
+
+ // Stream Cancellation
+ buf[0] = 0x40;
+
+ // Stream ID
+ int ret;
+ if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), stream_id, 7)) <
+ 0) {
+ return ret;
+ }
+ written += ret;
+
+ // Finalize and Schedule to send
+ instruction->fill(written);
+ this->_encoder_stream_sending_instructions->append_block(instruction);
+
+ return 0;
+}
+
+int
+QPACK::_read_insert_with_name_ref(QUICStreamIO &stream_io, bool &is_static, uint16_t &index, Arena &arena, char **value,
+ uint16_t &value_len)
+{
+ size_t read_len = 0;
+ int ret;
+ uint8_t input[16384];
+ int input_len;
+ input_len = stream_io.peek(input, sizeof(input));
+
+ // S flag
+ is_static = input[0] & 0x40;
+
+ // Name Index
+ uint64_t tmp;
+ if ((ret = xpack_decode_integer(tmp, input, input + input_len, 6)) < 0 && tmp > 0xFFFF) {
+ return -1;
+ }
+ index = tmp;
+ read_len += ret;
+
+ // Value
+ if ((ret = xpack_decode_string(arena, value, tmp, input + read_len, input + input_len, 7)) < 0 && tmp > 0xFF) {
+ return -1;
+ }
+ value_len = tmp;
+ read_len += ret;
+
+ stream_io.consume(read_len);
+
+ return 0;
+}
+
+int
+QPACK::_read_insert_without_name_ref(QUICStreamIO &stream_io, Arena &arena, char **name, uint16_t &name_len, char **value,
+ uint16_t &value_len)
+{
+ size_t read_len = 0;
+ int ret;
+ uint8_t input[16384];
+ int input_len;
+ input_len = stream_io.peek(input, sizeof(input));
+
+ // Name
+ uint64_t tmp;
+ if ((ret = xpack_decode_string(arena, name, tmp, input, input + input_len, 5)) < 0 && tmp > 0xFFFF) {
+ return -1;
+ }
+ name_len = tmp;
+ read_len += ret;
+
+ // Value
+ if ((ret = xpack_decode_string(arena, value, tmp, input + read_len, input + input_len, 7)) < 0 && tmp > 0xFFFF) {
+ return -1;
+ }
+ value_len = tmp;
+ read_len += ret;
+
+ stream_io.consume(read_len);
+
+ return 0;
+}
+
+int
+QPACK::_read_duplicate(QUICStreamIO &stream_io, uint16_t &index)
+{
+ size_t read_len = 0;
+ int ret;
+ uint8_t input[16];
+ int input_len;
+ input_len = stream_io.peek(input, sizeof(input));
+
+ // Index
+ uint64_t tmp;
+ if ((ret = xpack_decode_integer(tmp, input, input + input_len, 5)) < 0 && tmp > 0xFFFF) {
+ return -1;
+ }
+ index = tmp;
+ read_len += ret;
+
+ stream_io.consume(read_len);
+
+ return 0;
+}
+
+int
+QPACK::_read_dynamic_table_size_update(QUICStreamIO &stream_io, uint16_t &max_size)
+{
+ size_t read_len = 0;
+ int ret;
+ uint8_t input[16];
+ int input_len;
+ input_len = stream_io.peek(input, sizeof(input));
+ uint64_t tmp;
+
+ // Max Size
+ if ((ret = xpack_decode_integer(tmp, input, input + input_len, 5)) < 0 && tmp > 0xFFFF) {
+ return -1;
+ }
+ max_size = tmp;
+ read_len += ret;
+
+ stream_io.consume(read_len);
+
+ return 0;
+}
+
+int
+QPACK::_read_table_state_synchronize(QUICStreamIO &stream_io, uint16_t &insert_count)
+{
+ size_t read_len = 0;
+ int ret;
+ uint8_t input[16];
+ int input_len;
+ input_len = stream_io.peek(input, sizeof(input));
+ uint64_t tmp;
+
+ // Insert Count
+ if ((ret = xpack_decode_integer(tmp, input, input + input_len, 6)) < 0 && tmp > 0xFFFF) {
+ return -1;
+ }
+ insert_count = tmp;
+ read_len += ret;
+
+ stream_io.consume(read_len);
+
+ return 0;
+}
+
+int
+QPACK::_read_header_acknowledgement(QUICStreamIO &stream_io, uint64_t &stream_id)
+{
+ size_t read_len = 0;
+ int ret;
+ uint8_t input[16];
+ int input_len;
+ input_len = stream_io.peek(input, sizeof(input));
+
+ // Stream ID
+ // FIXME xpack_decode_integer does not support uint64_t
+ if ((ret = xpack_decode_integer(stream_id, input, input + input_len, 7)) < 0) {
+ return -1;
+ }
+ read_len += ret;
+
+ stream_io.consume(read_len);
+
+ return 0;
+}
+
+int
+QPACK::_read_stream_cancellation(QUICStreamIO &stream_io, uint64_t &stream_id)
+{
+ size_t read_len = 0;
+ int ret;
+ uint8_t input[16];
+ int input_len;
+ input_len = stream_io.peek(input, sizeof(input));
+
+ // Stream ID
+ // FIXME xpack_decode_integer does not support uint64_t
+ if ((ret = xpack_decode_integer(stream_id, input, input + input_len, 6)) < 0) {
+ return -1;
+ }
+ read_len += ret;
+
+ stream_io.consume(read_len);
+
+ return 0;
+}
+
+//
+// DynamicTableStorage
+//
+
+QPACK::DynamicTableStorage::DynamicTableStorage(uint16_t size) : _head(size * 2 - 1), _tail(size * 2 - 1)
+{
+ this->_data_size = size * 2;
+ this->_data = reinterpret_cast<uint8_t *>(ats_malloc(this->_data_size));
+ this->_overwrite_threshold = size;
+}
+
+QPACK::DynamicTableStorage::~DynamicTableStorage()
+{
+ if (this->_data) {
+ ats_free(this->_data);
+ this->_data = nullptr;
+ }
+}
+
+void
+QPACK::DynamicTableStorage::read(uint16_t offset, const char **name, uint16_t name_len, const char **value, uint16_t value_len)
+{
+ *name = reinterpret_cast<const char *>(this->_data + offset);
+ *value = reinterpret_cast<const char *>(this->_data + offset + name_len);
+}
+
+uint16_t
+QPACK::DynamicTableStorage::write(const char *name, uint16_t name_len, const char *value, uint16_t value_len)
+{
+ uint16_t offset = (this->_head + 1) % this->_data_size;
+ memcpy(this->_data + offset, name, name_len);
+ memcpy(this->_data + offset + name_len, value, value_len);
+
+ this->_head = (this->_head + (name_len + value_len)) % this->_data_size;
+ if (this->_head > this->_overwrite_threshold) {
+ this->_head = 0;
+ }
+
+ return offset;
+}
+
+void
+QPACK::DynamicTableStorage::erase(uint16_t name_len, uint16_t value_len)
+{
+ this->_tail = (this->_tail + (name_len + value_len)) % this->_data_size;
+}
diff --git a/proxy/hq/QPACK.h b/proxy/hq/QPACK.h
new file mode 100644
index 0000000..53845ef
--- /dev/null
+++ b/proxy/hq/QPACK.h
@@ -0,0 +1,322 @@
+/** @file
+ *
+ * A brief file description
+ *
+ * @section license License
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+#pragma once
+
+#include "I_EventSystem.h"
+#include "I_Event.h"
+#include "ts/IntrusiveDList.h"
+#include "MIME.h"
+#include "QUICApplication.h"
+
+class HTTPHdr;
+
+enum {
+ QPACK_EVENT_DECODE_COMPLETE = QPACK_EVENT_EVENTS_START,
+ QPACK_EVENT_DECODE_FAILED,
+};
+
+// FIXME This setting should be passed by HQ
+constexpr int SETTINGS_HEADER_TABLE_SIZE = 4096;
+constexpr int SETTINGS_QPACK_BLOCKED_STREAMS = 100;
+
+class QPACK : public QUICApplication
+{
+public:
+ QPACK(QUICConnection *qc, size_t maximum_size);
+ virtual ~QPACK();
+
+ int event_handler(int event, Event *data);
+
+ /*
+ * header_block must have enough size to store all headers in header_set.
+ * The maximum size can be estimated with QPACK::estimate_header_block_size().
+ */
+ int encode(uint64_t stream_id, HTTPHdr &header_set, MIOBuffer *header_block);
+
+ /*
+ * This will emit either of two events below:
+ * - QPACK_EVENT_DECODE_COMPLETE (Data: *HTTPHdr)
+ * - QPACK_EVENT_DECODE_FAILED (Data: nullptr)
+ */
+ int decode(uint64_t stream_id, const uint8_t *header_block, size_t header_block_len, HTTPHdr &hdr, Continuation *cont,
+ EThread *thread = this_ethread());
+
+ int cancel(uint64_t stream_id);
+
+ static size_t estimate_header_block_size(const HTTPHdr &header_set);
+
+private:
+ struct LookupResult {
+ uint16_t index = 0;
+ enum MatchType { NONE, NAME, EXACT } match_type = MatchType::NONE;
+ };
+
+ struct Header {
+ Header(const char *n, const char *v) : name(n), value(v), name_len(strlen(name)), value_len(strlen(value)) {}
+ const char *name;
+ const char *value;
+ const int name_len;
+ const int value_len;
+ };
+
+ class StaticTable
+ {
+ public:
+ static const LookupResult lookup(uint16_t index, const char **name, int *name_len, const char **value, int *value_len);
+ static const LookupResult lookup(const char *name, int name_len, const char *value, int value_len);
+
+ private:
+ static const Header STATIC_HEADER_FIELDS[];
+ };
+
+ struct DynamicTableEntry {
+ uint16_t index = 0;
+ uint16_t offset = 0;
+ uint16_t name_len = 0;
+ uint16_t value_len = 0;
+ uint16_t ref_count = 0;
+ };
+
+ class DynamicTableStorage
+ {
+ public:
+ DynamicTableStorage(uint16_t size);
+ ~DynamicTableStorage();
+ void read(uint16_t offset, const char **name, uint16_t name_len, const char **value, uint16_t value_len);
+ uint16_t write(const char *name, uint16_t name_len, const char *value, uint16_t value_len);
+ void erase(uint16_t name_len, uint16_t value_len);
+
+ private:
+ uint16_t _overwrite_threshold = 0;
+ uint8_t *_data = nullptr;
+ uint16_t _data_size = 0;
+ uint16_t _head = 0;
+ uint16_t _tail = 0;
+ };
+
+ class DynamicTable
+ {
+ public:
+ DynamicTable(uint16_t size);
+ ~DynamicTable();
+
+ const LookupResult lookup(uint16_t index, const char **name, int *name_len, const char **value, int *value_len);
+ const LookupResult lookup(const char *name, int name_len, const char *value, int value_len);
+ const LookupResult insert_entry(bool is_static, uint16_t index, const char *value, uint16_t value_len);
+ const LookupResult insert_entry(const char *name, uint16_t name_len, const char *value, uint16_t value_len);
+ const LookupResult duplicate_entry(uint16_t current_index);
+ bool should_duplicate(uint16_t index);
+ void update_size(uint16_t max_size);
+ void ref_entry(uint16_t index);
+ void unref_entry(uint16_t index);
+ uint16_t largest_index();
+
+ private:
+ uint16_t _available = 0;
+ uint16_t _entries_inserted = 0;
+
+ // FIXME It may be better to split this array into small arrays to reduce memory footprint
+ struct DynamicTableEntry _entries[SETTINGS_HEADER_TABLE_SIZE] = {{}};
+ uint16_t _entries_head = sizeof(_entries) - 1;
+ uint16_t _entries_tail = sizeof(_entries) - 1;
+ DynamicTableStorage *_storage = nullptr;
+ };
+
+ class DecodeRequest
+ {
+ public:
+ DecodeRequest(uint16_t largest_reference, EThread *thread, Continuation *continuation, uint64_t stream_id,
+ const uint8_t *header_blcok, size_t header_block_len, HTTPHdr &hdr)
+ : _largest_reference(largest_reference),
+ _thread(thread),
+ _continuation(continuation),
+ _stream_id(stream_id),
+ _header_block(header_blcok),
+ _header_block_len(header_block_len),
+ _hdr(hdr)
+ {
+ }
+
+ uint16_t
+ largest_reference()
+ {
+ return this->_largest_reference;
+ }
+
+ EThread *
+ thread()
+ {
+ return this->_thread;
+ }
+
+ Continuation *
+ continuation()
+ {
+ return this->_continuation;
+ }
+
+ uint64_t
+ stream_id()
+ {
+ return this->_stream_id;
+ }
+
+ const uint8_t *
+ header_block()
+ {
+ return this->_header_block;
+ }
+
+ size_t
+ header_block_len()
+ {
+ return this->_header_block_len;
+ }
+
+ HTTPHdr &
+ hdr()
+ {
+ return this->_hdr;
+ }
+
+ class Linkage
+ {
+ public:
+ static DecodeRequest *&
+ next_ptr(DecodeRequest *t)
+ {
+ return *reinterpret_cast<DecodeRequest **>(&t->_next);
+ }
+ static DecodeRequest *&
+ prev_ptr(DecodeRequest *t)
+ {
+ return *reinterpret_cast<DecodeRequest **>(&t->_prev);
+ }
+ };
+
+ private:
+ uint16_t _largest_reference;
+ EThread *_thread;
+ Continuation *_continuation;
+ uint64_t _stream_id;
+ const uint8_t *_header_block;
+ size_t _header_block_len;
+ HTTPHdr &_hdr;
+
+ // For IntrusiveDList support
+ DecodeRequest *_next = nullptr;
+ DecodeRequest *_prev = nullptr;
+ };
+
+ struct EntryReference {
+ uint16_t smallest;
+ uint16_t largest;
+ };
+
+ DynamicTable _dynamic_table;
+ std::map<uint64_t, struct EntryReference> _references;
+
+ Continuation *_event_handler = nullptr;
+ void _resume_decode();
+ void _abort_decode();
+
+ bool _invalid = false;
+
+ IntrusiveDList<DecodeRequest::Linkage> _blocked_list;
+ bool _add_to_blocked_list(DecodeRequest *decode_request);
+
+ uint16_t _largest_known_received_index = 0;
+ void _update_largest_known_received_index_by_insert_count(uint16_t insert_count);
+ void _update_largest_known_received_index_by_stream_id(uint64_t stream_id);
+
+ void _update_reference_counts(uint64_t stream_id);
+
+ // Encoder Stream
+ int _read_insert_with_name_ref(QUICStreamIO &stream_io, bool &is_static, uint16_t &index, Arena &arena, char **value,
+ uint16_t &value_len);
+ int _read_insert_without_name_ref(QUICStreamIO &stream_io, Arena &arena, char **name, uint16_t &name_len, char **value,
+ uint16_t &value_len);
+ int _read_duplicate(QUICStreamIO &stream_io, uint16_t &index);
+ int _read_dynamic_table_size_update(QUICStreamIO &stream_io, uint16_t &max_size);
+ int _write_insert_with_name_ref(uint16_t index, bool dynamic, const char *value, uint16_t value_len);
+ int _write_insert_without_name_ref(const char *name, int name_len, const char *value, uint16_t value_len);
+ int _write_duplicate(uint16_t index);
+ int _write_dynamic_table_size_update(uint16_t max_size);
+
+ // Decoder Stream
+ int _read_table_state_synchronize(QUICStreamIO &stream_io, uint16_t &insert_count);
+ int _read_header_acknowledgement(QUICStreamIO &stream_io, uint64_t &stream_id);
+ int _read_stream_cancellation(QUICStreamIO &stream_io, uint64_t &stream_id);
+ int _write_table_state_syncrhonize(uint16_t insert_count);
+ int _write_header_acknowledgement(uint64_t stream_id);
+ int _write_stream_cancellation(uint64_t stream_id);
+
+ // Request and Push Streams
+ int _encode_prefix(uint16_t largest_reference, uint16_t base_index, IOBufferBlock *prefix);
+ int _encode_header(const MIMEField &field, uint16_t base_index, IOBufferBlock *compressed_header, uint16_t &referred_index);
+ int _encode_indexed_header_field(uint16_t index, uint16_t base_index, bool dynamic_table, IOBufferBlock *compressed_header);
+ int _encode_indexed_header_field_with_postbase_index(uint16_t index, uint16_t base_index, bool never_index,
+ IOBufferBlock *compressed_header);
+ int _encode_literal_header_field_with_name_ref(uint16_t index, bool dynamic_table, uint16_t base_index, const char *value,
+ int value_len, bool never_index, IOBufferBlock *compressed_header);
+ int _encode_literal_header_field_without_name_ref(const char *name, int name_len, const char *value, int value_len,
+ bool never_index, IOBufferBlock *compressed_header);
+ int _encode_literal_header_field_with_postbase_name_ref(uint16_t index, uint16_t base_index, const char *value, int value_len,
+ bool never_index, IOBufferBlock *compressed_header);
+
+ void _decode(EThread *ethread, Continuation *cont, uint64_t stream_id, const uint8_t *header_block, size_t header_block_len,
+ HTTPHdr &hdr);
+ int _decode_header(const uint8_t *header_block, size_t header_block_len, HTTPHdr &hdr);
+ int _decode_indexed_header_field(int16_t base_index, const uint8_t *buf, size_t buf_len, HTTPHdr &hdr);
+ int _decode_indexed_header_field_with_postbase_index(int16_t base_index, const uint8_t *buf, size_t buf_len, HTTPHdr &hdr);
+ int _decode_literal_header_field_with_name_ref(int16_t base_index, const uint8_t *buf, size_t buf_len, HTTPHdr &hdr);
+ int _decode_literal_header_field_without_name_ref(const uint8_t *buf, size_t buf_len, HTTPHdr &hdr);
+ int _decode_literal_header_field_with_postbase_name_ref(int16_t base_index, const uint8_t *buf, size_t buf_len, HTTPHdr &hdr);
+
+ // Utilities
+ uint16_t _calc_absolute_index_from_relative_index(uint16_t base_index, uint16_t relative_index);
+ uint16_t _calc_absolute_index_from_postbase_index(uint16_t base_index, uint16_t postbase_index);
+ uint16_t _calc_relative_index_from_absolute_index(uint16_t base_index, uint16_t absolute_index);
+ uint16_t _calc_postbase_index_from_absolute_index(uint16_t base_index, uint16_t absolute_index);
+ void _attach_header(HTTPHdr &hdr, const char *name, int name_len, const char *value, int value_len);
+
+ int _on_read_ready(QUICStreamIO &stream_io);
+ int _on_decoder_stream_read_ready(QUICStreamIO &stream_io);
+ int _on_encoder_stream_read_ready(QUICStreamIO &stream_io);
+
+ int _on_write_ready(QUICStreamIO &stream_io);
+ int _on_decoder_write_ready(QUICStreamIO &stream_io);
+ int _on_encoder_write_ready(QUICStreamIO &stream_io);
+
+ // Stream numbers
+ // FIXME How are these stream ids negotiated? In interop, encoder stream id have to be 0 and decoder stream id must not be used.
+ uint64_t _encoder_stream_id = 0;
+ uint64_t _decoder_stream_id = 9999;
+
+ // Chain of sending instructions
+ MIOBuffer *_encoder_stream_sending_instructions;
+ MIOBuffer *_decoder_stream_sending_instructions;
+ IOBufferReader *_encoder_stream_sending_instructions_reader;
+ IOBufferReader *_decoder_stream_sending_instructions_reader;
+};
[trafficserver] 02/05: Separate out some of HPACK logic into hdrs/
Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
maskit pushed a commit to branch quic-latest
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
commit 062b8f481c5fe176f3fc73f986a275da7e3a4318
Author: Masakazu Kitajo <ma...@apache.org>
AuthorDate: Mon Jul 23 10:17:58 2018 +0900
Separate out some of HPACK logic into hdrs/
Primitive representations and Huffman encoding can be shared with HPACK and QPACK.
---
.gitignore | 2 +-
proxy/{http2 => hdrs}/HuffmanCodec.cc | 0
proxy/{http2 => hdrs}/HuffmanCodec.h | 0
proxy/hdrs/Makefile.am | 23 +++-
proxy/hdrs/XPACK.cc | 193 +++++++++++++++++++++++++++
proxy/hdrs/XPACK.h | 36 +++++
proxy/{http2 => hdrs}/test_Huffmancode.cc | 0
proxy/http2/HPACK.cc | 212 ++++--------------------------
proxy/http2/HPACK.h | 5 +-
proxy/http2/HTTP2.cc | 1 -
proxy/http2/Makefile.am | 16 +--
proxy/http2/RegressionHPACK.cc | 18 +--
12 files changed, 285 insertions(+), 221 deletions(-)
diff --git a/.gitignore b/.gitignore
index 33e0c16..a97b112 100644
--- a/.gitignore
+++ b/.gitignore
@@ -122,8 +122,8 @@ iocore/eventsystem/test_MIOBufferWriter
iocore/hostdb/test_RefCountCache
proxy/hdrs/test_mime
+proxy/hdrs/test_Huffmancode
proxy/http/test_ForwardedConfig
-proxy/http2/test_Huffmancode
proxy/http2/test_Http2DependencyTree
proxy/http2/test_HPACK
proxy/http2/hpack-tests/results
diff --git a/proxy/http2/HuffmanCodec.cc b/proxy/hdrs/HuffmanCodec.cc
similarity index 100%
rename from proxy/http2/HuffmanCodec.cc
rename to proxy/hdrs/HuffmanCodec.cc
diff --git a/proxy/http2/HuffmanCodec.h b/proxy/hdrs/HuffmanCodec.h
similarity index 100%
rename from proxy/http2/HuffmanCodec.h
rename to proxy/hdrs/HuffmanCodec.h
diff --git a/proxy/hdrs/Makefile.am b/proxy/hdrs/Makefile.am
index f2e5c27..930962b 100644
--- a/proxy/hdrs/Makefile.am
+++ b/proxy/hdrs/Makefile.am
@@ -43,7 +43,11 @@ libhdrs_a_SOURCES = \
MIME.cc \
MIME.h \
URL.cc \
- URL.h
+ URL.h \
+ HuffmanCodec.cc \
+ HuffmanCodec.h \
+ XPACK.cc \
+ XPACK.h
if BUILD_TESTS
libhdrs_a_SOURCES += \
@@ -61,9 +65,13 @@ load_http_hdr_LDADD = -L. -lhdrs \
$(top_builddir)/lib/ts/libtsutil.la \
@LIBTCL@
-check_PROGRAMS = test_mime
+check_PROGRAMS = \
+ test_mime \
+ test_Huffmancode
-TESTS = test_mime
+TESTS = \
+ test_mime \
+ test_Huffmancode
test_mime_LDADD = -L. -lhdrs \
$(top_builddir)/lib/ts/libtsutil.la \
@@ -76,6 +84,15 @@ test_mime_LDADD = -L. -lhdrs \
test_mime_SOURCES = test_mime.cc
+test_Huffmancode_LDADD = \
+ $(top_builddir)/lib/ts/libtsutil.la
+
+test_Huffmancode_SOURCES = \
+ test_Huffmancode.cc \
+ HuffmanCodec.cc \
+ HuffmanCodec.h
+
+
#test_UNUSED_SOURCES = \
# test_urlhash.cc
diff --git a/proxy/hdrs/XPACK.cc b/proxy/hdrs/XPACK.cc
new file mode 100644
index 0000000..c537872
--- /dev/null
+++ b/proxy/hdrs/XPACK.cc
@@ -0,0 +1,193 @@
+/** @file
+
+ Common functions for HPACK and QPACK
+
+ @section license License
+
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you 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.
+ */
+
+#include "ts/Arena.h"
+#include "ts/ink_memory.h"
+#include "XPACK.h"
+#include "HuffmanCodec.h"
+
+//
+// [RFC 7541] 5.1. Integer representation
+//
+int64_t
+xpack_decode_integer(uint64_t &dst, const uint8_t *buf_start, const uint8_t *buf_end, uint8_t n)
+{
+ if (buf_start >= buf_end) {
+ return XPACK_ERROR_COMPRESSION_ERROR;
+ }
+
+ const uint8_t *p = buf_start;
+
+ dst = (*p & ((1 << n) - 1));
+ if (dst == static_cast<uint64_t>(1 << n) - 1) {
+ int m = 0;
+ do {
+ if (++p >= buf_end) {
+ return XPACK_ERROR_COMPRESSION_ERROR;
+ }
+
+ uint64_t added_value = *p & 0x7f;
+ if ((UINT64_MAX >> m) < added_value) {
+ // Excessively large integer encodings - in value or octet
+ // length - MUST be treated as a decoding error.
+ return XPACK_ERROR_COMPRESSION_ERROR;
+ }
+ dst += added_value << m;
+ m += 7;
+ } while (*p & 0x80);
+ }
+
+ return p - buf_start + 1;
+}
+
+//
+// [RFC 7541] 5.2. String Literal Representation
+// return content from String Data (Length octets) with huffman decoding if it is encoded
+//
+int64_t
+xpack_decode_string(Arena &arena, char **str, uint64_t &str_length, const uint8_t *buf_start, const uint8_t *buf_end, uint8_t n)
+{
+ if (buf_start >= buf_end) {
+ return XPACK_ERROR_COMPRESSION_ERROR;
+ }
+
+ const uint8_t *p = buf_start;
+ bool isHuffman = *p & (0x01 << n);
+ uint64_t encoded_string_len = 0;
+ int64_t len = 0;
+
+ len = xpack_decode_integer(encoded_string_len, p, buf_end, n);
+ if (len == XPACK_ERROR_COMPRESSION_ERROR) {
+ return XPACK_ERROR_COMPRESSION_ERROR;
+ }
+ p += len;
+
+ if ((p + encoded_string_len) > buf_end) {
+ return XPACK_ERROR_COMPRESSION_ERROR;
+ }
+
+ if (isHuffman) {
+ // Allocate temporary area twice the size of before decoded data
+ *str = arena.str_alloc(encoded_string_len * 2);
+
+ len = huffman_decode(*str, p, encoded_string_len);
+ if (len < 0) {
+ return XPACK_ERROR_COMPRESSION_ERROR;
+ }
+ str_length = len;
+ } else {
+ *str = arena.str_alloc(encoded_string_len);
+
+ memcpy(*str, reinterpret_cast<const char *>(p), encoded_string_len);
+
+ str_length = encoded_string_len;
+ }
+
+ return p + encoded_string_len - buf_start;
+}
+
+//
+// [RFC 7541] 5.1. Integer representation
+//
+int64_t
+xpack_encode_integer(uint8_t *buf_start, const uint8_t *buf_end, uint64_t value, uint8_t n)
+{
+ if (buf_start >= buf_end) {
+ return -1;
+ }
+
+ uint8_t *p = buf_start;
+
+ if (value < (static_cast<uint64_t>(1 << n) - 1)) {
+ *(p++) = value;
+ } else {
+ *(p++) = (1 << n) - 1;
+ value -= (1 << n) - 1;
+ while (value >= 128) {
+ if (p >= buf_end) {
+ return -1;
+ }
+ *(p++) = (value & 0x7F) | 0x80;
+ value = value >> 7;
+ }
+ if (p + 1 >= buf_end) {
+ return -1;
+ }
+ *(p++) = value;
+ }
+ return p - buf_start;
+}
+
+int64_t
+xpack_encode_string(uint8_t *buf_start, const uint8_t *buf_end, const char *value, uint64_t value_len, uint8_t n)
+{
+ uint8_t *p = buf_start;
+ bool use_huffman = true;
+ char *data = nullptr;
+ int64_t data_len = 0;
+
+ // TODO Choose whether to use Huffman encoding wisely
+
+ if (use_huffman && value_len) {
+ data = static_cast<char *>(ats_malloc(value_len * 4));
+ if (data == nullptr) {
+ return -1;
+ }
+ data_len = huffman_encode(reinterpret_cast<uint8_t *>(data), reinterpret_cast<const uint8_t *>(value), value_len);
+ }
+
+ // Length
+ const int64_t len = xpack_encode_integer(p, buf_end, data_len, n);
+ if (len == -1) {
+ if (use_huffman) {
+ ats_free(data);
+ }
+
+ return -1;
+ }
+
+ if (use_huffman) {
+ *p |= 0x01 << n;
+ }
+ p += len;
+
+ if (buf_end < p || buf_end - p < data_len) {
+ if (use_huffman) {
+ ats_free(data);
+ }
+
+ return -1;
+ }
+
+ // Value
+ if (data_len) {
+ memcpy(p, data, data_len);
+ p += data_len;
+ }
+
+ if (use_huffman) {
+ ats_free(data);
+ }
+
+ return p - buf_start;
+}
diff --git a/proxy/hdrs/XPACK.h b/proxy/hdrs/XPACK.h
new file mode 100644
index 0000000..5e8eca3
--- /dev/null
+++ b/proxy/hdrs/XPACK.h
@@ -0,0 +1,36 @@
+/** @file
+ *
+ * A brief file description
+ *
+ * @section license License
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+#pragma once
+
+#include <cstdint>
+
+const static int XPACK_ERROR_COMPRESSION_ERROR = -1;
+const static int XPACK_ERROR_SIZE_EXCEEDED_ERROR = -2;
+
+// These primitives are shared with HPACK and QPACK.
+int64_t xpack_encode_integer(uint8_t *buf_start, const uint8_t *buf_end, uint64_t value, uint8_t n);
+int64_t xpack_decode_integer(uint64_t &dst, const uint8_t *buf_start, const uint8_t *buf_end, uint8_t n);
+int64_t xpack_encode_string(uint8_t *buf_start, const uint8_t *buf_end, const char *value, uint64_t value_len, uint8_t n = 7);
+int64_t xpack_decode_string(Arena &arena, char **str, uint64_t &str_length, const uint8_t *buf_start, const uint8_t *buf_end,
+ uint8_t n = 7);
diff --git a/proxy/http2/test_Huffmancode.cc b/proxy/hdrs/test_Huffmancode.cc
similarity index 100%
rename from proxy/http2/test_Huffmancode.cc
rename to proxy/hdrs/test_Huffmancode.cc
diff --git a/proxy/http2/HPACK.cc b/proxy/http2/HPACK.cc
index 1c45d1e..fd16460 100644
--- a/proxy/http2/HPACK.cc
+++ b/proxy/http2/HPACK.cc
@@ -409,92 +409,6 @@ HpackDynamicTable::length() const
return _headers.size();
}
-//
-// [RFC 7541] 5.1. Integer representation
-//
-int64_t
-encode_integer(uint8_t *buf_start, const uint8_t *buf_end, uint32_t value, uint8_t n)
-{
- if (buf_start >= buf_end) {
- return -1;
- }
-
- uint8_t *p = buf_start;
-
- if (value < (static_cast<uint32_t>(1 << n) - 1)) {
- *(p++) = value;
- } else {
- *(p++) = (1 << n) - 1;
- value -= (1 << n) - 1;
- while (value >= 128) {
- if (p >= buf_end) {
- return -1;
- }
- *(p++) = (value & 0x7F) | 0x80;
- value = value >> 7;
- }
- if (p + 1 >= buf_end) {
- return -1;
- }
- *(p++) = value;
- }
- return p - buf_start;
-}
-
-int64_t
-encode_string(uint8_t *buf_start, const uint8_t *buf_end, const char *value, size_t value_len)
-{
- uint8_t *p = buf_start;
- bool use_huffman = true;
- char *data = nullptr;
- int64_t data_len = 0;
-
- // TODO Choose whether to use Huffman encoding wisely
-
- if (use_huffman && value_len) {
- data = static_cast<char *>(ats_malloc(value_len * 4));
- if (data == nullptr) {
- return -1;
- }
- data_len = huffman_encode(reinterpret_cast<uint8_t *>(data), reinterpret_cast<const uint8_t *>(value), value_len);
- }
-
- // Length
- const int64_t len = encode_integer(p, buf_end, data_len, 7);
- if (len == -1) {
- if (use_huffman) {
- ats_free(data);
- }
-
- return -1;
- }
-
- if (use_huffman) {
- *p |= 0x80;
- }
- p += len;
-
- if (buf_end < p || buf_end - p < data_len) {
- if (use_huffman) {
- ats_free(data);
- }
-
- return -1;
- }
-
- // Value
- if (data_len) {
- memcpy(p, data, data_len);
- p += data_len;
- }
-
- if (use_huffman) {
- ats_free(data);
- }
-
- return p - buf_start;
-}
-
int64_t
encode_indexed_header_field(uint8_t *buf_start, const uint8_t *buf_end, uint32_t index)
{
@@ -505,7 +419,7 @@ encode_indexed_header_field(uint8_t *buf_start, const uint8_t *buf_end, uint32_t
uint8_t *p = buf_start;
// Index
- const int64_t len = encode_integer(p, buf_end, index, 7);
+ const int64_t len = xpack_encode_integer(p, buf_end, index, 7);
if (len == -1) {
return -1;
}
@@ -551,7 +465,7 @@ encode_literal_header_field_with_indexed_name(uint8_t *buf_start, const uint8_t
}
// Index
- len = encode_integer(p, buf_end, index, prefix);
+ len = xpack_encode_integer(p, buf_end, index, prefix);
if (len == -1) {
return -1;
}
@@ -566,7 +480,7 @@ encode_literal_header_field_with_indexed_name(uint8_t *buf_start, const uint8_t
// Value String
int value_len;
const char *value = header.value_get(&value_len);
- len = encode_string(p, buf_end, value, value_len);
+ len = xpack_encode_string(p, buf_end, value, value_len);
if (len == -1) {
return -1;
}
@@ -616,7 +530,7 @@ encode_literal_header_field_with_new_name(uint8_t *buf_start, const uint8_t *buf
}
// Name String
- len = encode_string(p, buf_end, lower_name, name_len);
+ len = xpack_encode_string(p, buf_end, lower_name, name_len);
if (len == -1) {
return -1;
}
@@ -625,7 +539,7 @@ encode_literal_header_field_with_new_name(uint8_t *buf_start, const uint8_t *buf
// Value String
int value_len;
const char *value = header.value_get(&value_len);
- len = encode_string(p, buf_end, value, value_len);
+ len = xpack_encode_string(p, buf_end, value, value_len);
if (len == -1) {
return -1;
}
@@ -639,7 +553,7 @@ encode_literal_header_field_with_new_name(uint8_t *buf_start, const uint8_t *buf
int64_t
encode_dynamic_table_size_update(uint8_t *buf_start, const uint8_t *buf_end, uint32_t size)
{
- const int64_t len = encode_integer(buf_start, buf_end, size, 5);
+ const int64_t len = xpack_encode_integer(buf_start, buf_end, size, 5);
if (len == -1) {
return -1;
}
@@ -649,97 +563,17 @@ encode_dynamic_table_size_update(uint8_t *buf_start, const uint8_t *buf_end, uin
}
//
-// [RFC 7541] 5.1. Integer representation
-//
-int64_t
-decode_integer(uint32_t &dst, const uint8_t *buf_start, const uint8_t *buf_end, uint8_t n)
-{
- if (buf_start >= buf_end) {
- return HPACK_ERROR_COMPRESSION_ERROR;
- }
-
- const uint8_t *p = buf_start;
-
- dst = (*p & ((1 << n) - 1));
- if (dst == static_cast<uint32_t>(1 << n) - 1) {
- int m = 0;
- do {
- if (++p >= buf_end) {
- return HPACK_ERROR_COMPRESSION_ERROR;
- }
-
- uint32_t added_value = *p & 0x7f;
- if ((UINT32_MAX >> m) < added_value) {
- // Excessively large integer encodings - in value or octet
- // length - MUST be treated as a decoding error.
- return HPACK_ERROR_COMPRESSION_ERROR;
- }
- dst += added_value << m;
- m += 7;
- } while (*p & 0x80);
- }
-
- return p - buf_start + 1;
-}
-
-//
-// [RFC 7541] 5.2. String Literal Representation
-// return content from String Data (Length octets) with huffman decoding if it is encoded
-//
-int64_t
-decode_string(Arena &arena, char **str, uint32_t &str_length, const uint8_t *buf_start, const uint8_t *buf_end)
-{
- if (buf_start >= buf_end) {
- return HPACK_ERROR_COMPRESSION_ERROR;
- }
-
- const uint8_t *p = buf_start;
- bool isHuffman = *p & 0x80;
- uint32_t encoded_string_len = 0;
- int64_t len = 0;
-
- len = decode_integer(encoded_string_len, p, buf_end, 7);
- if (len == HPACK_ERROR_COMPRESSION_ERROR) {
- return HPACK_ERROR_COMPRESSION_ERROR;
- }
- p += len;
-
- if ((p + encoded_string_len) > buf_end) {
- return HPACK_ERROR_COMPRESSION_ERROR;
- }
-
- if (isHuffman) {
- // Allocate temporary area twice the size of before decoded data
- *str = arena.str_alloc(encoded_string_len * 2);
-
- len = huffman_decode(*str, p, encoded_string_len);
- if (len < 0) {
- return HPACK_ERROR_COMPRESSION_ERROR;
- }
- str_length = len;
- } else {
- *str = arena.str_alloc(encoded_string_len);
-
- memcpy(*str, reinterpret_cast<const char *>(p), encoded_string_len);
-
- str_length = encoded_string_len;
- }
-
- return p + encoded_string_len - buf_start;
-}
-
-//
// [RFC 7541] 6.1. Indexed Header Field Representation
//
int64_t
decode_indexed_header_field(MIMEFieldWrapper &header, const uint8_t *buf_start, const uint8_t *buf_end,
HpackIndexingTable &indexing_table)
{
- uint32_t index = 0;
+ uint64_t index = 0;
int64_t len = 0;
- len = decode_integer(index, buf_start, buf_end, 7);
- if (len == HPACK_ERROR_COMPRESSION_ERROR) {
+ len = xpack_decode_integer(index, buf_start, buf_end, 7);
+ if (len == XPACK_ERROR_COMPRESSION_ERROR) {
return HPACK_ERROR_COMPRESSION_ERROR;
}
@@ -771,22 +605,22 @@ decode_literal_header_field(MIMEFieldWrapper &header, const uint8_t *buf_start,
{
const uint8_t *p = buf_start;
bool isIncremental = false;
- uint32_t index = 0;
+ uint64_t index = 0;
int64_t len = 0;
HpackField ftype = hpack_parse_field_type(*p);
bool has_http2_violation = false;
if (ftype == HpackField::INDEXED_LITERAL) {
- len = decode_integer(index, p, buf_end, 6);
+ len = xpack_decode_integer(index, p, buf_end, 6);
isIncremental = true;
} else if (ftype == HpackField::NEVERINDEX_LITERAL) {
- len = decode_integer(index, p, buf_end, 4);
+ len = xpack_decode_integer(index, p, buf_end, 4);
} else {
ink_assert(ftype == HpackField::NOINDEX_LITERAL);
- len = decode_integer(index, p, buf_end, 4);
+ len = xpack_decode_integer(index, p, buf_end, 4);
}
- if (len == HPACK_ERROR_COMPRESSION_ERROR) {
+ if (len == XPACK_ERROR_COMPRESSION_ERROR) {
return HPACK_ERROR_COMPRESSION_ERROR;
}
@@ -799,10 +633,10 @@ decode_literal_header_field(MIMEFieldWrapper &header, const uint8_t *buf_start,
indexing_table.get_header_field(index, header);
} else {
char *name_str = nullptr;
- uint32_t name_str_len = 0;
+ uint64_t name_str_len = 0;
- len = decode_string(arena, &name_str, name_str_len, p, buf_end);
- if (len == HPACK_ERROR_COMPRESSION_ERROR) {
+ len = xpack_decode_string(arena, &name_str, name_str_len, p, buf_end);
+ if (len == XPACK_ERROR_COMPRESSION_ERROR) {
return HPACK_ERROR_COMPRESSION_ERROR;
}
@@ -821,10 +655,10 @@ decode_literal_header_field(MIMEFieldWrapper &header, const uint8_t *buf_start,
// Decode header field value
char *value_str = nullptr;
- uint32_t value_str_len = 0;
+ uint64_t value_str_len = 0;
- len = decode_string(arena, &value_str, value_str_len, p, buf_end);
- if (len == HPACK_ERROR_COMPRESSION_ERROR) {
+ len = xpack_decode_string(arena, &value_str, value_str_len, p, buf_end);
+ if (len == XPACK_ERROR_COMPRESSION_ERROR) {
return HPACK_ERROR_COMPRESSION_ERROR;
}
@@ -867,9 +701,9 @@ update_dynamic_table_size(const uint8_t *buf_start, const uint8_t *buf_end, Hpac
}
// Update header table size if its required.
- uint32_t size = 0;
- int64_t len = decode_integer(size, buf_start, buf_end, 5);
- if (len == HPACK_ERROR_COMPRESSION_ERROR) {
+ uint64_t size = 0;
+ int64_t len = xpack_decode_integer(size, buf_start, buf_end, 5);
+ if (len == XPACK_ERROR_COMPRESSION_ERROR) {
return HPACK_ERROR_COMPRESSION_ERROR;
}
diff --git a/proxy/http2/HPACK.h b/proxy/http2/HPACK.h
index c20f40f..d581749 100644
--- a/proxy/http2/HPACK.h
+++ b/proxy/http2/HPACK.h
@@ -26,6 +26,7 @@
#include "ts/ink_platform.h"
#include "ts/Diags.h"
#include "HTTP.h"
+#include "../hdrs/XPACK.h"
#include <vector>
@@ -156,10 +157,6 @@ private:
};
// Low level interfaces
-int64_t encode_integer(uint8_t *buf_start, const uint8_t *buf_end, uint32_t value, uint8_t n);
-int64_t decode_integer(uint32_t &dst, const uint8_t *buf_start, const uint8_t *buf_end, uint8_t n);
-int64_t encode_string(uint8_t *buf_start, const uint8_t *buf_end, const char *value, size_t value_len);
-int64_t decode_string(Arena &arena, char **str, uint32_t &str_length, const uint8_t *buf_start, const uint8_t *buf_end);
int64_t encode_indexed_header_field(uint8_t *buf_start, const uint8_t *buf_end, uint32_t index);
int64_t encode_literal_header_field_with_indexed_name(uint8_t *buf_start, const uint8_t *buf_end, const MIMEFieldWrapper &header,
uint32_t index, HpackIndexingTable &indexing_table, HpackField type);
diff --git a/proxy/http2/HTTP2.cc b/proxy/http2/HTTP2.cc
index 4327e71..5aa80d2 100644
--- a/proxy/http2/HTTP2.cc
+++ b/proxy/http2/HTTP2.cc
@@ -23,7 +23,6 @@
#include "HTTP2.h"
#include "HPACK.h"
-#include "HuffmanCodec.h"
#include "ts/ink_assert.h"
#include "P_RecCore.h"
#include "P_RecProcess.h"
diff --git a/proxy/http2/Makefile.am b/proxy/http2/Makefile.am
index 67e2f5b..747aee8 100644
--- a/proxy/http2/Makefile.am
+++ b/proxy/http2/Makefile.am
@@ -48,9 +48,7 @@ libhttp2_a_SOURCES = \
Http2Stream.cc \
Http2Stream.h \
Http2SessionAccept.cc \
- Http2SessionAccept.h \
- HuffmanCodec.cc \
- HuffmanCodec.h
+ Http2SessionAccept.h
if BUILD_TESTS
libhttp2_a_SOURCES += \
@@ -58,23 +56,13 @@ libhttp2_a_SOURCES += \
endif
check_PROGRAMS = \
- test_Huffmancode \
test_Http2DependencyTree \
test_HPACK
TESTS = \
- test_Huffmancode \
test_Http2DependencyTree \
test_HPACK
-test_Huffmancode_LDADD = \
- $(top_builddir)/lib/ts/libtsutil.la
-
-test_Huffmancode_SOURCES = \
- test_Huffmancode.cc \
- HuffmanCodec.cc \
- HuffmanCodec.h
-
test_Http2DependencyTree_LDADD = \
$(top_builddir)/lib/ts/libtsutil.la
@@ -94,8 +82,6 @@ test_HPACK_LDADD = \
test_HPACK_SOURCES = \
test_HPACK.cc \
- HuffmanCodec.cc \
- HuffmanCodec.h \
HPACK.cc \
HPACK.h
diff --git a/proxy/http2/RegressionHPACK.cc b/proxy/http2/RegressionHPACK.cc
index 399df34..d074561 100644
--- a/proxy/http2/RegressionHPACK.cc
+++ b/proxy/http2/RegressionHPACK.cc
@@ -326,7 +326,7 @@ REGRESSION_TEST(HPACK_EncodeInteger)(RegressionTest *t, int, int *pstatus)
for (const auto &i : integer_test_case) {
memset(buf, 0, BUFSIZE_FOR_REGRESSION_TEST);
- int len = encode_integer(buf, buf + BUFSIZE_FOR_REGRESSION_TEST, i.raw_integer, i.prefix);
+ int len = xpack_encode_integer(buf, buf + BUFSIZE_FOR_REGRESSION_TEST, i.raw_integer, i.prefix);
box.check(len == i.encoded_field_len, "encoded length was %d, expecting %d", len, i.encoded_field_len);
box.check(len > 0 && memcmp(buf, i.encoded_field, len) == 0, "encoded value was invalid");
@@ -345,7 +345,8 @@ REGRESSION_TEST(HPACK_EncodeString)(RegressionTest *t, int, int *pstatus)
for (unsigned int i = 2; i < sizeof(string_test_case) / sizeof(string_test_case[0]); i++) {
memset(buf, 0, BUFSIZE_FOR_REGRESSION_TEST);
- len = encode_string(buf, buf + BUFSIZE_FOR_REGRESSION_TEST, string_test_case[i].raw_string, string_test_case[i].raw_string_len);
+ len = xpack_encode_string(buf, buf + BUFSIZE_FOR_REGRESSION_TEST, string_test_case[i].raw_string,
+ string_test_case[i].raw_string_len);
box.check(len == string_test_case[i].encoded_field_len, "encoded length was %d, expecting %d", len,
string_test_case[i].encoded_field_len);
@@ -471,13 +472,13 @@ REGRESSION_TEST(HPACK_DecodeInteger)(RegressionTest *t, int, int *pstatus)
TestBox box(t, pstatus);
box = REGRESSION_TEST_PASSED;
- uint32_t actual;
+ uint64_t actual;
for (const auto &i : integer_test_case) {
- int len = decode_integer(actual, i.encoded_field, i.encoded_field + i.encoded_field_len, i.prefix);
+ int len = xpack_decode_integer(actual, i.encoded_field, i.encoded_field + i.encoded_field_len, i.prefix);
box.check(len == i.encoded_field_len, "decoded length was %d, expecting %d", len, i.encoded_field_len);
- box.check(actual == i.raw_integer, "decoded value was %d, expected %d", actual, i.raw_integer);
+ box.check(actual == i.raw_integer, "decoded value was %" PRIu64 ", expected %d", actual, i.raw_integer);
}
}
@@ -488,15 +489,16 @@ REGRESSION_TEST(HPACK_DecodeString)(RegressionTest *t, int, int *pstatus)
Arena arena;
char *actual = nullptr;
- uint32_t actual_len = 0;
+ uint64_t actual_len = 0;
hpack_huffman_init();
for (const auto &i : string_test_case) {
- int len = decode_string(arena, &actual, actual_len, i.encoded_field, i.encoded_field + i.encoded_field_len);
+ int len = xpack_decode_string(arena, &actual, actual_len, i.encoded_field, i.encoded_field + i.encoded_field_len);
box.check(len == i.encoded_field_len, "decoded length was %d, expecting %d", len, i.encoded_field_len);
- box.check(actual_len == i.raw_string_len, "length of decoded string was %d, expecting %d", actual_len, i.raw_string_len);
+ box.check(actual_len == i.raw_string_len, "length of decoded string was %" PRIu64 ", expecting %d", actual_len,
+ i.raw_string_len);
box.check(memcmp(actual, i.raw_string, actual_len) == 0, "decoded string was invalid");
}
}
[trafficserver] 05/05: Add tests for QPACK
Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
maskit pushed a commit to branch quic-latest
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
commit 4c0dc178fa412e48dc0a043d0e97df88986a0e4a
Author: Masakazu Kitajo <ma...@apache.org>
AuthorDate: Fri Jul 27 12:23:49 2018 +0900
Add tests for QPACK
---
proxy/hq/Makefile.am | 31 +++-
proxy/hq/test/main_qpack.cc | 100 ++++++++++
proxy/hq/test/stub.cc | 88 +++++++++
proxy/hq/test/test_QPACK.cc | 433 ++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 651 insertions(+), 1 deletion(-)
diff --git a/proxy/hq/Makefile.am b/proxy/hq/Makefile.am
index c8dac9a..0afc84f 100644
--- a/proxy/hq/Makefile.am
+++ b/proxy/hq/Makefile.am
@@ -52,7 +52,8 @@ libhq_a_SOURCES = \
# Check Programs
#
check_PROGRAMS = \
- test_libhq
+ test_libhq \
+ test_qpack
TESTS = $(check_PROGRAMS)
@@ -73,6 +74,34 @@ test_libhq_LDADD = \
$(top_builddir)/iocore/net/quic/libquic.a \
$(top_builddir)/lib/ts/libtsutil.la
+test_qpack_CPPFLAGS = \
+ $(AM_CPPFLAGS) \
+ -I$(abs_top_srcdir)/tests/include
+
+test_qpack_LDFLAGS = \
+ @AM_LDFLAGS@
+
+test_qpack_SOURCES = \
+ ./test/main_qpack.cc \
+ ./test/test_HQFrame.cc \
+ ./test/test_HQFrameDispatcher.cc \
+ ./test/test_QPACK.cc \
+ $(top_builddir)/proxy/http/HttpConfig.cc \
+ $(top_builddir)/proxy/http/HttpConnectionCount.cc \
+ $(top_builddir)/proxy/http/ForwardedConfig.cc \
+ ./test/stub.cc
+
+test_qpack_LDADD = \
+ libhq.a \
+ $(top_builddir)/mgmt/libmgmt_p.la \
+ $(top_builddir)/iocore/net/quic/libquic.a \
+ $(top_builddir)/iocore/eventsystem/libinkevent.a \
+ $(top_builddir)/lib/ts/libtsutil.la \
+ $(top_builddir)/lib/records/librecords_p.a \
+ $(top_builddir)/proxy/libproxy.a \
+ $(top_builddir)/proxy/hdrs/libhdrs.a \
+ $(top_builddir)/proxy/shared/libUglyLogStubs.a
+
#
# clang-tidy
#
diff --git a/proxy/hq/test/main_qpack.cc b/proxy/hq/test/main_qpack.cc
new file mode 100644
index 0000000..b5c7380
--- /dev/null
+++ b/proxy/hq/test/main_qpack.cc
@@ -0,0 +1,100 @@
+/** @file
+ *
+ * A brief file description
+ *
+ * @section license License
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+// To make compile faster
+// https://github.com/philsquared/Catch/blob/master/docs/slow-compiles.md
+// #define CATCH_CONFIG_MAIN
+#define CATCH_CONFIG_RUNNER
+#include "catch.hpp"
+
+#include "ts/I_Layout.h"
+#include "ts/Diags.h"
+
+#include "I_EventSystem.h"
+#include "RecordsConfig.h"
+
+#include "QUICConfig.h"
+#include "HuffmanCodec.h"
+#include "QPACK.h"
+#include "HTTP.h"
+
+#define TEST_THREADS 1
+
+char qifdir[256] = "test/qif/";
+int tablesize = 4096;
+int streams = 100;
+int ackmode = 0;
+char appname[256] = "ats";
+
+struct EventProcessorListener : Catch::TestEventListenerBase {
+ using TestEventListenerBase::TestEventListenerBase; // inherit constructor
+
+ virtual void
+ testRunStarting(Catch::TestRunInfo const &testRunInfo) override
+ {
+ BaseLogFile *base_log_file = new BaseLogFile("stderr");
+ diags = new Diags(testRunInfo.name.c_str(), "" /* tags */, "" /* actions */, base_log_file);
+ diags->activate_taglist("qpack", DiagsTagType_Debug);
+ diags->config.enabled[DiagsTagType_Debug] = true;
+ diags->show_location = SHOW_LOCATION_DEBUG;
+
+ Layout::create();
+ RecProcessInit(RECM_STAND_ALONE);
+ LibRecordsConfigInit();
+
+ QUICConfig::startup();
+
+ ink_event_system_init(EVENT_SYSTEM_MODULE_VERSION);
+ eventProcessor.start(TEST_THREADS);
+
+ Thread *main_thread = new EThread;
+ main_thread->set_specific();
+
+ url_init();
+ mime_init();
+ http_init();
+ hpack_huffman_init();
+ }
+};
+CATCH_REGISTER_LISTENER(EventProcessorListener);
+
+int
+main(int argc, char *argv[])
+{
+ Catch::Session session;
+ using namespace Catch::clara;
+ auto cli = session.cli() | Opt(qifdir, "dir")["--q-qif-dir"]("path for a directory that contains QIF files (default:test/qif/") |
+ Opt(tablesize, "size")["--q-dynamic-table-size"]("dynamic table size: 0-65535 (default:4096)") |
+ Opt(streams, "n")["--q-max-blocked-streams"]("max blocked streams: 0-65535 (default:100)") |
+ Opt(ackmode, "mode")["--q-ack-mode"]("acknowledgement mode: none(default:0) or immediate(1)") |
+ Opt(appname, "app")["--q-app"]("app name: app name (default:ats)");
+
+ session.cli(cli);
+
+ int returnCode = session.applyCommandLine(argc, argv);
+ if (returnCode != 0) {
+ return returnCode;
+ }
+
+ return session.run();
+}
diff --git a/proxy/hq/test/stub.cc b/proxy/hq/test/stub.cc
new file mode 100644
index 0000000..5949178
--- /dev/null
+++ b/proxy/hq/test/stub.cc
@@ -0,0 +1,88 @@
+/** @file
+ *
+ * Stubs for QUICConfig
+ *
+ * @section license License
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+#include "ts/ink_assert.h"
+
+#include "P_SSLConfig.h"
+
+bool
+SSLParseCertificateConfiguration(const SSLConfigParams *, SSL_CTX *)
+{
+ return false;
+}
+
+SSLConfigParams *
+SSLConfig::acquire()
+{
+ return nullptr;
+}
+
+void
+SSLConfig::release(SSLConfigParams *)
+{
+ return;
+}
+
+#include "P_SSLNextProtocolSet.h"
+
+bool
+SSLNextProtocolSet::advertiseProtocols(const unsigned char **out, unsigned *len) const
+{
+ return true;
+}
+
+#include "InkAPIInternal.h"
+int
+APIHook::invoke(int, void *)
+{
+ ink_assert(false);
+ return 0;
+}
+
+APIHook *
+APIHook::next() const
+{
+ ink_assert(false);
+ return nullptr;
+}
+
+APIHook *
+APIHooks::get() const
+{
+ ink_assert(false);
+ return nullptr;
+}
+
+void
+APIHooks::clear()
+{
+}
+
+void
+APIHooks::prepend(INKContInternal *cont)
+{
+}
+
+void
+APIHooks::append(INKContInternal *cont)
+{
+}
diff --git a/proxy/hq/test/test_QPACK.cc b/proxy/hq/test/test_QPACK.cc
new file mode 100644
index 0000000..2e1e91c
--- /dev/null
+++ b/proxy/hq/test/test_QPACK.cc
@@ -0,0 +1,433 @@
+/** @file
+ *
+ * A brief file description
+ *
+ * @section license License
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+#include "catch.hpp"
+#include <cstdio>
+#include <cstdlib>
+#include <fstream>
+#include "XPACK.h"
+#include "QPACK.h"
+#include "HTTP.h"
+#include "../../iocore/net/quic/Mock.h"
+
+// Declared in main_qpack.cc
+extern char qifdir[256];
+extern int tablesize;
+extern int streams;
+extern int ackmode;
+extern char appname[256];
+
+constexpr int ACK_MODE_IMMEDIATE = 1;
+constexpr int ACK_MODE_NONE = 0;
+
+class TestQUICConnection : public MockQUICConnection
+{
+};
+
+class QUICApplicationDriver
+{
+public:
+ QUICApplicationDriver() {}
+
+ QUICConnection *
+ get_connection()
+ {
+ return &this->_connection;
+ }
+
+private:
+ TestQUICConnection _connection;
+};
+
+class TestQUICStream : public QUICStream
+{
+public:
+ TestQUICStream(QUICStreamId sid) : QUICStream(new MockQUICRTTProvider(), new MockQUICConnectionInfoProvider(), sid) {}
+
+ void
+ write(const uint8_t *buf, size_t buf_len, QUICOffset offset, bool last)
+ {
+ this->_write_to_read_vio(offset, buf, buf_len, last);
+ this->_signal_read_event();
+ }
+
+ size_t
+ read(uint8_t *buf, size_t buf_len)
+ {
+ this->_signal_write_event();
+ IOBufferReader *reader = this->_write_vio.get_reader();
+ return reader->read(buf, buf_len);
+ }
+};
+
+class TestQPACKEventHandler : public Continuation
+{
+public:
+ TestQPACKEventHandler() : Continuation() { SET_HANDLER(&TestQPACKEventHandler::event_handler); }
+
+ int
+ event_handler(int event, Event *data)
+ {
+ this->_event = event;
+ return 0;
+ }
+
+ int
+ last_event()
+ {
+ return this->_event;
+ }
+
+private:
+ int _event = 0;
+};
+
+static int
+load_qif_file(const char *filename, HTTPHdr **headers)
+{
+ HTTPHdr *hdr = nullptr;
+ int n = 0;
+ std::ifstream ifs(filename);
+ std::string line;
+
+ while (std::getline(ifs, line)) {
+ if (line.empty()) {
+ if (hdr) {
+ headers[n++] = hdr;
+ hdr = nullptr;
+ } else {
+ continue;
+ }
+ } else if (line.at(0) == '#') {
+ continue;
+ } else {
+ if (!hdr) {
+ hdr = new HTTPHdr();
+ hdr->create(HTTP_TYPE_REQUEST);
+ }
+ auto tab = line.find_first_of('\t');
+ auto name = line.substr(0, tab);
+ auto value = line.substr(tab + 1);
+ hdr->value_set(name.c_str(), tab, value.c_str(), line.length() - tab - 1);
+ }
+ }
+ if (hdr) {
+ headers[n++] = hdr;
+ }
+
+ return n;
+}
+
+void
+output_encoder_stream_data(FILE *fd, TestQUICStream *stream)
+{
+ uint8_t buf[1024];
+
+ // Write StreamId (0)
+ uint64_t stream_id = 0;
+ fwrite(reinterpret_cast<uint8_t *>(&stream_id), 8, 1, fd);
+
+ // Skip 32 bits for Legnth
+ fseek(fd, 4, SEEK_CUR);
+
+ // Write QPACKData
+ uint64_t total, nread;
+ total = 0;
+ while ((nread = stream->read(buf, sizeof(buf))) > 0) {
+ fwrite(buf, nread, 1, fd);
+ total += nread;
+ }
+
+ // Back to the posistion for Length
+ fseek(fd, -(total + 4), SEEK_CUR);
+
+ // Write Length
+ uint32_t len = htobe32(total);
+ fwrite(reinterpret_cast<uint8_t *>(&len), 4, 1, fd);
+
+ // Back to the tail
+ fseek(fd, 0, SEEK_END);
+}
+
+void
+output_encoded_data(FILE *fd, uint64_t stream_id, IOBufferReader *header_block_reader)
+{
+ uint8_t buf[1024];
+
+ // Write StreamId
+ stream_id = htobe64(stream_id);
+ fwrite(reinterpret_cast<uint8_t *>(&stream_id), 8, 1, fd);
+
+ // Skip 32 bits for Legnth
+ fseek(fd, 4, SEEK_CUR);
+
+ // Write QPACKData
+ int64_t total, nread;
+ total = 0;
+ while ((nread = header_block_reader->read(buf, sizeof(buf))) > 0) {
+ fwrite(buf, nread, 1, fd);
+ total += nread;
+ }
+
+ // Back to the posistion for Length
+ fseek(fd, -(total + 4), SEEK_CUR);
+
+ // Write Length
+ uint32_t len = htobe32(total);
+ fwrite(reinterpret_cast<uint8_t *>(&len), 4, 1, fd);
+
+ // Back to the tail
+ fseek(fd, 0, SEEK_END);
+}
+
+void
+output_decoded_headers(FILE *fd, HTTPHdr **headers, uint64_t n)
+{
+ for (uint64_t i = 0; i < n; ++i) {
+ HTTPHdr *header_set = headers[i];
+ if (!header_set) {
+ continue;
+ }
+ fprintf(fd, "# stream %llu\n", i + 1);
+ MIMEFieldIter field_iter;
+ for (MIMEField *field = header_set->iter_get_first(&field_iter); field != nullptr;
+ field = header_set->iter_get_next(&field_iter)) {
+ int name_len;
+ int value_len;
+ const char *name = field->name_get(&name_len);
+ const char *value = field->value_get(&value_len);
+ fprintf(fd, "%.*s\t%.*s\n", name_len, name, value_len, value);
+ }
+ fprintf(fd, "\n");
+ }
+}
+
+static int
+read_block(FILE *fd, uint64_t &stream_id, uint8_t **head, uint32_t &block_len)
+{
+ size_t len;
+
+ // Read Stream ID
+ len = fread(&stream_id, 1, 8, fd);
+ if (len != 8) {
+ return -1;
+ }
+ stream_id = be64toh(stream_id);
+
+ // Read Length
+ len = fread(&block_len, 1, 4, fd);
+ if (len != 4) {
+ return -1;
+ }
+ block_len = be32toh(block_len);
+
+ // Set the head of block
+ *head = reinterpret_cast<uint8_t *>(ats_malloc(block_len));
+ len = fread(*head, 1, block_len, fd);
+ if (len != block_len) {
+ ats_free(*head);
+ return -1;
+ }
+
+ return 0;
+}
+
+void
+acknowledge_header_block(TestQUICStream *stream, uint64_t stream_id)
+{
+ uint8_t buf[128];
+ int buf_len = 0;
+
+ buf[0] = 0x80;
+ int ret = xpack_encode_integer(buf, buf + sizeof(buf), stream_id, 7);
+ stream->write(buf, ret, 0, stream_id);
+}
+
+static int
+test_encode(const char *qif_file, int dts, int mbs, int am)
+{
+ int ret = 0;
+
+ char output_filename[256];
+ sprintf(output_filename, "%s.ats.%d.%d.%d", qif_file, dts, mbs, am);
+ FILE *fd = fopen(output_filename, "w");
+
+ HTTPHdr *requests[256] = {nullptr};
+ int n_requests = load_qif_file(qif_file, requests);
+
+ QUICApplicationDriver driver;
+ QPACK *qpack = new QPACK(driver.get_connection(), dts);
+ TestQUICStream *encoder_stream = new TestQUICStream(0);
+ TestQUICStream *decoder_stream = new TestQUICStream(9999);
+ qpack->set_stream(encoder_stream);
+ qpack->set_stream(decoder_stream);
+
+ uint64_t stream_id = 1;
+ MIOBuffer *header_block = new_MIOBuffer();
+ IOBufferReader *header_block_reader = header_block->alloc_reader();
+ for (int i = 0; i < n_requests; ++i) {
+ HTTPHdr *hdr = requests[i];
+ ret = qpack->encode(stream_id, *hdr, header_block);
+ if (ret < 0) {
+ break;
+ }
+
+ output_encoder_stream_data(fd, encoder_stream);
+ output_encoded_data(fd, stream_id, header_block_reader);
+
+ if (am == ACK_MODE_IMMEDIATE) {
+ acknowledge_header_block(decoder_stream, stream_id);
+ }
+
+ ++stream_id;
+ }
+
+ fflush(fd);
+ fclose(fd);
+
+ return ret;
+}
+
+static int
+test_decode(const char *qif_file, int dts, int mbs, int am, const char *app_name)
+{
+ int ret = 0;
+
+ char data_filename[256];
+ sprintf(data_filename, "%s.%s.%d.%d.%d", qif_file, app_name, dts, mbs, am);
+ FILE *fd_in = fopen(data_filename, "r");
+
+ char output_filename[256];
+ sprintf(output_filename, "%s.decoded", data_filename);
+ FILE *fd_out = fopen(output_filename, "w");
+
+ HTTPHdr *requests[256];
+ int n_requests = load_qif_file(qif_file, requests);
+
+ TestQPACKEventHandler *event_handler = new TestQPACKEventHandler();
+
+ QUICApplicationDriver driver;
+ QPACK *qpack = new QPACK(driver.get_connection(), dts);
+ TestQUICStream *encoder_stream = new TestQUICStream(0);
+ qpack->set_stream(encoder_stream);
+
+ int offset = 0;
+ uint8_t *block = nullptr;
+ uint32_t block_len;
+ int read_len = 0;
+
+ uint64_t stream_id = 1;
+ HTTPHdr *header_sets[256] = {nullptr};
+ int n_headers = 0;
+ while ((read_len = read_block(fd_in, stream_id, &block, block_len)) >= 0) {
+ if (stream_id == encoder_stream->id()) {
+ encoder_stream->write(block, block_len, offset, false);
+ offset += block_len;
+ } else {
+ if (!header_sets[stream_id - 1]) {
+ header_sets[stream_id - 1] = new HTTPHdr();
+ header_sets[stream_id - 1]->create(HTTP_TYPE_REQUEST);
+ ++n_headers;
+ }
+ qpack->decode(stream_id, block, block_len, *header_sets[stream_id - 1], event_handler, eventProcessor.all_ethreads[0]);
+ }
+ ats_free(block);
+ }
+
+ if (!feof(fd_in)) {
+ return -1;
+ }
+
+ sleep(3);
+
+ CHECK(event_handler->last_event() == QPACK_EVENT_DECODE_COMPLETE);
+
+ output_decoded_headers(fd_out, header_sets, n_headers);
+
+ for (unsigned int i = 0; i < countof(header_sets); ++i) {
+ if (header_sets[i]) {
+ header_sets[i]->destroy();
+ delete header_sets[i];
+ }
+ }
+
+ return ret;
+}
+
+TEST_CASE("Encoding", "[qpack]")
+{
+ struct dirent *d;
+ DIR *dir = opendir(qifdir);
+
+ if (dir == nullptr) {
+ return;
+ }
+
+ struct stat st;
+ char qif_file[PATH_MAX + 1] = "";
+ strcat(qif_file, qifdir);
+
+ while ((d = readdir(dir)) != nullptr) {
+ char section_name[128];
+ sprintf(section_name, "%s: DTS=%d, MBS=%d, AM=%d", d->d_name, tablesize, streams, ackmode);
+ SECTION(section_name)
+ {
+ qif_file[strlen(qifdir)] = '/';
+ qif_file[strlen(qifdir) + 1] = '\0';
+ ink_strlcat(qif_file, d->d_name, sizeof(qif_file));
+ stat(qif_file, &st);
+ if (S_ISREG(st.st_mode) && strstr(d->d_name, ".qif") == (d->d_name + (strlen(d->d_name) - 4))) {
+ CHECK(test_encode(qif_file, tablesize, streams, ackmode) == 0);
+ }
+ }
+ }
+}
+
+TEST_CASE("Decoding", "[qpack]")
+{
+ struct dirent *d;
+ DIR *dir = opendir(qifdir);
+
+ if (dir == nullptr) {
+ return;
+ }
+
+ struct stat st;
+ char qif_file[PATH_MAX + 1] = "";
+ strcat(qif_file, qifdir);
+
+ while ((d = readdir(dir)) != nullptr) {
+ char section_name[128];
+ sprintf(section_name, "%s: DTS=%d, MBS=%d, AM=%d, APP=%s", d->d_name, tablesize, streams, ackmode, appname);
+ SECTION(section_name)
+ {
+ qif_file[strlen(qifdir)] = '/';
+ qif_file[strlen(qifdir) + 1] = '\0';
+ ink_strlcat(qif_file, d->d_name, sizeof(qif_file));
+ stat(qif_file, &st);
+ if (S_ISREG(st.st_mode) && strstr(d->d_name, ".qif") == (d->d_name + (strlen(d->d_name) - 4))) {
+ CHECK(test_decode(qif_file, tablesize, streams, ackmode, appname) == 0);
+ }
+ }
+ }
+}
[trafficserver] 03/05: Preserve the prefix bits when encoding
integer with XPACK
Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
maskit pushed a commit to branch quic-latest
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
commit e755cc8500624689612ceccc49ee7ff228b6f834
Author: Masakazu Kitajo <ma...@apache.org>
AuthorDate: Fri Jul 27 12:19:32 2018 +0900
Preserve the prefix bits when encoding integer with XPACK
---
proxy/hdrs/XPACK.cc | 9 +++++++++
proxy/hdrs/XPACK.h | 1 +
proxy/http2/HPACK.cc | 3 ++-
3 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/proxy/hdrs/XPACK.cc b/proxy/hdrs/XPACK.cc
index c537872..73d6e80 100644
--- a/proxy/hdrs/XPACK.cc
+++ b/proxy/hdrs/XPACK.cc
@@ -118,6 +118,9 @@ xpack_encode_integer(uint8_t *buf_start, const uint8_t *buf_end, uint64_t value,
uint8_t *p = buf_start;
+ // Preserve the first n bits
+ uint8_t prefix = *buf_start & (0xFF << n);
+
if (value < (static_cast<uint64_t>(1 << n) - 1)) {
*(p++) = value;
} else {
@@ -135,6 +138,10 @@ xpack_encode_integer(uint8_t *buf_start, const uint8_t *buf_end, uint64_t value,
}
*(p++) = value;
}
+
+ // Restore the prefix
+ *buf_start |= prefix;
+
return p - buf_start;
}
@@ -168,6 +175,8 @@ xpack_encode_string(uint8_t *buf_start, const uint8_t *buf_end, const char *valu
if (use_huffman) {
*p |= 0x01 << n;
+ } else {
+ *p &= ~(0x01 << n);
}
p += len;
diff --git a/proxy/hdrs/XPACK.h b/proxy/hdrs/XPACK.h
index 5e8eca3..bb3bc71 100644
--- a/proxy/hdrs/XPACK.h
+++ b/proxy/hdrs/XPACK.h
@@ -24,6 +24,7 @@
#pragma once
#include <cstdint>
+#include "ts/Arena.h"
const static int XPACK_ERROR_COMPRESSION_ERROR = -1;
const static int XPACK_ERROR_SIZE_EXCEEDED_ERROR = -2;
diff --git a/proxy/http2/HPACK.cc b/proxy/http2/HPACK.cc
index fd16460..f51f947 100644
--- a/proxy/http2/HPACK.cc
+++ b/proxy/http2/HPACK.cc
@@ -465,6 +465,7 @@ encode_literal_header_field_with_indexed_name(uint8_t *buf_start, const uint8_t
}
// Index
+ *p = 0;
len = xpack_encode_integer(p, buf_end, index, prefix);
if (len == -1) {
return -1;
@@ -553,11 +554,11 @@ encode_literal_header_field_with_new_name(uint8_t *buf_start, const uint8_t *buf
int64_t
encode_dynamic_table_size_update(uint8_t *buf_start, const uint8_t *buf_end, uint32_t size)
{
+ buf_start[0] = 0x20;
const int64_t len = xpack_encode_integer(buf_start, buf_end, size, 5);
if (len == -1) {
return -1;
}
- buf_start[0] |= 0x20;
return len;
}
[trafficserver] 01/05: Add QUICStreamIO::peek
Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
maskit pushed a commit to branch quic-latest
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
commit 8b8d9a043c651d0e5bcd97658b007e6dc02ccb74
Author: Masakazu Kitajo <ma...@apache.org>
AuthorDate: Fri Jul 20 17:20:45 2018 +0900
Add QUICStreamIO::peek
---
iocore/net/quic/QUICApplication.cc | 14 +++++++++++++-
iocore/net/quic/QUICApplication.h | 2 ++
2 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/iocore/net/quic/QUICApplication.cc b/iocore/net/quic/QUICApplication.cc
index 4686398..54bf199 100644
--- a/iocore/net/quic/QUICApplication.cc
+++ b/iocore/net/quic/QUICApplication.cc
@@ -66,7 +66,7 @@ QUICStreamIO::read(uint8_t *buf, int64_t len)
this->_read_vio->ndone, this->_read_buffer_reader->read_avail(), len);
}
- int64_t nread = this->_read_buffer_reader->read(const_cast<uint8_t *>(buf), len);
+ int64_t nread = this->_read_buffer_reader->read(buf, len);
if (nread > 0) {
this->_read_vio->ndone += nread;
}
@@ -74,6 +74,18 @@ QUICStreamIO::read(uint8_t *buf, int64_t len)
return nread;
}
+int64_t
+QUICStreamIO::peek(uint8_t *buf, int64_t len)
+{
+ return this->_read_buffer_reader->memcpy(buf, len) - reinterpret_cast<char *>(buf);
+}
+
+void
+QUICStreamIO::consume(int64_t len)
+{
+ this->_read_buffer_reader->consume(len);
+}
+
bool
QUICStreamIO::is_read_done()
{
diff --git a/iocore/net/quic/QUICApplication.h b/iocore/net/quic/QUICApplication.h
index e9b8e4c..87d84eb 100644
--- a/iocore/net/quic/QUICApplication.h
+++ b/iocore/net/quic/QUICApplication.h
@@ -43,6 +43,8 @@ public:
uint32_t stream_id() const;
int64_t read(uint8_t *buf, int64_t len);
+ int64_t peek(uint8_t *buf, int64_t len);
+ void consume(int64_t len);
bool is_read_done();
virtual void read_reenable();