update with changes from go1.20.5 to go1.21.4

This commit is contained in:
Scott Feldman
2023-11-13 23:28:14 -08:00
committed by deadprogram
parent be25e1a28e
commit 7c11b88ce6
32 changed files with 139 additions and 347 deletions
+38 -33
View File
@@ -1,4 +1,4 @@
// TINYGO: The following is copied and modified from Go 1.20.5 official implementation.
// TINYGO: The following is copied and modified from Go 1.21.4 official implementation.
// TINYGO: Removed multipart stuff
// TINYGO: Removed trace stuff
@@ -22,7 +22,6 @@ import (
"io"
"mime"
"mime/multipart"
"net"
"net/http/internal/ascii"
"net/textproto"
"net/url"
@@ -30,6 +29,8 @@ import (
"strconv"
"strings"
"sync"
"golang.org/x/net/http/httpguts"
)
const (
@@ -50,6 +51,11 @@ type ProtocolError struct {
func (pe *ProtocolError) Error() string { return pe.ErrorString }
// Is lets http.ErrNotSupported match errors.ErrUnsupported.
func (pe *ProtocolError) Is(err error) bool {
return pe == ErrNotSupported && err == errors.ErrUnsupported
}
var (
// ErrNotSupported indicates that a feature is not supported.
//
@@ -573,12 +579,40 @@ func (r *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, waitF
// is not given, use the host from the request URL.
//
// Clean the host, in case it arrives with unexpected stuff in it.
host := cleanHost(r.Host)
host := r.Host
if host == "" {
if r.URL == nil {
return errMissingHost
}
host = cleanHost(r.URL.Host)
host = r.URL.Host
}
host, err = httpguts.PunycodeHostPort(host)
if err != nil {
return err
}
// Validate that the Host header is a valid header in general,
// but don't validate the host itself. This is sufficient to avoid
// header or request smuggling via the Host field.
// The server can (and will, if it's a net/http server) reject
// the request if it doesn't consider the host valid.
if !httpguts.ValidHostHeader(host) {
// Historically, we would truncate the Host header after '/' or ' '.
// Some users have relied on this truncation to convert a network
// address such as Unix domain socket path into a valid, ignored
// Host header (see https://go.dev/issue/61431).
//
// We don't preserve the truncation, because sending an altered
// header field opens a smuggling vector. Instead, zero out the
// Host header entirely if it isn't valid. (An empty Host is valid;
// see RFC 9112 Section 3.2.)
//
// Return an error if we're sending to a proxy, since the proxy
// probably can't do anything useful with an empty Host header.
if !usingProxy {
host = ""
} else {
return errors.New("http: invalid Host header")
}
}
// According to RFC 6874, an HTTP client, proxy, or other
@@ -706,35 +740,6 @@ func (r *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, waitF
// This error type should not escape the net/http package to users.
type requestBodyReadError struct{ error }
// cleanHost cleans up the host sent in request's Host header.
//
// It both strips anything after '/' or ' ', and puts the value
// into Punycode form, if necessary.
//
// Ideally we'd clean the Host header according to the spec:
//
// https://tools.ietf.org/html/rfc7230#section-5.4 (Host = uri-host [ ":" port ]")
// https://tools.ietf.org/html/rfc7230#section-2.7 (uri-host -> rfc3986's host)
// https://tools.ietf.org/html/rfc3986#section-3.2.2 (definition of host)
//
// But practically, what we are trying to avoid is the situation in
// issue 11206, where a malformed Host header used in the proxy context
// would create a bad request. So it is enough to just truncate at the
// first offending character.
// TINYGO: Removed IDNA checks...it doubled the binary size
func cleanHost(in string) string {
if i := strings.IndexAny(in, " /"); i != -1 {
in = in[:i]
}
host, port, err := net.SplitHostPort(in)
if err != nil { // input was just a host
return in
}
return net.JoinHostPort(host, port)
}
// removeZone removes IPv6 zone identifier from host.
// E.g., "[fe80::1%en0]:8080" to "[fe80::1]:8080"
func removeZone(host string) string {