hpack.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2014 The Go Authors.
  2. // See https://code.google.com/p/go/source/browse/CONTRIBUTORS
  3. // Licensed under the same terms as Go itself:
  4. // https://code.google.com/p/go/source/browse/LICENSE
  5. // Package hpack implements HPACK, a compression format for
  6. // efficiently representing HTTP header fields in the context of HTTP/2.
  7. //
  8. // See http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-08
  9. package hpack
  10. import "fmt"
  11. // A HeaderField is a name-value pair. Both the name and value are
  12. // treated as opaque sequences of octets.
  13. type HeaderField struct {
  14. Name, Value string
  15. }
  16. // A Decoder is the decoding context for incremental processing of
  17. // header blocks.
  18. type Decoder struct {
  19. ht headerTable
  20. refSet struct{} // TODO
  21. Emit func(f HeaderField, sensitive bool)
  22. }
  23. type headerTable []HeaderField
  24. func (s *headerTable) add(f HeaderField) {
  25. *s = append(*s, f)
  26. }
  27. func (s *headerTable) at(i int) HeaderField {
  28. if i < 1 {
  29. panic(fmt.Sprintf("header table index %d too small", i))
  30. }
  31. ht := *s
  32. max := len(ht) + len(staticTable)
  33. if i > max {
  34. panic(fmt.Sprintf("header table index %d too large (max = %d)", i, max))
  35. }
  36. if i <= len(ht) {
  37. return ht[i-1]
  38. }
  39. return staticTable[i-len(ht)-1]
  40. }