Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http3: add support for parsing and writing HTTP/3 capsules #3607

Merged
merged 1 commit into from Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 55 additions & 0 deletions http3/capsule.go
@@ -0,0 +1,55 @@
package http3

import (
"io"

"github.com/lucas-clemente/quic-go/quicvarint"
)

// CapsuleType is the type of the capsule.
type CapsuleType uint64

type exactReader struct {
R *io.LimitedReader
}

func (r *exactReader) Read(b []byte) (int, error) {
n, err := r.R.Read(b)
if r.R.N > 0 {
return n, io.ErrUnexpectedEOF
}
return n, err
}

// ParseCapsule parses the header of a Capsule.
// It returns an io.LimitedReader that can be used to read the Capsule value.
// The Capsule value must be read entirely (i.e. until the io.EOF) before using r again.
func ParseCapsule(r quicvarint.Reader) (CapsuleType, io.Reader, error) {
ct, err := quicvarint.Read(r)
if err != nil {
if err == io.EOF {
return 0, nil, io.ErrUnexpectedEOF
}
return 0, nil, err
}
l, err := quicvarint.Read(r)
if err != nil {
if err == io.EOF {
return 0, nil, io.ErrUnexpectedEOF
}
return 0, nil, err
}
return CapsuleType(ct), &exactReader{R: io.LimitReader(r, int64(l)).(*io.LimitedReader)}, nil
}

// WriteCapsule writes a capsule
func WriteCapsule(w quicvarint.Writer, ct CapsuleType, value []byte) error {
b := make([]byte, 0, 16)
b = quicvarint.Append(b, uint64(ct))
b = quicvarint.Append(b, uint64(len(value)))
if _, err := w.Write(b); err != nil {
return err
}
_, err := w.Write(value)
return err
}
57 changes: 57 additions & 0 deletions http3/capsule_test.go
@@ -0,0 +1,57 @@
package http3

import (
"bytes"
"io"

"github.com/lucas-clemente/quic-go/quicvarint"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Capsule", func() {
It("parses Capsules", func() {
var buf bytes.Buffer
quicvarint.Write(&buf, 1337)
quicvarint.Write(&buf, 6)
buf.WriteString("foobar")

ct, r, err := ParseCapsule(&buf)
Expect(err).ToNot(HaveOccurred())
Expect(ct).To(BeEquivalentTo(1337))
val, err := io.ReadAll(r)
Expect(err).ToNot(HaveOccurred())
Expect(string(val)).To(Equal("foobar"))
})

It("writes capsules", func() {
var buf bytes.Buffer
WriteCapsule(&buf, 1337, []byte("foobar"))

ct, r, err := ParseCapsule(&buf)
Expect(err).ToNot(HaveOccurred())
Expect(ct).To(BeEquivalentTo(1337))
val, err := io.ReadAll(r)
Expect(err).ToNot(HaveOccurred())
Expect(string(val)).To(Equal("foobar"))
})

It("errors on EOF", func() {
var buf bytes.Buffer
quicvarint.Write(&buf, 1337)
quicvarint.Write(&buf, 6)
buf.WriteString("foobar")
data := buf.Bytes()

for i := range data {
ct, r, err := ParseCapsule(bytes.NewReader(data[:i]))
if err != nil {
Expect(err).To(MatchError(io.ErrUnexpectedEOF))
continue
}
Expect(ct).To(BeEquivalentTo(1337))
_, err = io.ReadAll(r)
Expect(err).To(Equal(io.ErrUnexpectedEOF))
}
})
})