Browse Source

make dynamicTable slice a FIFO now, per spec

Brad Fitzpatrick 11 years ago
parent
commit
8050127db6
2 changed files with 14 additions and 3 deletions
  1. 5 1
      hpack/hpack.go
  2. 9 2
      hpack/hpack_test.go

+ 5 - 1
hpack/hpack.go

@@ -56,6 +56,10 @@ func (d *Decoder) SetMaxDynamicTableSize(v uint32) {
 }
 
 type dynamicTable struct {
+	// s is the FIFO described at
+	// http://http2.github.io/http2-spec/compression.html#rfc.section.2.3.2
+	// The newest (low index) is append at the end, and items are
+	// evicted from the front.
 	s       []HeaderField
 	size    uint32
 	maxSize uint32
@@ -95,5 +99,5 @@ func (dt *dynamicTable) at(i int) HeaderField {
 	if i <= len(staticTable) {
 		return staticTable[i-1]
 	}
-	return dt.s[i-len(staticTable)-1]
+	return dt.s[len(dt.s)-(i-len(staticTable))]
 }

+ 9 - 2
hpack/hpack_test.go

@@ -119,8 +119,15 @@ func TestHeaderTableAt(t *testing.T) {
 		t.Errorf("at(2) = %q; want %q", got, want)
 	}
 	dt.add(HeaderField{"foo", "bar"})
-	if got, want := dt.at(len(staticTable)+1), (HeaderField{"foo", "bar"}); got != want {
-		t.Errorf("at(1) = %q; want %q", got, want)
+	dt.add(HeaderField{"blake", "miz"})
+	if got, want := dt.at(len(staticTable)+1), (HeaderField{"blake", "miz"}); got != want {
+		t.Errorf("at(dyn 1) = %q; want %q", got, want)
+	}
+	if got, want := dt.at(len(staticTable)+2), (HeaderField{"foo", "bar"}); got != want {
+		t.Errorf("at(dyn 2) = %q; want %q", got, want)
+	}
+	if got, want := dt.at(3), (HeaderField{":method", "POST"}); got != want {
+		t.Errorf("at(3) = %q; want %q", got, want)
 	}
 }