You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by jp...@apache.org on 2014/10/31 23:40:36 UTC

[3/3] git commit: TS-2729: extend the notion of HPACK field type

TS-2729: extend the notion of HPACK field type

Add hpack_parse_field_type() to detect the HPACK field type, and
extend the enumerated type to include all HPACK fields. Remove raw
bit testing where possible.


Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/34a9a719
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/34a9a719
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/34a9a719

Branch: refs/heads/master
Commit: 34a9a719a113dd5cb6f17b18e0b6c19eed6e2cad
Parents: fa6c830
Author: James Peach <jp...@apache.org>
Authored: Fri Oct 24 15:42:36 2014 -0700
Committer: James Peach <jp...@apache.org>
Committed: Fri Oct 31 15:25:57 2014 -0700

----------------------------------------------------------------------
 proxy/http2/HPACK.cc | 64 ++++++++++++++++++++++++++++++++++++-----------
 proxy/http2/HPACK.h  | 26 ++++++++++++++-----
 proxy/http2/HTTP2.cc | 36 ++++++++++++++++----------
 3 files changed, 92 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/34a9a719/proxy/http2/HPACK.cc
----------------------------------------------------------------------
diff --git a/proxy/http2/HPACK.cc b/proxy/http2/HPACK.cc
index 95cbc71..f4afd95 100644
--- a/proxy/http2/HPACK.cc
+++ b/proxy/http2/HPACK.cc
@@ -264,6 +264,32 @@ Http2HeaderTable::add_header_field(const MIMEField * field)
   }
 }
 
