Skip to content

Clean on go1.20 #2406

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

Merged
merged 3 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions middleware/context_timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestContextTimeoutWithDefaultErrorMessage(t *testing.T) {
c := e.NewContext(req, rec)

err := m(func(c echo.Context) error {
if err := sleepWithContext(c.Request().Context(), time.Duration(20*time.Millisecond)); err != nil {
if err := sleepWithContext(c.Request().Context(), time.Duration(80*time.Millisecond)); err != nil {
return err
}
return c.String(http.StatusOK, "Hello, World!")
Expand Down Expand Up @@ -176,7 +176,7 @@ func TestContextTimeoutCanHandleContextDeadlineOnNextHandler(t *testing.T) {
return nil
}

timeout := 10 * time.Millisecond
timeout := 50 * time.Millisecond
m := ContextTimeoutWithConfig(ContextTimeoutConfig{
Timeout: timeout,
ErrorHandler: timeoutErrorHandler,
Expand All @@ -189,11 +189,11 @@ func TestContextTimeoutCanHandleContextDeadlineOnNextHandler(t *testing.T) {
c := e.NewContext(req, rec)

err := m(func(c echo.Context) error {
// NOTE: when difference between timeout duration and handler execution time is almost the same (in range of 100microseconds)
// the result of timeout does not seem to be reliable - could respond timeout, could respond handler output
// difference over 500microseconds (0.5millisecond) response seems to be reliable
// extremely short periods are not reliable for tests when it comes to goroutines. We can not guarantee in which
// order scheduler decides do execute: 1) request goroutine, 2) timeout timer goroutine.
// most of the time we get result we expect but Mac OS seems to be quite flaky

if err := sleepWithContext(c.Request().Context(), time.Duration(20*time.Millisecond)); err != nil {
if err := sleepWithContext(c.Request().Context(), 100*time.Millisecond); err != nil {
return err
}

Expand Down
28 changes: 11 additions & 17 deletions middleware/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/url"
"os"
"path"
"path/filepath"
"strings"

"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -157,9 +156,9 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
}

// Index template
t, err := template.New("index").Parse(html)
if err != nil {
panic(fmt.Sprintf("echo: %v", err))
t, tErr := template.New("index").Parse(html)
if tErr != nil {
panic(fmt.Errorf("echo: %w", tErr))
}

return func(next echo.HandlerFunc) echo.HandlerFunc {
Expand All @@ -176,7 +175,7 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
if err != nil {
return
}
name := filepath.Join(config.Root, filepath.Clean("/"+p)) // "/"+ for security
name := path.Join(config.Root, path.Clean("/"+p)) // "/"+ for security

if config.IgnoreBase {
routePath := path.Base(strings.TrimRight(c.Path(), "/*"))
Expand All @@ -187,12 +186,14 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
}
}

file, err := openFile(config.Filesystem, name)
file, err := config.Filesystem.Open(name)
if err != nil {
if !os.IsNotExist(err) {
if !isIgnorableOpenFileError(err) {
return err
}

// file with that path did not exist, so we continue down in middleware/handler chain, hoping that we end up in
// handler that is meant to handle this request
if err = next(c); err == nil {
return err
}
Expand All @@ -202,7 +203,7 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
return err
}

file, err = openFile(config.Filesystem, filepath.Join(config.Root, config.Index))
file, err = config.Filesystem.Open(path.Join(config.Root, config.Index))
if err != nil {
return err
}
Expand All @@ -216,15 +217,13 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
}

if info.IsDir() {
index, err := openFile(config.Filesystem, filepath.Join(name, config.Index))
index, err := config.Filesystem.Open(path.Join(name, config.Index))
if err != nil {
if config.Browse {
return listDir(t, name, file, c.Response())
}

if os.IsNotExist(err) {
return next(c)
}
return next(c)
}

defer index.Close()
Expand All @@ -242,11 +241,6 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
}
}

func openFile(fs http.FileSystem, name string) (http.File, error) {
pathWithSlashes := filepath.ToSlash(name)
return fs.Open(pathWithSlashes)
}

func serveFile(c echo.Context, file http.File, info os.FileInfo) error {
http.ServeContent(c.Response(), c.Request(), info.Name(), info.ModTime(), file)
return nil
Expand Down
12 changes: 12 additions & 0 deletions middleware/static_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build !windows

package middleware

import (
"os"
)

// We ignore these errors as there could be handler that matches request path.
func isIgnorableOpenFileError(err error) bool {
return os.IsNotExist(err)
}
23 changes: 23 additions & 0 deletions middleware/static_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package middleware

import (
"os"
)

// We ignore these errors as there could be handler that matches request path.
//
// As of 1.20 on Windows filepath.Clean has different behaviour on OS related filesystems so we need to use path.Clean
// which is more suitable for path coming from web but this has some caveats on Windows. When we eventually end up in
// os related filesystem Open methods we are getting different errors as earlier versions. As of 1.20 path checks are
// more strict on path you provide and consider path with [UNC](https://en.wikipedia.org/wiki/Path_(computing)#UNC)
// but missing host etc parts as invalid. Previously it would result you `fs.ErrNotExist`.
//
// So for 1.20@Windows we need to consider it as same not exist so we can continue next middleware/handler and not error
// which would result status 500 instead of potential route hit or 404.
func isIgnorableOpenFileError(err error) bool {
if os.IsNotExist(err) {
return true
}
errTxt := err.Error()
return errTxt == "http: invalid or unsafe file path" || errTxt == "invalid path"
}