From be25e1a28e74b3c24338dca5d25ae656ddabfebc Mon Sep 17 00:00:00 2001 From: Scott Feldman Date: Tue, 27 Jun 2023 02:25:12 -0700 Subject: [PATCH] revert some changes going from 1.19.3 to 1.20.5 There are some new features added in Go 1.20 like string.CutPrefix. Those will have to wait to be ported until TinyGo MIN is >= 1.20. --- http/fs.go | 16 +++++++++------- http/server.go | 10 +++++----- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/http/fs.go b/http/fs.go index 6796917..ed8250c 100644 --- a/http/fs.go +++ b/http/fs.go @@ -11,7 +11,6 @@ package http import ( "errors" "fmt" - "internal/safefilepath" "io" "io/fs" "mime" @@ -72,15 +71,16 @@ func mapOpenError(originalErr error, name string, sep rune, stat func(string) (f // Open implements FileSystem using os.Open, opening files for reading rooted // and relative to the directory d. func (d Dir) Open(name string) (File, error) { - path, err := safefilepath.FromFS(path.Clean("/" + name)) - if err != nil { - return nil, errors.New("http: invalid or unsafe file path") + // TINYGO: internal/safefilepath isn't avail until 1.20, so keep pre 1.20 + // TINYGO: code here until TinyGo min Go version is >= 1.20. + if filepath.Separator != '/' && strings.ContainsRune(name, filepath.Separator) { + return nil, errors.New("http: invalid character in file path") } dir := string(d) if dir == "" { dir = "." } - fullName := filepath.Join(dir, path) + fullName := filepath.Join(dir, filepath.FromSlash(path.Clean("/"+name))) f, err := os.Open(fullName) if err != nil { return nil, mapOpenError(err, fullName, filepath.Separator, os.Stat) @@ -449,7 +449,8 @@ func checkIfUnmodifiedSince(r *Request, modtime time.Time) condResult { // The Last-Modified header truncates sub-second precision so // the modtime needs to be truncated too. modtime = modtime.Truncate(time.Second) - if ret := modtime.Compare(t); ret <= 0 { + // TINYGO: time.Compare not until Go 1.20 + if modtime.Before(t) || modtime.Equal(t) { return condTrue } return condFalse @@ -500,7 +501,8 @@ func checkIfModifiedSince(r *Request, modtime time.Time) condResult { // The Last-Modified header truncates sub-second precision so // the modtime needs to be truncated too. modtime = modtime.Truncate(time.Second) - if ret := modtime.Compare(t); ret <= 0 { + // TINYGO: time.Compare not until Go 1.20 + if modtime.Before(t) || modtime.Equal(t) { return condFalse } return condTrue diff --git a/http/server.go b/http/server.go index c12d197..3992765 100644 --- a/http/server.go +++ b/http/server.go @@ -526,11 +526,12 @@ const TrailerPrefix = "Trailer:" func (w *response) finalTrailers() Header { var t Header for k, vv := range w.handlerHeader { - if kk, found := strings.CutPrefix(k, TrailerPrefix); found { + // TINYGO: CutPrefix not available until 1.20 + if strings.HasPrefix(k, TrailerPrefix) { if t == nil { t = make(Header) } - t[kk] = vv + t[strings.TrimPrefix(k, TrailerPrefix)] = vv } } for _, k := range w.trailers { @@ -760,8 +761,8 @@ func (cr *connReader) handleReadError(_ error) { // may be called from multiple goroutines. func (cr *connReader) closeNotify() { - res := cr.conn.curReq.Load() - if res != nil && !res.didCloseNotify.Swap(true) { + res, _ := cr.conn.curReq.Load().(*response) + if res != nil && atomic.CompareAndSwapInt32(&res.didCloseNotify, 0, 1) { res.closeNotifyCh <- true } } @@ -2674,7 +2675,6 @@ func (srv *Server) Close() error { srv.inShutdown.setTrue() srv.mu.Lock() defer srv.mu.Unlock() - srv.closeDoneChanLocked() err := srv.closeListenersLocked() // Unlock srv.mu while waiting for listenerGroup.