+// The first byte of an HPACK field unambiguously tells us what
+// kind of field it is. Field types are specified in the high 4 bits
+// and all bits are defined, so there's no way to get an invalid field type.
+HpackFieldType
+hpack_parse_field_type(uint8_t ftype)
+{
+  if (ftype & 0x80) {
+    return HPACK_FIELD_INDEX;
+  }
+
+  if (ftype & 0x40) {
+    return HPACK_FIELD_INDEXED_LITERAL;
+  }
+
+  if (ftype & 0x20) {
+    return HPACK_FIELD_TABLESIZE_UPDATE;
+  }
+
+  if (ftype & 0x10) {
+    return HPACK_FIELD_NEVERINDEX_LITERAL;
+  }
+
+  ink_assert((ftype & 0xf0) == 0x0);
+  return HPACK_FIELD_NOINDEX_LITERAL;
+}
+
 /*
  * Pseudo code
  *
@@ -335,29 +361,32 @@ encode_indexed_header_field(uint8_t *buf_start, const uint8_t *buf_end, uint32_t
   if (p+1 >= buf_end) {
     return -1;
   }
-  *p |= '\x80';
+
+  *p |= 0x80;
   p += len;
 
   return p - buf_start;
 }
 
 int64_t
-encode_literal_header_field(uint8_t *buf_start, const uint8_t *buf_end, const MIMEFieldWrapper& header, uint32_t index, HEADER_INDEXING_TYPE type)
+encode_literal_header_field(uint8_t *buf_start, const uint8_t *buf_end, const MIMEFieldWrapper& header, uint32_t index, HpackFieldType type)
 {
   uint8_t *p = buf_start;
   int64_t len;
   uint8_t prefix = 0, flag = 0;
 
+  ink_assert(hpack_field_is_literal(type));
+
   switch (type) {
-  case INC_INDEXING:
+  case HPACK_FIELD_INDEXED_LITERAL:
     prefix = 6;
     flag = 0x40;
     break;
-  case WITHOUT_INDEXING:
+  case HPACK_FIELD_NOINDEX_LITERAL:
     prefix = 4;
     flag = 0x00;
     break;
-  case NEVER_INDEXED:
+  case HPACK_FIELD_NEVERINDEX_LITERAL:
     prefix = 4;
     flag = 0x10;
     break;
@@ -387,21 +416,22 @@ encode_literal_header_field(uint8_t *buf_start, const uint8_t *buf_end, const MI
 }
 
 int64_t
-encode_literal_header_field(uint8_t *buf_start, const uint8_t *buf_end, const MIMEFieldWrapper& header, HEADER_INDEXING_TYPE type)
+encode_literal_header_field(uint8_t *buf_start, const uint8_t *buf_end, const MIMEFieldWrapper& header, HpackFieldType type)
 {
   uint8_t *p = buf_start;
   int64_t len;
   uint8_t flag = 0;
 
-  ink_assert(type >= INC_INDEXING && type <= NEVER_INDEXED);
+  ink_assert(hpack_field_is_literal(type));
+
   switch (type) {
-  case INC_INDEXING:
+  case HPACK_FIELD_INDEXED_LITERAL:
     flag = 0x40;
     break;
-  case WITHOUT_INDEXING:
+  case HPACK_FIELD_NOINDEX_LITERAL:
     flag = 0x00;
     break;
-  case NEVER_INDEXED:
+  case HPACK_FIELD_NEVERINDEX_LITERAL:
     flag = 0x10;
     break;
   default:
@@ -534,21 +564,25 @@ decode_literal_header_field(MIMEFieldWrapper& header, const uint8_t *buf_start,
   bool isIncremental = false;
   uint32_t index = 0;
   int64_t len = 0;
+  HpackFieldType ftype = hpack_parse_field_type(*p);
 
-  if (*p & 0x40) {
+  if (ftype == HPACK_FIELD_INDEXED_LITERAL) {
     // 7.2.1. index extraction based on Literal Header Field with Incremental Indexing
     len = decode_integer(index, p, buf_end, 6);
-    if (len == -1) return -1;
     isIncremental = true;
-  } else if (*p & 0x10) {
+  } else if (ftype == HPACK_FIELD_NEVERINDEX_LITERAL) {
     // 7.2.3. index extraction Literal Header Field Never Indexed
     len = decode_integer(index, p, buf_end, 4);
-    if (len == -1) return -1;
   } else {
     // 7.2.2. index extraction Literal Header Field without Indexing
+    ink_assert(ftype == HPACK_FIELD_NOINDEX_LITERAL);
     len = decode_integer(index, p, buf_end, 4);
-    if (len == -1) return -1;
   }
+
+  if (len == -1) {
+    return -1;
+  }
+
   p += len;
 
   if (index) {

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/34a9a719/proxy/http2/HPACK.h
----------------------------------------------------------------------
diff --git a/proxy/http2/HPACK.h b/proxy/http2/HPACK.h
index 4abccac..92b4e21 100644
--- a/proxy/http2/HPACK.h
+++ b/proxy/http2/HPACK.h
@@ -40,10 +40,13 @@ extern const unsigned HPACK_LEN_AUTHORITY;
 extern const unsigned HPACK_LEN_PATH;
 extern const unsigned HPACK_LEN_STATUS;
 
-enum HEADER_INDEXING_TYPE {
-  INC_INDEXING,
-  WITHOUT_INDEXING,
-  NEVER_INDEXED,
+enum HpackFieldType
+{
+  HPACK_FIELD_INDEX,              // HPACK 7.1 Indexed Header Field Representation
+  HPACK_FIELD_INDEXED_LITERAL,    // HPACK 7.2.1 Literal Header Field with Incremental Indexing
+  HPACK_FIELD_NOINDEX_LITERAL,    // HPACK 7.2.2 Literal Header Field without Indexing
+  HPACK_FIELD_NEVERINDEX_LITERAL, // HPACK 7.2.3 Literal Header Field never Indexed
+  HPACK_FIELD_TABLESIZE_UPDATE,   // HPACK 7.3 Header Table Size Update
 };
 
 class MIMEFieldWrapper
@@ -116,6 +119,17 @@ private:
   Vec<MIMEField *>  _headers;
 };
 
+HpackFieldType
+hpack_parse_field_type(uint8_t ftype);
+
+static inline bool
+hpack_field_is_literal(HpackFieldType ftype) {
+  return
+    ftype == HPACK_FIELD_INDEXED_LITERAL ||
+    ftype == HPACK_FIELD_NOINDEX_LITERAL ||
+    ftype == HPACK_FIELD_NEVERINDEX_LITERAL;
+}
+
 int64_t
 encode_integer(uint8_t *buf_start, const uint8_t *buf_end, uint32_t value, uint8_t n);
 int64_t
@@ -128,9 +142,9 @@ decode_string(char **c_str, uint32_t& c_str_length, const uint8_t *buf_start, co
 int64_t
 encode_indexed_header_field(uint8_t *buf_start, const uint8_t *buf_end, uint32_t index);
 int64_t
-encode_literal_header_field(uint8_t *buf_start, const uint8_t *buf_end, const MIMEFieldWrapper& header, uint32_t index, HEADER_INDEXING_TYPE type);
+encode_literal_header_field(uint8_t *buf_start, const uint8_t *buf_end, const MIMEFieldWrapper& header, uint32_t index, HpackFieldType type);
 int64_t
-encode_literal_header_field(uint8_t *buf_start, const uint8_t *buf_end, const MIMEFieldWrapper& header, HEADER_INDEXING_TYPE type);
+encode_literal_header_field(uint8_t *buf_start, const uint8_t *buf_end, const MIMEFieldWrapper& header, HpackFieldType type);
 
 int64_t
 decode_indexed_header_field(MIMEFieldWrapper& header, const uint8_t *buf_start, const uint8_t *buf_end, Http2HeaderTable& header_table);

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/34a9a719/proxy/http2/HTTP2.cc
----------------------------------------------------------------------
diff --git a/proxy/http2/HTTP2.cc b/proxy/http2/HTTP2.cc
index 1760ade..590d082 100644
--- a/proxy/http2/HTTP2.cc
+++ b/proxy/http2/HTTP2.cc
@@ -367,7 +367,7 @@ convert_from_1_1_to_2_header(HTTPHdr* in, uint8_t* out, uint64_t out_len, Http2H
   for (field = in->iter_get_first(&field_iter); field != NULL; field = in->iter_get_next(&field_iter)) {
     do {
       MIMEFieldWrapper header(field, in->m_heap, in->m_http->m_fields_impl);
-      if ((len = encode_literal_header_field(p, end, header, INC_INDEXING)) == -1) {
+      if ((len = encode_literal_header_field(p, end, header, HPACK_FIELD_INDEXED_LITERAL)) == -1) {
         return -1;
       }
       p += len;
@@ -397,16 +397,26 @@ http2_parse_header_fragment(HTTPHdr * hdr, IOVec iov, Http2HeaderTable& header_t
     // decode a header field encoded by HPACK
     MIMEField *field = mime_field_create(heap, hh->m_fields_impl);
     MIMEFieldWrapper header(field, heap, hh->m_fields_impl);
-    if (*cursor & 0x80) {
+    HpackFieldType ftype = hpack_parse_field_type(*cursor);
+
+    switch (ftype) {
+    case HPACK_FIELD_INDEX:
       if ((read_bytes = decode_indexed_header_field(header, cursor, buf_end, header_table)) == -1) {
         return PARSE_ERROR;
       }
       cursor += read_bytes;
-    } else {
+      break;
+    case HPACK_FIELD_INDEXED_LITERAL:
+    case HPACK_FIELD_NOINDEX_LITERAL:
+    case HPACK_FIELD_NEVERINDEX_LITERAL:
       if ((read_bytes = decode_literal_header_field(header, cursor, buf_end, header_table)) == -1) {
         return PARSE_ERROR;
       }
       cursor += read_bytes;
+      break;
+    case HPACK_FIELD_TABLESIZE_UPDATE:
+      // XXX not supported yet
+      return PARSE_ERROR;
     }
 
     // Store to HdrHeap
@@ -472,19 +482,19 @@ const static struct {
   char* raw_name;
   char* raw_value;
   int index;
-  HEADER_INDEXING_TYPE type;
+  HpackFieldType type;
   uint8_t* encoded_field;
   int encoded_field_len;
 } literal_test_case[] = {
-  { (char*)"custom-key", (char*)"custom-header", 0, INC_INDEXING, (uint8_t*)"\x40\x0a" "custom-key\x0d" "custom-header", 26 },
-  { (char*)"custom-key", (char*)"custom-header", 0, WITHOUT_INDEXING, (uint8_t*)"\x00\x0a" "custom-key\x0d" "custom-header", 26 },
-  { (char*)"custom-key", (char*)"custom-header", 0, NEVER_INDEXED, (uint8_t*)"\x10\x0a" "custom-key\x0d" "custom-header", 26 },
-  { (char*)":path", (char*)"/sample/path", 4, INC_INDEXING, (uint8_t*)"\x44\x0c" "/sample/path", 14 },
-  { (char*)":path", (char*)"/sample/path", 4, WITHOUT_INDEXING, (uint8_t*)"\x04\x0c" "/sample/path", 14 },
-  { (char*)":path", (char*)"/sample/path", 4, NEVER_INDEXED, (uint8_t*)"\x14\x0c" "/sample/path", 14 },
-  { (char*)"password", (char*)"secret", 0, INC_INDEXING, (uint8_t*)"\x40\x08" "password\x06" "secret", 17 },
-  { (char*)"password", (char*)"secret", 0, WITHOUT_INDEXING, (uint8_t*)"\x00\x08" "password\x06" "secret", 17 },
-  { (char*)"password", (char*)"secret", 0, NEVER_INDEXED, (uint8_t*)"\x10\x08" "password\x06" "secret", 17 }
+  { (char*)"custom-key", (char*)"custom-header", 0, HPACK_FIELD_INDEXED_LITERAL, (uint8_t*)"\x40\x0a" "custom-key\x0d" "custom-header", 26 },
+  { (char*)"custom-key", (char*)"custom-header", 0, HPACK_FIELD_NOINDEX_LITERAL, (uint8_t*)"\x00\x0a" "custom-key\x0d" "custom-header", 26 },
+  { (char*)"custom-key", (char*)"custom-header", 0, HPACK_FIELD_NEVERINDEX_LITERAL, (uint8_t*)"\x10\x0a" "custom-key\x0d" "custom-header", 26 },
+  { (char*)":path", (char*)"/sample/path", 4, HPACK_FIELD_INDEXED_LITERAL, (uint8_t*)"\x44\x0c" "/sample/path", 14 },
+  { (char*)":path", (char*)"/sample/path", 4, HPACK_FIELD_NOINDEX_LITERAL, (uint8_t*)"\x04\x0c" "/sample/path", 14 },
+  { (char*)":path", (char*)"/sample/path", 4, HPACK_FIELD_NEVERINDEX_LITERAL, (uint8_t*)"\x14\x0c" "/sample/path", 14 },
+  { (char*)"password", (char*)"secret", 0, HPACK_FIELD_INDEXED_LITERAL, (uint8_t*)"\x40\x08" "password\x06" "secret", 17 },
+  { (char*)"password", (char*)"secret", 0, HPACK_FIELD_NOINDEX_LITERAL, (uint8_t*)"\x00\x08" "password\x06" "secret", 17 },
+  { (char*)"password", (char*)"secret", 0, HPACK_FIELD_NEVERINDEX_LITERAL, (uint8_t*)"\x10\x08" "password\x06" "secret", 17 }
 };
 
 // D.3.  Request Examples without Huffman Coding - D.3.1.  First Request