http3: add HTTP Trailer support for servers (#4630)

This commit is contained in:
Kevin McDonald
2024-09-07 06:51:54 +02:00
committed by GitHub
parent ad8f3c0ff8
commit 09c2ac8aab
4 changed files with 203 additions and 4 deletions

View File

@@ -31,6 +31,7 @@ var _ = Describe("Response Writer", func() {
decodeHeader := func(str io.Reader) map[string][]string {
rw.Flush()
rw.flushTrailers()
fields := make(map[string][]string)
decoder := qpack.NewDecoder(nil)
@@ -184,4 +185,65 @@ var _ = Describe("Response Writer", func() {
Expect(func() { rw.WriteHeader(99) }).To(Panic())
Expect(func() { rw.WriteHeader(1000) }).To(Panic())
})
It("write announced trailer", func() {
rw.Header().Add("Trailer", "Key")
rw.WriteHeader(http.StatusTeapot)
n, err := rw.Write([]byte("foobar"))
Expect(n).To(Equal(6))
Expect(err).ToNot(HaveOccurred())
rw.Header().Set("Key", "Value")
// writeTrailers needs to be called after writing the full body
Expect(rw.writeTrailers()).ToNot(HaveOccurred())
fields := decodeHeader(strBuf)
Expect(fields).To(HaveKeyWithValue(":status", []string{"418"}))
Expect(fields).To(HaveKeyWithValue("trailer", []string{"Key"}))
Expect(getData(strBuf)).To(Equal([]byte("foobar")))
fields = decodeHeader(strBuf)
Expect(fields).To(HaveKeyWithValue("key", []string{"Value"}))
})
It("ignore non-announced trailer (without trailer prefix)", func() {
rw.Header().Set("Trailer", "Key")
rw.WriteHeader(200)
rw.Write([]byte("foobar"))
rw.Header().Set("UnknownKey", "Value")
rw.Header().Set("Key", "Value")
// Needs to call writeTrailers to simulate the end of the handler
Expect(rw.writeTrailers()).ToNot(HaveOccurred())
headers := decodeHeader(strBuf)
Expect(headers).To(HaveKeyWithValue(":status", []string{"200"}))
Expect(headers).To(HaveKeyWithValue("trailer", []string{"Key"}))
Expect(getData(strBuf)).To(Equal([]byte("foobar")))
trailers := decodeHeader(strBuf)
Expect(trailers).To(HaveKeyWithValue("key", []string{"Value"}))
Expect(trailers).To(Not(HaveKeyWithValue("unknownkey", []string{"Value"})))
})
It("write non-announced trailer (with trailer prefix)", func() {
rw.Header().Set("Trailer", "Key")
rw.WriteHeader(200)
rw.Write([]byte("foobar"))
rw.Header().Set("Key", "Value")
rw.Header().Set(http.TrailerPrefix+"Key2", "Value")
rw.Flush()
// Needs to call writeTrailers to simulate the end of the handler
Expect(rw.writeTrailers()).ToNot(HaveOccurred())
headers := decodeHeader(strBuf)
Expect(headers).To(HaveKeyWithValue(":status", []string{"200"}))
Expect(headers).To(HaveKeyWithValue("trailer", []string{"Key"}))
Expect(getData(strBuf)).To(Equal([]byte("foobar")))
trailers := decodeHeader(strBuf)
Expect(trailers).To(HaveKeyWithValue("key", []string{"Value"}))
Expect(trailers).To(HaveKeyWithValue("key2", []string{"Value"}))
})
})