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

add support for the Extended CONNECT method #3357

Merged
merged 1 commit into from Mar 25, 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
24 changes: 19 additions & 5 deletions http3/request.go
Expand Up @@ -12,9 +12,9 @@ import (
)

func requestFromHeaders(headers []qpack.HeaderField) (*http.Request, error) {
var path, authority, method, contentLengthStr string
httpHeaders := http.Header{}
var path, authority, method, protocol, scheme, contentLengthStr string

httpHeaders := http.Header{}
for _, h := range headers {
switch h.Name {
case ":path":
Expand All @@ -23,6 +23,10 @@ func requestFromHeaders(headers []qpack.HeaderField) (*http.Request, error) {
method = h.Value
case ":authority":
authority = h.Value
case ":protocol":
protocol = h.Value
case ":scheme":
scheme = h.Value
case "content-length":
contentLengthStr = h.Value
default:
Expand All @@ -39,7 +43,12 @@ func requestFromHeaders(headers []qpack.HeaderField) (*http.Request, error) {

isConnect := method == http.MethodConnect
if isConnect {
if path != "" || authority == "" {
// Extended CONNECT, see https://datatracker.ietf.org/doc/html/rfc8441#section-4
if protocol != "" {
if scheme == "" || path == "" || authority == "" {
return nil, errors.New("extended CONNECT: :scheme, :path and :authority must not be empty")
}
} else if path != "" || authority == "" { // normal CONNECT
return nil, errors.New(":path must be empty and :authority must not be empty")
}
} else if len(path) == 0 || len(authority) == 0 || len(method) == 0 {
Expand All @@ -51,9 +60,14 @@ func requestFromHeaders(headers []qpack.HeaderField) (*http.Request, error) {
var err error

if isConnect {
u = &url.URL{Host: authority}
u = &url.URL{
Scheme: scheme,
Host: authority,
Path: path,
}
requestURI = authority
} else {
protocol = "HTTP/3"
u, err = url.ParseRequestURI(path)
if err != nil {
return nil, err
Expand All @@ -72,7 +86,7 @@ func requestFromHeaders(headers []qpack.HeaderField) (*http.Request, error) {
return &http.Request{
Method: method,
URL: u,
Proto: "HTTP/3",
Proto: protocol,
ProtoMajor: 3,
ProtoMinor: 0,
Header: httpHeaders,
Expand Down
80 changes: 55 additions & 25 deletions http3/request_test.go
Expand Up @@ -81,17 +81,6 @@ var _ = Describe("Request", func() {
}))
})

It("handles CONNECT method", func() {
headers := []qpack.HeaderField{
{Name: ":authority", Value: "quic.clemente.io"},
{Name: ":method", Value: http.MethodConnect},
}
req, err := requestFromHeaders(headers)
Expect(err).NotTo(HaveOccurred())
Expect(req.Method).To(Equal(http.MethodConnect))
Expect(req.RequestURI).To(Equal("quic.clemente.io"))
})

It("errors with missing path", func() {
headers := []qpack.HeaderField{
{Name: ":authority", Value: "quic.clemente.io"},
Expand Down Expand Up @@ -119,22 +108,63 @@ var _ = Describe("Request", func() {
Expect(err).To(MatchError(":path, :authority and :method must not be empty"))
})

It("errors with missing authority in CONNECT method", func() {
headers := []qpack.HeaderField{
{Name: ":method", Value: http.MethodConnect},
}
_, err := requestFromHeaders(headers)
Expect(err).To(MatchError(":path must be empty and :authority must not be empty"))
Context("regular HTTP CONNECT", func() {
It("handles CONNECT method", func() {
headers := []qpack.HeaderField{
{Name: ":authority", Value: "quic.clemente.io"},
{Name: ":method", Value: http.MethodConnect},
}
req, err := requestFromHeaders(headers)
Expect(err).NotTo(HaveOccurred())
Expect(req.Method).To(Equal(http.MethodConnect))
Expect(req.RequestURI).To(Equal("quic.clemente.io"))
})

It("errors with missing authority in CONNECT method", func() {
headers := []qpack.HeaderField{
{Name: ":method", Value: http.MethodConnect},
}
_, err := requestFromHeaders(headers)
Expect(err).To(MatchError(":path must be empty and :authority must not be empty"))
})

It("errors with extra path in CONNECT method", func() {
headers := []qpack.HeaderField{
{Name: ":path", Value: "/foo"},
{Name: ":authority", Value: "quic.clemente.io"},
{Name: ":method", Value: http.MethodConnect},
}
_, err := requestFromHeaders(headers)
Expect(err).To(MatchError(":path must be empty and :authority must not be empty"))
})
})

It("errors with extra path in CONNECT method", func() {
headers := []qpack.HeaderField{
{Name: ":path", Value: "/foo"},
{Name: ":authority", Value: "quic.clemente.io"},
{Name: ":method", Value: http.MethodConnect},
}
_, err := requestFromHeaders(headers)
Expect(err).To(MatchError(":path must be empty and :authority must not be empty"))
Context("Extended CONNECT", func() {
It("handles Extended CONNECT method", func() {
headers := []qpack.HeaderField{
{Name: ":protocol", Value: "webtransport"},
{Name: ":scheme", Value: "ftp"},
{Name: ":method", Value: http.MethodConnect},
{Name: ":authority", Value: "quic.clemente.io"},
{Name: ":path", Value: "/foo"},
}
req, err := requestFromHeaders(headers)
Expect(err).NotTo(HaveOccurred())
Expect(req.Method).To(Equal(http.MethodConnect))
Expect(req.Proto).To(Equal("webtransport"))
Expect(req.URL.String()).To(Equal("ftp://quic.clemente.io/foo"))
})

It("errors with missing scheme", func() {
headers := []qpack.HeaderField{
{Name: ":protocol", Value: "webtransport"},
{Name: ":method", Value: http.MethodConnect},
{Name: ":authority", Value: "quic.clemente.io"},
{Name: ":path", Value: "/foo"},
}
_, err := requestFromHeaders(headers)
Expect(err).To(MatchError("extended CONNECT: :scheme, :path and :authority must not be empty"))
})
})

Context("extracting the hostname from a request", func() {
Expand Down