mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
net/http: allow Handlers to handle http2 upgrade PRI requests
The http2 spec defines a magic string which initates an http2 session:
"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
It was intentionally chosen to kinda look like an HTTP request, but
just different enough to break things not ready for it. This change
makes Go ready for it.
Notably: Go now accepts the request header (the prefix "PRI *
HTTP/2.0\r\n\r\n") as a valid request, even though it doesn't have a
Host header. But we now mark it as "Connection: close" and teach the
Server to never read a second request from the connection once that's
seen. If the http.Handler wants to deal with the upgrade, it has to
hijack the request, read out the "body", compare it against
"SM\r\n\r\n", and then speak http2. One of the new tests demonstrates
that hijacking.
Fixes #14451
Updates #14141 (h2c)
Change-Id: Ib46142f31c55be7d00c56fa2624ec8a232e00c43
Reviewed-on: https://go-review.googlesource.com/21327
Reviewed-by: Andrew Gerrand <adg@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
0026cb788b
commit
a6557a05a0
4 changed files with 96 additions and 2 deletions
|
|
@ -343,6 +343,12 @@ func (r *Request) multipartReader() (*multipart.Reader, error) {
|
|||
return multipart.NewReader(r.Body, boundary), nil
|
||||
}
|
||||
|
||||
// isH2Upgrade reports whether r represents the http2 "client preface"
|
||||
// magic string.
|
||||
func (r *Request) isH2Upgrade() bool {
|
||||
return r.Method == "PRI" && len(r.Header) == 0 && r.URL.Path == "*" && r.Proto == "HTTP/2.0"
|
||||
}
|
||||
|
||||
// Return value if nonempty, def otherwise.
|
||||
func valueOrDefault(value, def string) string {
|
||||
if value != "" {
|
||||
|
|
@ -794,6 +800,16 @@ func readRequest(b *bufio.Reader, deleteHostHeader bool) (req *Request, err erro
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if req.isH2Upgrade() {
|
||||
// Because it's neither chunked, nor declared:
|
||||
req.ContentLength = -1
|
||||
|
||||
// We want to give handlers a chance to hijack the
|
||||
// connection, but we need to prevent the Server from
|
||||
// dealing with the connection further if it's not
|
||||
// hijacked. Set Close to ensure that:
|
||||
req.Close = true
|
||||
}
|
||||
return req, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue