Skip to content

Commit

Permalink
update with changes from go1.20.5 to go1.21.4
Browse files Browse the repository at this point in the history
  • Loading branch information
scottfeldman authored and deadprogram committed Nov 24, 2023
1 parent be25e1a commit 7c11b88
Show file tree
Hide file tree
Showing 32 changed files with 137 additions and 345 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ packages in a TinyGo application.

## "net" Package

The "net" package is ported from Go 1.20.5. The tree listings below shows the
The "net" package is ported from Go 1.21.4. The tree listings below shows the
files copied. If the file is marked with an '\*', it is copied _and_ modified
to work with netdev. If the file is marked with an '+', the file is new. If
there is no mark, it is a straight copy.
Expand Down
7 changes: 4 additions & 3 deletions dial.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// 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: Omit DualStack support
// TINYGO: Omit Fast Fallback support
// TINYGO: Don't allow alternate resolver
// TINYGO: Omit DialTimeout
// TINYGO: Omit Multipath TCP

// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand All @@ -17,9 +18,9 @@ import (
"time"
)

// defaultTCPKeepAlive is a default constant value for TCPKeepAlive times
// See golang.org/issue/31510
const (
// defaultTCPKeepAlive is a default constant value for TCPKeepAlive times
// See go.dev/issue/31510
defaultTCPKeepAlive = 15 * time.Second
)

Expand Down
2 changes: 1 addition & 1 deletion http/client.go
Original file line number Diff line number Diff line change
@@ -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.

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
2 changes: 1 addition & 1 deletion http/clone.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// TINYGO: The following is copied from Go 1.20.5 official implementation.
// TINYGO: The following is copied from Go 1.21.4 official implementation.

// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
2 changes: 1 addition & 1 deletion http/cookie.go
Original file line number Diff line number Diff line change
@@ -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.

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
2 changes: 1 addition & 1 deletion http/fs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// TINYGO: The following is copied from Go 1.20.5 official implementation.
// TINYGO: The following is copied from Go 1.21.4 official implementation.

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
2 changes: 1 addition & 1 deletion http/header.go
Original file line number Diff line number Diff line change
@@ -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 trace stuff

Expand Down
12 changes: 9 additions & 3 deletions http/http.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// TINYGO: The following is copied from Go 1.20.5 official implementation.
// TINYGO: The following is copied from Go 1.21.4 official implementation.

// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down Expand Up @@ -88,14 +88,20 @@ func hexEscapeNonASCII(s string) string {
return s
}
b := make([]byte, 0, newLen)
var pos int
for i := 0; i < len(s); i++ {
if s[i] >= utf8.RuneSelf {
if pos < i {
b = append(b, s[pos:i]...)
}
b = append(b, '%')
b = strconv.AppendInt(b, int64(s[i]), 16)
} else {
b = append(b, s[i])
pos = i + 1
}
}
if pos < len(s) {
b = append(b, s[pos:]...)
}
return string(b)
}

Expand Down
2 changes: 1 addition & 1 deletion http/internal/ascii/print.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// TINYGO: The following is copied from Go 1.20.5 official implementation.
// TINYGO: The following is copied from Go 1.21.4 official implementation.

// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
2 changes: 1 addition & 1 deletion http/internal/ascii/print_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// TINYGO: The following is copied from Go 1.20.5 official implementation.
// TINYGO: The following is copied from Go 1.21.4 official implementation.

// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
2 changes: 1 addition & 1 deletion http/internal/chunked.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// TINYGO: The following is copied from Go 1.20.5 official implementation.
// TINYGO: The following is copied from Go 1.21.4 official implementation.

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
2 changes: 1 addition & 1 deletion http/internal/chunked_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// TINYGO: The following is copied from Go 1.20.5 official implementation.
// TINYGO: The following is copied from Go 1.21.4 official implementation.

// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
2 changes: 1 addition & 1 deletion http/jar.go
Original file line number Diff line number Diff line change
@@ -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.

// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
2 changes: 1 addition & 1 deletion http/method.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// TINYGO: The following is copied from Go 1.20.5 official implementation.
// TINYGO: The following is copied from Go 1.21.4 official implementation.

// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
71 changes: 38 additions & 33 deletions http/request.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,14 +22,15 @@ import (
"io"
"mime"
"mime/multipart"
"net"
"net/http/internal/ascii"
"net/textproto"
"net/url"
urlpkg "net/url"
"strconv"
"strings"
"sync"

"golang.org/x/net/http/httpguts"
)

const (
Expand All @@ -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.
//
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion http/response.go
Original file line number Diff line number Diff line change
@@ -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 TLS connection state
// TINYGO: Added onEOF hook to get callback when response has been read
Expand Down
61 changes: 29 additions & 32 deletions http/server.go
Original file line number Diff line number Diff line change
@@ -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: atomic.Pointer and atomic.Uint64 are added in Go 1.19, so keep
// pre-1.19 code to cover min TinyGo. If TinyGo min moves to 1.19 or higher,
Expand Down Expand Up @@ -469,6 +469,10 @@ type response struct {
// Content-Length.
closeAfterReply bool

// When fullDuplex is false (the default), we consume any remaining
// request body before starting to write a response.
fullDuplex bool

// requestBodyLimitHit is set by requestTooLarge when
// maxBytesReader hits its max size. It is checked in
// WriteHeader, to make sure we don't consume the
Expand Down Expand Up @@ -506,6 +510,11 @@ func (c *response) SetWriteDeadline(deadline time.Time) error {
return c.conn.rwc.SetWriteDeadline(deadline)
}

func (c *response) EnableFullDuplex() error {
c.fullDuplex = true
return nil
}

// TrailerPrefix is a magic prefix for ResponseWriter.Header map keys
// that, if present, signals that the map entry is actually for
// the response trailers, and not the response headers. The prefix
Expand Down Expand Up @@ -1162,8 +1171,11 @@ func (w *response) WriteHeader(code int) {
}
checkWriteHeaderCode(code)

// Handle informational headers
if code >= 100 && code <= 199 {
// Handle informational headers.
//
// We shouldn't send any further headers after 101 Switching Protocols,
// so it takes the non-informational path.
if code >= 100 && code <= 199 && code != StatusSwitchingProtocols {
// Prevent a potential race with an automatically-sent 100 Continue triggered by Request.Body.Read()
if code == 100 && w.canWriteContinue.isSet() {
w.writeContinueMu.Lock()
Expand Down Expand Up @@ -1325,7 +1337,7 @@ func (cw *chunkWriter) writeHeader(p []byte) {
// send a Content-Length header.
// Further, we don't send an automatic Content-Length if they
// set a Transfer-Encoding, because they're generally incompatible.
if w.handlerDone.isSet() && !trailers && !hasTE && bodyAllowedForStatus(w.status) && header.get("Content-Length") == "" && (!isHEAD || len(p) > 0) {
if w.handlerDone.isSet() && !trailers && !hasTE && bodyAllowedForStatus(w.status) && !header.has("Content-Length") && (!isHEAD || len(p) > 0) {
w.contentLength = int64(len(p))
setHeader.contentLength = strconv.AppendInt(cw.res.clenBuf[:0], int64(len(p)), 10)
}
Expand Down Expand Up @@ -1371,14 +1383,14 @@ func (cw *chunkWriter) writeHeader(p []byte) {
w.closeAfterReply = true
}

// Per RFC 2616, we should consume the request body before
// replying, if the handler hasn't already done so. But we
// don't want to do an unbounded amount of reading here for
// DoS reasons, so we only try up to a threshold.
// TODO(bradfitz): where does RFC 2616 say that? See Issue 15527
// about HTTP/1.x Handlers concurrently reading and writing, like
// HTTP/2 handlers can do. Maybe this code should be relaxed?
if w.req.ContentLength != 0 && !w.closeAfterReply {
// We do this by default because there are a number of clients that
// send a full request before starting to read the response, and they
// can deadlock if we start writing the response with unconsumed body
// remaining. See Issue 15527 for some history.
//
// If full duplex mode has been enabled with ResponseController.EnableFullDuplex,
// then leave the request body alone.
if w.req.ContentLength != 0 && !w.closeAfterReply && !w.fullDuplex {
var discard, tooBig bool

switch bdy := w.req.Body.(type) {
Expand Down Expand Up @@ -1766,7 +1778,7 @@ type closeWriter interface {

var _ closeWriter = (*net.TCPConn)(nil)

// closeWrite flushes any outstanding data and sends a FIN packet (if
// closeWriteAndWait flushes any outstanding data and sends a FIN packet (if
// client is connected via TCP), signaling that we're done. We then
// pause for a bit, hoping the client processes it before any
// subsequent RST.
Expand Down Expand Up @@ -1861,7 +1873,9 @@ func isCommonNetReadError(err error) bool {

// Serve a new connection.
func (c *conn) serve(ctx context.Context) {
c.remoteAddr = c.rwc.RemoteAddr().String()
if ra := c.rwc.RemoteAddr(); ra != nil {
c.remoteAddr = ra.String()
}
ctx = context.WithValue(ctx, LocalAddrContextKey, c.rwc.LocalAddr())
var inFlightResponse *response
defer func() {
Expand Down Expand Up @@ -2258,7 +2272,7 @@ func RedirectHandler(url string, code int) Handler {
// Longer patterns take precedence over shorter ones, so that
// if there are handlers registered for both "/images/"
// and "/images/thumbnails/", the latter handler will be
// called for paths beginning "/images/thumbnails/" and the
// called for paths beginning with "/images/thumbnails/" and the
// former will receive requests for any other paths in the
// "/images/" subtree.
//
Expand Down Expand Up @@ -2873,23 +2887,9 @@ func (sh serverHandler) ServeHTTP(rw ResponseWriter, req *Request) {
handler = globalOptionsHandler{}
}

if req.URL != nil && strings.Contains(req.URL.RawQuery, ";") {
var allowQuerySemicolonsInUse int32
req = req.WithContext(context.WithValue(req.Context(), silenceSemWarnContextKey, func() {
atomic.StoreInt32(&allowQuerySemicolonsInUse, 1)
}))
defer func() {
if atomic.LoadInt32(&allowQuerySemicolonsInUse) == 0 {
sh.srv.logf("http: URL query contains semicolon, which is no longer a supported separator; parts of the query may be stripped when parsed; see golang.org/issue/25192")
}
}()
}

handler.ServeHTTP(rw, req)
}

var silenceSemWarnContextKey = &contextKey{"silence-semicolons"}

// AllowQuerySemicolons returns a handler that serves requests by converting any
// unescaped semicolons in the URL query to ampersands, and invoking the handler h.
//
Expand All @@ -2901,9 +2901,6 @@ var silenceSemWarnContextKey = &contextKey{"silence-semicolons"}
// AllowQuerySemicolons should be invoked before Request.ParseForm is called.
func AllowQuerySemicolons(h Handler) Handler {
return HandlerFunc(func(w ResponseWriter, r *Request) {
if silenceSemicolonsWarning, ok := r.Context().Value(silenceSemWarnContextKey).(func()); ok {
silenceSemicolonsWarning()
}
if strings.Contains(r.URL.RawQuery, ";") {
r2 := new(Request)
*r2 = *r
Expand Down
2 changes: 1 addition & 1 deletion http/sniff.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// TINYGO: The following is copied from Go 1.20.5 official implementation.
// TINYGO: The following is copied from Go 1.21.4 official implementation.

// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
2 changes: 1 addition & 1 deletion http/status.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// TINYGO: The following is copied from Go 1.20.5 official implementation.
// TINYGO: The following is copied from Go 1.21.4 official implementation.

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
Loading

0 comments on commit 7c11b88

Please sign in to comment.