Browse Source

some header table stuff

Brad Fitzpatrick 11 years ago
parent
commit
32a7cea86a
2 changed files with 43 additions and 1 deletions
  1. 26 1
      hpack/hpack.go
  2. 17 0
      hpack/hpack_test.go

+ 26 - 1
hpack/hpack.go

@@ -9,6 +9,8 @@
 // See http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-08
 package hpack
 
+import "fmt"
+
 // A HeaderField is a name-value pair. Both the name and value are
 // treated as opaque sequences of octets.
 type HeaderField struct {
@@ -18,5 +20,28 @@ type HeaderField struct {
 // A Decoder is the decoding context for incremental processing of
 // header blocks.
 type Decoder struct {
-	// ...
+	ht     headerTable
+	refSet struct{} // TODO
+	Emit   func(f HeaderField, sensitive bool)
+}
+
+type headerTable []HeaderField
+
+func (s *headerTable) add(f HeaderField) {
+	*s = append(*s, f)
+}
+
+func (s *headerTable) at(i int) HeaderField {
+	if i < 1 {
+		panic(fmt.Sprintf("header table index %d too small", i))
+	}
+	ht := *s
+	max := len(ht) + len(staticTable)
+	if i > max {
+		panic(fmt.Sprintf("header table index %d too large (max = %d)", i, max))
+	}
+	if i <= len(ht) {
+		return ht[i-1]
+	}
+	return staticTable[i-len(ht)-1]
 }

+ 17 - 0
hpack/hpack_test.go

@@ -110,3 +110,20 @@ func TestStaticTable(t *testing.T) {
 		t.Error(err)
 	}
 }
+
+func TestHeaderTableAt(t *testing.T) {
+	var ht headerTable
+	if got, want := ht.at(2), (HeaderField{":method", "GET"}); got != want {
+		t.Errorf("at(2) = %q; want %q", got, want)
+	}
+	ht.add(HeaderField{"foo", "bar"})
+	if got, want := ht.at(1), (HeaderField{"foo", "bar"}); got != want {
+		t.Errorf("at(1) = %q; want %q", got, want)
+	}
+	if got, want := ht.at(3), (HeaderField{":method", "GET"}); got != want {
+		t.Errorf("at(3) = %q; want %q", got, want)
+	}
+	if got, want := ht.at(62), (HeaderField{"www-authenticate", ""}); got != want {
+		t.Errorf("at(62) = %q; want %q", got, want)
+	}
+}